Skip to content

Commit 63ba8b0

Browse files
committed
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.
1 parent 5ad82a1 commit 63ba8b0

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

Cargo.lock

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crate-type = ["cdylib"]
1313
base64 = "0.22.1"
1414
bytes = "1.10.1"
1515
constcat = "0.6.1"
16-
http = "1.3.1"
16+
http = "1.4.0"
1717
http-body = "1.0.1"
1818
http-body-util = "0.1.3"
1919
http-serde = "2.1.1"

src/net/http.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,22 @@ impl HttpClient for NgxHttpClient<'_> {
107107
<B as Body>::Data: Send,
108108
<B as Body>::Error: StdError + Send + Sync,
109109
{
110-
let uri = req.uri().clone();
110+
const DEFAULT_PATH: http::uri::PathAndQuery = http::uri::PathAndQuery::from_static("/");
111+
112+
let path_and_query = req
113+
.uri()
114+
.path_and_query()
115+
// filter empty ("") values that are represented as "/"
116+
.filter(|x| x.as_str() != "/")
117+
.cloned()
118+
.unwrap_or(DEFAULT_PATH);
119+
120+
let uri = core::mem::replace(req.uri_mut(), path_and_query.into());
111121

112122
let authority = uri
113123
.authority()
114124
.ok_or(HttpClientError::Uri("missing authority"))?;
115125

116-
let path_and_query = uri
117-
.path_and_query()
118-
.ok_or(HttpClientError::Uri("missing path"))?;
119-
120-
*req.uri_mut() = path_and_query.clone().into();
121-
122126
{
123127
let headers = req.headers_mut();
124128
headers.insert(

0 commit comments

Comments
 (0)