Skip to content

Commit

Permalink
fix(http): Consume entire chunked encoding message
Browse files Browse the repository at this point in the history
The trailing newline otherwise breaks keepalive after a chunked request.
  • Loading branch information
sfackler committed Mar 1, 2017
1 parent 1e740fb commit 4147fcd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/http/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,17 @@ impl<R: Read> Read for HttpReader<R> {
trace!("Chunked read, remaining={:?}", rem);

if rem == 0 {
if opt_remaining.is_none() {
try!(eat(body, LINE_ENDING.as_bytes()));
}

*opt_remaining = Some(0);

// chunk of size 0 signals the end of the chunked stream
// if the 0 digit was missing from the stream, it would
// be an InvalidInput error instead.
trace!("end of chunked");

return Ok(0)
}

Expand Down Expand Up @@ -1098,6 +1103,19 @@ mod tests {
assert_eq!(r.read(&mut buf).unwrap(), 0);
}

#[test]
fn test_read_chunked_fully_consumes() {
let mut r = super::HttpReader::ChunkedReader(MockStream::with_input(b"0\r\n\r\n"), None);
let mut buf = [0; 1];
assert_eq!(r.read(&mut buf).unwrap(), 0);
assert_eq!(r.read(&mut buf).unwrap(), 0);

match r {
super::HttpReader::ChunkedReader(mut r, _) => assert_eq!(r.read(&mut buf).unwrap(), 0),
_ => unreachable!(),
}
}

#[test]
fn test_message_get_incoming_invalid_content_length() {
let raw = MockStream::with_input(
Expand Down

0 comments on commit 4147fcd

Please sign in to comment.