2021-02-07 19:56:39 +01:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
2021-06-17 18:57:58 +02:00
|
|
|
net,
|
2021-02-07 19:56:39 +01:00
|
|
|
pin::Pin,
|
|
|
|
rc::Rc,
|
|
|
|
task::{Context, Poll},
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use actix_http::{
|
2021-12-25 03:33:37 +01:00
|
|
|
body::{BodyStream, MessageBody},
|
2021-12-05 15:37:20 +01:00
|
|
|
error::HttpError,
|
2021-12-13 17:08:08 +01:00
|
|
|
header::{self, HeaderMap, HeaderName, TryIntoHeaderValue},
|
2021-12-04 20:40:47 +01:00
|
|
|
RequestHead, RequestHeadType,
|
2021-02-07 19:56:39 +01: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;
|
2021-02-17 18:10:46 +01:00
|
|
|
use futures_core::Stream;
|
2019-09-12 06:40:56 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "__compress")]
|
2021-12-24 18:47:47 +01:00
|
|
|
use actix_http::{encoding::Decoder, header::ContentEncoding, Payload};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
2021-06-17 18:57:58 +02:00
|
|
|
use crate::{
|
2021-12-04 20:40:47 +01:00
|
|
|
any_body::AnyBody,
|
2021-12-25 03:23:22 +01:00
|
|
|
client::ClientConfig,
|
2021-06-17 18:57:58 +02:00
|
|
|
error::{FreezeRequestError, InvalidUrl, SendRequestError},
|
2021-12-25 03:23:22 +01:00
|
|
|
BoxError, ClientResponse, ConnectRequest, ConnectResponse,
|
2021-06-17 18:57:58 +02:00
|
|
|
};
|
2019-09-12 06:40:56 +02:00
|
|
|
|
|
|
|
#[derive(Debug, From)]
|
|
|
|
pub(crate) enum PrepForSendingError {
|
|
|
|
Url(InvalidUrl),
|
|
|
|
Http(HttpError),
|
2021-06-17 18:57:58 +02:00
|
|
|
Json(serde_json::Error),
|
|
|
|
Form(serde_urlencoded::ser::Error),
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-07 02:13:46 +01:00
|
|
|
impl From<PrepForSendingError> for FreezeRequestError {
|
|
|
|
fn from(err: PrepForSendingError) -> FreezeRequestError {
|
|
|
|
match err {
|
2021-06-17 18:57:58 +02:00
|
|
|
PrepForSendingError::Url(err) => FreezeRequestError::Url(err),
|
|
|
|
PrepForSendingError::Http(err) => FreezeRequestError::Http(err),
|
|
|
|
PrepForSendingError::Json(err) => {
|
|
|
|
FreezeRequestError::Custom(Box::new(err), Box::new("json serialization error"))
|
|
|
|
}
|
|
|
|
PrepForSendingError::Form(err) => {
|
|
|
|
FreezeRequestError::Custom(Box::new(err), Box::new("form serialization error"))
|
|
|
|
}
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 02:13:46 +01:00
|
|
|
impl From<PrepForSendingError> for SendRequestError {
|
|
|
|
fn from(err: PrepForSendingError) -> SendRequestError {
|
|
|
|
match err {
|
2019-09-12 06:40:56 +02:00
|
|
|
PrepForSendingError::Url(e) => SendRequestError::Url(e),
|
|
|
|
PrepForSendingError::Http(e) => SendRequestError::Http(e),
|
2021-06-17 18:57:58 +02:00
|
|
|
PrepForSendingError::Json(err) => {
|
|
|
|
SendRequestError::Custom(Box::new(err), Box::new("json serialization error"))
|
|
|
|
}
|
|
|
|
PrepForSendingError::Form(err) => {
|
|
|
|
SendRequestError::Custom(Box::new(err), Box::new("form serialization error"))
|
|
|
|
}
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future that sends request's payload and resolves to a server response.
|
|
|
|
#[must_use = "futures do nothing unless polled"]
|
|
|
|
pub enum SendClientRequest {
|
|
|
|
Fut(
|
2021-02-17 18:10:46 +01:00
|
|
|
Pin<Box<dyn Future<Output = Result<ConnectResponse, SendRequestError>>>>,
|
|
|
|
// FIXME: use a pinned Sleep instead of box.
|
2021-01-04 00:47:04 +01:00
|
|
|
Option<Pin<Box<Sleep>>>,
|
2019-09-12 06:40:56 +02:00
|
|
|
bool,
|
|
|
|
),
|
|
|
|
Err(Option<SendRequestError>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SendClientRequest {
|
|
|
|
pub(crate) fn new(
|
2021-02-17 18:10:46 +01:00
|
|
|
send: Pin<Box<dyn Future<Output = Result<ConnectResponse, 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "__compress")]
|
2019-09-12 06:40:56 +02:00
|
|
|
impl Future for SendClientRequest {
|
2021-12-24 18:47:47 +01:00
|
|
|
type Output = Result<ClientResponse<Decoder<Payload>>, 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) => {
|
2021-02-17 12:55:11 +01:00
|
|
|
if let Some(delay) = delay {
|
|
|
|
if delay.as_mut().poll(cx).is_ready() {
|
|
|
|
return Poll::Ready(Err(SendRequestError::Timeout));
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:10:46 +01:00
|
|
|
let res = futures_core::ready!(send.as_mut().poll(cx)).map(|res| {
|
|
|
|
res.into_client_response()._timeout(delay.take()).map_body(
|
|
|
|
|head, payload| {
|
|
|
|
if *response_decompress {
|
2021-12-24 18:47:47 +01:00
|
|
|
Payload::Stream {
|
|
|
|
payload: Decoder::from_headers(payload, &head.headers),
|
|
|
|
}
|
2021-02-17 18:10:46 +01:00
|
|
|
} else {
|
2021-12-24 18:47:47 +01:00
|
|
|
Payload::Stream {
|
|
|
|
payload: Decoder::new(payload, ContentEncoding::Identity),
|
|
|
|
}
|
2021-02-17 18:10:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
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"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(not(feature = "__compress"))]
|
2019-12-15 08:28:54 +01:00
|
|
|
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, _) => {
|
2021-02-17 12:55:11 +01:00
|
|
|
if let Some(delay) = delay {
|
|
|
|
if delay.as_mut().poll(cx).is_ready() {
|
|
|
|
return Poll::Ready(Err(SendRequestError::Timeout));
|
2019-12-15 08:28:54 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 12:55:11 +01:00
|
|
|
send.as_mut()
|
|
|
|
.poll(cx)
|
2021-02-17 18:10:46 +01:00
|
|
|
.map_ok(|res| res.into_client_response()._timeout(delay.take()))
|
2019-12-15 08:28:54 +01:00
|
|
|
}
|
|
|
|
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<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 {
|
2021-12-28 04:22:22 +01:00
|
|
|
pub(crate) fn send_body(
|
2019-09-12 06:40:56 +02:00
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
2021-12-28 04:22:22 +01:00
|
|
|
body: impl MessageBody + 'static,
|
|
|
|
) -> SendClientRequest {
|
2021-02-17 18:10:46 +01:00
|
|
|
let req = match self {
|
2021-12-25 03:33:37 +01:00
|
|
|
RequestSender::Owned(head) => ConnectRequest::Client(
|
|
|
|
RequestHeadType::Owned(head),
|
|
|
|
AnyBody::from_message_body(body).into_boxed(),
|
|
|
|
addr,
|
|
|
|
),
|
2021-02-17 18:10:46 +01:00
|
|
|
RequestSender::Rc(head, extra_headers) => ConnectRequest::Client(
|
2021-02-07 19:56:39 +01:00
|
|
|
RequestHeadType::Rc(head, extra_headers),
|
2021-12-25 03:33:37 +01:00
|
|
|
AnyBody::from_message_body(body).into_boxed(),
|
2021-02-07 19:56:39 +01:00
|
|
|
addr,
|
|
|
|
),
|
2019-09-12 06:40:56 +02:00
|
|
|
};
|
|
|
|
|
2021-02-17 18:10:46 +01:00
|
|
|
let fut = config.connector.call(req);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
pub(crate) fn send_json(
|
2019-09-12 06:40:56 +02:00
|
|
|
mut self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
2021-12-28 04:22:22 +01:00
|
|
|
value: impl Serialize,
|
2019-09-12 06:40:56 +02:00
|
|
|
) -> SendClientRequest {
|
2021-12-28 04:22:22 +01:00
|
|
|
let body = match serde_json::to_string(&value) {
|
2019-09-12 06:40:56 +02:00
|
|
|
Ok(body) => body,
|
2021-06-17 18:57:58 +02:00
|
|
|
Err(err) => return PrepForSendingError::Json(err).into(),
|
2019-09-12 06:40:56 +02:00
|
|
|
};
|
|
|
|
|
2021-02-12 00:03:17 +01:00
|
|
|
if let Err(e) = self.set_header_if_none(header::CONTENT_TYPE, "application/json") {
|
2019-09-12 06:40:56 +02:00
|
|
|
return e.into();
|
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
self.send_body(addr, response_decompress, timeout, config, body)
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
pub(crate) fn send_form(
|
2019-09-12 06:40:56 +02:00
|
|
|
mut self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
2021-12-28 04:22:22 +01:00
|
|
|
value: impl Serialize,
|
2019-09-12 06:40:56 +02:00
|
|
|
) -> SendClientRequest {
|
|
|
|
let body = match serde_urlencoded::to_string(value) {
|
|
|
|
Ok(body) => body,
|
2021-06-17 18:57:58 +02:00
|
|
|
Err(err) => return PrepForSendingError::Form(err).into(),
|
2019-09-12 06:40:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// set content-type
|
2021-12-28 04:22:22 +01:00
|
|
|
if let Err(err) =
|
2021-02-12 00:03:17 +01:00
|
|
|
self.set_header_if_none(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
{
|
2021-12-28 04:22:22 +01:00
|
|
|
return err.into();
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
2021-12-28 04:22:22 +01:00
|
|
|
self.send_body(addr, response_decompress, timeout, config, body)
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send_stream<S, E>(
|
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
stream: S,
|
|
|
|
) -> SendClientRequest
|
|
|
|
where
|
2021-12-28 04:22:22 +01:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + 'static,
|
2021-12-04 20:40:47 +01:00
|
|
|
E: Into<BoxError> + 'static,
|
2019-09-12 06:40:56 +02:00
|
|
|
{
|
|
|
|
self.send_body(
|
|
|
|
addr,
|
|
|
|
response_decompress,
|
|
|
|
timeout,
|
|
|
|
config,
|
2021-12-28 04:22:22 +01:00
|
|
|
BodyStream::new(stream),
|
2019-09-12 06:40:56 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn send(
|
|
|
|
self,
|
|
|
|
addr: Option<net::SocketAddr>,
|
|
|
|
response_decompress: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
config: &ClientConfig,
|
|
|
|
) -> SendClientRequest {
|
2021-12-28 04:22:22 +01:00
|
|
|
self.send_body(addr, response_decompress, timeout, config, ())
|
2019-09-12 06:40:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 00:03:17 +01:00
|
|
|
fn set_header_if_none<V>(&mut self, key: HeaderName, value: V) -> Result<(), HttpError>
|
2019-09-12 06:40:56 +02:00
|
|
|
where
|
2021-12-13 17:08:08 +01:00
|
|
|
V: TryIntoHeaderValue,
|
2019-09-12 06:40:56 +02:00
|
|
|
{
|
|
|
|
match self {
|
|
|
|
RequestSender::Owned(head) => {
|
|
|
|
if !head.headers.contains_key(&key) {
|
2021-01-15 03:11:10 +01:00
|
|
|
match value.try_into_value() {
|
2021-02-09 23:59:17 +01:00
|
|
|
Ok(value) => {
|
|
|
|
head.headers.insert(key, value);
|
|
|
|
}
|
2019-09-12 06:40:56 +02:00
|
|
|
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))
|
|
|
|
{
|
2021-01-15 03:11:10 +01:00
|
|
|
match value.try_into_value() {
|
2019-09-12 06:40:56 +02:00
|
|
|
Ok(v) => {
|
|
|
|
let h = extra_headers.get_or_insert(HeaderMap::new());
|
|
|
|
h.insert(key, v)
|
|
|
|
}
|
|
|
|
Err(e) => return Err(e.into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|