remove proxy handling in favor of hyper-proxy crate
This commit is contained in:
205
src/proxy.rs
205
src/proxy.rs
@@ -1,8 +1,7 @@
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
||||
use hyper::Uri;
|
||||
use {into_url, IntoUrl, Url};
|
||||
use {IntoUrl};
|
||||
use hyper_proxy::Intercept;
|
||||
use hyper_proxy::Proxy as HyperProxy;
|
||||
|
||||
/// Configuration of a proxy that a `Client` should pass requests to.
|
||||
///
|
||||
@@ -15,7 +14,11 @@ use {into_url, IntoUrl, Url};
|
||||
///
|
||||
/// ```rust
|
||||
/// # fn run() -> Result<(), Box<::std::error::Error>> {
|
||||
/// let proxy = reqwest::Proxy::http("https://secure.example")?;
|
||||
/// let mut proxy = reqwest::Proxy::http("https://secure.example")?;
|
||||
/// // proxy.set_authorization(Basic {
|
||||
/// // username: "John Doe".into(),
|
||||
/// // password: Some("Agent1234".into()),
|
||||
/// // });
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
@@ -28,9 +31,9 @@ use {into_url, IntoUrl, Url};
|
||||
/// check each `Proxy` in the order it was added. This could mean that a
|
||||
/// `Proxy` added first with eager intercept rules, such as `Proxy::all`,
|
||||
/// would prevent a `Proxy` later in the list from ever working, so take care.
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct Proxy {
|
||||
intercept: Intercept,
|
||||
pub(crate) inner: HyperProxy<()>,
|
||||
}
|
||||
|
||||
impl Proxy {
|
||||
@@ -49,8 +52,7 @@ impl Proxy {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
pub fn http<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
||||
Ok(Proxy::new(Intercept::Http(uri)))
|
||||
Proxy::new(Intercept::Http, url)
|
||||
}
|
||||
|
||||
/// Proxy all HTTPS traffic to the passed URL.
|
||||
@@ -68,8 +70,7 @@ impl Proxy {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
pub fn https<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
||||
Ok(Proxy::new(Intercept::Https(uri)))
|
||||
Proxy::new(Intercept::Https, url)
|
||||
}
|
||||
|
||||
/// Proxy **all** traffic to the passed URL.
|
||||
@@ -87,8 +88,7 @@ impl Proxy {
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
pub fn all<U: IntoUrl>(url: U) -> ::Result<Proxy> {
|
||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
||||
Ok(Proxy::new(Intercept::All(uri)))
|
||||
Proxy::new(Intercept::All, url)
|
||||
}
|
||||
|
||||
/// Provide a custom function to determine what traffix to proxy to where.
|
||||
@@ -111,9 +111,9 @@ impl Proxy {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// # fn main() {}
|
||||
pub fn custom<F>(fun: F) -> Proxy
|
||||
where F: Fn(&Url) -> Option<Url> + Send + Sync + 'static {
|
||||
Proxy::new(Intercept::Custom(Custom(Arc::new(fun))))
|
||||
pub fn custom<F, U: IntoUrl>(fun: F, url: U) -> ::Result<Proxy>
|
||||
where F: Fn(&Uri) -> bool + 'static + Send + Sync {
|
||||
Proxy::new(Intercept::Custom(Box::new(fun)), url)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -122,174 +122,13 @@ impl Proxy {
|
||||
}
|
||||
*/
|
||||
|
||||
fn new(intercept: Intercept) -> Proxy {
|
||||
Proxy {
|
||||
intercept: intercept,
|
||||
}
|
||||
/// Get a new empty proxy which will never intercept Uris
|
||||
pub(crate) fn empty() -> Proxy {
|
||||
Proxy { inner: HyperProxy::unsecured((), Intercept::None, Uri::default()) }
|
||||
}
|
||||
|
||||
fn proxies(&self, url: &Url) -> bool {
|
||||
match self.intercept {
|
||||
Intercept::All(..) => true,
|
||||
Intercept::Http(..) => url.scheme() == "http",
|
||||
Intercept::Https(..) => url.scheme() == "https",
|
||||
Intercept::Custom(ref fun) => (fun.0)(url).is_some(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn intercept(&self, uri: &Uri) -> Option<Uri> {
|
||||
match self.intercept {
|
||||
Intercept::All(ref u) => Some(u.clone()),
|
||||
Intercept::Http(ref u) => {
|
||||
if uri.scheme() == Some("http") {
|
||||
Some(u.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
Intercept::Https(ref u) => {
|
||||
if uri.scheme() == Some("https") {
|
||||
Some(u.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
Intercept::Custom(ref fun) => {
|
||||
(fun.0)(&into_url::to_url(uri))
|
||||
.map(|u| into_url::to_uri(&u))
|
||||
},
|
||||
}
|
||||
fn new<U: IntoUrl>(intercept: Intercept, url: U) -> ::Result<Proxy> {
|
||||
let uri = ::into_url::to_uri(&try_!(url.into_url()));
|
||||
Ok(Proxy { inner: try_!(HyperProxy::new((), intercept, uri)) })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Intercept {
|
||||
All(Uri),
|
||||
Http(Uri),
|
||||
Https(Uri),
|
||||
Custom(Custom),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Custom(Arc<Fn(&Url) -> Option<Url> + Send + Sync + 'static>);
|
||||
|
||||
impl fmt::Debug for Custom {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str("_")
|
||||
}
|
||||
}
|
||||
|
||||
// pub(crate)
|
||||
|
||||
pub fn intercept(proxy: &Proxy, uri: &Uri) -> Option<Uri> {
|
||||
proxy.intercept(uri)
|
||||
}
|
||||
|
||||
pub fn is_proxied(proxies: &[Proxy], uri: &Url) -> bool {
|
||||
proxies.iter().any(|p| p.proxies(uri))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn uri(s: &str) -> Uri {
|
||||
s.parse().unwrap()
|
||||
}
|
||||
|
||||
fn url(s: &str) -> Url {
|
||||
s.parse().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_http() {
|
||||
let target = "http://example.domain/";
|
||||
let p = Proxy::http(target).unwrap();
|
||||
|
||||
let http = "http://hyper.rs";
|
||||
let other = "https://hyper.rs";
|
||||
|
||||
assert!(p.proxies(&url(http)));
|
||||
assert_eq!(p.intercept(&uri(http)).unwrap(), target);
|
||||
assert!(!p.proxies(&url(other)));
|
||||
assert!(p.intercept(&uri(other)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_https() {
|
||||
let target = "http://example.domain/";
|
||||
let p = Proxy::https(target).unwrap();
|
||||
|
||||
let http = "http://hyper.rs";
|
||||
let other = "https://hyper.rs";
|
||||
|
||||
assert!(!p.proxies(&url(http)));
|
||||
assert!(p.intercept(&uri(http)).is_none());
|
||||
assert!(p.proxies(&url(other)));
|
||||
assert_eq!(p.intercept(&uri(other)).unwrap(), target);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all() {
|
||||
let target = "http://example.domain/";
|
||||
let p = Proxy::all(target).unwrap();
|
||||
|
||||
let http = "http://hyper.rs";
|
||||
let https = "https://hyper.rs";
|
||||
let other = "x-youve-never-heard-of-me-mr-proxy://hyper.rs";
|
||||
|
||||
assert!(p.proxies(&url(http)));
|
||||
assert!(p.proxies(&url(https)));
|
||||
assert!(p.proxies(&url(other)));
|
||||
|
||||
assert_eq!(p.intercept(&uri(http)).unwrap(), target);
|
||||
assert_eq!(p.intercept(&uri(https)).unwrap(), target);
|
||||
assert_eq!(p.intercept(&uri(other)).unwrap(), target);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_custom() {
|
||||
let target1 = "http://example.domain/";
|
||||
let target2 = "https://example.domain/";
|
||||
let p = Proxy::custom(move |url| {
|
||||
if url.host_str() == Some("hyper.rs") {
|
||||
target1.parse().ok()
|
||||
} else if url.scheme() == "http" {
|
||||
target2.parse().ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
let http = "http://seanmonstar.com";
|
||||
let https = "https://hyper.rs";
|
||||
let other = "x-youve-never-heard-of-me-mr-proxy://seanmonstar.com";
|
||||
|
||||
assert!(p.proxies(&url(http)));
|
||||
assert!(p.proxies(&url(https)));
|
||||
assert!(!p.proxies(&url(other)));
|
||||
|
||||
assert_eq!(p.intercept(&uri(http)).unwrap(), target2);
|
||||
assert_eq!(p.intercept(&uri(https)).unwrap(), target1);
|
||||
assert!(p.intercept(&uri(other)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_proxied() {
|
||||
let proxies = vec![
|
||||
Proxy::http("http://example.domain").unwrap(),
|
||||
Proxy::https("http://other.domain").unwrap(),
|
||||
];
|
||||
|
||||
let http = "http://hyper.rs".parse().unwrap();
|
||||
let https = "https://hyper.rs".parse().unwrap();
|
||||
let other = "x-other://hyper.rs".parse().unwrap();
|
||||
|
||||
assert!(is_proxied(&proxies, &http));
|
||||
assert!(is_proxied(&proxies, &https));
|
||||
assert!(!is_proxied(&proxies, &other));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user