Skip to content
New issue

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

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

Already on GitHub? # to your account

Add x-forwarded-host header #41

Open
wants to merge 3 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
32 changes: 19 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
];

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 @@ -75,7 +76,7 @@
}

fn get_upgrade_type(headers: &HeaderMap) -> Option<String> {
#[allow(clippy::blocks_in_if_conditions)]

Check failure on line 79 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

lint `clippy::blocks_in_if_conditions` has been renamed to `clippy::blocks_in_conditions`
if headers
.get(&*CONNECTION_HEADER)
.map(|value| {
Expand Down Expand Up @@ -128,7 +129,7 @@

let split_url = forward_url.split('?').collect::<Vec<&str>>();

let mut base_url: &str = split_url.get(0).unwrap_or(&"");

Check failure on line 132 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

accessing first element with `split_url.get(0)`
let forward_url_query: &str = split_url.get(1).unwrap_or(&"");

let path2 = req.uri().path();
Expand Down Expand Up @@ -226,7 +227,7 @@
debug!("Setting headers of proxied request");

// remove the original HOST header. It will be set by the client that sends the request
request.headers_mut().remove(HOST);
let original_host = request.headers_mut().remove(HOST);

*request.uri_mut() = uri;

Expand All @@ -253,22 +254,27 @@
}

// 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 All @@ -277,7 +283,7 @@
Ok(request)
}

pub async fn call<'a, T: hyper::client::connect::Connect + Clone + Send + Sync + 'static>(

Check failure on line 286 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

the following explicit lifetimes could be elided: 'a
client_ip: IpAddr,
forward_uri: &str,
mut request: Request<Body>,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ async fn test_get(ctx: &mut ProxyTestContext) {
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)
Expand Down
Loading