From 9c54b2e0f2e3fec5298de215d8752bc5a5ead61b Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Fri, 25 Oct 2024 22:28:57 +0800 Subject: [PATCH] feat(local-http): Adding Host header with non-standard port --- .../src/local/http/http_client.rs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/shadowsocks-service/src/local/http/http_client.rs b/crates/shadowsocks-service/src/local/http/http_client.rs index 275bf02378b9..3d52d1850aca 100644 --- a/crates/shadowsocks-service/src/local/http/http_client.rs +++ b/crates/shadowsocks-service/src/local/http/http_client.rs @@ -141,7 +141,7 @@ where pub async fn send_request( &self, context: Arc, - mut req: Request, + req: Request, balancer: Option<&PingBalancer>, ) -> Result, HttpClientError> { let host = match host_addr(req.uri()) { @@ -150,16 +150,24 @@ where }; // Set Host header if it was missing in the Request - { - let headers = req.headers_mut(); + let (mut req_parts, req_body) = req.into_parts(); + if let Some(authority) = req_parts.uri.authority() { + let headers = &mut req_parts.headers; if !headers.contains_key("Host") { - let host_value = match host { - Address::DomainNameAddress(ref domain, _) => HeaderValue::from_str(domain)?, - Address::SocketAddress(ref saddr) => HeaderValue::from_str(saddr.ip().to_string().as_str())?, + let uri = &req_parts.uri; + let host_value = if (uri.scheme_str() == Some("http") + && matches!(authority.port_u16(), None | Some(80))) + || (uri.scheme_str() == Some("https") && matches!(authority.port_u16(), None | Some(443))) + { + HeaderValue::from_str(authority.host())? + } else { + HeaderValue::from_str(authority.as_str())? }; + headers.insert("Host", host_value); } } + let req = Request::from_parts(req_parts, req_body); // 1. Check if there is an available client //