1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-26 23:17:42 +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

@ -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>;