diff --git a/ylong_http/src/h1/response/decoder.rs b/ylong_http/src/h1/response/decoder.rs index 461af7eb5ef984e8e7876097d8ef867889b5b60a..d72b9cde93d083b1b1313d82b04b1406befa614f 100644 --- a/ylong_http/src/h1/response/decoder.rs +++ b/ylong_http/src/h1/response/decoder.rs @@ -543,7 +543,7 @@ fn decode_reason(buffer: &[u8]) -> Result, HttpError> { for (i, b) in buffer.iter().enumerate() { if *b == b'\r' || *b == b'\n' { return Ok(Some(&buffer[i..])); - } else if !is_valid_byte(*b) { + } else if !is_legal_reason_byte(*b) { return Err(ErrorKind::H1(H1Error::InvalidResponse).into()); } } @@ -627,6 +627,11 @@ fn is_valid_byte(byte: u8) -> bool { byte > 0x1F && byte < 0x7F } +// reason-phase = 1*(HTAB / SP / VCHAR / obs-text) +fn is_legal_reason_byte(byte: u8) -> bool { + byte == 0x09 || byte == 0x20 || (0x21..=0x7E).contains(&byte) || (0x80..=0xFF).contains(&byte) +} + // token = 1*tchar // tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" // / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" @@ -953,7 +958,7 @@ mod ut_decoder { test_unit_invalid!("HTTP/1.2 304 OK\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); test_unit_invalid!("HTTP/1.1 3040 OK\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); test_unit_invalid!("HTTP/1.1 3 4 OK\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); - test_unit_invalid!("HTTP/1.1 304 \tK\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); + test_unit_invalid!("HTTP/1.1 304 \0K\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); test_unit_invalid!("HTTP/1.1 304 OK\r\r\nAge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); test_unit_invalid!("HTTP/1.1 304 OK\r\nA;ge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse)))); test_unit_invalid!("HTTP/1.1 304 OK\r\nA;ge:270646\r\nDate:Mon, 19 Dec 2022 01:46:59 GMT\r\nEtag:\"3147526947+gzip\"\r\n\r\nbody part".as_bytes(), Some(HttpError::from(ErrorKind::H1(H1Error::InvalidResponse))));