Error when LimitedRead gets too few bytes. (#106)

This commit is contained in:
Jacob Hoffman-Andrews
2020-07-05 14:25:11 -07:00
committed by GitHub
parent 6e69f6cf69
commit f16f1a5e47

View File

@@ -631,6 +631,15 @@ impl<R: Read> Read for LimitedRead<R> {
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<R: Read> Read for LimitedRead<R> {
}
}
#[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<R: Read> From<LimitedRead<R>> for Stream
where
Stream: From<R>,