Skip to content

Commit

Permalink
Fixes issue #88 (empty frame panic). (#92)
Browse files Browse the repository at this point in the history
* Fixes issue #88 (empty frame panic).

* Better check for empty frame.
  • Loading branch information
meowjesty committed Apr 21, 2023
1 parent 7bf321a commit ec8eefa
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions http-body-util/src/collected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ impl<B: Buf> Collected<B> {
pub(crate) fn push_frame(&mut self, frame: Frame<B>) {
let frame = match frame.into_data() {
Ok(data) => {
self.bufs.push(data);
// Only push this frame if it has some data in it, to avoid crashing on
// `BufList::push`.
if data.has_remaining() {
self.bufs.push(data);
}
return;
}
Err(frame) => frame,
Expand Down Expand Up @@ -112,7 +116,6 @@ mod tests {
#[tokio::test]
async fn segmented_body() {
let bufs = [&b"hello"[..], &b"world"[..], &b"!"[..]];

let body = StreamBody::new(stream::iter(bufs.map(Frame::data).map(Ok::<_, Infallible>)));

let buffered = body.collect().await.unwrap();
Expand Down Expand Up @@ -161,4 +164,15 @@ mod tests {

assert_eq!(&buf.copy_to_bytes(buf.remaining())[..], b"helloworld!");
}

/// Test for issue [#88](https://github.com/hyperium/http-body/issues/88).
#[tokio::test]
async fn empty_frame() {
let bufs: [&[u8]; 1] = [&[]];

let body = StreamBody::new(stream::iter(bufs.map(Frame::data).map(Ok::<_, Infallible>)));
let buffered = body.collect().await.unwrap();

assert_eq!(buffered.to_bytes().len(), 0);
}
}

0 comments on commit ec8eefa

Please sign in to comment.