mirror of
https://github.com/fafhrd91/actix-web
synced 2025-07-01 16:55:08 +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:
@ -1,7 +1,7 @@
|
||||
use std::{fmt, str};
|
||||
|
||||
pub use self::Encoding::{
|
||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers,
|
||||
Brotli, Chunked, Compress, Deflate, EncodingExt, Gzip, Identity, Trailers, Zstd,
|
||||
};
|
||||
|
||||
/// A value to represent an encoding used in `Transfer-Encoding`
|
||||
@ -22,6 +22,8 @@ pub enum Encoding {
|
||||
Identity,
|
||||
/// The `trailers` encoding.
|
||||
Trailers,
|
||||
/// The `zstd` encoding.
|
||||
Zstd,
|
||||
/// Some other encoding that is less common, can be any String.
|
||||
EncodingExt(String),
|
||||
}
|
||||
@ -36,6 +38,7 @@ impl fmt::Display for Encoding {
|
||||
Compress => "compress",
|
||||
Identity => "identity",
|
||||
Trailers => "trailers",
|
||||
Zstd => "zstd",
|
||||
EncodingExt(ref s) => s.as_ref(),
|
||||
})
|
||||
}
|
||||
@ -52,6 +55,7 @@ impl str::FromStr for Encoding {
|
||||
"compress" => Ok(Compress),
|
||||
"identity" => Ok(Identity),
|
||||
"trailers" => Ok(Trailers),
|
||||
"zstd" => Ok(Zstd),
|
||||
_ => Ok(EncodingExt(s.to_owned())),
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@
|
||||
//! * Streaming and pipelining
|
||||
//! * Keep-alive and slow requests handling
|
||||
//! * Client/server [WebSockets](https://actix.rs/docs/websockets/) support
|
||||
//! * Transparent content compression/decompression (br, gzip, deflate)
|
||||
//! * Transparent content compression/decompression (br, gzip, deflate, zstd)
|
||||
//! * Powerful [request routing](https://actix.rs/docs/url-dispatch/)
|
||||
//! * Multipart streams
|
||||
//! * Static assets
|
||||
@ -140,7 +140,8 @@ pub mod dev {
|
||||
pub use actix_http::body::{
|
||||
AnyBody, Body, BodySize, MessageBody, ResponseBody, SizedStream,
|
||||
};
|
||||
#[cfg(feature = "compress")]
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use actix_http::encoding::Decoder as Decompress;
|
||||
pub use actix_http::ResponseBuilder as BaseHttpResponseBuilder;
|
||||
pub use actix_http::{Extensions, Payload, PayloadStream, RequestHead, ResponseHead};
|
||||
|
@ -144,7 +144,7 @@ mod tests {
|
||||
use crate::{web, App, HttpResponse};
|
||||
|
||||
#[actix_rt::test]
|
||||
#[cfg(all(feature = "cookies", feature = "compress"))]
|
||||
#[cfg(all(feature = "cookies", feature = "__compress"))]
|
||||
async fn test_scope_middleware() {
|
||||
use crate::middleware::Compress;
|
||||
|
||||
@ -167,7 +167,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
#[cfg(all(feature = "cookies", feature = "compress"))]
|
||||
#[cfg(all(feature = "cookies", feature = "__compress"))]
|
||||
async fn test_resource_scope_middleware() {
|
||||
use crate::middleware::Compress;
|
||||
|
||||
|
@ -14,7 +14,8 @@ pub use self::err_handlers::{ErrorHandlerResponse, ErrorHandlers};
|
||||
pub use self::logger::Logger;
|
||||
pub use self::normalize::{NormalizePath, TrailingSlash};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
mod compress;
|
||||
#[cfg(feature = "compress")]
|
||||
|
||||
#[cfg(feature = "__compress")]
|
||||
pub use self::compress::Compress;
|
||||
|
@ -16,7 +16,7 @@ use futures_core::{future::LocalBoxFuture, ready};
|
||||
use futures_util::{FutureExt as _, StreamExt as _};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
use crate::dev::Decompress;
|
||||
use crate::{
|
||||
error::UrlencodedError, extract::FromRequest, http::header::CONTENT_LENGTH, web, Error,
|
||||
@ -255,9 +255,9 @@ impl Default for FormConfig {
|
||||
/// - content type is not `application/x-www-form-urlencoded`
|
||||
/// - content length is greater than [limit](UrlEncoded::limit())
|
||||
pub struct UrlEncoded<T> {
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
stream: Option<Decompress<Payload>>,
|
||||
#[cfg(not(feature = "compress"))]
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
stream: Option<Payload>,
|
||||
|
||||
limit: usize,
|
||||
@ -293,10 +293,15 @@ impl<T> UrlEncoded<T> {
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
let payload = Decompress::from_headers(payload.take(), req.headers());
|
||||
#[cfg(not(feature = "compress"))]
|
||||
let payload = payload.take();
|
||||
let payload = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "__compress")] {
|
||||
Decompress::from_headers(payload.take(), req.headers())
|
||||
} else {
|
||||
payload.take()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
UrlEncoded {
|
||||
encoding,
|
||||
|
@ -16,7 +16,7 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use actix_http::Payload;
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
use crate::dev::Decompress;
|
||||
use crate::{
|
||||
error::{Error, JsonPayloadError},
|
||||
@ -300,9 +300,9 @@ pub enum JsonBody<T> {
|
||||
Body {
|
||||
limit: usize,
|
||||
length: Option<usize>,
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
payload: Decompress<Payload>,
|
||||
#[cfg(not(feature = "compress"))]
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
payload: Payload,
|
||||
buf: BytesMut,
|
||||
_res: PhantomData<T>,
|
||||
@ -345,10 +345,15 @@ where
|
||||
// As the internal usage always call JsonBody::limit after JsonBody::new.
|
||||
// And limit check to return an error variant of JsonBody happens there.
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
let payload = Decompress::from_headers(payload.take(), req.headers());
|
||||
#[cfg(not(feature = "compress"))]
|
||||
let payload = payload.take();
|
||||
let payload = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "__compress")] {
|
||||
Decompress::from_headers(payload.take(), req.headers())
|
||||
} else {
|
||||
payload.take()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
JsonBody::Body {
|
||||
limit: DEFAULT_LIMIT,
|
||||
|
@ -282,9 +282,9 @@ impl Default for PayloadConfig {
|
||||
pub struct HttpMessageBody {
|
||||
limit: usize,
|
||||
length: Option<usize>,
|
||||
#[cfg(feature = "compress")]
|
||||
#[cfg(feature = "__compress")]
|
||||
stream: dev::Decompress<dev::Payload>,
|
||||
#[cfg(not(feature = "compress"))]
|
||||
#[cfg(not(feature = "__compress"))]
|
||||
stream: dev::Payload,
|
||||
buf: BytesMut,
|
||||
err: Option<PayloadError>,
|
||||
@ -312,10 +312,15 @@ impl HttpMessageBody {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "compress")]
|
||||
let stream = dev::Decompress::from_headers(payload.take(), req.headers());
|
||||
#[cfg(not(feature = "compress"))]
|
||||
let stream = payload.take();
|
||||
let stream = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "__compress")] {
|
||||
dev::Decompress::from_headers(payload.take(), req.headers())
|
||||
} else {
|
||||
payload.take()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
HttpMessageBody {
|
||||
stream,
|
||||
|
Reference in New Issue
Block a user