From 33dbe15760a0784ca08096485662a7c8d56d5e88 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 14 Jan 2018 13:50:38 -0800 Subject: [PATCH] use Binary for writer trait --- src/pipeline.rs | 4 ++-- src/server/h1writer.rs | 16 +++++++--------- src/server/h2writer.rs | 18 +++++++----------- src/server/mod.rs | 8 ++++---- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/pipeline.rs b/src/pipeline.rs index 85aa744ce..fbaf7399e 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -480,7 +480,7 @@ impl ProcessResponse { }, Ok(Async::Ready(Some(chunk))) => { self.iostate = IOState::Payload(body); - match io.write(chunk.as_ref()) { + match io.write(chunk.into()) { Err(err) => { info.error = Some(err.into()); return Ok(FinishingMiddlewares::init(info, self.resp)) @@ -522,7 +522,7 @@ impl ProcessResponse { break 'outter }, Frame::Chunk(Some(chunk)) => { - match io.write(chunk.as_ref()) { + match io.write(chunk) { Err(err) => { info.error = Some(err.into()); return Ok( diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index 2a2601c5d..0befa9ad3 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -6,7 +6,7 @@ use http::Version; use http::header::{HeaderValue, CONNECTION, DATE}; use helpers; -use body::Body; +use body::{Body, Binary}; use helpers::SharedBytes; use httprequest::HttpMessage; use httpresponse::HttpResponse; @@ -63,7 +63,7 @@ impl H1Writer { self.flags.contains(Flags::KEEPALIVE) && !self.flags.contains(Flags::UPGRADE) } - fn write_to_stream(&mut self) -> Result { + fn write_to_stream(&mut self) -> io::Result { let buffer = self.encoder.get_mut(); while !buffer.is_empty() { @@ -106,9 +106,7 @@ impl Writer for H1Writer { } } - fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) - -> Result - { + fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) -> io::Result { // prepare task self.flags.insert(Flags::STARTED); self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg); @@ -183,16 +181,16 @@ impl Writer for H1Writer { Ok(WriterState::Done) } - fn write(&mut self, payload: &[u8]) -> Result { + fn write(&mut self, payload: Binary) -> io::Result { self.written += payload.len() as u64; if !self.flags.contains(Flags::DISCONNECTED) { if self.flags.contains(Flags::STARTED) { // TODO: add warning, write after EOF - self.encoder.write(payload)?; + self.encoder.write(payload.as_ref())?; return Ok(WriterState::Done) } else { // might be response to EXCEPT - self.encoder.get_mut().extend_from_slice(payload) + self.encoder.get_mut().extend_from_slice(payload.as_ref()) } } @@ -203,7 +201,7 @@ impl Writer for H1Writer { } } - fn write_eof(&mut self) -> Result { + fn write_eof(&mut self) -> io::Result { self.encoder.write_eof()?; if !self.encoder.is_eof() { diff --git a/src/server/h2writer.rs b/src/server/h2writer.rs index c016de2e7..1cf1b0b61 100644 --- a/src/server/h2writer.rs +++ b/src/server/h2writer.rs @@ -7,7 +7,7 @@ use http::{Version, HttpTryFrom, Response}; use http::header::{HeaderValue, CONNECTION, TRANSFER_ENCODING, DATE, CONTENT_LENGTH}; use helpers; -use body::Body; +use body::{Body, Binary}; use helpers::SharedBytes; use httprequest::HttpMessage; use httpresponse::HttpResponse; @@ -52,7 +52,7 @@ impl H2Writer { } } - fn write_to_stream(&mut self) -> Result { + fn write_to_stream(&mut self) -> io::Result { if !self.flags.contains(Flags::STARTED) { return Ok(WriterState::Done) } @@ -115,11 +115,7 @@ impl Writer for H2Writer { Ok(Async::Ready(())) } - fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) - -> Result - { - // trace!("Prepare response with status: {:?}", msg.status()); - + fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) -> io::Result { // prepare response self.flags.insert(Flags::STARTED); self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg); @@ -183,16 +179,16 @@ impl Writer for H2Writer { } } - fn write(&mut self, payload: &[u8]) -> Result { + fn write(&mut self, payload: Binary) -> io::Result { self.written = payload.len() as u64; if !self.flags.contains(Flags::DISCONNECTED) { if self.flags.contains(Flags::STARTED) { // TODO: add warning, write after EOF - self.encoder.write(payload)?; + self.encoder.write(payload.as_ref())?; } else { // might be response for EXCEPT - self.encoder.get_mut().extend_from_slice(payload) + self.encoder.get_mut().extend_from_slice(payload.as_ref()) } } @@ -203,7 +199,7 @@ impl Writer for H2Writer { } } - fn write_eof(&mut self) -> Result { + fn write_eof(&mut self) -> io::Result { self.encoder.write_eof()?; self.flags.insert(Flags::EOF); diff --git a/src/server/mod.rs b/src/server/mod.rs index 566712077..44e2c7676 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -20,6 +20,7 @@ mod utils; pub use self::srv::HttpServer; pub use self::settings::ServerSettings; +use body::Binary; use error::Error; use httprequest::{HttpMessage, HttpRequest}; use httpresponse::HttpResponse; @@ -96,12 +97,11 @@ pub enum WriterState { pub trait Writer { fn written(&self) -> u64; - fn start(&mut self, req: &mut HttpMessage, resp: &mut HttpResponse) - -> Result; + fn start(&mut self, req: &mut HttpMessage, resp: &mut HttpResponse) -> io::Result; - fn write(&mut self, payload: &[u8]) -> Result; + fn write(&mut self, payload: Binary) -> io::Result; - fn write_eof(&mut self) -> Result; + fn write_eof(&mut self) -> io::Result; fn flush(&mut self) -> Poll<(), io::Error>;