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

Make HTTP(S)_PROXY variables take precedence over ALL_PROXY #2370

Merged
merged 2 commits into from
Aug 5, 2024
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
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