2019-11-15 10:54:11 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-04-10 21:24:17 +02:00
|
|
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
|
|
|
|
|
|
|
use crate::body::{BodySize, MessageBody, ResponseBody};
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::h1::{Codec, Message};
|
|
|
|
use crate::response::Response;
|
|
|
|
|
|
|
|
/// Send http/1 response
|
2019-11-19 13:54:19 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-04-10 21:24:17 +02:00
|
|
|
pub struct SendResponse<T, B> {
|
|
|
|
res: Option<Message<(Response<()>, BodySize)>>,
|
|
|
|
body: Option<ResponseBody<B>>,
|
|
|
|
framed: Option<Framed<T, Codec>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, B> SendResponse<T, B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
|
|
|
pub fn new(framed: Framed<T, Codec>, response: Response<B>) -> Self {
|
|
|
|
let (res, body) = response.into_parts();
|
|
|
|
|
|
|
|
SendResponse {
|
|
|
|
res: Some((res, body.size()).into()),
|
|
|
|
body: Some(body),
|
|
|
|
framed: Some(framed),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, B> Future for SendResponse<T, B>
|
|
|
|
where
|
2019-11-19 13:54:19 +01:00
|
|
|
T: AsyncRead + AsyncWrite,
|
2019-04-10 21:24:17 +02:00
|
|
|
B: MessageBody,
|
|
|
|
{
|
2019-11-15 10:54:11 +01:00
|
|
|
type Output = Result<Framed<T, Codec>, Error>;
|
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-15 10:54:11 +01:00
|
|
|
let this = self.get_mut();
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
loop {
|
2019-11-15 10:54:11 +01:00
|
|
|
let mut body_ready = this.body.is_some();
|
|
|
|
let framed = this.framed.as_mut().unwrap();
|
2019-04-10 21:24:17 +02:00
|
|
|
|
|
|
|
// send body
|
2019-11-15 10:54:11 +01:00
|
|
|
if this.res.is_none() && this.body.is_some() {
|
|
|
|
while body_ready && this.body.is_some() && !framed.is_write_buf_full() {
|
|
|
|
match this.body.as_mut().unwrap().poll_next(cx)? {
|
|
|
|
Poll::Ready(item) => {
|
2019-04-10 21:24:17 +02:00
|
|
|
// body is done
|
|
|
|
if item.is_none() {
|
2019-11-15 10:54:11 +01:00
|
|
|
let _ = this.body.take();
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
2019-11-18 13:42:27 +01:00
|
|
|
framed.write(Message::Chunk(item))?;
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Pending => body_ready = false,
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush write buffer
|
2019-04-11 23:18:58 +02:00
|
|
|
if !framed.is_write_buf_empty() {
|
2019-11-15 10:54:11 +01:00
|
|
|
match framed.flush(cx)? {
|
|
|
|
Poll::Ready(_) => {
|
2019-04-10 21:24:17 +02:00
|
|
|
if body_ready {
|
|
|
|
continue;
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Pending;
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Pending => return Poll::Pending,
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send response
|
2019-11-15 10:54:11 +01:00
|
|
|
if let Some(res) = this.res.take() {
|
2019-11-18 13:42:27 +01:00
|
|
|
framed.write(res)?;
|
2019-04-10 21:24:17 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:54:11 +01:00
|
|
|
if this.body.is_some() {
|
2019-04-10 21:24:17 +02:00
|
|
|
if body_ready {
|
|
|
|
continue;
|
|
|
|
} else {
|
2019-11-15 10:54:11 +01:00
|
|
|
return Poll::Pending;
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:54:11 +01:00
|
|
|
Poll::Ready(Ok(this.framed.take().unwrap()))
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
|
|
|
}
|