use std::marker::PhantomData; use actix_net::codec::Framed; use actix_net::service::{NewService, Service}; use futures::future::{ok, Either, FutureResult}; use futures::{Async, Future, Poll, Sink}; use tokio_io::{AsyncRead, AsyncWrite}; use body::{BodyLength, MessageBody, ResponseBody}; use error::{Error, ResponseError}; use h1::{Codec, Message}; use response::Response; pub struct SendError(PhantomData<(T, R, E)>); impl Default for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { fn default() -> Self { SendError(PhantomData) } } impl NewService for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { type Request = Result)>; type Response = R; type Error = (E, Framed); type InitError = (); type Service = SendError; type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(SendError(PhantomData)) } } impl Service for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { type Request = Result)>; type Response = R; type Error = (E, Framed); type Future = Either)>, SendErrorFut>; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, req: Self::Request) -> Self::Future { match req { Ok(r) => Either::A(ok(r)), Err((e, framed)) => { let mut res = e.error_response().set_body(format!("{}", e)); let (res, _body) = res.replace_body(()); Either::B(SendErrorFut { framed: Some(framed), res: Some((res, BodyLength::Empty).into()), err: Some(e), _t: PhantomData, }) } } } } pub struct SendErrorFut { res: Option, BodyLength)>>, framed: Option>, err: Option, _t: PhantomData, } impl Future for SendErrorFut where E: ResponseError, T: AsyncRead + AsyncWrite, { type Item = R; type Error = (E, Framed); fn poll(&mut self) -> Poll { if let Some(res) = self.res.take() { if let Err(_) = self.framed.as_mut().unwrap().force_send(res) { return Err((self.err.take().unwrap(), self.framed.take().unwrap())); } } match self.framed.as_mut().unwrap().poll_complete() { Ok(Async::Ready(_)) => { Err((self.err.take().unwrap(), self.framed.take().unwrap())) } Ok(Async::NotReady) => Ok(Async::NotReady), Err(_) => Err((self.err.take().unwrap(), self.framed.take().unwrap())), } } } pub struct SendResponse(PhantomData<(T, B)>); impl Default for SendResponse { fn default() -> Self { SendResponse(PhantomData) } } impl SendResponse where T: AsyncRead + AsyncWrite, B: MessageBody, { pub fn send( framed: Framed, res: Response, ) -> impl Future, Error = Error> { // extract body from response let (res, body) = res.replace_body(()); // write response SendResponseFut { res: Some(Message::Item((res, body.length()))), body: Some(body), framed: Some(framed), } } } impl NewService for SendResponse where T: AsyncRead + AsyncWrite, B: MessageBody, { type Request = (Response, Framed); type Response = Framed; type Error = Error; type InitError = (); type Service = SendResponse; type Future = FutureResult; fn new_service(&self) -> Self::Future { ok(SendResponse(PhantomData)) } } impl Service for SendResponse where T: AsyncRead + AsyncWrite, B: MessageBody, { type Request = (Response, Framed); type Response = Framed; type Error = Error; type Future = SendResponseFut; fn poll_ready(&mut self) -> Poll<(), Self::Error> { Ok(Async::Ready(())) } fn call(&mut self, (res, framed): Self::Request) -> Self::Future { let (res, body) = res.replace_body(()); SendResponseFut { res: Some(Message::Item((res, body.length()))), body: Some(body), framed: Some(framed), } } } pub struct SendResponseFut { res: Option, BodyLength)>>, body: Option>, framed: Option>, } impl Future for SendResponseFut where T: AsyncRead + AsyncWrite, B: MessageBody, { type Item = Framed; type Error = Error; fn poll(&mut self) -> Poll { loop { let mut body_ready = self.body.is_some(); let framed = self.framed.as_mut().unwrap(); // send body if self.res.is_none() && self.body.is_some() { while body_ready && self.body.is_some() && !framed.is_write_buf_full() { match self.body.as_mut().unwrap().poll_next()? { Async::Ready(item) => { // body is done if item.is_none() { let _ = self.body.take(); } framed.force_send(Message::Chunk(item))?; } Async::NotReady => body_ready = false, } } } // flush write buffer if !framed.is_write_buf_empty() { match framed.poll_complete()? { Async::Ready(_) => if body_ready { continue; } else { return Ok(Async::NotReady); }, Async::NotReady => return Ok(Async::NotReady), } } // send response if let Some(res) = self.res.take() { framed.force_send(res)?; continue; } if self.body.is_some() { if body_ready { continue; } else { return Ok(Async::NotReady); } } else { break; } } return Ok(Async::Ready(self.framed.take().unwrap())); } }