1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

use Binary for writer trait

This commit is contained in:
Nikolay Kim 2018-01-14 13:50:38 -08:00
parent e95c7dfc29
commit 33dbe15760
4 changed files with 20 additions and 26 deletions

View File

@ -480,7 +480,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
}, },
Ok(Async::Ready(Some(chunk))) => { Ok(Async::Ready(Some(chunk))) => {
self.iostate = IOState::Payload(body); self.iostate = IOState::Payload(body);
match io.write(chunk.as_ref()) { match io.write(chunk.into()) {
Err(err) => { Err(err) => {
info.error = Some(err.into()); info.error = Some(err.into());
return Ok(FinishingMiddlewares::init(info, self.resp)) return Ok(FinishingMiddlewares::init(info, self.resp))
@ -522,7 +522,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
break 'outter break 'outter
}, },
Frame::Chunk(Some(chunk)) => { Frame::Chunk(Some(chunk)) => {
match io.write(chunk.as_ref()) { match io.write(chunk) {
Err(err) => { Err(err) => {
info.error = Some(err.into()); info.error = Some(err.into());
return Ok( return Ok(

View File

@ -6,7 +6,7 @@ use http::Version;
use http::header::{HeaderValue, CONNECTION, DATE}; use http::header::{HeaderValue, CONNECTION, DATE};
use helpers; use helpers;
use body::Body; use body::{Body, Binary};
use helpers::SharedBytes; use helpers::SharedBytes;
use httprequest::HttpMessage; use httprequest::HttpMessage;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -63,7 +63,7 @@ impl<T: AsyncWrite> H1Writer<T> {
self.flags.contains(Flags::KEEPALIVE) && !self.flags.contains(Flags::UPGRADE) self.flags.contains(Flags::KEEPALIVE) && !self.flags.contains(Flags::UPGRADE)
} }
fn write_to_stream(&mut self) -> Result<WriterState, io::Error> { fn write_to_stream(&mut self) -> io::Result<WriterState> {
let buffer = self.encoder.get_mut(); let buffer = self.encoder.get_mut();
while !buffer.is_empty() { while !buffer.is_empty() {
@ -106,9 +106,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
} }
} }
fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) -> io::Result<WriterState> {
-> Result<WriterState, io::Error>
{
// prepare task // prepare task
self.flags.insert(Flags::STARTED); self.flags.insert(Flags::STARTED);
self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg); self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg);
@ -183,16 +181,16 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
Ok(WriterState::Done) Ok(WriterState::Done)
} }
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error> { fn write(&mut self, payload: Binary) -> io::Result<WriterState> {
self.written += payload.len() as u64; self.written += payload.len() as u64;
if !self.flags.contains(Flags::DISCONNECTED) { if !self.flags.contains(Flags::DISCONNECTED) {
if self.flags.contains(Flags::STARTED) { if self.flags.contains(Flags::STARTED) {
// TODO: add warning, write after EOF // TODO: add warning, write after EOF
self.encoder.write(payload)?; self.encoder.write(payload.as_ref())?;
return Ok(WriterState::Done) return Ok(WriterState::Done)
} else { } else {
// might be response to EXCEPT // 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<T: AsyncWrite> Writer for H1Writer<T> {
} }
} }
fn write_eof(&mut self) -> Result<WriterState, io::Error> { fn write_eof(&mut self) -> io::Result<WriterState> {
self.encoder.write_eof()?; self.encoder.write_eof()?;
if !self.encoder.is_eof() { if !self.encoder.is_eof() {

View File

@ -7,7 +7,7 @@ use http::{Version, HttpTryFrom, Response};
use http::header::{HeaderValue, CONNECTION, TRANSFER_ENCODING, DATE, CONTENT_LENGTH}; use http::header::{HeaderValue, CONNECTION, TRANSFER_ENCODING, DATE, CONTENT_LENGTH};
use helpers; use helpers;
use body::Body; use body::{Body, Binary};
use helpers::SharedBytes; use helpers::SharedBytes;
use httprequest::HttpMessage; use httprequest::HttpMessage;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -52,7 +52,7 @@ impl H2Writer {
} }
} }
fn write_to_stream(&mut self) -> Result<WriterState, io::Error> { fn write_to_stream(&mut self) -> io::Result<WriterState> {
if !self.flags.contains(Flags::STARTED) { if !self.flags.contains(Flags::STARTED) {
return Ok(WriterState::Done) return Ok(WriterState::Done)
} }
@ -115,11 +115,7 @@ impl Writer for H2Writer {
Ok(Async::Ready(())) Ok(Async::Ready(()))
} }
fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse) -> io::Result<WriterState> {
-> Result<WriterState, io::Error>
{
// trace!("Prepare response with status: {:?}", msg.status());
// prepare response // prepare response
self.flags.insert(Flags::STARTED); self.flags.insert(Flags::STARTED);
self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg); self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg);
@ -183,16 +179,16 @@ impl Writer for H2Writer {
} }
} }
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error> { fn write(&mut self, payload: Binary) -> io::Result<WriterState> {
self.written = payload.len() as u64; self.written = payload.len() as u64;
if !self.flags.contains(Flags::DISCONNECTED) { if !self.flags.contains(Flags::DISCONNECTED) {
if self.flags.contains(Flags::STARTED) { if self.flags.contains(Flags::STARTED) {
// TODO: add warning, write after EOF // TODO: add warning, write after EOF
self.encoder.write(payload)?; self.encoder.write(payload.as_ref())?;
} else { } else {
// might be response for EXCEPT // 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<WriterState, io::Error> { fn write_eof(&mut self) -> io::Result<WriterState> {
self.encoder.write_eof()?; self.encoder.write_eof()?;
self.flags.insert(Flags::EOF); self.flags.insert(Flags::EOF);

View File

@ -20,6 +20,7 @@ mod utils;
pub use self::srv::HttpServer; pub use self::srv::HttpServer;
pub use self::settings::ServerSettings; pub use self::settings::ServerSettings;
use body::Binary;
use error::Error; use error::Error;
use httprequest::{HttpMessage, HttpRequest}; use httprequest::{HttpMessage, HttpRequest};
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
@ -96,12 +97,11 @@ pub enum WriterState {
pub trait Writer { pub trait Writer {
fn written(&self) -> u64; fn written(&self) -> u64;
fn start(&mut self, req: &mut HttpMessage, resp: &mut HttpResponse) fn start(&mut self, req: &mut HttpMessage, resp: &mut HttpResponse) -> io::Result<WriterState>;
-> Result<WriterState, io::Error>;
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error>; fn write(&mut self, payload: Binary) -> io::Result<WriterState>;
fn write_eof(&mut self) -> Result<WriterState, io::Error>; fn write_eof(&mut self) -> io::Result<WriterState>;
fn flush(&mut self) -> Poll<(), io::Error>; fn flush(&mut self) -> Poll<(), io::Error>;