Skip to content

Commit

Permalink
Fix X-Forwarded-Port when not present
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <eloi.demolis@clever-cloud.com>
  • Loading branch information
Wonshtrum committed Oct 20, 2023
1 parent 2760078 commit cda2f01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/src/protocol/kawa_h1/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,9 @@ impl HttpContext {
let has_forwarded = forwarded.is_some();

if let Some(header) = x_for {
header.val = kawa::Store::from_string(format!(
"{}, {}",
unsafe { from_utf8_unchecked(header.val.data(buf)) },
peer_ip
));
header.val = kawa::Store::from_string(format!("{}, {peer_ip}", unsafe {
from_utf8_unchecked(header.val.data(buf))
}));
}
if let Some(header) = &mut forwarded {
let value = unsafe { from_utf8_unchecked(header.val.data(buf)) };
Expand Down Expand Up @@ -208,19 +206,13 @@ impl HttpContext {
val: kawa::Store::from_string(peer_ip.to_string()),
}));
}
if !has_x_port {
request.push_block(kawa::Block::Header(kawa::Pair {
key: kawa::Store::Static(b"X-Forwarded-Port"),
val: kawa::Store::from_string(peer_port.to_string()),
}));
}
if !has_forwarded {
let value = match (peer_ip, public_ip) {
(IpAddr::V4(_), IpAddr::V4(_)) => {
format!("proto={proto};for={peer_ip}:{peer_port};by={public_ip}")
}
(IpAddr::V4(_), IpAddr::V6(_)) => {
format!("proto={proto};for={peer_ip}:{peer_port};by=\"{public_ip}")
format!("proto={proto};for={peer_ip}:{peer_port};by=\"{public_ip}\"")
}
(IpAddr::V6(_), IpAddr::V4(_)) => {
format!("proto={proto};for=\"{peer_ip}:{peer_port}\";by={public_ip}")
Expand All @@ -235,6 +227,12 @@ impl HttpContext {
}));
}
}
if !has_x_port {
request.push_block(kawa::Block::Header(kawa::Pair {
key: kawa::Store::Static(b"X-Forwarded-Port"),
val: kawa::Store::from_string(public_port.to_string()),
}));
}
if !has_x_proto {
request.push_block(kawa::Block::Header(kawa::Pair {
key: kawa::Store::Static(b"X-Forwarded-Proto"),
Expand Down
3 changes: 3 additions & 0 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
self.container_backend_timeout.cancel();
self.frontend_readiness.interest = Ready::READABLE | Ready::HUP | Ready::ERROR;
self.backend_readiness.interest = Ready::HUP | Ready::ERROR;
if !self.request_stream.storage.is_empty() {
self.frontend_readiness.event.insert(Ready::READABLE);
}
}

pub fn readable(&mut self, metrics: &mut SessionMetrics) -> StateResult {
Expand Down

0 comments on commit cda2f01

Please sign in to comment.