feat: rewrite host header (#19)

This commit is contained in:
Christof Weickhardt 2022-03-21 00:39:47 +01:00 committed by GitHub
parent 8e4efa3c10
commit df087ff55e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,7 +97,7 @@
//! //!
use hyper::client::{connect::dns::GaiResolver, HttpConnector}; use hyper::client::{connect::dns::GaiResolver, HttpConnector};
use hyper::header::{HeaderMap, HeaderValue}; use hyper::header::{HeaderMap, HeaderValue, HOST};
use hyper::http::header::{InvalidHeaderValue, ToStrError}; use hyper::http::header::{InvalidHeaderValue, ToStrError};
use hyper::http::uri::InvalidUri; use hyper::http::uri::InvalidUri;
use hyper::{Body, Client, Error, Request, Response, Uri}; use hyper::{Body, Client, Error, Request, Response, Uri};
@ -171,7 +171,11 @@ fn remove_hop_headers(headers: &HeaderMap<HeaderValue>) -> HeaderMap<HeaderValue
result result
} }
fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> { fn create_proxied_response<B>(mut response: Response<B>, host: HeaderValue) -> 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
} }
@ -190,8 +194,10 @@ fn create_proxied_request<B>(
forward_url: &str, forward_url: &str,
mut request: Request<B>, mut request: Request<B>,
) -> Result<Request<B>, ProxyError> { ) -> Result<Request<B>, ProxyError> {
let uri = forward_uri(forward_url, &request)?;
*request.headers_mut() = remove_hop_headers(request.headers()); *request.headers_mut() = remove_hop_headers(request.headers());
*request.uri_mut() = forward_uri(forward_url, &request)?; *request.uri_mut() = uri.to_owned();
let x_forwarded_for_header_name = "x-forwarded-for"; let x_forwarded_for_header_name = "x-forwarded-for";
@ -207,6 +213,8 @@ fn create_proxied_request<B>(
} }
} }
request.headers_mut().insert(HOST, uri.host().unwrap().parse()?);
Ok(request) Ok(request)
} }
@ -226,10 +234,12 @@ 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); let proxied_response = create_proxied_response(response, host);
Ok(proxied_response) Ok(proxied_response)
} }