Skip to content

Commit

Permalink
Fix host header
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Jun 18, 2022
1 parent 102d50a commit 1f924d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,8 @@ fn create_proxied_request<B>(

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;

Expand Down
10 changes: 8 additions & 2 deletions tests/test_http.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1f924d6

Please sign in to comment.