1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 06:57:43 +02:00

Select compression algorithm using features flags (#2250)

Add compress-* feature flags in actix-http / actix-web / awc.
This allow enable / disable not wanted compression algorithm.
This commit is contained in:
Arthur Le Moigne
2021-06-19 21:21:13 +02:00
committed by GitHub
parent c260fb1c48
commit baa5a663c4
19 changed files with 204 additions and 72 deletions

View File

@ -2,11 +2,15 @@
## Unreleased - 2021-xx-xx
### Changed
* Change compression algorithm features flags. [#2250]
[#2250]: https://github.com/actix/actix-web/pull/2250
## 3.0.0-beta.6 - 2021-06-17
* No significant changes since 3.0.0-beta.5.
## 3.0.0-beta.5 - 2021-04-17
### Removed
* Deprecated methods on `ClientRequest`: `if_true`, `if_some`. [#2148]

View File

@ -24,10 +24,10 @@ path = "src/lib.rs"
[package.metadata.docs.rs]
# features that docs.rs will build with
features = ["openssl", "rustls", "compress", "cookies"]
features = ["openssl", "rustls", "compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
[features]
default = ["compress", "cookies"]
default = ["compress-brotli", "compress-gzip", "compress-zstd", "cookies"]
# openssl
openssl = ["tls-openssl", "actix-http/openssl"]
@ -35,8 +35,12 @@ openssl = ["tls-openssl", "actix-http/openssl"]
# rustls
rustls = ["tls-rustls", "actix-http/rustls"]
# content-encoding support
compress = ["actix-http/compress"]
# Brotli algorithm content-encoding support
compress-brotli = ["actix-http/compress-brotli", "__compress"]
# Gzip and deflate algorithms content-encoding support
compress-gzip = ["actix-http/compress-gzip", "__compress"]
# Zstd algorithm content-encoding support
compress-zstd = ["actix-http/compress-zstd", "__compress"]
# cookie parsing and cookie jar
cookies = ["cookie"]
@ -44,6 +48,10 @@ cookies = ["cookie"]
# trust-dns as dns resolver
trust-dns = ["actix-http/trust-dns"]
# Internal (PRIVATE!) features used to aid testing and cheking feature status.
# Don't rely on these whatsoever. They may disappear at anytime.
__compress = []
[dependencies]
actix-codec = "0.4.0"
actix-service = "2.0.0"
@ -52,6 +60,7 @@ actix-rt = { version = "2.1", default-features = false }
base64 = "0.13"
bytes = "1"
cfg-if = "1"
cookie = { version = "0.15", features = ["percent-encode"], optional = true }
derive_more = "0.99.5"
futures-core = { version = "0.3.7", default-features = false }

View File

@ -8,7 +8,7 @@ use actix_http::{
body::Body,
http::{
header::{self, IntoHeaderPair},
uri, ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
ConnectionType, Error as HttpError, HeaderMap, HeaderValue, Method, Uri, Version,
},
RequestHead,
};
@ -22,11 +22,6 @@ use crate::{
ClientConfig,
};
#[cfg(feature = "compress")]
const HTTPS_ENCODING: &str = "br, gzip, deflate";
#[cfg(not(feature = "compress"))]
const HTTPS_ENCODING: &str = "br";
/// An HTTP Client request builder
///
/// This type can be used to construct an instance of `ClientRequest` through a
@ -480,22 +475,37 @@ impl ClientRequest {
let mut slf = self;
// Set Accept-Encoding HTTP header depending on enabled feature.
// If decompress is not ask, then we are not able to find which encoding is
// supported, so we cannot guess Accept-Encoding HTTP header.
if slf.response_decompress {
let https = slf
.head
.uri
.scheme()
.map(|s| s == &uri::Scheme::HTTPS)
.unwrap_or(true);
// Set Accept-Encoding with compression algorithm awc is built with.
#[cfg(feature = "__compress")]
let accept_encoding = {
let mut encoding = vec![];
if https {
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, HTTPS_ENCODING));
} else {
#[cfg(feature = "compress")]
#[cfg(feature = "compress-brotli")]
encoding.push("br");
#[cfg(feature = "compress-gzip")]
{
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, "gzip, deflate"));
encoding.push("gzip");
encoding.push("deflate");
}
#[cfg(feature = "compress-zstd")]
encoding.push("zstd");
assert!(!encoding.is_empty(), "encoding cannot be empty unless __compress feature has been explictily enabled.");
encoding.join(", ")
};
// Otherwise tell the server, we do not support any compression algorithm.
// So we clearly indicate that we do want identity encoding.
#[cfg(not(feature = "__compress"))]
let accept_encoding = "identity";
slf = slf.insert_header_if_none((header::ACCEPT_ENCODING, accept_encoding));
}
Ok(slf)

View File

@ -22,7 +22,7 @@ use derive_more::From;
use futures_core::Stream;
use serde::Serialize;
#[cfg(feature = "compress")]
#[cfg(feature = "__compress")]
use actix_http::{encoding::Decoder, http::header::ContentEncoding, Payload, PayloadStream};
use crate::{
@ -91,7 +91,7 @@ impl SendClientRequest {
}
}
#[cfg(feature = "compress")]
#[cfg(feature = "__compress")]
impl Future for SendClientRequest {
type Output = Result<ClientResponse<Decoder<Payload<PayloadStream>>>, SendRequestError>;
@ -131,7 +131,7 @@ impl Future for SendClientRequest {
}
}
#[cfg(not(feature = "compress"))]
#[cfg(not(feature = "__compress"))]
impl Future for SendClientRequest {
type Output = Result<ClientResponse, SendRequestError>;