Skip to content

Commit

Permalink
Merge pull request #52 from bugadani/panic
Browse files Browse the repository at this point in the history
Don't panic if chunked encoding contains no chunk length
  • Loading branch information
rmja committed Oct 24, 2023
2 parents a21c15a + 7c090d7 commit 2f71cc9
Showing 1 changed file with 19 additions and 4 deletions.
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

0 comments on commit 2f71cc9

Please sign in to comment.