add x-forwarded-host header
This commit is contained in:
parent
1f924d65fb
commit
6025dd50ca
32
src/lib.rs
32
src/lib.rs
@ -140,6 +140,7 @@ lazy_static! {
|
|||||||
];
|
];
|
||||||
|
|
||||||
static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
|
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)]
|
#[derive(Debug)]
|
||||||
@ -334,7 +335,7 @@ fn create_proxied_request<B>(
|
|||||||
debug!("Setting headers of proxied request");
|
debug!("Setting headers of proxied request");
|
||||||
|
|
||||||
// remove the original HOST header. It will be set by the client that sends the 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;
|
*request.uri_mut() = uri;
|
||||||
|
|
||||||
@ -361,22 +362,27 @@ fn create_proxied_request<B>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add forwarding information in the headers
|
// Add forwarding information in the headers
|
||||||
|
let forwarded_for_value = client_ip.to_string().parse()?;
|
||||||
match request.headers_mut().entry(&*X_FORWARDED_FOR) {
|
match request.headers_mut().entry(&*X_FORWARDED_FOR) {
|
||||||
hyper::header::Entry::Vacant(entry) => {
|
hyper::header::Entry::Vacant(entry) => {
|
||||||
debug!("X-Fowraded-for header was vacant");
|
debug!("x-forwarded-for header was vacant");
|
||||||
entry.insert(client_ip.to_string().parse()?);
|
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) => {
|
if let Some(host) = original_host {
|
||||||
debug!("X-Fowraded-for header was occupied");
|
match request.headers_mut().entry(&*X_FORWARDED_HOST) {
|
||||||
let client_ip_str = client_ip.to_string();
|
hyper::header::Entry::Vacant(entry) => {
|
||||||
let mut addr =
|
debug!("x-forwarded-host header was vacant");
|
||||||
String::with_capacity(entry.get().as_bytes().len() + 2 + client_ip_str.len());
|
entry.insert(host.to_owned());
|
||||||
|
}
|
||||||
addr.push_str(std::str::from_utf8(entry.get().as_bytes()).unwrap());
|
hyper::header::Entry::Occupied(mut _entry) => {
|
||||||
addr.push(',');
|
debug!("x-forwarded-host header was occupied");
|
||||||
addr.push(' ');
|
}
|
||||||
addr.push_str(&client_ip_str);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +90,27 @@ async fn test_get(ctx: &mut ProxyTestContext) {
|
|||||||
HOST,
|
HOST,
|
||||||
format!("127.0.0.1:{}", ctx.http_back.port).parse().unwrap(),
|
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(
|
ctx.http_back.add(
|
||||||
HandlerBuilder::new("/foo")
|
HandlerBuilder::new("/foo")
|
||||||
.status_code(StatusCode::OK)
|
.status_code(StatusCode::OK)
|
||||||
|
Loading…
Reference in New Issue
Block a user