From c303bfb09d626869e99cf4642aabc6b5e8cd1de3 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sat, 6 Dec 2025 02:00:33 -0800 Subject: [PATCH] Net: work around handling of empty PathAndQuery in `http`. An absolute http::uri::Uri with an empty but set PathAndQuery ("") will pretend that the path is "/" for comparison, display or HTTP request construction purposes. However, a relative URI constructed from such PathAndQuery will be empty. A directory URI without a path (i.e. "https://example.com") thus resulted in an empty path in the request line and 400 Bad Request from the server. Fixes #97. --- Cargo.lock | 11 ++--------- Cargo.toml | 2 +- src/net/http.rs | 17 ++++++++++------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a9a8130..63237e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,12 +157,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "foreign-types" version = "0.3.2" @@ -201,12 +195,11 @@ checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" [[package]] name = "http" -version = "1.3.1" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" dependencies = [ "bytes", - "fnv", "itoa", ] diff --git a/Cargo.toml b/Cargo.toml index 6d06058..2336104 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["cdylib"] base64 = "0.22.1" bytes = "1.10.1" constcat = "0.6.1" -http = "1.3.1" +http = "1.4.0" http-body = "1.0.1" http-body-util = "0.1.3" http-serde = "2.1.1" diff --git a/src/net/http.rs b/src/net/http.rs index 9152adc..15e905a 100644 --- a/src/net/http.rs +++ b/src/net/http.rs @@ -107,18 +107,21 @@ impl HttpClient for NgxHttpClient<'_> { ::Data: Send, ::Error: StdError + Send + Sync, { - let uri = req.uri().clone(); + const DEFAULT_PATH: http::uri::PathAndQuery = http::uri::PathAndQuery::from_static("/"); + + let path_and_query = req + .uri() + .path_and_query() + .filter(|x| x.as_str() != "/") // can be empty ("") + .cloned() + .unwrap_or(DEFAULT_PATH); + + let uri = core::mem::replace(req.uri_mut(), path_and_query.into()); let authority = uri .authority() .ok_or(HttpClientError::Uri("missing authority"))?; - let path_and_query = uri - .path_and_query() - .ok_or(HttpClientError::Uri("missing path"))?; - - *req.uri_mut() = path_and_query.clone().into(); - { let headers = req.headers_mut(); headers.insert(