From f16f1a5e47f1b1ad04a106bd3c500d326cf64511 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Sun, 5 Jul 2020 14:25:11 -0700 Subject: [PATCH] Error when LimitedRead gets too few bytes. (#106) --- src/response.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/response.rs b/src/response.rs index 571bba2..3611567 100644 --- a/src/response.rs +++ b/src/response.rs @@ -631,6 +631,15 @@ impl Read for LimitedRead { buf }; match self.reader.read(from) { + // https://tools.ietf.org/html/rfc7230#page-33 + // If the sender closes the connection or + // the recipient times out before the indicated number of octets are + // received, the recipient MUST consider the message to be + // incomplete and close the connection. + Ok(0) => Err(IoError::new( + ErrorKind::InvalidData, + "response body closed before all bytes were read", + )), Ok(amount) => { self.position += amount; Ok(amount) @@ -640,6 +649,15 @@ impl Read for LimitedRead { } } +#[test] +fn short_read() { + use std::io::Cursor; + let mut lr = LimitedRead::new(Cursor::new(vec![b'a'; 3]), 10); + let mut buf = vec![0; 1000]; + let result = lr.read_to_end(&mut buf); + assert!(result.is_err()); +} + impl From> for Stream where Stream: From,