Skip to content

Commit

Permalink
fix: Make HTTP(S)_PROXY variables take precedence over ALL_PROXY (#2370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Aug 5, 2024
1 parent b63cc56 commit 5715050
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@
//!
//! System proxies look in environment variables to set HTTP or HTTPS proxies.
//!
//! `HTTP_PROXY` or `http_proxy` provide http proxies for http connections while
//! `HTTP_PROXY` or `http_proxy` provide HTTP proxies for HTTP connections while
//! `HTTPS_PROXY` or `https_proxy` provide HTTPS proxies for HTTPS connections.
//! `ALL_PROXY` or `all_proxy` provide proxies for both HTTP and HTTPS connections.
//! If both the all proxy and HTTP or HTTPS proxy variables are set the more specific
//! HTTP or HTTPS proxies take precedence.
//!
//! These can be overwritten by adding a [`Proxy`] to `ClientBuilder`
//! i.e. `let proxy = reqwest::Proxy::http("https://secure.example")?;`
Expand Down
19 changes: 11 additions & 8 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,13 @@ fn insert_proxy(proxies: &mut SystemProxyMap, scheme: impl Into<String>, addr: S
fn get_from_environment() -> SystemProxyMap {
let mut proxies = HashMap::new();

if !(insert_from_env(&mut proxies, "http", "ALL_PROXY")
&& insert_from_env(&mut proxies, "https", "ALL_PROXY"))
{
insert_from_env(&mut proxies, "http", "all_proxy");
insert_from_env(&mut proxies, "https", "all_proxy");
}

if is_cgi() {
if log::log_enabled!(log::Level::Warn) && env::var_os("HTTP_PROXY").is_some() {
log::warn!("HTTP_PROXY environment variable ignored in CGI");
Expand All @@ -909,13 +916,6 @@ fn get_from_environment() -> SystemProxyMap {
insert_from_env(&mut proxies, "https", "https_proxy");
}

if !(insert_from_env(&mut proxies, "http", "ALL_PROXY")
&& insert_from_env(&mut proxies, "https", "ALL_PROXY"))
{
insert_from_env(&mut proxies, "http", "all_proxy");
insert_from_env(&mut proxies, "https", "all_proxy");
}

proxies
}

Expand Down Expand Up @@ -1300,7 +1300,10 @@ mod tests {
assert_eq!(p.host(), "127.0.0.1");

assert_eq!(all_proxies.len(), 2);
assert!(all_proxies.values().all(|p| p.host() == "127.0.0.2"));
// Set by ALL_PROXY
assert_eq!(all_proxies["https"].host(), "127.0.0.2");
// Overwritten by the more specific HTTP_PROXY
assert_eq!(all_proxies["http"].host(), "127.0.0.1");
}

#[cfg(any(target_os = "windows", target_os = "macos"))]
Expand Down

0 comments on commit 5715050

Please sign in to comment.