From 4b3e3ffda1cae39a5b4dea3ff8e4c74bfa2655eb Mon Sep 17 00:00:00 2001 From: Christof Weickhardt Date: Wed, 13 Apr 2022 04:23:09 +0200 Subject: [PATCH] fix: host header not set on responses (#23) --- src/lib.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e4c082e..d7a0ae8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,11 +168,7 @@ fn remove_hop_headers(headers: &HeaderMap) -> HeaderMap(mut response: Response, host: HeaderValue) -> Response { - if host.to_str().unwrap_or("") != "" { - response.headers_mut().insert(HOST, host); - } - +fn create_proxied_response(mut response: Response) -> Response { *response.headers_mut() = remove_hop_headers(response.headers()); response } @@ -204,10 +200,14 @@ fn create_proxied_request( mut request: Request, ) -> Result, ProxyError> { let uri: hyper::Uri = forward_uri(forward_url, &request).parse()?; - let host = uri.host().map(|e|e.to_owned()); + + request + .headers_mut() + .insert(HOST, HeaderValue::from_str(uri.host().unwrap())?); + + *request.uri_mut() = uri; *request.headers_mut() = remove_hop_headers(request.headers()); - *request.uri_mut() = uri; // Add forwarding information in the headers match request.headers_mut().entry(&*X_FORWARDED_FOR) { @@ -221,8 +221,6 @@ fn create_proxied_request( } } - request.headers_mut().insert(HOST, host.unwrap().parse::()?); - Ok(request) } @@ -242,12 +240,10 @@ pub async fn call( forward_uri: &str, request: Request, ) -> Result, ProxyError> { - let host = request.headers().get(HOST).unwrap_or(&HeaderValue::from_str("").unwrap()).to_owned(); - let proxied_request = create_proxied_request(client_ip, forward_uri, request)?; let client = build_client(); let response = client.request(proxied_request).await?; - let proxied_response = create_proxied_response(response, host); + let proxied_response = create_proxied_response(response); Ok(proxied_response) }