1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-24 22:37:35 +02:00

allow to specify server address for http and ws requests

This commit is contained in:
Nikolay Kim
2019-04-19 18:03:44 -07:00
parent 7292d0b696
commit fc9b14a933
11 changed files with 129 additions and 78 deletions

View File

@ -3,8 +3,8 @@ use std::fmt;
use std::rc::Rc;
use std::time::Duration;
use actix_http::client::{ConnectError, Connection, Connector};
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom, Uri};
use actix_http::client::{Connect, ConnectError, Connection, Connector};
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom};
use actix_service::Service;
use crate::connect::ConnectorWrapper;
@ -40,7 +40,7 @@ impl ClientBuilder {
/// Use custom connector service.
pub fn connector<T>(mut self, connector: T) -> Self
where
T: Service<Request = Uri, Error = ConnectError> + 'static,
T: Service<Request = Connect, Error = ConnectError> + 'static,
T::Response: Connection,
<T::Response as Connection>::Future: 'static,
T::Future: 'static,

View File

@ -1,10 +1,12 @@
use std::{fmt, io};
use std::{fmt, io, net};
use actix_codec::{AsyncRead, AsyncWrite, Framed};
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::{http, RequestHead, ResponseHead};
use actix_http::{RequestHead, ResponseHead};
use actix_service::Service;
use futures::{Future, Poll};
@ -17,12 +19,14 @@ pub(crate) trait Connect {
&mut self,
head: RequestHead,
body: Body,
addr: Option<net::SocketAddr>,
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>>;
/// Send request, returns Response and Framed
fn open_tunnel(
&mut self,
head: RequestHead,
addr: Option<net::SocketAddr>,
) -> Box<
Future<
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
@ -33,7 +37,7 @@ pub(crate) trait Connect {
impl<T> Connect for ConnectorWrapper<T>
where
T: Service<Request = http::Uri, Error = ConnectError>,
T: Service<Request = ClientConnect, Error = ConnectError>,
T::Response: Connection,
<T::Response as Connection>::Io: 'static,
<T::Response as Connection>::Future: 'static,
@ -44,11 +48,15 @@ where
&mut self,
head: RequestHead,
body: Body,
addr: Option<net::SocketAddr>,
) -> Box<Future<Item = ClientResponse, Error = SendRequestError>> {
Box::new(
self.0
// connect to the host
.call(head.uri.clone())
.call(ClientConnect {
uri: head.uri.clone(),
addr,
})
.from_err()
// send request
.and_then(move |connection| connection.send_request(head, body))
@ -59,6 +67,7 @@ where
fn open_tunnel(
&mut self,
head: RequestHead,
addr: Option<net::SocketAddr>,
) -> Box<
Future<
Item = (ResponseHead, Framed<BoxedSocket, ClientCodec>),
@ -68,7 +77,10 @@ where
Box::new(
self.0
// connect to the host
.call(head.uri.clone())
.call(ClientConnect {
uri: head.uri.clone(),
addr,
})
.from_err()
// send request
.and_then(move |connection| connection.open_tunnel(head))

View File

@ -1,8 +1,8 @@
use std::fmt;
use std::fmt::Write as FmtWrite;
use std::io::Write;
use std::rc::Rc;
use std::time::Duration;
use std::{fmt, net};
use bytes::{BufMut, Bytes, BytesMut};
use futures::future::{err, Either};
@ -60,6 +60,7 @@ const HTTPS_ENCODING: &str = "gzip, deflate";
pub struct ClientRequest {
pub(crate) head: RequestHead,
err: Option<HttpError>,
addr: Option<net::SocketAddr>,
cookies: Option<CookieJar>,
response_decompress: bool,
timeout: Option<Duration>,
@ -76,6 +77,7 @@ impl ClientRequest {
config,
head: RequestHead::default(),
err: None,
addr: None,
cookies: None,
timeout: None,
response_decompress: true,
@ -97,6 +99,15 @@ impl ClientRequest {
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.
#[inline]
pub fn method(mut self, method: Method) -> Self {
@ -435,7 +446,7 @@ impl ClientRequest {
let fut = config
.connector
.borrow_mut()
.send_request(head, body.into())
.send_request(head, body.into(), slf.addr)
.map(move |res| {
res.map_body(|head, payload| {
if response_decompress {

View File

@ -1,5 +1,6 @@
//! Websockets client
use std::fmt::Write as FmtWrite;
use std::net::SocketAddr;
use std::rc::Rc;
use std::{fmt, str};
@ -29,6 +30,7 @@ pub struct WebsocketsRequest {
err: Option<HttpError>,
origin: Option<HeaderValue>,
protocols: Option<String>,
addr: Option<SocketAddr>,
max_size: usize,
server_mode: bool,
cookies: Option<CookieJar>,
@ -55,6 +57,7 @@ impl WebsocketsRequest {
head,
err,
config,
addr: None,
origin: None,
protocols: None,
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
pub fn protocols<U, V>(mut self, protos: U) -> Self
where
@ -274,7 +286,7 @@ impl WebsocketsRequest {
.config
.connector
.borrow_mut()
.open_tunnel(head)
.open_tunnel(head, self.addr)
.from_err()
.and_then(move |(head, framed)| {
// verify response