Skip to content
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

Don't panic if chunked encoding contains no chunk length #52

Merged
merged 1 commit into from
Oct 24, 2023
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
23 changes: 19 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,13 @@ where
return Err(Error::Codec);
}
} else {
if total_read == 0 || header_buf[total_read - 1] != b'\r' {
return Err(Error::Codec);
}
break 'read_size;
}
}

if header_buf[total_read - 1] != b'\r' {
return Err(Error::Codec);
}

let hex_digits = total_read - 1;

// Prepend hex with zeros
Expand Down Expand Up @@ -687,6 +686,7 @@ mod tests {
reader::BufferingReader,
request::Method,
response::{ChunkState, ChunkedBodyReader, Response},
Error,
};

#[tokio::test]
Expand Down Expand Up @@ -727,6 +727,21 @@ mod tests {
assert_eq!(11, response.body().discard().await.unwrap());
}

#[tokio::test]
async fn incorrect_fragment_length_does_not_panic() {
let mut response =
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n\n\r\nHELLO WORLD\r\n0\r\n\r\n".as_slice();
let mut header_buf = [0; 200];

let response = Response::read(&mut response, Method::GET, &mut header_buf)
.await
.unwrap();

let error = response.body().read_to_end().await.unwrap_err();

assert!(matches!(error, Error::Codec))
}

#[tokio::test]
async fn can_read_with_chunked_encoding() {
let mut response =
Expand Down