From e8f9a1b1293bf752b9fee1304e3e6989e373b4d2 Mon Sep 17 00:00:00 2001 From: Jan <59206115+Threated@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:36:18 +0200 Subject: [PATCH] fix: Make HTTP(S)_PROXY variables take precedence over ALL_PROXY (#2370) --- src/lib.rs | 5 ++++- src/proxy.rs | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d62cb8210..cf3d39d0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")?;` diff --git a/src/proxy.rs b/src/proxy.rs index f5e8f66ac..98708c775 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -897,6 +897,13 @@ fn insert_proxy(proxies: &mut SystemProxyMap, scheme: impl Into, 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"); @@ -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 } @@ -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"))]