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