2018-10-22 18:59:20 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2018-12-11 03:08:33 +01:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
use actix_service::{NewService, Service};
|
2018-10-22 18:59:20 +02:00
|
|
|
use futures::future::{ok, Either, FutureResult};
|
2018-11-19 02:52:56 +01:00
|
|
|
use futures::{Async, Future, Poll, Sink};
|
2018-10-22 18:59:20 +02:00
|
|
|
|
2019-03-27 17:24:55 +01:00
|
|
|
use crate::body::{BodySize, MessageBody, ResponseBody};
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::error::{Error, ResponseError};
|
|
|
|
use crate::h1::{Codec, Message};
|
|
|
|
use crate::response::Response;
|
2018-10-22 18:59:20 +02:00
|
|
|
|
|
|
|
pub struct SendError<T, R, E>(PhantomData<(T, R, E)>);
|
|
|
|
|
|
|
|
impl<T, R, E> Default for SendError<T, R, E>
|
|
|
|
where
|
2018-11-19 02:52:56 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-10-22 18:59:20 +02:00
|
|
|
E: ResponseError,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
SendError(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
impl<T, R, E> NewService for SendError<T, R, E>
|
2018-10-22 18:59:20 +02:00
|
|
|
where
|
2018-11-19 02:52:56 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-10-22 18:59:20 +02:00
|
|
|
E: ResponseError,
|
|
|
|
{
|
2019-03-09 16:37:23 +01:00
|
|
|
type Request = Result<R, (E, Framed<T, Codec>)>;
|
2018-10-22 18:59:20 +02:00
|
|
|
type Response = R;
|
|
|
|
type Error = (E, Framed<T, Codec>);
|
|
|
|
type InitError = ();
|
|
|
|
type Service = SendError<T, R, E>;
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
2019-02-22 23:21:35 +01:00
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
2018-10-22 18:59:20 +02:00
|
|
|
ok(SendError(PhantomData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
impl<T, R, E> Service for SendError<T, R, E>
|
2018-10-22 18:59:20 +02:00
|
|
|
where
|
2018-11-19 02:52:56 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-10-22 18:59:20 +02:00
|
|
|
E: ResponseError,
|
|
|
|
{
|
2019-03-09 16:37:23 +01:00
|
|
|
type Request = Result<R, (E, Framed<T, Codec>)>;
|
2018-10-22 18:59:20 +02:00
|
|
|
type Response = R;
|
|
|
|
type Error = (E, Framed<T, Codec>);
|
|
|
|
type Future = Either<FutureResult<R, (E, Framed<T, Codec>)>, SendErrorFut<T, R, E>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:57:57 +01:00
|
|
|
fn call(&mut self, req: Result<R, (E, Framed<T, Codec>)>) -> Self::Future {
|
2018-10-22 18:59:20 +02:00
|
|
|
match req {
|
|
|
|
Ok(r) => Either::A(ok(r)),
|
2018-10-30 19:53:48 +01:00
|
|
|
Err((e, framed)) => {
|
2018-12-06 23:32:52 +01:00
|
|
|
let res = e.error_response().set_body(format!("{}", e));
|
2018-11-18 05:21:28 +01:00
|
|
|
let (res, _body) = res.replace_body(());
|
2018-10-30 19:53:48 +01:00
|
|
|
Either::B(SendErrorFut {
|
|
|
|
framed: Some(framed),
|
2019-03-27 17:24:55 +01:00
|
|
|
res: Some((res, BodySize::Empty).into()),
|
2018-10-30 19:53:48 +01:00
|
|
|
err: Some(e),
|
|
|
|
_t: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
2018-10-22 18:59:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SendErrorFut<T, R, E> {
|
2019-03-27 17:24:55 +01:00
|
|
|
res: Option<Message<(Response<()>, BodySize)>>,
|
2018-10-22 18:59:20 +02:00
|
|
|
framed: Option<Framed<T, Codec>>,
|
|
|
|
err: Option<E>,
|
|
|
|
_t: PhantomData<R>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, R, E> Future for SendErrorFut<T, R, E>
|
|
|
|
where
|
|
|
|
E: ResponseError,
|
2018-11-19 02:52:56 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-10-22 18:59:20 +02:00
|
|
|
{
|
|
|
|
type Item = R;
|
|
|
|
type Error = (E, Framed<T, Codec>);
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Some(res) = self.res.take() {
|
2019-01-29 19:14:00 +01:00
|
|
|
if self.framed.as_mut().unwrap().force_send(res).is_err() {
|
2018-11-19 02:52:56 +01:00
|
|
|
return Err((self.err.take().unwrap(), self.framed.take().unwrap()));
|
2018-10-22 18:59:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
match self.framed.as_mut().unwrap().poll_complete() {
|
|
|
|
Ok(Async::Ready(_)) => {
|
2018-10-30 00:39:46 +01:00
|
|
|
Err((self.err.take().unwrap(), self.framed.take().unwrap()))
|
2018-10-22 18:59:20 +02:00
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-10-30 00:39:46 +01:00
|
|
|
Err(_) => Err((self.err.take().unwrap(), self.framed.take().unwrap())),
|
2018-10-22 18:59:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
pub struct SendResponse<T, B>(PhantomData<(T, B)>);
|
2018-10-22 18:59:20 +02:00
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, B> Default for SendResponse<T, B> {
|
2018-10-22 18:59:20 +02:00
|
|
|
fn default() -> Self {
|
|
|
|
SendResponse(PhantomData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, B> SendResponse<T, B>
|
2018-11-15 03:57:58 +01:00
|
|
|
where
|
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-11-15 03:57:58 +01:00
|
|
|
{
|
|
|
|
pub fn send(
|
2018-11-19 02:52:56 +01:00
|
|
|
framed: Framed<T, Codec>,
|
2018-11-18 05:21:28 +01:00
|
|
|
res: Response<B>,
|
2018-11-15 03:57:58 +01:00
|
|
|
) -> impl Future<Item = Framed<T, Codec>, Error = Error> {
|
|
|
|
// extract body from response
|
2018-11-19 02:52:56 +01:00
|
|
|
let (res, body) = res.replace_body(());
|
2018-11-15 03:57:58 +01:00
|
|
|
|
|
|
|
// write response
|
|
|
|
SendResponseFut {
|
2018-11-19 02:52:56 +01:00
|
|
|
res: Some(Message::Item((res, body.length()))),
|
2018-11-15 03:57:58 +01:00
|
|
|
body: Some(body),
|
|
|
|
framed: Some(framed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
impl<T, B> NewService for SendResponse<T, B>
|
2018-10-22 18:59:20 +02:00
|
|
|
where
|
2018-11-15 03:57:58 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-10-22 18:59:20 +02:00
|
|
|
{
|
2019-03-09 16:37:23 +01:00
|
|
|
type Request = (Response<B>, Framed<T, Codec>);
|
2018-10-22 18:59:20 +02:00
|
|
|
type Response = Framed<T, Codec>;
|
2018-11-15 03:57:58 +01:00
|
|
|
type Error = Error;
|
2018-10-22 18:59:20 +02:00
|
|
|
type InitError = ();
|
2018-11-18 05:21:28 +01:00
|
|
|
type Service = SendResponse<T, B>;
|
2018-10-22 18:59:20 +02:00
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
2019-02-22 23:21:35 +01:00
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
2018-10-22 18:59:20 +02:00
|
|
|
ok(SendResponse(PhantomData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 16:37:23 +01:00
|
|
|
impl<T, B> Service for SendResponse<T, B>
|
2018-10-22 18:59:20 +02:00
|
|
|
where
|
2018-11-15 03:57:58 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-10-22 18:59:20 +02:00
|
|
|
{
|
2019-03-09 16:37:23 +01:00
|
|
|
type Request = (Response<B>, Framed<T, Codec>);
|
2018-10-22 18:59:20 +02:00
|
|
|
type Response = Framed<T, Codec>;
|
2018-11-15 03:57:58 +01:00
|
|
|
type Error = Error;
|
2018-11-18 05:21:28 +01:00
|
|
|
type Future = SendResponseFut<T, B>;
|
2018-10-22 18:59:20 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:57:57 +01:00
|
|
|
fn call(&mut self, (res, framed): (Response<B>, Framed<T, Codec>)) -> Self::Future {
|
2018-11-19 02:52:56 +01:00
|
|
|
let (res, body) = res.replace_body(());
|
2018-10-22 18:59:20 +02:00
|
|
|
SendResponseFut {
|
2018-11-19 02:52:56 +01:00
|
|
|
res: Some(Message::Item((res, body.length()))),
|
2018-11-15 03:57:58 +01:00
|
|
|
body: Some(body),
|
2018-10-22 18:59:20 +02:00
|
|
|
framed: Some(framed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
pub struct SendResponseFut<T, B> {
|
2019-03-27 17:24:55 +01:00
|
|
|
res: Option<Message<(Response<()>, BodySize)>>,
|
2018-11-21 16:49:24 +01:00
|
|
|
body: Option<ResponseBody<B>>,
|
2018-10-22 18:59:20 +02:00
|
|
|
framed: Option<Framed<T, Codec>>,
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
impl<T, B> Future for SendResponseFut<T, B>
|
2018-10-22 18:59:20 +02:00
|
|
|
where
|
2018-11-15 03:57:58 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2018-11-18 05:21:28 +01:00
|
|
|
B: MessageBody,
|
2018-10-22 18:59:20 +02:00
|
|
|
{
|
|
|
|
type Item = Framed<T, Codec>;
|
2018-11-15 03:57:58 +01:00
|
|
|
type Error = Error;
|
2018-10-22 18:59:20 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-11-18 05:21:28 +01:00
|
|
|
loop {
|
|
|
|
let mut body_ready = self.body.is_some();
|
2018-11-15 03:57:58 +01:00
|
|
|
let framed = self.framed.as_mut().unwrap();
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
// 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();
|
2018-11-15 03:57:58 +01:00
|
|
|
}
|
2018-11-18 05:21:28 +01:00
|
|
|
framed.force_send(Message::Chunk(item))?;
|
2018-11-15 03:57:58 +01:00
|
|
|
}
|
2018-11-18 05:21:28 +01:00
|
|
|
Async::NotReady => body_ready = false,
|
2018-11-15 03:57:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
// flush write buffer
|
|
|
|
if !framed.is_write_buf_empty() {
|
|
|
|
match framed.poll_complete()? {
|
2018-12-06 23:32:52 +01:00
|
|
|
Async::Ready(_) => {
|
|
|
|
if body_ready {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 05:21:28 +01:00
|
|
|
Async::NotReady => return Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 03:57:58 +01:00
|
|
|
|
2018-11-18 05:21:28 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 19:14:00 +01:00
|
|
|
Ok(Async::Ready(self.framed.take().unwrap()))
|
2018-10-22 18:59:20 +02:00
|
|
|
}
|
|
|
|
}
|