From 1f924d65fbf277d044378c7b4d2bcc8fd2918bc8 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Sun, 19 Jun 2022 00:46:50 +0200 Subject: [PATCH] Fix host header --- src/lib.rs | 5 ++--- tests/test_http.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 51e8add..76d9146 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -333,9 +333,8 @@ fn create_proxied_request( debug!("Setting headers of proxied request"); - request - .headers_mut() - .insert(HOST, HeaderValue::from_str(uri.host().unwrap())?); + // remove the original HOST header. It will be set by the client that sends the request + request.headers_mut().remove(HOST); *request.uri_mut() = uri; diff --git a/tests/test_http.rs b/tests/test_http.rs index d1bacbf..d28108e 100644 --- a/tests/test_http.rs +++ b/tests/test_http.rs @@ -1,9 +1,9 @@ use hyper::client::connect::dns::GaiResolver; use hyper::client::HttpConnector; -use hyper::header::{CONNECTION, UPGRADE}; +use hyper::header::{CONNECTION, HOST, UPGRADE}; use hyper::server::conn::AddrStream; use hyper::service::{make_service_fn, service_fn}; -use hyper::{Body, Client, Request, Response, Server, StatusCode, Uri}; +use hyper::{Body, Client, HeaderMap, Request, Response, Server, StatusCode, Uri}; use hyper_reverse_proxy::ReverseProxy; use std::convert::Infallible; use std::net::{IpAddr, SocketAddr}; @@ -85,9 +85,15 @@ async fn test_upgrade_unrequested(ctx: &mut ProxyTestContext) { #[test_context(ProxyTestContext)] #[tokio::test] async fn test_get(ctx: &mut ProxyTestContext) { + let mut headers = HeaderMap::new(); + headers.insert( + HOST, + format!("127.0.0.1:{}", ctx.http_back.port).parse().unwrap(), + ); ctx.http_back.add( HandlerBuilder::new("/foo") .status_code(StatusCode::OK) + .headers(headers) .build(), ); let resp = Client::new().get(ctx.uri("/foo")).await.unwrap();