-
Notifications
You must be signed in to change notification settings - Fork 9
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
Improve handling of Transfer-Encoding errors #46
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -222,12 +222,23 @@ fn request_body_len(request: &Request) -> Result<usize, ParseError> { | |||||||
// the Transfer-Encoding overrides the Content-Length | ||||||||
if request | ||||||||
.headers_with_name("Transfer-Encoding") | ||||||||
.next() | ||||||||
.is_some() | ||||||||
.any(|h| { | ||||||||
std::str::from_utf8(h.value.0.as_bytes()) | ||||||||
.unwrap_or("") | ||||||||
.split(',') | ||||||||
.any(|v| v.trim() != "identity") | ||||||||
}) | ||||||||
{ | ||||||||
Err(ParseError( | ||||||||
"Transfer-Encoding not supported yet".to_string(), | ||||||||
)) | ||||||||
let bad_values = request | ||||||||
.headers_with_name("Transfer-Encoding") | ||||||||
.map(|h| std::str::from_utf8(h.value.0.as_bytes()).unwrap_or("")) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
.collect::<Vec<_>>() | ||||||||
.join(", "); | ||||||||
|
||||||||
Err(ParseError(format!( | ||||||||
"Transfer-Encoding other than identity not supported yet: {:?}", | ||||||||
bad_values | ||||||||
Comment on lines
+239
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
))) | ||||||||
} else if let Some(h) = request.headers_with_name("Content-Length").next() { | ||||||||
// If a valid Content-Length header field is present without Transfer-Encoding, its decimal value | ||||||||
// defines the expected message body length in octets. | ||||||||
|
@@ -258,12 +269,23 @@ fn response_body_len(response: &Response) -> Result<usize, ParseError> { | |||||||
|
||||||||
if response | ||||||||
.headers_with_name("Transfer-Encoding") | ||||||||
.next() | ||||||||
.is_some() | ||||||||
.any(|h| { | ||||||||
std::str::from_utf8(h.value.0.as_bytes()) | ||||||||
.unwrap_or("") | ||||||||
.split(',') | ||||||||
.any(|v| v.trim() != "identity") | ||||||||
}) | ||||||||
{ | ||||||||
Err(ParseError( | ||||||||
"Transfer-Encoding not supported yet".to_string(), | ||||||||
)) | ||||||||
let bad_values = response | ||||||||
.headers_with_name("Transfer-Encoding") | ||||||||
.map(|h| std::str::from_utf8(h.value.0.as_bytes()).unwrap_or("")) | ||||||||
.collect::<Vec<_>>() | ||||||||
.join(", "); | ||||||||
|
||||||||
Err(ParseError(format!( | ||||||||
"Transfer-Encoding other than identity not supported yet: {:?}", | ||||||||
bad_values | ||||||||
))) | ||||||||
} else if let Some(h) = response.headers_with_name("Content-Length").next() { | ||||||||
// If a valid Content-Length header field is present without Transfer-Encoding, its decimal value | ||||||||
// defines the expected message body length in octets. | ||||||||
|
@@ -363,6 +385,46 @@ mod tests { | |||||||
Content-Length: 14\r\n\r\n\ | ||||||||
{\"foo\": \"bar\"}"; | ||||||||
|
||||||||
const TEST_REQUEST_TRANSFER_ENCODING_IDENTITY: &[u8] = b"\ | ||||||||
POST / HTTP/1.1\r\n\ | ||||||||
Transfer-Encoding: identity\r\n\ | ||||||||
Content-Length: 12\r\n\r\n\ | ||||||||
Hello World!"; | ||||||||
|
||||||||
const TEST_RESPONSE_TRANSFER_ENCODING_IDENTITY: &[u8] = b"\ | ||||||||
HTTP/1.1 200 OK\r\n\ | ||||||||
Transfer-Encoding: identity\r\n\ | ||||||||
Content-Length: 12\r\n\r\n\ | ||||||||
Hello World!"; | ||||||||
|
||||||||
const TEST_REQUEST_TRANSFER_ENCODING_CHUNKED: &[u8] = b"\ | ||||||||
POST / HTTP/1.1\r\n\ | ||||||||
Transfer-Encoding: chunked\r\n\r\n\ | ||||||||
a\r\n\ | ||||||||
Hello World!\r\n\ | ||||||||
0\r\n\r\n"; | ||||||||
|
||||||||
const TEST_RESPONSE_TRANSFER_ENCODING_CHUNKED: &[u8] = b"\ | ||||||||
HTTP/1.1 200 OK\r\n\ | ||||||||
Transfer-Encoding: chunked\r\n\r\n\ | ||||||||
a\r\n\ | ||||||||
Hello World!\r\n\ | ||||||||
0\r\n\r\n"; | ||||||||
|
||||||||
const TEST_REQUEST_TRANSFER_ENCODING_MULTIPLE: &[u8] = b"\ | ||||||||
POST / HTTP/1.1\r\n\ | ||||||||
Transfer-Encoding: chunked, identity\r\n\r\n\ | ||||||||
a\r\n\ | ||||||||
Hello World!\r\n\ | ||||||||
0\r\n\r\n"; | ||||||||
|
||||||||
const TEST_RESPONSE_TRANSFER_ENCODING_MULTIPLE: &[u8] = b"\ | ||||||||
HTTP/1.1 200 OK\r\n\ | ||||||||
Transfer-Encoding: chunked, identity\r\n\r\n\ | ||||||||
a\r\n\ | ||||||||
Hello World!\r\n\ | ||||||||
0\r\n\r\n"; | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_request() { | ||||||||
let req = parse_request(TEST_REQUEST).unwrap(); | ||||||||
|
@@ -496,4 +558,44 @@ mod tests { | |||||||
|
||||||||
assert_eq!(value.span(), "{\"foo\": \"bar\"}"); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_request_transfer_encoding_identity() { | ||||||||
let req = parse_request(TEST_REQUEST_TRANSFER_ENCODING_IDENTITY).unwrap(); | ||||||||
assert_eq!(req.body.unwrap().span(), b"Hello World!".as_slice()); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_response_transfer_encoding_identity() { | ||||||||
let res = parse_response(TEST_RESPONSE_TRANSFER_ENCODING_IDENTITY).unwrap(); | ||||||||
assert_eq!(res.body.unwrap().span(), b"Hello World!".as_slice()); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_request_transfer_encoding_chunked() { | ||||||||
let err = parse_request(TEST_REQUEST_TRANSFER_ENCODING_CHUNKED).unwrap_err(); | ||||||||
assert!(matches!(err, ParseError(_))); | ||||||||
assert!(err.to_string().contains("Transfer-Encoding other than identity not supported yet")); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_response_transfer_encoding_chunked() { | ||||||||
let err = parse_response(TEST_RESPONSE_TRANSFER_ENCODING_CHUNKED).unwrap_err(); | ||||||||
assert!(matches!(err, ParseError(_))); | ||||||||
assert!(err.to_string().contains("Transfer-Encoding other than identity not supported yet")); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_request_transfer_encoding_multiple() { | ||||||||
let err = parse_request(TEST_REQUEST_TRANSFER_ENCODING_MULTIPLE).unwrap_err(); | ||||||||
assert!(matches!(err, ParseError(_))); | ||||||||
assert!(err.to_string().contains("Transfer-Encoding other than identity not supported yet")); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_parse_response_transfer_encoding_multiple() { | ||||||||
let err = parse_response(TEST_RESPONSE_TRANSFER_ENCODING_MULTIPLE).unwrap_err(); | ||||||||
assert!(matches!(err, ParseError(_))); | ||||||||
assert!(err.to_string().contains("Transfer-Encoding other than identity not supported yet")); | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be sufficient to just handle the first header with this key, ie