Skip to content

Commit

Permalink
absolute uri host has priority over headers
Browse files Browse the repository at this point in the history
closes #317
  • Loading branch information
GlenDC committed Sep 16, 2024
1 parent a74f2c2 commit 288fe7e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
4 changes: 2 additions & 2 deletions rama-http/src/layer/required_header/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ mod test {
let svc = AddRequiredRequestHeadersLayer::new()
.overwrite(true)
.layer(service_fn(|_ctx: Context<()>, req: Request| async move {
assert_eq!(req.headers().get(HOST).unwrap(), "example.com:80");
assert_eq!(req.headers().get(HOST).unwrap(), "127.0.0.1:80");
assert_eq!(
req.headers().get(USER_AGENT).unwrap(),
RAMA_ID_HEADER_VALUE.to_str().unwrap()
Expand All @@ -297,7 +297,7 @@ mod test {
.overwrite(true)
.user_agent_header_value(HeaderValue::from_static("foo"))
.layer(service_fn(|_ctx: Context<()>, req: Request| async move {
assert_eq!(req.headers().get(HOST).unwrap(), "example.com:80");
assert_eq!(req.headers().get(HOST).unwrap(), "127.0.0.1:80");
assert_eq!(
req.headers().get(USER_AGENT).and_then(|v| v.to_str().ok()),
Some("foo")
Expand Down
21 changes: 10 additions & 11 deletions rama-net/src/http/request_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ impl<Body, State> TryFrom<(&Context<State>, &Request<Body>)> for RequestContext
let default_port = uri.port_u16().unwrap_or_else(|| protocol.default_port());
tracing::trace!(uri = %uri, "request context: detected default port: {default_port}");

let authority = ctx
.get::<Forwarded>()
.and_then(|f| {
f.client_host().map(|fauth| {
let (host, port) = fauth.clone().into_parts();
let port = port.unwrap_or(default_port);
(host, port).into()
let authority = uri
.host()
.and_then(|h| Host::try_from(h).ok().map(|h| (h, default_port).into()))
.or_else(|| {
ctx.get::<Forwarded>().and_then(|f| {
f.client_host().map(|fauth| {
let (host, port) = fauth.clone().into_parts();
let port = port.unwrap_or(default_port);
(host, port).into()
})
})
})
.or_else(|| {
Expand All @@ -62,10 +65,6 @@ impl<Body, State> TryFrom<(&Context<State>, &Request<Body>)> for RequestContext
.ok()
})
})
.or_else(|| {
uri.host()
.and_then(|h| Host::try_from(h).ok().map(|h| (h, default_port).into()))
})
.ok_or_else(|| {
OpaqueError::from_display("RequestContext: no authourity found in http::Request")
})?;
Expand Down

0 comments on commit 288fe7e

Please sign in to comment.