Skip to content

Test and fix a regression in parsing partial requests in the 1.9.x series #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,12 +955,14 @@ fn parse_token<'a>(bytes: &mut Bytes<'a>) -> Result<&'a str> {
pub fn parse_uri<'a>(bytes: &mut Bytes<'a>) -> Result<&'a str> {
let start = bytes.pos();
simd::match_uri_vectored(bytes);
// URI must have at least one char
if bytes.pos() == start {
return Err(Error::Token);
}
let end = bytes.pos();

if next!(bytes) == b' ' {
// URI must have at least one char
if end == start {
return Err(Error::Token);
}

return Ok(Status::Complete(
// SAFETY: all bytes up till `i` must have been `is_token` and therefore also utf-8.
unsafe { str::from_utf8_unchecked(bytes.slice_skip(1)) },
Expand Down Expand Up @@ -1547,6 +1549,34 @@ mod tests {
}
}

// A single byte which is part of a method is not invalid
req! {
test_request_one_byte_method,
b"G", Ok(Status::Partial),
|_req| {}
}

// A subset of a method is a partial method, not invalid
req! {
test_request_partial_method,
b"GE", Ok(Status::Partial),
|_req| {}
}

// A method, without the delimiting space, is a partial request
req! {
test_request_method_no_delimiter,
b"GET", Ok(Status::Partial),
|_req| {}
}

// Regression test: assert that a partial read with just the method and
// space results in a partial, rather than a token error from uri parsing.
req! {
test_request_method_only,
b"GET ", Ok(Status::Partial),
|_req| {}
}

req! {
test_request_partial,
Expand All @@ -1560,6 +1590,18 @@ mod tests {
|_req| {}
}

req! {
test_request_method_path_no_delimiter,
b"GET /", Ok(Status::Partial),
|_req| {}
}

req! {
test_request_method_path_only,
b"GET / ", Ok(Status::Partial),
|_req| {}
}

req! {
test_request_partial_parses_headers_as_much_as_it_can,
b"GET / HTTP/1.1\r\nHost: yolo\r\n",
Expand Down Expand Up @@ -1794,6 +1836,24 @@ mod tests {
}
}

/// Check all subset permutations of a partial request line with no headers
#[test]
fn partial_permutations() {
let req_str = "GET / HTTP/1.1\r\n\r\n";
let mut headers = [EMPTY_HEADER; NUM_OF_HEADERS];
let mut req = Request::new(&mut headers[..]);
for i in 0..req_str.len() {
let status = req.parse(req_str[..i].as_bytes());
assert_eq!(
status,
Ok(Status::Partial),
"partial request line should return partial. \
Portion which failed: '{seg}' (below {i})",
seg = &req_str[..i]
);
}
}

static RESPONSE_WITH_WHITESPACE_BETWEEN_HEADER_NAME_AND_COLON: &[u8] =
b"HTTP/1.1 200 OK\r\nAccess-Control-Allow-Credentials : true\r\nBread: baguette\r\n\r\n";

Expand Down
Loading