Add support for system/environment proxies (#547)

This commit is contained in:
WindSoilder
2019-07-02 07:27:58 +08:00
committed by Sean McArthur
parent 564a08f230
commit 577d06c363
9 changed files with 255 additions and 5 deletions

View File

@@ -36,6 +36,7 @@ use into_url::{expect_uri, try_uri};
use cookie;
use redirect::{self, RedirectPolicy, remove_sensitive_headers};
use {IntoUrl, Method, Proxy, StatusCode, Url};
use ::proxy::get_proxies;
#[cfg(feature = "tls")]
use {Certificate, Identity};
#[cfg(feature = "tls")]
@@ -327,6 +328,26 @@ impl ClientBuilder {
self
}
/// Clear all `Proxies`, so `Client` will use no proxy anymore.
pub fn no_proxy(mut self) -> ClientBuilder {
self.config.proxies.clear();
self
}
/// Add system proxy setting to the list of proxies
pub fn use_sys_proxy(mut self) -> ClientBuilder {
let proxies = get_proxies();
self.config.proxies.push(Proxy::custom(move |url| {
return if proxies.contains_key(url.scheme()) {
Some((*proxies.get(url.scheme()).unwrap()).clone())
} else {
None
}
}));
self
}
/// Set a `RedirectPolicy` for this client.
///
/// Default will follow redirects up to a maximum of 10.