2020-05-18 04:46:57 +02:00
|
|
|
use std::future::Future;
|
2019-09-12 06:40:56 +02:00
|
|
|
use std::net;
|
2019-11-19 04:55:17 +01:00
|
|
|
use std::pin::Pin;
|
2019-09-12 06:40:56 +02:00
|
|
|
use std::rc::Rc;
|
2019-11-19 04:55:17 +01:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
use std::time::Duration;
|
2019-09-12 06:40:56 +02:00
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
use actix_rt::time::{sleep, Sleep};
|
2019-09-12 06:40:56 +02:00
|
|
|
use bytes::Bytes;
|
|
|
|
use derive_more::From;
|
2020-05-18 04:46:57 +02:00
|
|
|
use futures_core::Stream;
|
2019-09-12 06:40:56 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use actix_http::body::{Body, BodyStream};
|
2019-12-15 08:28:54 +01:00
|
|
|
use actix_http::http::header::{self, IntoHeaderValue};
|
2019-09-12 06:40:56 +02:00
|
|
|
use actix_http::http::{Error as HttpError, HeaderMap, HeaderName};
|
2019-12-15 08:28:54 +01:00
|
|
|
use actix_http::{Error, RequestHead};
|
|
|
|
|
|
|
|
#[cfg(feature = "compress")]
|
|
|
|
use actix_http::encoding::Decoder;
|
|
|
|
#[cfg(feature = "compress")]
|
|
|
|
use actix_http::http::header::ContentEncoding;
|
|
|
|
#[cfg(feature = "compress")]
|
|
|
|
use actix_http::{Payload, PayloadStream};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
|
|
|
use crate::error::{FreezeRequestError, InvalidUrl, SendRequestError};
|
|
|
|
use crate::response::ClientResponse;
|
|
|
|
use crate::ClientConfig;
|
|
|
|
|
|
|
|
#[derive(Debug, From)]
|
|
|
|
pub(crate) enum PrepForSendingError {
|
|
|
|
Url(InvalidUrl),
|
|
|
|
Http(HttpError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<FreezeRequestError> for PrepForSendingError {
|
|
|
|
fn into(self) -> FreezeRequestError {
|
|
|
|
match self {
|
|
|
|
PrepForSendingError::Url(e) => FreezeRequestError::Url(e),
|
|
|
|
PrepForSendingError::Http(e) => FreezeRequestError::Http(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<SendRequestError> for PrepForSendingError {
|
|
|
|
fn into(self) -> SendRequestError {
|
|
|
|
match self {
|
|
|
|
PrepForSendingError::Url(e) => SendRequestError::Url(e),
|
|
|
|
PrepForSendingError::Http(e) => SendRequestError::Http(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future that sends request's payload and resolves to a server response.
|
|
|
|
#[must_use = "futures do nothing unless polled"]
|
|
|
|
pub enum SendClientRequest {
|
|
|
|
Fut(
|
2019-12-13 06:24:57 +01:00
|
|
|
Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>,
|
2021-01-04 00:47:04 +01:00
|
|
|
// FIXME: use a pinned Sleep instead of box.
|
|
|
|
Option<Pin<Box<Sleep>>>,
|
2019-09-12 06:40:56 +02:00
|
|
|
bool,
|
|
|
|
),
|
|
|
|
Err(Option<SendRequestError>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SendClientRequest {
|
|
|
|
pub(crate) fn new(
|
2019-12-13 06:24:57 +01:00
|
|
|
send: Pin<Box<dyn Future<Output = Result<ClientResponse, SendRequestError>>>>,
|
2019-09-12 06:40:56 +02:00
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
) -> SendClientRequest {
|
2021-01-04 00:47:04 +01:00
|
|
|
let delay = timeout.map(|d| Box::pin(sleep(d)));
|
2019-09-12 06:40:56 +02:00
|
|
|
SendClientRequest::Fut(send, delay, response_decompress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:28:54 +01:00
|
|
|
#[cfg(feature = "compress")]
|
2019-09-12 06:40:56 +02:00
|
|
|
impl Future for SendClientRequest {
|
2019-11-19 04:55:17 +01:00
|
|
|
type Output =
|
|
|
|
Result<ClientResponse<Decoder<Payload<PayloadStream>>>, SendRequestError>;
|
2019-09-12 06:40:56 +02:00
|
|
|
|
2019-12-08 07:31:16 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-19 04:55:17 +01:00
|
|
|
let this = self.get_mut();
|
|
|
|
|
|
|
|
match this {
|
2019-09-12 06:40:56 +02:00
|
|
|
SendClientRequest::Fut(send, delay, response_decompress) => {
|
|
|
|
if delay.is_some() {
|
2019-11-19 04:55:17 +01:00
|
|
|
match Pin::new(delay.as_mut().unwrap()).poll(cx) {
|
2021-01-04 01:49:02 +01:00
|
|
|
Poll::Pending => {},
|
2019-11-19 04:55:17 +01:00
|
|
|
_ => return Poll::Ready(Err(SendRequestError::Timeout)),
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:28:54 +01:00
|
|
|
let res = futures_core::ready!(Pin::new(send).poll(cx)).map(|res| {
|
2019-11-19 04:55:17 +01:00
|
|
|
res.map_body(|head, payload| {
|
|
|
|
if *response_decompress {
|
|
|
|
Payload::Stream(Decoder::from_headers(
|
|
|
|
payload,
|
|
|
|
&head.headers,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Payload::Stream(Decoder::new(
|
|
|
|
payload,
|
|
|
|
ContentEncoding::Identity,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
2019-09-12 06:40:56 +02:00
|
|
|
});
|
|
|
|
|
2019-11-19 04:55:17 +01:00
|
|
|
Poll::Ready(res)
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
SendClientRequest::Err(ref mut e) => match e.take() {
|
2019-12-15 08:28:54 +01:00
|
|
|
Some(e) => Poll::Ready(Err(e)),
|
|
|
|
None => panic!("Attempting to call completed future"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "compress"))]
|
|
|
|
impl Future for SendClientRequest {
|
|
|
|
type Output = Result<ClientResponse, SendRequestError>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
|
|
|
match this {
|
|
|
|
SendClientRequest::Fut(send, delay, _) => {
|
|
|
|
if delay.is_some() {
|
|
|
|
match Pin::new(delay.as_mut().unwrap()).poll(cx) {
|
2021-01-04 01:49:02 +01:00
|
|
|
Poll::Pending => {},
|
2019-12-15 08:28:54 +01:00
|
|
|
_ => return Poll::Ready(Err(SendRequestError::Timeout)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pin::new(send).poll(cx)
|
|
|
|
}
|
|
|
|
SendClientRequest::Err(ref mut e) => match e.take() {
|
2019-11-19 04:55:17 +01:00
|
|
|
Some(e) => Poll::Ready(Err(e)),
|
2019-09-12 06:40:56 +02:00
|
|
|
None => panic!("Attempting to call completed future"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SendRequestError> for SendClientRequest {
|
|
|
|
fn from(e: SendRequestError) -> Self {
|
|
|
|
SendClientRequest::Err(Some(e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Error> for SendClientRequest {
|
|
|
|
fn from(e: Error) -> Self {
|
|
|
|
SendClientRequest::Err(Some(e.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HttpError> for SendClientRequest {
|
|
|
|
fn from(e: HttpError) -> Self {
|
|
|
|
SendClientRequest::Err(Some(e.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PrepForSendingError> for SendClientRequest {
|
|
|
|
fn from(e: PrepForSendingError) -> Self {
|
|
|
|
SendClientRequest::Err(Some(e.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum RequestSender {
|
|
|
|
Owned(RequestHead),
|
|
|
|
Rc(Rc<RequestHead>, Option<HeaderMap>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestSender {
|
|
|
|
pub(crate) fn send_body<B>(
|
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
body: B,
|
|
|
|
) -> SendClientRequest
|
|
|
|
where
|
|
|
|
B: Into<Body>,
|
|
|
|
{
|
|
|
|
let mut connector = config.connector.borrow_mut();
|
|
|
|
|
|
|
|
let fut = match self {
|
|
|
|
RequestSender::Owned(head) => {
|
|
|
|
connector.send_request(head, body.into(), addr)
|
|
|
|
}
|
|
|
|
RequestSender::Rc(head, extra_headers) => {
|
|
|
|
connector.send_request_extra(head, extra_headers, body.into(), addr)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-10 15:46:35 +02:00
|
|
|
SendClientRequest::new(fut, response_decompress, timeout.or(config.timeout))
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send_json<T: Serialize>(
|
|
|
|
mut self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
value: &T,
|
|
|
|
) -> SendClientRequest {
|
|
|
|
let body = match serde_json::to_string(value) {
|
|
|
|
Ok(body) => body,
|
|
|
|
Err(e) => return Error::from(e).into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json")
|
|
|
|
{
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.send_body(
|
|
|
|
addr,
|
|
|
|
response_decompress,
|
|
|
|
timeout,
|
|
|
|
config,
|
|
|
|
Body::Bytes(Bytes::from(body)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send_form<T: Serialize>(
|
|
|
|
mut self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
value: &T,
|
|
|
|
) -> SendClientRequest {
|
|
|
|
let body = match serde_urlencoded::to_string(value) {
|
|
|
|
Ok(body) => body,
|
|
|
|
Err(e) => return Error::from(e).into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// set content-type
|
|
|
|
if let Err(e) = self.set_header_if_none(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
) {
|
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.send_body(
|
|
|
|
addr,
|
|
|
|
response_decompress,
|
|
|
|
timeout,
|
|
|
|
config,
|
|
|
|
Body::Bytes(Bytes::from(body)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send_stream<S, E>(
|
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
stream: S,
|
|
|
|
) -> SendClientRequest
|
|
|
|
where
|
2019-11-19 04:55:17 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
2019-09-12 06:40:56 +02:00
|
|
|
E: Into<Error> + 'static,
|
|
|
|
{
|
|
|
|
self.send_body(
|
|
|
|
addr,
|
|
|
|
response_decompress,
|
|
|
|
timeout,
|
|
|
|
config,
|
|
|
|
Body::from_message(BodyStream::new(stream)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send(
|
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
) -> SendClientRequest {
|
|
|
|
self.send_body(addr, response_decompress, timeout, config, Body::Empty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_header_if_none<V>(
|
|
|
|
&mut self,
|
|
|
|
key: HeaderName,
|
|
|
|
value: V,
|
|
|
|
) -> Result<(), HttpError>
|
|
|
|
where
|
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
RequestSender::Owned(head) => {
|
|
|
|
if !head.headers.contains_key(&key) {
|
|
|
|
match value.try_into() {
|
|
|
|
Ok(value) => head.headers.insert(key, value),
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RequestSender::Rc(head, extra_headers) => {
|
|
|
|
if !head.headers.contains_key(&key)
|
|
|
|
&& !extra_headers.iter().any(|h| h.contains_key(&key))
|
|
|
|
{
|
|
|
|
match value.try_into() {
|
|
|
|
Ok(v) => {
|
|
|
|
let h = extra_headers.get_or_insert(HeaderMap::new());
|
|
|
|
h.insert(key, v)
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|