1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 08:52:42 +01:00
actix-web/src/client/pipeline.rs

190 lines
5.4 KiB
Rust
Raw Normal View History

2018-11-14 07:53:30 +01:00
use std::collections::VecDeque;
use actix_net::codec::Framed;
use actix_net::service::Service;
use bytes::Bytes;
use futures::future::{err, ok, Either};
use futures::{Async, Future, Poll, Sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
use super::error::{ConnectorError, SendRequestError};
use super::response::ClientResponse;
use super::{Connect, Connection};
use body::{BodyType, MessageBody, PayloadStream};
use error::PayloadError;
2018-11-14 07:53:30 +01:00
use h1;
2018-11-17 04:28:07 +01:00
use message::RequestHead;
2018-11-14 07:53:30 +01:00
2018-11-15 20:10:23 +01:00
pub(crate) fn send_request<T, I, B>(
2018-11-14 07:53:30 +01:00
head: RequestHead,
body: B,
connector: &mut T,
) -> impl Future<Item = ClientResponse, Error = SendRequestError>
where
2018-11-15 20:10:23 +01:00
T: Service<Request = Connect, Response = I, Error = ConnectorError>,
2018-11-14 07:53:30 +01:00
B: MessageBody,
2018-11-15 20:10:23 +01:00
I: Connection,
2018-11-14 07:53:30 +01:00
{
let tp = body.tp();
connector
2018-11-15 20:10:23 +01:00
// connect to the host
2018-11-14 07:53:30 +01:00
.call(Connect::new(head.uri.clone()))
.from_err()
2018-11-15 20:10:23 +01:00
// create Framed and send reqest
2018-11-14 07:53:30 +01:00
.map(|io| Framed::new(io, h1::ClientCodec::default()))
.and_then(|framed| framed.send((head, tp).into()).from_err())
2018-11-15 20:10:23 +01:00
// send request body
2018-11-14 07:53:30 +01:00
.and_then(move |framed| match body.tp() {
BodyType::None | BodyType::Zero => Either::A(ok(framed)),
_ => Either::B(SendBody::new(body, framed)),
2018-11-15 20:10:23 +01:00
})
// read response and init read body
.and_then(|framed| {
2018-11-14 07:53:30 +01:00
framed
.into_future()
.map_err(|(e, _)| SendRequestError::from(e))
.and_then(|(item, framed)| {
2018-11-14 19:52:40 +01:00
if let Some(res) = item {
2018-11-14 07:53:30 +01:00
match framed.get_codec().message_type() {
h1::MessageType::None => release_connection(framed),
2018-11-14 19:52:40 +01:00
_ => {
*res.payload.borrow_mut() = Some(Payload::stream(framed))
}
2018-11-14 07:53:30 +01:00
}
ok(res)
} else {
err(ConnectorError::Disconnected.into())
}
})
})
}
2018-11-15 20:10:23 +01:00
/// Future responsible for sending request body to the peer
struct SendBody<I, B> {
2018-11-14 07:53:30 +01:00
body: Option<B>,
2018-11-15 20:10:23 +01:00
framed: Option<Framed<I, h1::ClientCodec>>,
2018-11-14 07:53:30 +01:00
write_buf: VecDeque<h1::Message<(RequestHead, BodyType)>>,
flushed: bool,
}
2018-11-15 20:10:23 +01:00
impl<I, B> SendBody<I, B>
2018-11-14 07:53:30 +01:00
where
2018-11-15 20:10:23 +01:00
I: AsyncRead + AsyncWrite + 'static,
2018-11-14 07:53:30 +01:00
B: MessageBody,
{
2018-11-15 20:10:23 +01:00
fn new(body: B, framed: Framed<I, h1::ClientCodec>) -> Self {
2018-11-14 07:53:30 +01:00
SendBody {
body: Some(body),
framed: Some(framed),
write_buf: VecDeque::new(),
flushed: true,
}
}
}
2018-11-15 20:10:23 +01:00
impl<I, B> Future for SendBody<I, B>
2018-11-14 07:53:30 +01:00
where
2018-11-15 20:10:23 +01:00
I: Connection,
2018-11-14 07:53:30 +01:00
B: MessageBody,
{
2018-11-15 20:10:23 +01:00
type Item = Framed<I, h1::ClientCodec>;
2018-11-14 07:53:30 +01:00
type Error = SendRequestError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut body_ready = true;
loop {
while body_ready
&& self.body.is_some()
&& !self.framed.as_ref().unwrap().is_write_buf_full()
{
match self.body.as_mut().unwrap().poll_next()? {
2018-11-14 19:52:40 +01:00
Async::Ready(item) => {
2018-11-14 07:53:30 +01:00
self.flushed = false;
self.framed
.as_mut()
.unwrap()
2018-11-14 19:52:40 +01:00
.force_send(h1::Message::Chunk(item))?;
2018-11-14 07:53:30 +01:00
break;
}
Async::NotReady => body_ready = false,
}
}
if !self.flushed {
match self.framed.as_mut().unwrap().poll_complete()? {
Async::Ready(_) => {
self.flushed = true;
continue;
}
Async::NotReady => return Ok(Async::NotReady),
}
}
if self.body.is_none() {
return Ok(Async::Ready(self.framed.take().unwrap()));
}
return Ok(Async::NotReady);
}
}
}
struct EmptyPayload;
impl Stream for EmptyPayload {
type Item = Bytes;
type Error = PayloadError;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
Ok(Async::Ready(None))
}
}
pub(crate) struct Payload<Io> {
2018-11-15 20:10:23 +01:00
framed: Option<Framed<Io, h1::ClientPayloadCodec>>,
}
impl Payload<()> {
pub fn empty() -> PayloadStream {
Box::new(EmptyPayload)
}
2018-11-14 07:53:30 +01:00
}
2018-11-15 20:10:23 +01:00
impl<Io: Connection> Payload<Io> {
fn stream(framed: Framed<Io, h1::ClientCodec>) -> PayloadStream {
2018-11-14 07:53:30 +01:00
Box::new(Payload {
framed: Some(framed.map_codec(|codec| codec.into_payload_codec())),
2018-11-14 07:53:30 +01:00
})
}
}
2018-11-15 20:10:23 +01:00
impl<Io: Connection> Stream for Payload<Io> {
2018-11-14 07:53:30 +01:00
type Item = Bytes;
type Error = PayloadError;
2018-11-14 07:53:30 +01:00
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
2018-11-14 07:53:30 +01:00
match self.framed.as_mut().unwrap().poll()? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(Some(chunk)) => if let Some(chunk) = chunk {
Ok(Async::Ready(Some(chunk)))
} else {
release_connection(self.framed.take().unwrap());
Ok(Async::Ready(None))
2018-11-14 07:53:30 +01:00
},
Async::Ready(None) => Ok(Async::Ready(None)),
}
}
}
2018-11-15 20:10:23 +01:00
fn release_connection<T, U>(framed: Framed<T, U>)
2018-11-14 07:53:30 +01:00
where
2018-11-15 20:10:23 +01:00
T: Connection,
2018-11-14 07:53:30 +01:00
{
2018-11-15 20:10:23 +01:00
let mut parts = framed.into_parts();
2018-11-14 07:53:30 +01:00
if parts.read_buf.is_empty() && parts.write_buf.is_empty() {
parts.io.release()
} else {
parts.io.close()
}
}