Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-forwarded-host header #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ lazy_static! {
];

static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
static ref X_FORWARDED_HOST: HeaderName = HeaderName::from_static("x-forwarded-host");
}

#[derive(Debug)]
Expand Down Expand Up @@ -333,9 +334,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
let original_host = request.headers_mut().remove(HOST);

*request.uri_mut() = uri;

Expand All @@ -362,22 +362,27 @@ fn create_proxied_request<B>(
}

// Add forwarding information in the headers
let forwarded_for_value = client_ip.to_string().parse()?;
match request.headers_mut().entry(&*X_FORWARDED_FOR) {
hyper::header::Entry::Vacant(entry) => {
debug!("X-Fowraded-for header was vacant");
entry.insert(client_ip.to_string().parse()?);
debug!("x-forwarded-for header was vacant");
entry.insert(forwarded_for_value);
}
hyper::header::Entry::Occupied(mut entry) => {
debug!("x-forwarded-for header was occupied");
entry.append(forwarded_for_value);
}
}

hyper::header::Entry::Occupied(entry) => {
debug!("X-Fowraded-for header was occupied");
let client_ip_str = client_ip.to_string();
let mut addr =
String::with_capacity(entry.get().as_bytes().len() + 2 + client_ip_str.len());

addr.push_str(std::str::from_utf8(entry.get().as_bytes()).unwrap());
addr.push(',');
addr.push(' ');
addr.push_str(&client_ip_str);
if let Some(host) = original_host {
match request.headers_mut().entry(&*X_FORWARDED_HOST) {
hyper::header::Entry::Vacant(entry) => {
debug!("x-forwarded-host header was vacant");
entry.insert(host.to_owned());
}
hyper::header::Entry::Occupied(mut _entry) => {
debug!("x-forwarded-host header was occupied");
}
}
}

Expand Down
31 changes: 29 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,36 @@ 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();
assert_eq!(200, resp.status());
}

#[test_context(ProxyTestContext)]
#[tokio::test]
async fn test_headers(ctx: &mut ProxyTestContext) {
let mut headers = HeaderMap::new();
headers.insert("x-forwarded-for", "127.0.0.1".parse().unwrap());
headers.insert(
"x-forwarded-host",
format!("localhost:{}", ctx.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