Skip to content

Commit 1b41567

Browse files
authored
Merge pull request #825 from inejge/client-proxy-config
Let the user specify a custom TLS wrapper for proxied connections
2 parents a75adee + 0476196 commit 1b41567

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/client/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ use url::ParseError as UrlError;
6868
use header::{Headers, Header, HeaderFormat};
6969
use header::{ContentLength, Host, Location};
7070
use method::Method;
71-
use net::{NetworkConnector, NetworkStream};
71+
use net::{HttpConnector, NetworkConnector, NetworkStream, SslClient};
7272
use Error;
7373

74-
use self::proxy::tunnel;
74+
use self::proxy::{Proxy, tunnel};
7575
pub use self::pool::Pool;
7676
pub use self::request::Request;
7777
pub use self::response::Response;
@@ -84,6 +84,11 @@ pub mod response;
8484
use http::Protocol;
8585
use http::h1::Http11Protocol;
8686

87+
/// Proxy server configuration with a custom TLS wrapper.
88+
pub struct ProxyConfig<H, S>(pub H, pub u16, pub S)
89+
where H: Into<Cow<'static, str>>,
90+
S: SslClient<<HttpConnector as NetworkConnector>::Stream> + Send + Sync + 'static;
91+
8792
/// A Client to use additional features with Requests.
8893
///
8994
/// Clients can handle things such as: redirect policy, connection pooling.
@@ -127,6 +132,21 @@ impl Client {
127132
client
128133
}
129134

135+
pub fn with_proxy_config<H, S>(proxy_config: ProxyConfig<H, S>) -> Client
136+
where H: Into<Cow<'static, str>>,
137+
S: SslClient<<HttpConnector as NetworkConnector>::Stream> + Send + Sync + 'static {
138+
let host = proxy_config.0.into();
139+
let port = proxy_config.1;
140+
let proxy = Proxy {
141+
connector: HttpConnector,
142+
proxy: (host.clone(), port),
143+
ssl: proxy_config.2
144+
};
145+
let mut client = Client::with_connector(Pool::with_connector(Default::default(), proxy));
146+
client.proxy = Some((host, port));
147+
client
148+
}
149+
130150
/// Create a new client with a specific connector.
131151
pub fn with_connector<C, S>(connector: C) -> Client
132152
where C: NetworkConnector<Stream=S> + Send + Sync + 'static, S: NetworkStream + Send {

0 commit comments

Comments
 (0)