From ea83edc609da0952c2919efd217339d8aaf7f403 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 21 Feb 2021 11:21:21 +0100 Subject: [PATCH] Allow status lines with missing reason phrase The spec says the reason phrase at least must be a space. However in the wild, there are sites that just ends after the status code. To be more compatible, this commit relaxes ureq's parsing. Close #316 --- src/response.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/response.rs b/src/response.rs index 5ae3dfc..4ffe496 100644 --- a/src/response.rs +++ b/src/response.rs @@ -475,7 +475,12 @@ fn parse_status_line(line: &str) -> Result<(ResponseStatusIndex, u16), Error> { } // https://tools.ietf.org/html/rfc7230#section-3.1.2 // status-line = HTTP-version SP status-code SP reason-phrase CRLF - let split: Vec<&str> = line.splitn(3, ' ').collect(); + let mut split: Vec<&str> = line.splitn(3, ' ').collect(); + if split.len() == 2 { + // As a special case, we are lenient parsing lines without reason phrase. + // This is technically against spec. "HTTP/1.1 200\r\n" + split.push(""); + } if split.len() != 3 { return Err(BadStatus.msg("Wrong number of tokens in status line")); } @@ -753,6 +758,13 @@ mod tests { assert_eq!(err.kind(), BadStatus); } + #[test] + fn parse_header_without_reason() { + let s = "HTTP/1.1 302\r\n\r\n".to_string(); + let resp = s.parse::().unwrap(); + assert_eq!(resp.status_text(), ""); + } + #[test] fn history() { let mut response0 = Response::new(302, "Found", "").unwrap();