fix: host header not set on responses (#23)

This commit is contained in:
Christof Weickhardt 2022-04-13 04:23:09 +02:00 committed by GitHub
parent b7f0bf9d81
commit 4b3e3ffda1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -168,11 +168,7 @@ fn remove_hop_headers(headers: &HeaderMap<HeaderValue>) -> HeaderMap<HeaderValue
result result
} }
fn create_proxied_response<B>(mut response: Response<B>, host: HeaderValue) -> Response<B> { fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> {
if host.to_str().unwrap_or("") != "" {
response.headers_mut().insert(HOST, host);
}
*response.headers_mut() = remove_hop_headers(response.headers()); *response.headers_mut() = remove_hop_headers(response.headers());
response response
} }
@ -204,10 +200,14 @@ fn create_proxied_request<B>(
mut request: Request<B>, mut request: Request<B>,
) -> Result<Request<B>, ProxyError> { ) -> Result<Request<B>, ProxyError> {
let uri: hyper::Uri = forward_uri(forward_url, &request).parse()?; 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.headers_mut() = remove_hop_headers(request.headers());
*request.uri_mut() = uri;
// Add forwarding information in the headers // Add forwarding information in the headers
match request.headers_mut().entry(&*X_FORWARDED_FOR) { match request.headers_mut().entry(&*X_FORWARDED_FOR) {
@ -221,8 +221,6 @@ fn create_proxied_request<B>(
} }
} }
request.headers_mut().insert(HOST, host.unwrap().parse::<HeaderValue>()?);
Ok(request) Ok(request)
} }
@ -242,12 +240,10 @@ pub async fn call(
forward_uri: &str, forward_uri: &str,
request: Request<Body>, request: Request<Body>,
) -> Result<Response<Body>, ProxyError> { ) -> Result<Response<Body>, 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 proxied_request = create_proxied_request(client_ip, forward_uri, request)?;
let client = build_client(); let client = build_client();
let response = client.request(proxied_request).await?; 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) Ok(proxied_response)
} }