store intercept to set_proxy on requests

This commit is contained in:
Johann Tuffe
2017-12-18 17:53:17 +08:00
parent 8b1bb0b53e
commit 07f89984c4
4 changed files with 15 additions and 3 deletions

View File

@@ -26,7 +26,8 @@ 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.2.0" hyper-proxy = { version = "0.2.0", path = "../hyper-proxy" }
#hyper-proxy = "0.2.0"
[dev-dependencies] [dev-dependencies]
env_logger = "0.4" env_logger = "0.4"

View File

@@ -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::Intercept;
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use native_tls::{TlsConnector, TlsConnectorBuilder}; use native_tls::{TlsConnector, TlsConnectorBuilder};
use tokio_core::reactor::Handle; use tokio_core::reactor::Handle;
@@ -128,6 +129,7 @@ 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 intercept = config.proxy.inner.intercept().clone();
let mut connector = config.proxy.inner.with_connector(https_connector); let mut connector = config.proxy.inner.with_connector(https_connector);
let tls = try_!(config.tls.build()); let tls = try_!(config.tls.build());
connector.set_tls(Some(tls)); connector.set_tls(Some(tls));
@@ -140,6 +142,7 @@ impl ClientBuilder {
inner: Arc::new(ClientRef { inner: Arc::new(ClientRef {
gzip: config.gzip, gzip: config.gzip,
hyper: hyper_client, hyper: hyper_client,
intercept: intercept,
headers: config.headers, headers: config.headers,
redirect_policy: config.redirect_policy, redirect_policy: config.redirect_policy,
referer: config.referer, referer: config.referer,
@@ -412,6 +415,9 @@ impl Client {
reusable reusable
}); });
if self.inner.intercept.matches(&uri) && uri.scheme() == Some("http") {
req.set_proxy(true);
}
let in_flight = self.inner.hyper.request(req); let in_flight = self.inner.hyper.request(req);
Pending { Pending {
@@ -451,6 +457,7 @@ impl fmt::Debug for ClientBuilder {
struct ClientRef { struct ClientRef {
gzip: bool, gzip: bool,
headers: Headers, headers: Headers,
intercept: Intercept,
hyper: HyperClient, hyper: HyperClient,
redirect_policy: RedirectPolicy, redirect_policy: RedirectPolicy,
referer: bool, referer: bool,
@@ -549,6 +556,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.intercept.matches(&uri) && uri.scheme() == Some("http") {
req.set_proxy(true);
}
self.in_flight = self.client.hyper.request(req); self.in_flight = self.client.hyper.request(req);
continue; continue;
}, },

View File

@@ -113,7 +113,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(Box::new(fun)), url) Proxy::new(Intercept::Custom(fun.into()), url)
} }
/* /*

View File

@@ -52,7 +52,8 @@ pub fn spawn(txns: Vec<Txn>) -> Server {
let mut n = 0; let mut n = 0;
while n < expected.len() { while n < expected.len() {
match socket.read(&mut buf[n..]) { match socket.read(&mut buf[n..]) {
Ok(0) | Err(_) => break, Ok(0) => break,
Err(e) => panic!(e),
Ok(nread) => n += nread, Ok(nread) => n += nread,
} }
} }