Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the env_proxy crate for proxy environment variable handling #598

Merged
merged 3 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/download/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
63 changes: 14 additions & 49 deletions src/download/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ pub mod rustls {
pub mod hyper_base {

extern crate hyper;
extern crate env_proxy;

use super::Event;
use std::io;
Expand Down Expand Up @@ -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"));
Expand All @@ -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<bool> {
Expand Down