From 1e740fbcc3fb60af2fe8d2227457fa29582151c3 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 27 Feb 2017 14:57:12 -0800 Subject: [PATCH] fix(http): fix returning EarlyEof if supplied buffer is zero-len --- src/http/h1.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/http/h1.rs b/src/http/h1.rs index 77a3dda96d..2c2097104b 100644 --- a/src/http/h1.rs +++ b/src/http/h1.rs @@ -560,6 +560,9 @@ impl fmt::Debug for HttpReader { impl Read for HttpReader { fn read(&mut self, buf: &mut [u8]) -> io::Result { + if buf.is_empty() { + return Ok(0); + } match *self { SizedReader(ref mut body, ref mut remaining) => { trace!("Sized read, remaining={:?}", remaining); @@ -1076,6 +1079,25 @@ mod tests { assert_eq!(e.description(), "early eof"); } + #[test] + fn test_read_sized_zero_len_buf() { + let mut r = super::HttpReader::SizedReader(MockStream::with_input(b"foo bar"), 7); + let mut buf = [0u8; 0]; + assert_eq!(r.read(&mut buf).unwrap(), 0); + } + + #[test] + fn test_read_chunked_zero_len_buf() { + let mut r = super::HttpReader::ChunkedReader(MockStream::with_input(b"\ + 7\r\n\ + foo bar\ + 0\r\n\r\n\ + "), None); + + let mut buf = [0u8; 0]; + assert_eq!(r.read(&mut buf).unwrap(), 0); + } + #[test] fn test_message_get_incoming_invalid_content_length() { let raw = MockStream::with_input(