diff --git a/Cargo.toml b/Cargo.toml index 9b2e717..eb96880 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ tokio-io = "0.1" tokio-tls = "0.1" url = "1.2" uuid = { version = "0.5", features = ["v4"] } -hyper-proxy = "0.3.0" +hyper-proxy = "0.4.0" [dev-dependencies] env_logger = "0.4" diff --git a/src/async_impl/client.rs b/src/async_impl/client.rs index 7c55802..3677b83 100644 --- a/src/async_impl/client.rs +++ b/src/async_impl/client.rs @@ -7,6 +7,7 @@ use futures::{Async, Future, Poll}; use hyper::client::{Connect, FutureResponse, HttpConnector}; use hyper::header::{Headers, Location, Referer, UserAgent, Accept, Encoding, AcceptEncoding, Range, qitem}; +use hyper_proxy::ProxyConnector; use hyper_proxy::Proxy as HyperProxy; use hyper_tls::HttpsConnector; use native_tls::{TlsConnector, TlsConnectorBuilder}; @@ -66,8 +67,7 @@ struct Config { gzip: bool, headers: Headers, hostname_verification: bool, - // TODO: investigate Vec as before - proxy: Proxy, + proxies: Vec, redirect_policy: RedirectPolicy, referer: bool, timeout: Option, @@ -89,7 +89,7 @@ impl ClientBuilder { gzip: true, headers: headers, hostname_verification: true, - proxy: Proxy::empty(), + proxies: Vec::new(), redirect_policy: RedirectPolicy::default(), referer: true, timeout: None, @@ -129,19 +129,24 @@ impl ClientBuilder { if !config.hostname_verification { https_connector.danger_disable_hostname_verification(true); } - let mut connector = config.proxy.inner.clone().with_connector(https_connector); + let mut connector = ProxyConnector::unsecured(https_connector); let tls = try_!(config.tls.build()); connector.set_tls(Some(tls)); + connector.extend_proxies(config.proxies.iter().map(|p| p.inner.clone())); let hyper_client = ::hyper::Client::configure() .connector(connector) .build(handle); + // save proxies for http request + let mut proxy = ProxyConnector::unsecured(()); + proxy.extend_proxies(config.proxies.into_iter().map(|p| p.inner)); + Ok(Client { inner: Arc::new(ClientRef { gzip: config.gzip, hyper: hyper_client, - proxy: config.proxy.inner, + proxy: proxy, headers: config.headers, redirect_policy: config.redirect_policy, referer: config.referer, @@ -224,7 +229,7 @@ impl ClientBuilder { #[inline] pub fn proxy(&mut self, proxy: Proxy) -> &mut ClientBuilder { if let Some(config) = config_mut(&mut self.config, &self.err) { - config.proxy = proxy; + config.proxies.push(proxy); } self } @@ -278,7 +283,7 @@ fn config_mut<'a>(config: &'a mut Option, err: &Option<::Error>) -> Opti } } -type HyperClient = ::hyper::Client<::hyper_proxy::Proxy>>; +type HyperClient = ::hyper::Client<::hyper_proxy::ProxyConnector>>; impl Client { /// Constructs a new `Client`. @@ -414,9 +419,9 @@ impl Client { reusable }); - if self.inner.proxy.intercept().matches(&uri) && uri.scheme() == Some("http") { + if let Some(headers) = self.inner.proxy.http_headers(&uri) { req.set_proxy(true); - req.headers_mut().extend(self.inner.proxy.headers().iter()); + req.headers_mut().extend(headers.iter()); } let in_flight = self.inner.hyper.request(req); @@ -457,7 +462,7 @@ impl fmt::Debug for ClientBuilder { struct ClientRef { gzip: bool, headers: Headers, - proxy: HyperProxy<()>, + proxy: ProxyConnector<()>, hyper: HyperClient, redirect_policy: RedirectPolicy, referer: bool, @@ -556,9 +561,9 @@ impl Future for PendingRequest { if let Some(Some(ref body)) = self.body { req.set_body(body.clone()); } - if self.client.proxy.intercept().matches(&uri) && uri.scheme() == Some("http") { + if let Some(headers) = self.client.proxy.http_headers(&uri) { req.set_proxy(true); - req.headers_mut().extend(self.client.proxy.headers().iter()); + req.headers_mut().extend(headers.iter()); } self.in_flight = self.client.hyper.request(req); continue; diff --git a/src/proxy.rs b/src/proxy.rs index c83b7c0..f3c0a16 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -37,7 +37,7 @@ use hyper_proxy::Proxy as HyperProxy; /// would prevent a `Proxy` later in the list from ever working, so take care. #[derive(Debug)] pub struct Proxy { - pub(crate) inner: HyperProxy<()>, + pub(crate) inner: HyperProxy, } impl Proxy { @@ -112,7 +112,7 @@ impl Proxy { /// # fn main() {} pub fn custom(fun: F, url: U) -> ::Result where F: Fn(&Uri) -> bool + 'static + Send + Sync { - Proxy::new(Intercept::Custom(fun.into()), url) + Proxy::new(fun, url) } /// Set proxy authorization @@ -126,13 +126,8 @@ impl Proxy { } */ - /// Get a new empty proxy which will never intercept Uris - pub(crate) fn empty() -> Proxy { - Proxy { inner: HyperProxy::unsecured((), Intercept::None, Uri::default()) } - } - - fn new(intercept: Intercept, url: U) -> ::Result { + fn new>(intercept: I, url: U) -> ::Result { let uri = ::into_url::to_uri(&try_!(url.into_url())); - Ok(Proxy { inner: try_!(HyperProxy::new((), intercept, uri)) }) + Ok(Proxy { inner: HyperProxy::new(intercept, uri) }) } }