Skip to content

Commit

Permalink
feat(client): enable use of custom TLS wrapper for proxied connections
Browse files Browse the repository at this point in the history
The new struct ProxyConfig lets one specify a custom TLS wrapper for
proxied connections. The function Client::with_proxy_config takes an
instance of that struct and returns an appropriately initialized new
Client. The connector for talking to the proxy is still fixed to
HttpConnector.

Fixes hyperium#824
  • Loading branch information
inejge committed Jun 12, 2016
1 parent a75adee commit 0476196
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ use url::ParseError as UrlError;
use header::{Headers, Header, HeaderFormat};
use header::{ContentLength, Host, Location};
use method::Method;
use net::{NetworkConnector, NetworkStream};
use net::{HttpConnector, NetworkConnector, NetworkStream, SslClient};
use Error;

use self::proxy::tunnel;
use self::proxy::{Proxy, tunnel};
pub use self::pool::Pool;
pub use self::request::Request;
pub use self::response::Response;
Expand All @@ -84,6 +84,11 @@ pub mod response;
use http::Protocol;
use http::h1::Http11Protocol;

/// Proxy server configuration with a custom TLS wrapper.
pub struct ProxyConfig<H, S>(pub H, pub u16, pub S)
where H: Into<Cow<'static, str>>,
S: SslClient<<HttpConnector as NetworkConnector>::Stream> + Send + Sync + 'static;

/// A Client to use additional features with Requests.
///
/// Clients can handle things such as: redirect policy, connection pooling.
Expand Down Expand Up @@ -127,6 +132,21 @@ impl Client {
client
}

pub fn with_proxy_config<H, S>(proxy_config: ProxyConfig<H, S>) -> Client
where H: Into<Cow<'static, str>>,
S: SslClient<<HttpConnector as NetworkConnector>::Stream> + Send + Sync + 'static {
let host = proxy_config.0.into();
let port = proxy_config.1;
let proxy = Proxy {
connector: HttpConnector,
proxy: (host.clone(), port),
ssl: proxy_config.2
};
let mut client = Client::with_connector(Pool::with_connector(Default::default(), proxy));
client.proxy = Some((host, port));
client
}

/// Create a new client with a specific connector.
pub fn with_connector<C, S>(connector: C) -> Client
where C: NetworkConnector<Stream=S> + Send + Sync + 'static, S: NetworkStream + Send {
Expand Down

0 comments on commit 0476196

Please sign in to comment.