From 6c709b33cc1ab2a15e759b75e12b273ea8ffad17 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 10 Mar 2018 10:42:46 -0800 Subject: [PATCH] return error on write zero bytes --- CHANGES.md | 6 +++--- src/json.rs | 4 ++-- src/server/h1writer.rs | 30 ++++++++++++------------------ 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6bfb3b77..6c3de104 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,13 +1,13 @@ # Changes -## 0.4.6 (2018-03-09) +## 0.4.6 (2018-03-10) * Fix client cookie handling -* Fix CORS middleware #117 - * Fix json content type detection +* Fix CORS middleware #117 + * Optimize websockets stream support diff --git a/src/json.rs b/src/json.rs index e655c5f4..04bf13d5 100644 --- a/src/json.rs +++ b/src/json.rs @@ -196,8 +196,8 @@ mod tests { let mut req = HttpRequest::default(); req.headers_mut().insert(header::CONTENT_TYPE, - header::HeaderValue::from_static("application/json")); - let mut json = req.json::().content_type("text/json"); + header::HeaderValue::from_static("application/text")); + let mut json = req.json::(); assert_eq!(json.poll().err().unwrap(), JsonPayloadError::ContentType); let mut req = HttpRequest::default(); diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index 72e34a1e..3ca735d4 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -68,22 +68,22 @@ impl H1Writer { self.flags.contains(Flags::KEEPALIVE) && !self.flags.contains(Flags::UPGRADE) } - fn write_data(&mut self, data: &[u8]) -> io::Result<(usize, bool)> { + fn write_data(&mut self, data: &[u8]) -> io::Result { let mut written = 0; while written < data.len() { match self.stream.write(&data[written..]) { Ok(0) => { self.disconnected(); - return Ok((0, true)); + return Err(io::Error::new(io::ErrorKind::WriteZero, "")) }, Ok(n) => written += n, Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { - return Ok((written, false)) + return Ok(written) } Err(err) => return Err(err), } } - Ok((written, false)) + Ok(written) } } @@ -211,12 +211,10 @@ impl Writer for H1Writer { // shortcut for upgraded connection if self.flags.contains(Flags::UPGRADE) { if self.buffer.is_empty() { - match self.write_data(payload.as_ref())? { - (_, true) => return Ok(WriterState::Done), - (n, false) => if payload.len() < n { - self.buffer.extend_from_slice(&payload.as_ref()[n..]); - return Ok(WriterState::Done); - } + let n = self.write_data(payload.as_ref())?; + if payload.len() < n { + self.buffer.extend_from_slice(&payload.as_ref()[n..]); + return Ok(WriterState::Done); } } else { self.buffer.extend(payload); @@ -255,14 +253,10 @@ impl Writer for H1Writer { fn poll_completed(&mut self, shutdown: bool) -> Poll<(), io::Error> { if !self.buffer.is_empty() { let buf: &[u8] = unsafe{mem::transmute(self.buffer.as_ref())}; - match self.write_data(buf)? { - (_, true) => (), - (n, false) => { - let _ = self.buffer.split_to(n); - if self.buffer.len() > self.buffer_capacity { - return Ok(Async::NotReady) - } - } + let written = self.write_data(buf)?; + let _ = self.buffer.split_to(written); + if self.buffer.len() > self.buffer_capacity { + return Ok(Async::NotReady) } } if shutdown {