Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

fix: Fix incorrect Accept-Encoding header combinations in Accepts::as_str #89

Merged
merged 3 commits into from
Aug 7, 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
1 change: 1 addition & 0 deletions examples/headers_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client = rquest::Client::builder()
.impersonate(Impersonate::Edge127)
.header_order(vec![
header::USER_AGENT,
header::HOST,
HeaderName::from_static("priority"),
header::COOKIE,
Expand Down
58 changes: 56 additions & 2 deletions src/async_impl/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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());
}
}
}
2 changes: 1 addition & 1 deletion src/async_impl/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;`
Expand Down
14 changes: 7 additions & 7 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,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() {
Expand All @@ -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
}

Expand Down
Loading