Skip to content

Commit

Permalink
gzip and brotli support
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jul 12, 2024
1 parent 38b86bb commit b55ed8f
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 23 deletions.
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ exclude = ["/cargo_deny.sh", "/deny.toml", "/test.sh"]
rust-version = "1.67"

[package.metadata.docs.rs]
features = ["rustls", "native-roots", "native-tls", "socks-proxy", "cookies"]
features = ["rustls", "native-roots", "native-tls", "socks-proxy", "cookies", "gzip", "brotli"]

[features]
default = ["rustls", "native-roots", "native-tls", "socks-proxy", "cookies"]
default = ["rustls", "native-roots", "native-tls", "socks-proxy", "cookies", "gzip", "brotli"]
rustls = ["dep:rustls", "_tls"]
native-tls = ["dep:native-tls", "dep:der", "_tls"]
native-roots = ["dep:rustls-native-certs"]
socks-proxy = ["dep:socks"]
cookies = ["dep:cookie_store", "_url"]
gzip = ["dep:flate2"]
brotli = ["dep:brotli-decompressor"]

# Underscore prefixed features are internal
_url = ["dep:url"]
Expand All @@ -40,6 +42,7 @@ http = "1.1.0"
log = "0.4.22"
thiserror = "1.0.61"
once_cell = "1.19.0"
smallvec = "1.13.2"

# These are used regardless of TLS implementation.
rustls-pemfile = { version = "2.1.2", optional = true, default-features = false, features = ["std"] }
Expand All @@ -58,6 +61,9 @@ socks = { version = "0.3.4", optional = true }
cookie_store = { version = "0.21.0", optional = true, default-features = false, features = ["preserve_order"] }
url = { version = "2.3.1", optional = true, default-features = false }

flate2 = { version = "1.0.30", optional = true }
brotli-decompressor = { version = "4.0.1", optional = true }

[build-dependencies]
cc = "1.0.106"

Expand Down
26 changes: 24 additions & 2 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use hoot::client::flow::RedirectAuthHeaders;
use http::{Method, Request, Response, Uri};

use crate::body::Body;
use crate::body::{Body, ResponseInfo};
use crate::pool::{Connection, ConnectionPool};
use crate::proxy::Proxy;
use crate::resolver::{DefaultResolver, Resolver};
Expand Down Expand Up @@ -285,6 +285,27 @@ impl Agent {
unit.handle_input(current_time(), input, &mut [])?;
}

#[cfg(any(feature = "gzip", feature = "brotli"))]
{
use once_cell::sync::Lazy;
static ACCEPTS: Lazy<String> = Lazy::new(|| {
let mut value = String::with_capacity(10);
#[cfg(feature = "gzip")]
value.push_str("gzip");
#[cfg(all(feature = "gzip", feature = "brotli"))]
value.push_str(", ");
#[cfg(feature = "brotli")]
value.push_str("br");
value
});
let input = Input::Header {
name: http::HeaderName::from_static("accept-encoding"),
// unwrap is ok because above ACCEPTS will produce a valid value
value: http::HeaderValue::from_str(&*ACCEPTS).unwrap(),

Check failure on line 304 in src/agent.rs

View workflow job for this annotation

GitHub Actions / Lint

deref which would be done by auto-deref

Check failure on line 304 in src/agent.rs

View workflow job for this annotation

GitHub Actions / Lint

deref which would be done by auto-deref
};
unit.handle_input(current_time(), input, &mut [])?;
}

unit.handle_input(current_time(), Input::Prepared, &mut [])?;
}

Expand Down Expand Up @@ -371,7 +392,8 @@ impl Agent {
let unit = unit.release_body();

let (parts, _) = response.into_parts();
let recv_body = Body::new(unit, connection, current_time);
let info = ResponseInfo::new(&parts.headers);
let recv_body = Body::new(unit, connection, info, current_time);
let response = Response::from_parts(parts, recv_body);

info!("{}", response.status());
Expand Down
Loading

0 comments on commit b55ed8f

Please sign in to comment.