Skip to content

Commit

Permalink
parse responses that do not include a reason phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 4, 2016
1 parent 9083050 commit 2e2344d
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/lib.rs
Expand Up @@ -322,8 +322,29 @@ impl<'h, 'b> Response<'h, 'b> {
self.version = Some(complete!(parse_version(&mut bytes)));
space!(bytes or Error::Version);
self.code = Some(complete!(parse_code(&mut bytes)));
space!(bytes or Error::Status);
self.reason = Some(complete!(parse_reason(&mut bytes)));

// RFC7230 says there must be 'SP' and then reason-phrase, but admits
// its only for legacy reasons. With the reason-phrase completely
// optional (and preferred to be omitted) in HTTP2, we'll just
// handle any response that doesn't include a reason-phrase, because
// it's more lenient, and we don't care anyways.
//
// So, a SP means parse a reason-phrase.
// A newline means go to headers.
// Anything else we'll say is a malformed status.
match next!(bytes) {
b' ' => {
bytes.slice();
self.reason = Some(complete!(parse_reason(&mut bytes)));
},
b'\r' => {
expect!(bytes.next() == b'\n' => Err(Error::Status));
self.reason = Some("");
},
b'\n' => self.reason = Some(""),
_ => return Err(Error::Status),
}


let len = orig_len - bytes.len();
let headers_len = complete!(parse_headers_iter(&mut self.headers, &mut bytes));
Expand Down Expand Up @@ -834,6 +855,16 @@ mod tests {
}
}

res! {
test_response_reason_missing_no_space,
b"HTTP/1.1 200\r\n\r\n",
|res| {
assert_eq!(res.version.unwrap(), 1);
assert_eq!(res.code.unwrap(), 200);
assert_eq!(res.reason.unwrap(), "");
}
}

res! {
test_response_reason_with_space_and_tab,
b"HTTP/1.1 101 Switching Protocols\t\r\n\r\n",
Expand Down

0 comments on commit 2e2344d

Please sign in to comment.