From 7db0a67f072d212697cda117bd65d658590e279e Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 19 Aug 2024 13:53:05 +0200 Subject: remove authority from uri when translating to h1 --- Cargo.lock | 1 + Cargo.toml | 1 + src/error.rs | 2 ++ src/main.rs | 18 +++++++++++++----- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd3a1bf..b8284b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -595,6 +595,7 @@ dependencies = [ "futures-util", "headers", "hex", + "http", "http-body-util", "httpdate", "humansize", diff --git a/Cargo.toml b/Cargo.toml index 6653d38..3f89a8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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, addr: SocketAddr, ) -> Result>, 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!( -- cgit v1.2.3-70-g09d2