From 4147fcd0d688b6d5b8d6b32f26c147819321a390 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 28 Feb 2017 20:18:58 -0800 Subject: [PATCH] fix(http): Consume entire chunked encoding message The trailing newline otherwise breaks keepalive after a chunked request. --- src/http/h1.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/http/h1.rs b/src/http/h1.rs index 2c2097104b..c5ca944280 100644 --- a/src/http/h1.rs +++ b/src/http/h1.rs @@ -591,12 +591,17 @@ impl Read for HttpReader { 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) } @@ -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(