diff options
author | metamuffin <metamuffin@disroot.org> | 2024-08-19 13:53:05 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-08-19 13:53:05 +0200 |
commit | 7db0a67f072d212697cda117bd65d658590e279e (patch) | |
tree | bba60d66721c3343a87500dbe15c02c8afd11d2f | |
parent | ad13722e62d96eb84fa72f595bf3cfd3b570155f (diff) | |
download | gnix-7db0a67f072d212697cda117bd65d658590e279e.tar gnix-7db0a67f072d212697cda117bd65d658590e279e.tar.bz2 gnix-7db0a67f072d212697cda117bd65d658590e279e.tar.zst |
remove authority from uri when translating to h1
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 18 |
4 files changed, 17 insertions, 5 deletions
@@ -595,6 +595,7 @@ dependencies = [ "futures-util", "headers", "hex", + "http", "http-body-util", "httpdate", "humansize", @@ -14,6 +14,7 @@ hyper-util = { version = "0.1.7", features = [ "tokio", ] } http-body-util = "0.1.2" +http = "1.1.0" headers = "0.4.0" percent-encoding = "2.3.1" base64 = "0.22.1" diff --git a/src/error.rs b/src/error.rs index e6f7646..636f226 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,6 +40,8 @@ pub enum ServiceError { ParseIntError(#[from] std::num::ParseIntError), #[error("invalid header")] InvalidHeader, + #[error("invalid uri")] + InvalidUri, #[error("impossible error")] Other, } diff --git a/src/main.rs b/src/main.rs index 14675b4..a13d171 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use hyper::{ header::{CONTENT_TYPE, HOST, SERVER}, http::HeaderValue, service::service_fn, - Request, Response, StatusCode, + Request, Response, StatusCode, Uri, }; use hyper_util::rt::TokioExecutor; use log::{debug, error, info, warn, LevelFilter}; @@ -222,10 +222,18 @@ async fn service( mut request: Request<Incoming>, addr: SocketAddr, ) -> Result<hyper::Response<BoxBody<bytes::Bytes, ServiceError>>, ServiceError> { - // copy uri authority used in HTTP/2 to Host header field - if let Some(host) = request.uri().authority().map(|a| a.host()) { - let host = HeaderValue::from_str(host).map_err(|_| ServiceError::InvalidHeader)?; - request.headers_mut().insert(HOST, host); + // move uri authority used in HTTP/2 to Host header field + { + let uri = request.uri_mut(); + if let Some(authority) = uri.authority() { + let host = + HeaderValue::from_str(authority.host()).map_err(|_| ServiceError::InvalidUri)?; + + let mut new_uri = http::uri::Parts::default(); + new_uri.path_and_query = uri.path_and_query().cloned(); + *uri = Uri::from_parts(new_uri).map_err(|_| ServiceError::InvalidUri)?; + request.headers_mut().insert(HOST, host); + } } debug!( |