From 125469a4ad14073aa61153780c005fe0a22cfce6 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Wed, 7 Aug 2024 09:26:07 +0800 Subject: [PATCH 1/3] fix(proxy): Make HTTP(S)_PROXY variables take precedence over ALL_PROXY --- examples/headers_order.rs | 1 + src/lib.rs | 5 ++++- src/proxy.rs | 14 +++++++------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/headers_order.rs b/examples/headers_order.rs index 4f4114f5..63c3ff10 100644 --- a/examples/headers_order.rs +++ b/examples/headers_order.rs @@ -8,6 +8,7 @@ async fn main() -> Result<(), Box> { let client = rquest::Client::builder() .impersonate(Impersonate::Edge127) .header_order(vec![ + header::USER_AGENT, header::HOST, HeaderName::from_static("priority"), header::COOKIE, diff --git a/src/lib.rs b/src/lib.rs index d2150f45..cd6aa732 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,8 +203,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 = rquest::Proxy::http("https://secure.example")?;` diff --git a/src/proxy.rs b/src/proxy.rs index e60db098..6dfae0ca 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -874,6 +874,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() { @@ -887,13 +894,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 } From 80130f5b9acf5770e4cb4b359e8ef88008bd4dca Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Wed, 7 Aug 2024 09:29:41 +0800 Subject: [PATCH 2/3] refactor: change Debug of Error to output url as str --- src/async_impl/response.rs | 2 +- src/error.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index 0cab71bb..36b77d5e 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -389,7 +389,7 @@ impl Response { impl fmt::Debug for Response { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Response") - .field("url", self.url()) + .field("url", &self.url().as_str()) .field("status", &self.status()) .field("headers", self.headers()) .finish() diff --git a/src/error.rs b/src/error.rs index cefdc290..dd92201e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -171,7 +171,7 @@ impl fmt::Debug for Error { builder.field("kind", &self.inner.kind); if let Some(ref url) = self.inner.url { - builder.field("url", url); + builder.field("url", &url.as_str()); } if let Some(ref source) = self.inner.source { builder.field("source", source); From 0ba55e3b1de251115fdb560635e7d0370c9960f0 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Wed, 7 Aug 2024 09:36:19 +0800 Subject: [PATCH 3/3] fix: Fix incorrect Accept-Encoding header combinations in Accepts::as_str --- src/async_impl/decoder.rs | 58 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/async_impl/decoder.rs b/src/async_impl/decoder.rs index 9be02150..607b8b28 100644 --- a/src/async_impl/decoder.rs +++ b/src/async_impl/decoder.rs @@ -447,9 +447,9 @@ impl Accepts { (true, true, true, false) => Some("gzip, br, zstd"), (true, true, false, false) => Some("gzip, br"), (true, false, true, true) => Some("gzip, zstd, deflate"), - (true, false, false, true) => Some("gzip, zstd, deflate"), + (true, false, false, true) => Some("gzip, deflate"), (false, true, true, true) => Some("br, zstd, deflate"), - (false, true, false, true) => Some("br, zstd, deflate"), + (false, true, false, true) => Some("br, deflate"), (true, false, true, false) => Some("gzip, zstd"), (true, false, false, false) => Some("gzip"), (false, true, true, false) => Some("br, zstd"), @@ -524,3 +524,57 @@ impl Default for Accepts { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn accepts_as_str() { + fn format_accept_encoding(accepts: &Accepts) -> String { + let mut encodings = vec![]; + if accepts.is_gzip() { + encodings.push("gzip"); + } + if accepts.is_brotli() { + encodings.push("br"); + } + if accepts.is_zstd() { + encodings.push("zstd"); + } + if accepts.is_deflate() { + encodings.push("deflate"); + } + encodings.join(", ") + } + + let state = [true, false]; + let mut permutations = Vec::new(); + + #[allow(unused_variables)] + for gzip in state { + for brotli in state { + for zstd in state { + for deflate in state { + permutations.push(Accepts { + #[cfg(feature = "gzip")] + gzip, + #[cfg(feature = "brotli")] + brotli, + #[cfg(feature = "zstd")] + zstd, + #[cfg(feature = "deflate")] + deflate, + }); + } + } + } + } + + for accepts in permutations { + let expected = format_accept_encoding(&accepts); + let got = accepts.as_str().unwrap_or(""); + assert_eq!(got, expected.as_str()); + } + } +} \ No newline at end of file