diff --git a/Cargo.lock b/Cargo.lock index a8b90921fb..28e23cca94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,7 @@ version = "0.3.0" dependencies = [ "ca-loader 0.1.0", "curl 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_proxy 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -202,6 +203,14 @@ dependencies = [ "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "env_proxy" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "error-chain" version = "0.2.1" diff --git a/src/download/Cargo.toml b/src/download/Cargo.toml index e7cd5bc784..22a968c6b6 100644 --- a/src/download/Cargo.toml +++ b/src/download/Cargo.toml @@ -11,8 +11,8 @@ license = "MIT/Apache-2.0" default = ["hyper-backend"] curl-backend = ["curl"] -hyper-backend = ["hyper", "native-tls", "openssl-sys"] -rustls-backend = ["hyper", "rustls", "lazy_static", "ca-loader"] +hyper-backend = ["hyper", "env_proxy", "native-tls", "openssl-sys"] +rustls-backend = ["hyper", "env_proxy", "rustls", "lazy_static", "ca-loader"] [dependencies] error-chain = "0.2.1" @@ -25,6 +25,10 @@ version = "0.9.8" default-features = false optional = true +[dependencies.env_proxy] +version = "0.1.1" +optional = true + [dependencies.native-tls] git = "https://github.com/sfackler/rust-native-tls.git" optional = true diff --git a/src/download/src/lib.rs b/src/download/src/lib.rs index 3648400b66..b8211ed04f 100644 --- a/src/download/src/lib.rs +++ b/src/download/src/lib.rs @@ -531,6 +531,7 @@ pub mod rustls { pub mod hyper_base { extern crate hyper; + extern crate env_proxy; use super::Event; use std::io; @@ -558,30 +559,21 @@ pub mod hyper_base { use self::hyper::header::ContentLength; use self::hyper::net::{HttpsConnector}; - // The Hyper HTTP client - let client; - S::maybe_init_certs(); - let maybe_proxy = proxy_from_env(url); - if url.scheme() == "https" { - if maybe_proxy.is_none() { - // Connect with hyper + native_tls - client = Client::with_connector(HttpsConnector::new(S::new())); - } else { - let proxy_host_port = maybe_proxy.unwrap(); - client = Client::with_proxy_config(ProxyConfig(proxy_host_port.0, proxy_host_port.1, S::new())); - } - } else if url.scheme() == "http" { - if maybe_proxy.is_none() { - client = Client::new(); - } else { - let proxy_host_port = maybe_proxy.unwrap(); - client = Client::with_http_proxy(proxy_host_port.0, proxy_host_port.1); - } - } else { - return Err(format!("unsupported URL scheme: '{}'", url.scheme()).into()); - } + // The Hyper HTTP client + let maybe_proxy = env_proxy::for_url(url); + let client = match url.scheme() { + "https" => match maybe_proxy { + None => Client::with_connector(HttpsConnector::new(S::new())), + Some(host_port) => Client::with_proxy_config(ProxyConfig(host_port.0, host_port.1, S::new())) + }, + "http" => match maybe_proxy { + None => Client::new(), + Some(host_port) => Client::with_http_proxy(host_port.0, host_port.1) + }, + _ => return Err(format!("unsupported URL scheme: '{}'", url.scheme()).into()) + }; let mut res = try!(client.get(url.clone()).send() .chain_err(|| "failed to make network request")); @@ -608,33 +600,6 @@ pub mod hyper_base { } } - fn proxy_from_env(url: &Url) -> Option<(String, u16)> { - use std::env::var_os; - - let mut maybe_https_proxy = var_os("https_proxy").map(|ref v| v.to_str().unwrap_or("").to_string()); - if maybe_https_proxy.is_none() { - maybe_https_proxy = var_os("HTTPS_PROXY").map(|ref v| v.to_str().unwrap_or("").to_string()); - } - let maybe_http_proxy = var_os("http_proxy").map(|ref v| v.to_str().unwrap_or("").to_string()); - let mut maybe_all_proxy = var_os("all_proxy").map(|ref v| v.to_str().unwrap_or("").to_string()); - if maybe_all_proxy.is_none() { - maybe_all_proxy = var_os("ALL_PROXY").map(|ref v| v.to_str().unwrap_or("").to_string()); - } - if let Some(url_value) = match url.scheme() { - "https" => maybe_https_proxy.or(maybe_http_proxy.or(maybe_all_proxy)), - "http" => maybe_http_proxy.or(maybe_all_proxy), - _ => maybe_all_proxy, - } { - if let Ok(proxy_url) = Url::parse(&url_value) { - if let Some(host) = proxy_url.host_str() { - let port = proxy_url.port().unwrap_or(8080); - return Some((host.to_string(), port)); - } - } - } - None - } - fn download_from_file_url(url: &Url, callback: &Fn(Event) -> Result<()>) -> Result {