Skip to content

Commit

Permalink
dist: honor environment variables defining HTTP proxies
Browse files Browse the repository at this point in the history
I'm a bit surprised reqwest doesn't do this by default. But alas.

This should close #15.
  • Loading branch information
indygreg committed Jun 26, 2019
1 parent 25afc1d commit ae6ed70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ None yet.
Bug Fixes
^^^^^^^^^

* Honor ``HTTP_PROXY`` and ``HTTPS_PROXY`` environment variables when
downloading Python distributions. (#15)
* Handle BOM when compiling Python source files to bytecode. (#13)

All Relevant Changes
Expand Down
32 changes: 31 additions & 1 deletion pyoxidizer/src/pyrepackager/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,32 @@ fn sha256_path(path: &PathBuf) -> Vec<u8> {
hasher.result().to_vec()
}

pub fn get_http_client() -> reqwest::Result<reqwest::Client> {
let mut builder = reqwest::ClientBuilder::new();

for (key, value) in std::env::vars() {
let key = key.to_lowercase();
if key.ends_with("_proxy") {
let end = key.len() - "_proxy".len();
let schema = &key[..end];

if let Ok(url) = Url::parse(&value) {
if let Some(proxy) = match schema {
"http" => Some(reqwest::Proxy::http(url)),
"https" => Some(reqwest::Proxy::https(url)),
_ => None,
} {
if let Ok(proxy) = proxy {
builder = builder.proxy(proxy);
}
}
}
}
}

builder.build()
}

/// Ensure a Python distribution at a URL is available in a local directory.
///
/// The path to the downloaded and validated file is returned.
Expand Down Expand Up @@ -606,7 +632,11 @@ pub fn download_distribution(url: &str, sha256: &str, cache_dir: &Path) -> PathB
let mut data: Vec<u8> = Vec::new();

println!("downloading {}", url);
let mut response = reqwest::get(url).expect("unable to perform HTTP request");
let client = get_http_client().expect("unable to get HTTP client");
let mut response = client
.get(url)
.send()
.expect("unable to perform HTTP request");
response
.read_to_end(&mut data)
.expect("unable to download URL");
Expand Down

0 comments on commit ae6ed70

Please sign in to comment.