Skip to content

Commit 919c05f

Browse files
vkilljbr
authored andcommitted
Fix server url decode
1 parent 005b94c commit 919c05f

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/server/decode.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ where
131131
}
132132

133133
fn url_from_httparse_req(req: &httparse::Request<'_, '_>) -> http_types::Result<Url> {
134-
let path = req.path.ok_or_else(|| format_err!("No uri found"))?;
134+
let path_and_query = req.path.ok_or_else(|| format_err!("No uri found"))?;
135+
135136
let host = req
136137
.headers
137138
.iter()
@@ -141,15 +142,25 @@ fn url_from_httparse_req(req: &httparse::Request<'_, '_>) -> http_types::Result<
141142

142143
let host = std::str::from_utf8(host)?;
143144

144-
if path.starts_with("http://") || path.starts_with("https://") {
145-
Ok(Url::parse(path)?)
146-
} else if path.starts_with('/') {
145+
if path_and_query.starts_with("http://") || path_and_query.starts_with("https://") {
146+
Ok(Url::parse(path_and_query)?)
147+
} else if path_and_query.starts_with('/') {
147148
let mut url = Url::parse(&format!("http://{}/", host))?;
148149
// path might start with `//`, so set the path explicitly using `set_path`
149-
url.set_path(path);
150+
151+
let mut split = path_and_query.split("?");
152+
if let Some(path) = split.next() {
153+
url.set_path(path);
154+
} else {
155+
return Err(format_err!("unexpected uri format"));
156+
}
157+
if let Some(query) = split.next() {
158+
url.set_query(Some(query));
159+
}
160+
150161
Ok(url)
151162
} else if req.method.unwrap().eq_ignore_ascii_case("connect") {
152-
Ok(Url::parse(&format!("http://{}/", path))?)
163+
Ok(Url::parse(&format!("http://{}/", path_and_query))?)
153164
} else {
154165
Err(format_err!("unexpected uri format"))
155166
}
@@ -233,4 +244,15 @@ mod tests {
233244
},
234245
)
235246
}
247+
248+
#[test]
249+
fn url_for_query() {
250+
httparse_req(
251+
"GET /foo?bar=1 HTTP/1.1\r\nHost: server.example.com:443\r\n",
252+
|req| {
253+
let url = url_from_httparse_req(&req).unwrap();
254+
assert_eq!(url.as_str(), "http://server.example.com:443/foo?bar=1");
255+
},
256+
)
257+
}
236258
}

0 commit comments

Comments
 (0)