Skip to content
Draft
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
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ charset = ["dep:encoding_rs", "dep:mime"]

cookies = ["dep:cookie_crate", "dep:cookie_store"]

gzip = ["dep:async-compression", "async-compression?/gzip", "dep:futures-util", "dep:tokio-util"]
gzip = ["tower-http/decompression-gzip"]

brotli = ["dep:async-compression", "async-compression?/brotli", "dep:futures-util", "dep:tokio-util"]
brotli = ["tower-http/decompression-br"]

zstd = ["dep:async-compression", "async-compression?/zstd", "dep:futures-util", "dep:tokio-util"]
zstd = ["tower-http/decompression-zstd"]

deflate = ["dep:async-compression", "async-compression?/zlib", "dep:futures-util", "dep:tokio-util"]
deflate = ["tower-http/decompression-deflate"]

json = ["dep:serde_json"]

Expand Down Expand Up @@ -159,7 +159,6 @@ cookie_crate = { version = "0.18.0", package = "cookie", optional = true }
cookie_store = { version = "0.21.0", optional = true }

## compression
async-compression = { version = "0.4.0", default-features = false, features = ["tokio"], optional = true }
tokio-util = { version = "0.7.9", default-features = false, features = ["codec", "io"], optional = true }

## hickory-dns
Expand Down
6 changes: 3 additions & 3 deletions src/async_impl/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pin_project! {
}

/// Converts any `impl Body` into a `impl Stream` of just its DATA frames.
#[cfg(any(feature = "stream", feature = "multipart",))]
#[cfg(any(feature = "stream", feature = "multipart", feature = "blocking"))]
pub(crate) struct DataStream<B>(pub(crate) B);

impl Body {
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Body {
}
}

#[cfg(feature = "multipart")]
#[cfg(any(feature = "multipart", feature = "blocking"))]
pub(crate) fn into_stream(self) -> DataStream<Body> {
DataStream(self)
}
Expand Down Expand Up @@ -423,7 +423,7 @@ where

// ===== impl DataStream =====

#[cfg(any(feature = "stream", feature = "multipart",))]
#[cfg(any(feature = "stream", feature = "multipart", feature = "blocking",))]
impl<B> futures_core::Stream for DataStream<B>
where
B: HttpBody<Data = Bytes> + Unpin,
Expand Down
113 changes: 93 additions & 20 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::time::Duration;
use std::{collections::HashMap, convert::TryInto, net::SocketAddr};
use std::{fmt, str};

use super::decoder::Accepts;
use super::request::{Request, RequestBuilder};
use super::response::Response;
use super::Body;
Expand Down Expand Up @@ -45,9 +44,7 @@ use crate::Certificate;
use crate::Identity;
use crate::{IntoUrl, Method, Proxy, Url};

use http::header::{
Entry, HeaderMap, HeaderValue, ACCEPT, ACCEPT_ENCODING, PROXY_AUTHORIZATION, RANGE, USER_AGENT,
};
use http::header::{Entry, HeaderMap, HeaderValue, ACCEPT, PROXY_AUTHORIZATION, USER_AGENT};
use http::uri::Scheme;
use http::Uri;
use hyper_util::client::legacy::connect::HttpConnector;
Expand All @@ -61,6 +58,13 @@ use quinn::VarInt;
use tokio::time::Sleep;
use tower::util::BoxCloneSyncServiceLayer;
use tower::{Layer, Service};
#[cfg(any(
feature = "gzip",
feature = "brotli",
feature = "zstd",
feature = "deflate"
))]
use tower_http::decompression::Decompression;
use tower_http::follow_redirect::FollowRedirect;

/// An asynchronous `Client` to make Requests with.
Expand Down Expand Up @@ -96,6 +100,33 @@ enum HttpVersionPref {
All,
}

#[derive(Clone, Copy, Debug)]
struct Accepts {
#[cfg(feature = "gzip")]
gzip: bool,
#[cfg(feature = "brotli")]
brotli: bool,
#[cfg(feature = "zstd")]
zstd: bool,
#[cfg(feature = "deflate")]
deflate: bool,
}

impl Default for Accepts {
fn default() -> Accepts {
Accepts {
#[cfg(feature = "gzip")]
gzip: true,
#[cfg(feature = "brotli")]
brotli: true,
#[cfg(feature = "zstd")]
zstd: true,
#[cfg(feature = "deflate")]
deflate: true,
}
}
}

#[derive(Clone)]
struct HyperService {
hyper: HyperClient,
Expand Down Expand Up @@ -978,6 +1009,21 @@ impl ClientBuilder {
#[cfg(feature = "cookies")]
let svc = CookieService::new(svc, config.cookie_store.clone());
let hyper = FollowRedirect::with_policy(svc, redirect_policy.clone());
#[cfg(any(
feature = "gzip",
feature = "brotli",
feature = "zstd",
feature = "deflate"
))]
let hyper = Decompression::new(hyper);
#[cfg(feature = "gzip")]
let hyper = hyper.gzip(config.accepts.gzip);
#[cfg(feature = "brotli")]
let hyper = hyper.br(config.accepts.brotli);
#[cfg(feature = "zstd")]
let hyper = hyper.zstd(config.accepts.zstd);
#[cfg(feature = "deflate")]
let hyper = hyper.deflate(config.accepts.deflate);

Ok(Client {
inner: Arc::new(ClientRef {
Expand All @@ -993,7 +1039,23 @@ impl ClientBuilder {
let svc = tower::retry::Retry::new(retry_policy, h3_service);
#[cfg(feature = "cookies")]
let svc = CookieService::new(svc, config.cookie_store);
Some(FollowRedirect::with_policy(svc, redirect_policy))
let svc = FollowRedirect::with_policy(svc, redirect_policy);
#[cfg(any(
feature = "gzip",
feature = "brotli",
feature = "zstd",
feature = "deflate"
))]
let svc = Decompression::new(svc);
#[cfg(feature = "gzip")]
let svc = svc.gzip(config.accepts.gzip);
#[cfg(feature = "brotli")]
let svc = svc.br(config.accepts.brotli);
#[cfg(feature = "zstd")]
let svc = svc.zstd(config.accepts.zstd);
#[cfg(feature = "deflate")]
let svc = svc.deflate(config.accepts.deflate);
Some(svc)
}
None => None,
},
Expand Down Expand Up @@ -2484,14 +2546,6 @@ impl Client {
}
}

let accept_encoding = self.inner.accepts.as_str();

if let Some(accept_encoding) = accept_encoding {
if !headers.contains_key(ACCEPT_ENCODING) && !headers.contains_key(RANGE) {
headers.insert(ACCEPT_ENCODING, HeaderValue::from_static(accept_encoding));
}
}

let uri = match try_uri(&url) {
Ok(uri) => uri,
_ => return Pending::new_err(error::url_invalid_uri(url)),
Expand Down Expand Up @@ -2776,12 +2830,32 @@ impl Config {
}

#[cfg(not(feature = "cookies"))]
type LayeredService<T> =
FollowRedirect<tower::retry::Retry<crate::retry::Policy, T>, TowerRedirectPolicy>;
type MaybeCookieService<T> = T;

#[cfg(feature = "cookies")]
type LayeredService<T> = FollowRedirect<
CookieService<tower::retry::Retry<crate::retry::Policy, T>>,
TowerRedirectPolicy,
type MaybeCookieService<T> = CookieService<T>;

#[cfg(not(any(
feature = "gzip",
feature = "brotli",
feature = "zstd",
feature = "deflate"
)))]
type MaybeDecompression<T> = T;

#[cfg(any(
feature = "gzip",
feature = "brotli",
feature = "zstd",
feature = "deflate"
))]
type MaybeDecompression<T> = Decompression<T>;

type LayeredService<T> = MaybeDecompression<
FollowRedirect<
MaybeCookieService<tower::retry::Retry<crate::retry::Policy, T>>,
TowerRedirectPolicy,
>,
>;
type LayeredFuture<T> = <LayeredService<T> as Service<http::Request<Body>>>::Future;

Expand Down Expand Up @@ -2947,7 +3021,7 @@ impl Future for PendingRequest {
Err(e) => {
return Poll::Ready(Err(crate::error::request(e).with_url(self.url.clone())));
}
Ok(res) => res,
Ok(res) => res.map(super::body::boxed),
},
};

Expand All @@ -2964,7 +3038,6 @@ impl Future for PendingRequest {
let res = Response::new(
res,
self.url.clone(),
self.client.accepts,
self.total_timeout.take(),
self.read_timeout,
);
Expand Down
Loading
Loading