From fc5b9cce3176776e4c916cd1b907b1649a538f00 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 17 Jul 2017 11:19:32 -0700 Subject: [PATCH] fix(http): fix encoding when buffer is full --- src/http/conn.rs | 2 ++ src/http/h1/encode.rs | 5 ++++- src/http/io.rs | 7 ++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/http/conn.rs b/src/http/conn.rs index b3eb699956..c88888620f 100644 --- a/src/http/conn.rs +++ b/src/http/conn.rs @@ -803,6 +803,8 @@ mod tests { #[test] fn test_conn_body_write_length() { + extern crate pretty_env_logger; + let _ = pretty_env_logger::init(); let _: Result<(), ()> = future::lazy(|| { let io = AsyncIo::new_buf(vec![], 0); let mut conn = Conn::<_, http::Chunk, ServerTransaction>::new(io, Default::default()); diff --git a/src/http/h1/encode.rs b/src/http/h1/encode.rs index ed643898cc..3f374ddf8c 100644 --- a/src/http/h1/encode.rs +++ b/src/http/h1/encode.rs @@ -54,9 +54,12 @@ impl Encoder { chunked.encode(w, msg) }, Kind::Length(ref mut remaining) => { + if msg.is_empty() { + return Ok(0); + } let n = { let max = cmp::min(*remaining as usize, msg.len()); - trace!("sized write, len = {}", max); + trace!("sized write, len = {}, remaining = {}", max, remaining); let slice = &msg[..max]; try!(w.write_atomic(&[slice])) diff --git a/src/http/io.rs b/src/http/io.rs index 0da9b9597c..88c054e6a9 100644 --- a/src/http/io.rs +++ b/src/http/io.rs @@ -130,7 +130,12 @@ impl Buffered { impl Write for Buffered { fn write(&mut self, data: &[u8]) -> io::Result { - Ok(self.write_buf.buffer(data)) + let n = self.write_buf.buffer(data); + if n == 0 { + Err(io::Error::from(io::ErrorKind::WouldBlock)) + } else { + Ok(n) + } } fn flush(&mut self) -> io::Result<()> {