mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
allow to specify server address for http and ws requests
This commit is contained in:
parent
7292d0b696
commit
fc9b14a933
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
* Cookie::max_age_time() accepts value in time::Duration
|
* Cookie::max_age_time() accepts value in time::Duration
|
||||||
|
|
||||||
|
* Allow to specify server address for client connector
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0] - 2019-04-16
|
## [0.1.0] - 2019-04-16
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ secure-cookies = ["ring"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.3.6"
|
actix-service = "0.3.6"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.1.2"
|
||||||
actix-connect = "0.1.4"
|
actix-connect = "0.1.5"
|
||||||
actix-utils = "0.3.5"
|
actix-utils = "0.3.5"
|
||||||
actix-server-config = "0.1.1"
|
actix-server-config = "0.1.1"
|
||||||
actix-threadpool = "0.1.0"
|
actix-threadpool = "0.1.0"
|
||||||
|
@ -14,6 +14,7 @@ use tokio_tcp::TcpStream;
|
|||||||
use super::connection::Connection;
|
use super::connection::Connection;
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
use super::pool::{ConnectionPool, Protocol};
|
use super::pool::{ConnectionPool, Protocol};
|
||||||
|
use super::Connect;
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "ssl")]
|
||||||
use openssl::ssl::SslConnector;
|
use openssl::ssl::SslConnector;
|
||||||
@ -177,15 +178,17 @@ where
|
|||||||
/// its combinator chain.
|
/// its combinator chain.
|
||||||
pub fn finish(
|
pub fn finish(
|
||||||
self,
|
self,
|
||||||
) -> impl Service<Request = Uri, Response = impl Connection, Error = ConnectError> + Clone
|
) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectError>
|
||||||
{
|
+ Clone {
|
||||||
#[cfg(not(feature = "ssl"))]
|
#[cfg(not(feature = "ssl"))]
|
||||||
{
|
{
|
||||||
let connector = TimeoutService::new(
|
let connector = TimeoutService::new(
|
||||||
self.timeout,
|
self.timeout,
|
||||||
apply_fn(self.connector, |msg: Uri, srv| srv.call(msg.into()))
|
apply_fn(self.connector, |msg: Connect, srv| {
|
||||||
.map_err(ConnectError::from)
|
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
||||||
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
})
|
||||||
|
.map_err(ConnectError::from)
|
||||||
|
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
||||||
)
|
)
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
TimeoutError::Service(e) => e,
|
TimeoutError::Service(e) => e,
|
||||||
@ -209,26 +212,28 @@ where
|
|||||||
|
|
||||||
let ssl_service = TimeoutService::new(
|
let ssl_service = TimeoutService::new(
|
||||||
self.timeout,
|
self.timeout,
|
||||||
apply_fn(self.connector.clone(), |msg: Uri, srv| srv.call(msg.into()))
|
apply_fn(self.connector.clone(), |msg: Connect, srv| {
|
||||||
.map_err(ConnectError::from)
|
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
||||||
.and_then(
|
})
|
||||||
OpensslConnector::service(self.ssl)
|
.map_err(ConnectError::from)
|
||||||
.map_err(ConnectError::from)
|
.and_then(
|
||||||
.map(|stream| {
|
OpensslConnector::service(self.ssl)
|
||||||
let sock = stream.into_parts().0;
|
.map_err(ConnectError::from)
|
||||||
let h2 = sock
|
.map(|stream| {
|
||||||
.get_ref()
|
let sock = stream.into_parts().0;
|
||||||
.ssl()
|
let h2 = sock
|
||||||
.selected_alpn_protocol()
|
.get_ref()
|
||||||
.map(|protos| protos.windows(2).any(|w| w == H2))
|
.ssl()
|
||||||
.unwrap_or(false);
|
.selected_alpn_protocol()
|
||||||
if h2 {
|
.map(|protos| protos.windows(2).any(|w| w == H2))
|
||||||
(sock, Protocol::Http2)
|
.unwrap_or(false);
|
||||||
} else {
|
if h2 {
|
||||||
(sock, Protocol::Http1)
|
(sock, Protocol::Http2)
|
||||||
}
|
} else {
|
||||||
}),
|
(sock, Protocol::Http1)
|
||||||
),
|
}
|
||||||
|
}),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
TimeoutError::Service(e) => e,
|
TimeoutError::Service(e) => e,
|
||||||
@ -237,9 +242,11 @@ where
|
|||||||
|
|
||||||
let tcp_service = TimeoutService::new(
|
let tcp_service = TimeoutService::new(
|
||||||
self.timeout,
|
self.timeout,
|
||||||
apply_fn(self.connector.clone(), |msg: Uri, srv| srv.call(msg.into()))
|
apply_fn(self.connector.clone(), |msg: Connect, srv| {
|
||||||
.map_err(ConnectError::from)
|
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
||||||
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
})
|
||||||
|
.map_err(ConnectError::from)
|
||||||
|
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
||||||
)
|
)
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
TimeoutError::Service(e) => e,
|
TimeoutError::Service(e) => e,
|
||||||
@ -264,15 +271,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[deprecated(since = "0.1.0-alpha4", note = "please use `.finish()` method")]
|
|
||||||
pub fn service(
|
|
||||||
self,
|
|
||||||
) -> impl Service<Request = Uri, Response = impl Connection, Error = ConnectError> + Clone
|
|
||||||
{
|
|
||||||
self.finish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "ssl"))]
|
#[cfg(not(feature = "ssl"))]
|
||||||
@ -286,7 +284,7 @@ mod connect_impl {
|
|||||||
pub(crate) struct InnerConnector<T, Io>
|
pub(crate) struct InnerConnector<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
Io: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
pub(crate) tcp_pool: ConnectionPool<T, Io>,
|
pub(crate) tcp_pool: ConnectionPool<T, Io>,
|
||||||
}
|
}
|
||||||
@ -294,7 +292,7 @@ mod connect_impl {
|
|||||||
impl<T, Io> Clone for InnerConnector<T, Io>
|
impl<T, Io> Clone for InnerConnector<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
Io: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io, Protocol), Error = ConnectError>
|
T: Service<Request = Connect, Response = (Io, Protocol), Error = ConnectError>
|
||||||
+ Clone,
|
+ Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -307,9 +305,9 @@ mod connect_impl {
|
|||||||
impl<T, Io> Service for InnerConnector<T, Io>
|
impl<T, Io> Service for InnerConnector<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
Io: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
type Request = Uri;
|
type Request = Connect;
|
||||||
type Response = IoConnection<Io>;
|
type Response = IoConnection<Io>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<
|
type Future = Either<
|
||||||
@ -346,8 +344,8 @@ mod connect_impl {
|
|||||||
where
|
where
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
T1: Service<Request = Uri, Response = (Io1, Protocol), Error = ConnectError>,
|
T1: Service<Request = Connect, Response = (Io1, Protocol), Error = ConnectError>,
|
||||||
T2: Service<Request = Uri, Response = (Io2, Protocol), Error = ConnectError>,
|
T2: Service<Request = Connect, Response = (Io2, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
pub(crate) tcp_pool: ConnectionPool<T1, Io1>,
|
pub(crate) tcp_pool: ConnectionPool<T1, Io1>,
|
||||||
pub(crate) ssl_pool: ConnectionPool<T2, Io2>,
|
pub(crate) ssl_pool: ConnectionPool<T2, Io2>,
|
||||||
@ -357,9 +355,9 @@ mod connect_impl {
|
|||||||
where
|
where
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
T1: Service<Request = Uri, Response = (Io1, Protocol), Error = ConnectError>
|
T1: Service<Request = Connect, Response = (Io1, Protocol), Error = ConnectError>
|
||||||
+ Clone,
|
+ Clone,
|
||||||
T2: Service<Request = Uri, Response = (Io2, Protocol), Error = ConnectError>
|
T2: Service<Request = Connect, Response = (Io2, Protocol), Error = ConnectError>
|
||||||
+ Clone,
|
+ Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -374,10 +372,10 @@ mod connect_impl {
|
|||||||
where
|
where
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
T1: Service<Request = Uri, Response = (Io1, Protocol), Error = ConnectError>,
|
T1: Service<Request = Connect, Response = (Io1, Protocol), Error = ConnectError>,
|
||||||
T2: Service<Request = Uri, Response = (Io2, Protocol), Error = ConnectError>,
|
T2: Service<Request = Connect, Response = (Io2, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
type Request = Uri;
|
type Request = Connect;
|
||||||
type Response = EitherConnection<Io1, Io2>;
|
type Response = EitherConnection<Io1, Io2>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<
|
type Future = Either<
|
||||||
@ -392,8 +390,8 @@ mod connect_impl {
|
|||||||
self.tcp_pool.poll_ready()
|
self.tcp_pool.poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Uri) -> Self::Future {
|
fn call(&mut self, req: Connect) -> Self::Future {
|
||||||
match req.scheme_str() {
|
match req.uri.scheme_str() {
|
||||||
Some("https") | Some("wss") => {
|
Some("https") | Some("wss") => {
|
||||||
Either::B(Either::B(InnerConnectorResponseB {
|
Either::B(Either::B(InnerConnectorResponseB {
|
||||||
fut: self.ssl_pool.call(req),
|
fut: self.ssl_pool.call(req),
|
||||||
@ -411,7 +409,7 @@ mod connect_impl {
|
|||||||
pub(crate) struct InnerConnectorResponseA<T, Io1, Io2>
|
pub(crate) struct InnerConnectorResponseA<T, Io1, Io2>
|
||||||
where
|
where
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io1, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io1, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
fut: <ConnectionPool<T, Io1> as Service>::Future,
|
fut: <ConnectionPool<T, Io1> as Service>::Future,
|
||||||
_t: PhantomData<Io2>,
|
_t: PhantomData<Io2>,
|
||||||
@ -419,7 +417,7 @@ mod connect_impl {
|
|||||||
|
|
||||||
impl<T, Io1, Io2> Future for InnerConnectorResponseA<T, Io1, Io2>
|
impl<T, Io1, Io2> Future for InnerConnectorResponseA<T, Io1, Io2>
|
||||||
where
|
where
|
||||||
T: Service<Request = Uri, Response = (Io1, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io1, Protocol), Error = ConnectError>,
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
{
|
{
|
||||||
@ -437,7 +435,7 @@ mod connect_impl {
|
|||||||
pub(crate) struct InnerConnectorResponseB<T, Io1, Io2>
|
pub(crate) struct InnerConnectorResponseB<T, Io1, Io2>
|
||||||
where
|
where
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io2, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io2, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
fut: <ConnectionPool<T, Io2> as Service>::Future,
|
fut: <ConnectionPool<T, Io2> as Service>::Future,
|
||||||
_t: PhantomData<Io1>,
|
_t: PhantomData<Io1>,
|
||||||
@ -445,7 +443,7 @@ mod connect_impl {
|
|||||||
|
|
||||||
impl<T, Io1, Io2> Future for InnerConnectorResponseB<T, Io1, Io2>
|
impl<T, Io1, Io2> Future for InnerConnectorResponseB<T, Io1, Io2>
|
||||||
where
|
where
|
||||||
T: Service<Request = Uri, Response = (Io2, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io2, Protocol), Error = ConnectError>,
|
||||||
Io1: AsyncRead + AsyncWrite + 'static,
|
Io1: AsyncRead + AsyncWrite + 'static,
|
||||||
Io2: AsyncRead + AsyncWrite + 'static,
|
Io2: AsyncRead + AsyncWrite + 'static,
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
//! Http client api
|
//! Http client api
|
||||||
|
use http::Uri;
|
||||||
|
|
||||||
mod connection;
|
mod connection;
|
||||||
mod connector;
|
mod connector;
|
||||||
mod error;
|
mod error;
|
||||||
@ -10,3 +12,9 @@ pub use self::connection::Connection;
|
|||||||
pub use self::connector::Connector;
|
pub use self::connector::Connector;
|
||||||
pub use self::error::{ConnectError, InvalidUrl, SendRequestError};
|
pub use self::error::{ConnectError, InvalidUrl, SendRequestError};
|
||||||
pub use self::pool::Protocol;
|
pub use self::pool::Protocol;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Connect {
|
||||||
|
pub uri: Uri,
|
||||||
|
pub addr: Option<std::net::SocketAddr>,
|
||||||
|
}
|
||||||
|
@ -13,13 +13,14 @@ use futures::unsync::oneshot;
|
|||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
use h2::client::{handshake, Handshake};
|
use h2::client::{handshake, Handshake};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use http::uri::{Authority, Uri};
|
use http::uri::Authority;
|
||||||
use indexmap::IndexSet;
|
use indexmap::IndexSet;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use tokio_timer::{sleep, Delay};
|
use tokio_timer::{sleep, Delay};
|
||||||
|
|
||||||
use super::connection::{ConnectionType, IoConnection};
|
use super::connection::{ConnectionType, IoConnection};
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
|
use super::Connect;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
/// Protocol version
|
/// Protocol version
|
||||||
@ -48,7 +49,7 @@ pub(crate) struct ConnectionPool<T, Io: AsyncRead + AsyncWrite + 'static>(
|
|||||||
impl<T, Io> ConnectionPool<T, Io>
|
impl<T, Io> ConnectionPool<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
Io: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
connector: T,
|
connector: T,
|
||||||
@ -87,9 +88,9 @@ where
|
|||||||
impl<T, Io> Service for ConnectionPool<T, Io>
|
impl<T, Io> Service for ConnectionPool<T, Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite + 'static,
|
Io: AsyncRead + AsyncWrite + 'static,
|
||||||
T: Service<Request = Uri, Response = (Io, Protocol), Error = ConnectError>,
|
T: Service<Request = Connect, Response = (Io, Protocol), Error = ConnectError>,
|
||||||
{
|
{
|
||||||
type Request = Uri;
|
type Request = Connect;
|
||||||
type Response = IoConnection<Io>;
|
type Response = IoConnection<Io>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<
|
type Future = Either<
|
||||||
@ -101,8 +102,8 @@ where
|
|||||||
self.0.poll_ready()
|
self.0.poll_ready()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Uri) -> Self::Future {
|
fn call(&mut self, req: Connect) -> Self::Future {
|
||||||
let key = if let Some(authority) = req.authority_part() {
|
let key = if let Some(authority) = req.uri.authority_part() {
|
||||||
authority.clone().into()
|
authority.clone().into()
|
||||||
} else {
|
} else {
|
||||||
return Either::A(err(ConnectError::Unresolverd));
|
return Either::A(err(ConnectError::Unresolverd));
|
||||||
@ -292,7 +293,10 @@ pub(crate) struct Inner<Io> {
|
|||||||
limit: usize,
|
limit: usize,
|
||||||
acquired: usize,
|
acquired: usize,
|
||||||
available: HashMap<Key, VecDeque<AvailableConnection<Io>>>,
|
available: HashMap<Key, VecDeque<AvailableConnection<Io>>>,
|
||||||
waiters: Slab<(Uri, oneshot::Sender<Result<IoConnection<Io>, ConnectError>>)>,
|
waiters: Slab<(
|
||||||
|
Connect,
|
||||||
|
oneshot::Sender<Result<IoConnection<Io>, ConnectError>>,
|
||||||
|
)>,
|
||||||
waiters_queue: IndexSet<(Key, usize)>,
|
waiters_queue: IndexSet<(Key, usize)>,
|
||||||
task: AtomicTask,
|
task: AtomicTask,
|
||||||
}
|
}
|
||||||
@ -331,14 +335,14 @@ where
|
|||||||
/// connection is not available, wait
|
/// connection is not available, wait
|
||||||
fn wait_for(
|
fn wait_for(
|
||||||
&mut self,
|
&mut self,
|
||||||
connect: Uri,
|
connect: Connect,
|
||||||
) -> (
|
) -> (
|
||||||
oneshot::Receiver<Result<IoConnection<Io>, ConnectError>>,
|
oneshot::Receiver<Result<IoConnection<Io>, ConnectError>>,
|
||||||
usize,
|
usize,
|
||||||
) {
|
) {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
let key: Key = connect.authority_part().unwrap().clone().into();
|
let key: Key = connect.uri.authority_part().unwrap().clone().into();
|
||||||
let entry = self.waiters.vacant_entry();
|
let entry = self.waiters.vacant_entry();
|
||||||
let token = entry.key();
|
let token = entry.key();
|
||||||
entry.insert((connect, tx));
|
entry.insert((connect, tx));
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## [0.1.1] - 2019-04-xx
|
## [0.1.1] - 2019-04-19
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* Allow to specify server address for http and ws requests.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "awc"
|
name = "awc"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix http client."
|
description = "Actix http client."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -3,8 +3,8 @@ use std::fmt;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_http::client::{ConnectError, Connection, Connector};
|
use actix_http::client::{Connect, ConnectError, Connection, Connector};
|
||||||
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom, Uri};
|
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
|
||||||
use crate::connect::ConnectorWrapper;
|
use crate::connect::ConnectorWrapper;
|
||||||
@ -40,7 +40,7 @@ impl ClientBuilder {
|
|||||||
/// Use custom connector service.
|
/// Use custom connector service.
|
||||||
pub fn connector<T>(mut self, connector: T) -> Self
|
pub fn connector<T>(mut self, connector: T) -> Self
|
||||||
where
|
where
|
||||||
T: Service<Request = Uri, Error = ConnectError> + 'static,
|
T: Service<Request = Connect, Error = ConnectError> + 'static,
|
||||||
T::Response: Connection,
|
T::Response: Connection,
|
||||||
<T::Response as Connection>::Future: 'static,
|
<T::Response as Connection>::Future: 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
use std::{fmt, io};
|
use std::{fmt, io, net};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Framed};
|
||||||
use actix_http::body::Body;
|
use actix_http::body::Body;
|
||||||
use actix_http::client::{ConnectError, Connection, SendRequestError};
|
use actix_http::client::{
|
||||||
|
Connect as ClientConnect, ConnectError, Connection, SendRequestError,
|
||||||
|
};
|
||||||
use actix_http::h1::ClientCodec;
|
use actix_http::h1::ClientCodec;
|
||||||
use actix_http::{http, RequestHead, ResponseHead};
|
use actix_http::{RequestHead, ResponseHead};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use futures::{Future, Poll};
|
use futures::{Future, Poll};
|
||||||
|
|
||||||
@ -17,12 +19,14 @@ pub(crate) trait Connect {
|
|||||||
&mut self,
|
&mut self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
|
||||||
|
|
||||||
/// Send request, returns Response and Framed
|
/// Send request, returns Response and Framed
|
||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<
|
) -> Box<
|
||||||
Future<
|
Future<
|
||||||
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
|
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
|
||||||
@ -33,7 +37,7 @@ pub(crate) trait Connect {
|
|||||||
|
|
||||||
impl<T> Connect for ConnectorWrapper<T>
|
impl<T> Connect for ConnectorWrapper<T>
|
||||||
where
|
where
|
||||||
T: Service<Request = http::Uri, Error = ConnectError>,
|
T: Service<Request = ClientConnect, Error = ConnectError>,
|
||||||
T::Response: Connection,
|
T::Response: Connection,
|
||||||
<T::Response as Connection>::Io: 'static,
|
<T::Response as Connection>::Io: 'static,
|
||||||
<T::Response as Connection>::Future: 'static,
|
<T::Response as Connection>::Future: 'static,
|
||||||
@ -44,11 +48,15 @@ where
|
|||||||
&mut self,
|
&mut self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
body: Body,
|
body: Body,
|
||||||
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
|
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
|
||||||
Box::new(
|
Box::new(
|
||||||
self.0
|
self.0
|
||||||
// connect to the host
|
// connect to the host
|
||||||
.call(head.uri.clone())
|
.call(ClientConnect {
|
||||||
|
uri: head.uri.clone(),
|
||||||
|
addr,
|
||||||
|
})
|
||||||
.from_err()
|
.from_err()
|
||||||
// send request
|
// send request
|
||||||
.and_then(move |connection| connection.send_request(head, body))
|
.and_then(move |connection| connection.send_request(head, body))
|
||||||
@ -59,6 +67,7 @@ where
|
|||||||
fn open_tunnel(
|
fn open_tunnel(
|
||||||
&mut self,
|
&mut self,
|
||||||
head: RequestHead,
|
head: RequestHead,
|
||||||
|
addr: Option<net::SocketAddr>,
|
||||||
) -> Box<
|
) -> Box<
|
||||||
Future<
|
Future<
|
||||||
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
|
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
|
||||||
@ -68,7 +77,10 @@ where
|
|||||||
Box::new(
|
Box::new(
|
||||||
self.0
|
self.0
|
||||||
// connect to the host
|
// connect to the host
|
||||||
.call(head.uri.clone())
|
.call(ClientConnect {
|
||||||
|
uri: head.uri.clone(),
|
||||||
|
addr,
|
||||||
|
})
|
||||||
.from_err()
|
.from_err()
|
||||||
// send request
|
// send request
|
||||||
.and_then(move |connection| connection.open_tunnel(head))
|
.and_then(move |connection| connection.open_tunnel(head))
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::fmt;
|
|
||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use std::{fmt, net};
|
||||||
|
|
||||||
use bytes::{BufMut, Bytes, BytesMut};
|
use bytes::{BufMut, Bytes, BytesMut};
|
||||||
use futures::future::{err, Either};
|
use futures::future::{err, Either};
|
||||||
@ -60,6 +60,7 @@ const HTTPS_ENCODING: &str = "gzip, deflate";
|
|||||||
pub struct ClientRequest {
|
pub struct ClientRequest {
|
||||||
pub(crate) head: RequestHead,
|
pub(crate) head: RequestHead,
|
||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
|
addr: Option<net::SocketAddr>,
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
@ -76,6 +77,7 @@ impl ClientRequest {
|
|||||||
config,
|
config,
|
||||||
head: RequestHead::default(),
|
head: RequestHead::default(),
|
||||||
err: None,
|
err: None,
|
||||||
|
addr: None,
|
||||||
cookies: None,
|
cookies: None,
|
||||||
timeout: None,
|
timeout: None,
|
||||||
response_decompress: true,
|
response_decompress: true,
|
||||||
@ -97,6 +99,15 @@ impl ClientRequest {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set socket address of the server.
|
||||||
|
///
|
||||||
|
/// This address is used for connection. If address is not
|
||||||
|
/// provided url's host name get resolved.
|
||||||
|
pub fn address(mut self, addr: net::SocketAddr) -> Self {
|
||||||
|
self.addr = Some(addr);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set HTTP method of this request.
|
/// Set HTTP method of this request.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn method(mut self, method: Method) -> Self {
|
pub fn method(mut self, method: Method) -> Self {
|
||||||
@ -435,7 +446,7 @@ impl ClientRequest {
|
|||||||
let fut = config
|
let fut = config
|
||||||
.connector
|
.connector
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.send_request(head, body.into())
|
.send_request(head, body.into(), slf.addr)
|
||||||
.map(move |res| {
|
.map(move |res| {
|
||||||
res.map_body(|head, payload| {
|
res.map_body(|head, payload| {
|
||||||
if response_decompress {
|
if response_decompress {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
//! Websockets client
|
//! Websockets client
|
||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
|
use std::net::SocketAddr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::{fmt, str};
|
use std::{fmt, str};
|
||||||
|
|
||||||
@ -29,6 +30,7 @@ pub struct WebsocketsRequest {
|
|||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
origin: Option<HeaderValue>,
|
origin: Option<HeaderValue>,
|
||||||
protocols: Option<String>,
|
protocols: Option<String>,
|
||||||
|
addr: Option<SocketAddr>,
|
||||||
max_size: usize,
|
max_size: usize,
|
||||||
server_mode: bool,
|
server_mode: bool,
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
@ -55,6 +57,7 @@ impl WebsocketsRequest {
|
|||||||
head,
|
head,
|
||||||
err,
|
err,
|
||||||
config,
|
config,
|
||||||
|
addr: None,
|
||||||
origin: None,
|
origin: None,
|
||||||
protocols: None,
|
protocols: None,
|
||||||
max_size: 65_536,
|
max_size: 65_536,
|
||||||
@ -63,6 +66,15 @@ impl WebsocketsRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set socket address of the server.
|
||||||
|
///
|
||||||
|
/// This address is used for connection. If address is not
|
||||||
|
/// provided url's host name get resolved.
|
||||||
|
pub fn address(mut self, addr: SocketAddr) -> Self {
|
||||||
|
self.addr = Some(addr);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set supported websocket protocols
|
/// Set supported websocket protocols
|
||||||
pub fn protocols<U, V>(mut self, protos: U) -> Self
|
pub fn protocols<U, V>(mut self, protos: U) -> Self
|
||||||
where
|
where
|
||||||
@ -274,7 +286,7 @@ impl WebsocketsRequest {
|
|||||||
.config
|
.config
|
||||||
.connector
|
.connector
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.open_tunnel(head)
|
.open_tunnel(head, self.addr)
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(move |(head, framed)| {
|
.and_then(move |(head, framed)| {
|
||||||
// verify response
|
// verify response
|
||||||
|
Loading…
Reference in New Issue
Block a user