mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 06:39:22 +02:00
refactor actix_http connection types and connector services (#2081)
This commit is contained in:
@ -1,6 +1,10 @@
|
||||
# Changes
|
||||
|
||||
## Unreleased - 2021-xx-xx
|
||||
### Changed
|
||||
* `ConnectorService` type is renamed to `BoxConnectorService` [#2081]
|
||||
|
||||
[#2081]: https://github.com/actix/actix-web/pull/2081
|
||||
|
||||
|
||||
## 3.0.0-beta.3 - 2021-03-08
|
||||
|
@ -6,7 +6,7 @@ use std::time::Duration;
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite};
|
||||
use actix_http::{
|
||||
client::{Connector, TcpConnect, TcpConnectError, TcpConnection},
|
||||
client::{Connector, ConnectorService, TcpConnect, TcpConnectError, TcpConnection},
|
||||
http::{self, header, Error as HttpError, HeaderMap, HeaderName, Uri},
|
||||
};
|
||||
use actix_rt::net::TcpStream;
|
||||
@ -15,7 +15,7 @@ use actix_service::{boxed, Service};
|
||||
use crate::connect::DefaultConnector;
|
||||
use crate::error::SendRequestError;
|
||||
use crate::middleware::{NestTransform, Redirect, Transform};
|
||||
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse, ConnectorService};
|
||||
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse};
|
||||
|
||||
/// An HTTP Client builder
|
||||
///
|
||||
@ -234,7 +234,7 @@ where
|
||||
/// Finish build process and create `Client` instance.
|
||||
pub fn finish(self) -> Client
|
||||
where
|
||||
M: Transform<ConnectorService, ConnectRequest> + 'static,
|
||||
M: Transform<DefaultConnector<ConnectorService<S, Io>>, ConnectRequest> + 'static,
|
||||
M::Transform:
|
||||
Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||
{
|
||||
@ -250,7 +250,7 @@ where
|
||||
|
||||
fn _finish(self) -> Client
|
||||
where
|
||||
M: Transform<ConnectorService, ConnectRequest> + 'static,
|
||||
M: Transform<DefaultConnector<ConnectorService<S, Io>>, ConnectRequest> + 'static,
|
||||
M::Transform:
|
||||
Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||
{
|
||||
@ -269,16 +269,14 @@ where
|
||||
connector = connector.local_address(val);
|
||||
}
|
||||
|
||||
let connector = boxed::service(DefaultConnector::new(connector.finish()));
|
||||
let connector = boxed::service(self.middleware.new_transform(connector));
|
||||
let connector = DefaultConnector::new(connector.finish());
|
||||
let connector = boxed::rc_service(self.middleware.new_transform(connector));
|
||||
|
||||
let config = ClientConfig {
|
||||
headers: self.headers,
|
||||
Client(ClientConfig {
|
||||
headers: Rc::new(self.headers),
|
||||
timeout: self.timeout,
|
||||
connector,
|
||||
};
|
||||
|
||||
Client(Rc::new(config))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ use std::{
|
||||
future::Future,
|
||||
net,
|
||||
pin::Pin,
|
||||
rc::Rc,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
@ -19,7 +20,7 @@ use futures_core::{future::LocalBoxFuture, ready};
|
||||
|
||||
use crate::response::ClientResponse;
|
||||
|
||||
pub type ConnectorService = Box<
|
||||
pub type BoxConnectorService = Rc<
|
||||
dyn Service<
|
||||
ConnectRequest,
|
||||
Response = ConnectResponse,
|
||||
@ -28,6 +29,8 @@ pub type ConnectorService = Box<
|
||||
>,
|
||||
>;
|
||||
|
||||
pub type BoxedSocket = Box<dyn ConnectionIo>;
|
||||
|
||||
pub enum ConnectRequest {
|
||||
Client(RequestHeadType, Body, Option<net::SocketAddr>),
|
||||
Tunnel(RequestHead, Option<net::SocketAddr>),
|
||||
@ -58,7 +61,7 @@ impl ConnectResponse {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct DefaultConnector<S> {
|
||||
pub struct DefaultConnector<S> {
|
||||
connector: S,
|
||||
}
|
||||
|
||||
@ -68,15 +71,14 @@ impl<S> DefaultConnector<S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Service<ConnectRequest> for DefaultConnector<S>
|
||||
impl<S, Io> Service<ConnectRequest> for DefaultConnector<S>
|
||||
where
|
||||
S: Service<ClientConnect, Error = ConnectError>,
|
||||
S::Response: Connection,
|
||||
<S::Response as Connection>::Io: 'static,
|
||||
S: Service<ClientConnect, Error = ConnectError, Response = Connection<Io>>,
|
||||
Io: ConnectionIo,
|
||||
{
|
||||
type Response = ConnectResponse;
|
||||
type Error = SendRequestError;
|
||||
type Future = ConnectRequestFuture<S::Future, <S::Response as Connection>::Io>;
|
||||
type Future = ConnectRequestFuture<S::Future, Io>;
|
||||
|
||||
actix_service::forward_ready!(connector);
|
||||
|
||||
@ -102,7 +104,10 @@ where
|
||||
|
||||
pin_project_lite::pin_project! {
|
||||
#[project = ConnectRequestProj]
|
||||
pub(crate) enum ConnectRequestFuture<Fut, Io> {
|
||||
pub enum ConnectRequestFuture<Fut, Io>
|
||||
where
|
||||
Io: ConnectionIo
|
||||
{
|
||||
Connection {
|
||||
#[pin]
|
||||
fut: Fut,
|
||||
@ -114,16 +119,15 @@ pin_project_lite::pin_project! {
|
||||
Tunnel {
|
||||
fut: LocalBoxFuture<
|
||||
'static,
|
||||
Result<(ResponseHead, Framed<Io, ClientCodec>), SendRequestError>,
|
||||
Result<(ResponseHead, Framed<Connection<Io>, ClientCodec>), SendRequestError>,
|
||||
>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Fut, C, Io> Future for ConnectRequestFuture<Fut, Io>
|
||||
impl<Fut, Io> Future for ConnectRequestFuture<Fut, Io>
|
||||
where
|
||||
Fut: Future<Output = Result<C, ConnectError>>,
|
||||
C: Connection<Io = Io>,
|
||||
Fut: Future<Output = Result<Connection<Io>, ConnectError>>,
|
||||
Io: ConnectionIo,
|
||||
{
|
||||
type Output = Result<ConnectResponse, SendRequestError>;
|
||||
@ -165,5 +169,3 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BoxedSocket = Box<dyn ConnectionIo>;
|
||||
|
@ -23,7 +23,7 @@ pub struct FrozenClientRequest {
|
||||
pub(crate) addr: Option<net::SocketAddr>,
|
||||
pub(crate) response_decompress: bool,
|
||||
pub(crate) timeout: Option<Duration>,
|
||||
pub(crate) config: Rc<ClientConfig>,
|
||||
pub(crate) config: ClientConfig,
|
||||
}
|
||||
|
||||
impl FrozenClientRequest {
|
||||
@ -51,7 +51,7 @@ impl FrozenClientRequest {
|
||||
self.addr,
|
||||
self.response_decompress,
|
||||
self.timeout,
|
||||
self.config.as_ref(),
|
||||
&self.config,
|
||||
body,
|
||||
)
|
||||
}
|
||||
@ -62,7 +62,7 @@ impl FrozenClientRequest {
|
||||
self.addr,
|
||||
self.response_decompress,
|
||||
self.timeout,
|
||||
self.config.as_ref(),
|
||||
&self.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -73,7 +73,7 @@ impl FrozenClientRequest {
|
||||
self.addr,
|
||||
self.response_decompress,
|
||||
self.timeout,
|
||||
self.config.as_ref(),
|
||||
&self.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -88,7 +88,7 @@ impl FrozenClientRequest {
|
||||
self.addr,
|
||||
self.response_decompress,
|
||||
self.timeout,
|
||||
self.config.as_ref(),
|
||||
&self.config,
|
||||
stream,
|
||||
)
|
||||
}
|
||||
@ -99,7 +99,7 @@ impl FrozenClientRequest {
|
||||
self.addr,
|
||||
self.response_decompress,
|
||||
self.timeout,
|
||||
self.config.as_ref(),
|
||||
&self.config,
|
||||
)
|
||||
}
|
||||
|
||||
@ -168,7 +168,7 @@ impl FrozenSendBuilder {
|
||||
self.req.addr,
|
||||
self.req.response_decompress,
|
||||
self.req.timeout,
|
||||
self.req.config.as_ref(),
|
||||
&self.req.config,
|
||||
body,
|
||||
)
|
||||
}
|
||||
@ -183,7 +183,7 @@ impl FrozenSendBuilder {
|
||||
self.req.addr,
|
||||
self.req.response_decompress,
|
||||
self.req.timeout,
|
||||
self.req.config.as_ref(),
|
||||
&self.req.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -198,7 +198,7 @@ impl FrozenSendBuilder {
|
||||
self.req.addr,
|
||||
self.req.response_decompress,
|
||||
self.req.timeout,
|
||||
self.req.config.as_ref(),
|
||||
&self.req.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -217,7 +217,7 @@ impl FrozenSendBuilder {
|
||||
self.req.addr,
|
||||
self.req.response_decompress,
|
||||
self.req.timeout,
|
||||
self.req.config.as_ref(),
|
||||
&self.req.config,
|
||||
stream,
|
||||
)
|
||||
}
|
||||
@ -232,7 +232,7 @@ impl FrozenSendBuilder {
|
||||
self.req.addr,
|
||||
self.req.response_decompress,
|
||||
self.req.timeout,
|
||||
self.req.config.as_ref(),
|
||||
&self.req.config,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ pub mod test;
|
||||
pub mod ws;
|
||||
|
||||
pub use self::builder::ClientBuilder;
|
||||
pub use self::connect::{BoxedSocket, ConnectRequest, ConnectResponse, ConnectorService};
|
||||
pub use self::connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse};
|
||||
pub use self::frozen::{FrozenClientRequest, FrozenSendBuilder};
|
||||
pub use self::request::ClientRequest;
|
||||
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
||||
@ -147,11 +147,12 @@ pub use self::sender::SendClientRequest;
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone)]
|
||||
pub struct Client(Rc<ClientConfig>);
|
||||
pub struct Client(ClientConfig);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ClientConfig {
|
||||
pub(crate) connector: ConnectorService,
|
||||
pub(crate) headers: HeaderMap,
|
||||
pub(crate) connector: BoxConnectorService,
|
||||
pub(crate) headers: Rc<HeaderMap>,
|
||||
pub(crate) timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
|
@ -189,7 +189,7 @@ where
|
||||
// remove body
|
||||
.call(ConnectRequest::Client(head, Body::None, addr));
|
||||
|
||||
self.as_mut().set(RedirectServiceFuture::Client {
|
||||
self.set(RedirectServiceFuture::Client {
|
||||
fut,
|
||||
max_redirect_times,
|
||||
uri: Some(uri),
|
||||
@ -236,7 +236,7 @@ where
|
||||
.unwrap()
|
||||
.call(ConnectRequest::Client(head, body_new, addr));
|
||||
|
||||
self.as_mut().set(RedirectServiceFuture::Client {
|
||||
self.set(RedirectServiceFuture::Client {
|
||||
fut,
|
||||
max_redirect_times,
|
||||
uri: Some(uri),
|
||||
|
@ -57,7 +57,7 @@ pub struct ClientRequest {
|
||||
addr: Option<net::SocketAddr>,
|
||||
response_decompress: bool,
|
||||
timeout: Option<Duration>,
|
||||
config: Rc<ClientConfig>,
|
||||
config: ClientConfig,
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: Option<CookieJar>,
|
||||
@ -65,7 +65,7 @@ pub struct ClientRequest {
|
||||
|
||||
impl ClientRequest {
|
||||
/// Create new client request builder.
|
||||
pub(crate) fn new<U>(method: Method, uri: U, config: Rc<ClientConfig>) -> Self
|
||||
pub(crate) fn new<U>(method: Method, uri: U, config: ClientConfig) -> Self
|
||||
where
|
||||
Uri: TryFrom<U>,
|
||||
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
||||
@ -398,7 +398,7 @@ impl ClientRequest {
|
||||
slf.addr,
|
||||
slf.response_decompress,
|
||||
slf.timeout,
|
||||
slf.config.as_ref(),
|
||||
&slf.config,
|
||||
body,
|
||||
)
|
||||
}
|
||||
@ -414,7 +414,7 @@ impl ClientRequest {
|
||||
slf.addr,
|
||||
slf.response_decompress,
|
||||
slf.timeout,
|
||||
slf.config.as_ref(),
|
||||
&slf.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -432,7 +432,7 @@ impl ClientRequest {
|
||||
slf.addr,
|
||||
slf.response_decompress,
|
||||
slf.timeout,
|
||||
slf.config.as_ref(),
|
||||
&slf.config,
|
||||
value,
|
||||
)
|
||||
}
|
||||
@ -452,7 +452,7 @@ impl ClientRequest {
|
||||
slf.addr,
|
||||
slf.response_decompress,
|
||||
slf.timeout,
|
||||
slf.config.as_ref(),
|
||||
&slf.config,
|
||||
stream,
|
||||
)
|
||||
}
|
||||
@ -468,7 +468,7 @@ impl ClientRequest {
|
||||
slf.addr,
|
||||
slf.response_decompress,
|
||||
slf.timeout,
|
||||
slf.config.as_ref(),
|
||||
&slf.config,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,6 @@
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::net::SocketAddr;
|
||||
use std::rc::Rc;
|
||||
use std::{fmt, str};
|
||||
|
||||
use actix_codec::Framed;
|
||||
@ -56,7 +55,7 @@ pub struct WebsocketsRequest {
|
||||
addr: Option<SocketAddr>,
|
||||
max_size: usize,
|
||||
server_mode: bool,
|
||||
config: Rc<ClientConfig>,
|
||||
config: ClientConfig,
|
||||
|
||||
#[cfg(feature = "cookies")]
|
||||
cookies: Option<CookieJar>,
|
||||
@ -64,7 +63,7 @@ pub struct WebsocketsRequest {
|
||||
|
||||
impl WebsocketsRequest {
|
||||
/// Create new WebSocket connection
|
||||
pub(crate) fn new<U>(uri: U, config: Rc<ClientConfig>) -> Self
|
||||
pub(crate) fn new<U>(uri: U, config: ClientConfig) -> Self
|
||||
where
|
||||
Uri: TryFrom<U>,
|
||||
<Uri as TryFrom<U>>::Error: Into<HttpError>,
|
||||
|
Reference in New Issue
Block a user