🔥 使用 Rust 重构 #5

Closed
LittleSheep wants to merge 9 commits from refactor/rust into master
2 changed files with 21 additions and 1 deletions
Showing only changes of commit 4c08d78bed - Show all commits

View File

@ -63,10 +63,28 @@ pub async fn repond_websocket(req: Builder, ws: WebSocket) -> Response {
pub async fn respond_hypertext( pub async fn respond_hypertext(
uri: String, uri: String,
ori: &Uri, ori: &Uri,
req: &Request,
method: Method, method: Method,
body: Body, body: Body,
headers: &HeaderMap, headers: &HeaderMap,
) -> Result<Response, Error> { ) -> Result<Response, Error> {
let ip = req.remote_addr().to_string();
let proto = req.uri().scheme_str().unwrap();
let host = req.uri().host().unwrap();
let mut headers = headers.clone();
headers.insert("Server", "RoadSign".parse().unwrap());
headers.insert("X-Forward-For", ip.parse().unwrap());
headers.insert("X-Forwarded-Proto", proto.parse().unwrap());
headers.insert("X-Forwarded-Host", host.parse().unwrap());
headers.insert("X-Real-IP", ip.parse().unwrap());
headers.insert(
"Forwarded",
format!("by={};for={};host={};proto={}", ip, ip, host, proto)
.parse()
.unwrap(),
);
let res = CLIENT let res = CLIENT
.request(method, uri + ori.path() + ori.query().unwrap_or("")) .request(method, uri + ori.path() + ori.query().unwrap_or(""))
.headers(headers.clone()) .headers(headers.clone())
@ -81,6 +99,8 @@ pub async fn respond_hypertext(
result.headers().iter().for_each(|(key, val)| { result.headers().iter().for_each(|(key, val)| {
res.headers_mut().insert(key, val.to_owned()); res.headers_mut().insert(key, val.to_owned());
}); });
res.headers_mut()
.insert("Server", "RoadSign".parse().unwrap());
res.set_status(result.status()); res.set_status(result.status());
res.set_version(result.version()); res.set_version(result.version());
res.set_body(result.bytes().await.unwrap()); res.set_body(result.bytes().await.unwrap());

View File

@ -77,7 +77,7 @@ pub async fn handle(
)); ));
}; };
responder::respond_hypertext(uri, ori, method, body, headers).await responder::respond_hypertext(uri, ori, req, method, body, headers).await
} }
DestinationType::StaticFiles => { DestinationType::StaticFiles => {
let Ok(cfg) = end.get_static_config() else { let Ok(cfg) = end.get_static_config() else {