From e7cae5a95b80ab1c4513a95f723eb35432c4d9af Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 15 Jan 2022 14:03:16 +0000 Subject: [PATCH] migrate to `brotli` crate (#2538) --- Cargo.toml | 2 +- actix-http/Cargo.toml | 4 ++-- actix-http/src/encoding/decoder.rs | 7 ++----- actix-http/src/encoding/encoder.rs | 23 ++++++++++++++--------- awc/Cargo.toml | 2 +- awc/tests/utils.rs | 14 ++++++++++---- tests/utils.rs | 14 ++++++++++---- 7 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 945d72ef8..39f2ac32a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ actix-files = "0.6.0-beta.14" actix-test = { version = "0.1.0-beta.11", features = ["openssl", "rustls"] } awc = { version = "3.0.0-beta.18", features = ["openssl"] } -brotli2 = "0.3.2" +brotli = "3.3.3" const-str = "0.3" criterion = { version = "0.3", features = ["html_reports"] } env_logger = "0.9" diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 519230aab..df9e11419 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -33,7 +33,7 @@ openssl = ["actix-tls/accept", "actix-tls/openssl"] rustls = ["actix-tls/accept", "actix-tls/rustls"] # enable compression support -compress-brotli = ["brotli2", "__compress"] +compress-brotli = ["brotli", "__compress"] compress-gzip = ["flate2", "__compress"] compress-zstd = ["zstd", "__compress"] @@ -74,7 +74,7 @@ smallvec = "1.6.1" actix-tls = { version = "3.0.0", default-features = false, optional = true } # compression -brotli2 = { version="0.3.2", optional = true } +brotli = { version = "3.3.3", optional = true } flate2 = { version = "1.0.13", optional = true } zstd = { version = "0.9", optional = true } diff --git a/actix-http/src/encoding/decoder.rs b/actix-http/src/encoding/decoder.rs index da4b56c6a..2ed7be899 100644 --- a/actix-http/src/encoding/decoder.rs +++ b/actix-http/src/encoding/decoder.rs @@ -11,9 +11,6 @@ use actix_rt::task::{spawn_blocking, JoinHandle}; use bytes::Bytes; use futures_core::{ready, Stream}; -#[cfg(feature = "compress-brotli")] -use brotli2::write::BrotliDecoder; - #[cfg(feature = "compress-gzip")] use flate2::write::{GzDecoder, ZlibDecoder}; @@ -48,7 +45,7 @@ where let decoder = match encoding { #[cfg(feature = "compress-brotli")] ContentEncoding::Brotli => Some(ContentDecoder::Brotli(Box::new( - BrotliDecoder::new(Writer::new()), + brotli::DecompressorWriter::new(Writer::new(), 8_096), ))), #[cfg(feature = "compress-gzip")] ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new( @@ -165,7 +162,7 @@ enum ContentDecoder { #[cfg(feature = "compress-gzip")] Gzip(Box>), #[cfg(feature = "compress-brotli")] - Brotli(Box>), + Brotli(Box>), // We need explicit 'static lifetime here because ZstdDecoder need lifetime // argument, and we use `spawn_blocking` in `Decoder::poll_next` that require `FnOnce() -> R + Send + 'static` #[cfg(feature = "compress-zstd")] diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 2848ad697..9696da6f1 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -14,9 +14,6 @@ use derive_more::Display; use futures_core::ready; use pin_project_lite::pin_project; -#[cfg(feature = "compress-brotli")] -use brotli2::write::BrotliEncoder; - #[cfg(feature = "compress-gzip")] use flate2::write::{GzEncoder, ZlibEncoder}; @@ -268,7 +265,7 @@ enum ContentEncoder { Gzip(GzEncoder), #[cfg(feature = "compress-brotli")] - Brotli(BrotliEncoder), + Brotli(Box>), // Wwe need explicit 'static lifetime here because ZstdEncoder needs a lifetime argument and we // use `spawn_blocking` in `Encoder::poll_next` that requires `FnOnce() -> R + Send + 'static`. @@ -292,9 +289,7 @@ impl ContentEncoder { ))), #[cfg(feature = "compress-brotli")] - ContentEncoding::Brotli => { - Some(ContentEncoder::Brotli(BrotliEncoder::new(Writer::new(), 3))) - } + ContentEncoding::Brotli => Some(ContentEncoder::Brotli(new_brotli_compressor())), #[cfg(feature = "compress-zstd")] ContentEncoding::Zstd => { @@ -326,8 +321,8 @@ impl ContentEncoder { fn finish(self) -> Result { match self { #[cfg(feature = "compress-brotli")] - ContentEncoder::Brotli(encoder) => match encoder.finish() { - Ok(writer) => Ok(writer.buf.freeze()), + ContentEncoder::Brotli(mut encoder) => match encoder.flush() { + Ok(()) => Ok(encoder.into_inner().buf.freeze()), Err(err) => Err(err), }, @@ -392,6 +387,16 @@ impl ContentEncoder { } } +#[cfg(feature = "compress-brotli")] +fn new_brotli_compressor() -> Box> { + Box::new(brotli::CompressorWriter::new( + Writer::new(), + 8 * 1024, // 32 KiB buffer + 3, // BROTLI_PARAM_QUALITY + 22, // BROTLI_PARAM_LGWIN + )) +} + #[derive(Debug, Display)] #[non_exhaustive] pub enum EncoderError { diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 8accf4d0d..16c2083d8 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -101,7 +101,7 @@ actix-tls = { version = "3.0.0", features = ["openssl", "rustls"] } actix-utils = "3.0.0" actix-web = { version = "4.0.0-beta.20", features = ["openssl"] } -brotli2 = "0.3.2" +brotli = "3.3.3" const-str = "0.3" env_logger = "0.9" flate2 = "1.0.13" diff --git a/awc/tests/utils.rs b/awc/tests/utils.rs index 9a3743d8b..2532640c6 100644 --- a/awc/tests/utils.rs +++ b/awc/tests/utils.rs @@ -41,16 +41,22 @@ pub mod deflate { pub mod brotli { use super::*; - use ::brotli2::{read::BrotliDecoder, write::BrotliEncoder}; + use ::brotli::{reader::Decompressor as BrotliDecoder, CompressorWriter as BrotliEncoder}; pub fn encode(bytes: impl AsRef<[u8]>) -> Vec { - let mut encoder = BrotliEncoder::new(Vec::new(), 3); + let mut encoder = BrotliEncoder::new( + Vec::new(), + 8 * 1024, // 32 KiB buffer + 3, // BROTLI_PARAM_QUALITY + 22, // BROTLI_PARAM_LGWIN + ); encoder.write_all(bytes.as_ref()).unwrap(); - encoder.finish().unwrap() + encoder.flush().unwrap(); + encoder.into_inner() } pub fn decode(bytes: impl AsRef<[u8]>) -> Vec { - let mut decoder = BrotliDecoder::new(bytes.as_ref()); + let mut decoder = BrotliDecoder::new(bytes.as_ref(), 8_096); let mut buf = Vec::new(); decoder.read_to_end(&mut buf).unwrap(); buf diff --git a/tests/utils.rs b/tests/utils.rs index 9a3743d8b..2532640c6 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -41,16 +41,22 @@ pub mod deflate { pub mod brotli { use super::*; - use ::brotli2::{read::BrotliDecoder, write::BrotliEncoder}; + use ::brotli::{reader::Decompressor as BrotliDecoder, CompressorWriter as BrotliEncoder}; pub fn encode(bytes: impl AsRef<[u8]>) -> Vec { - let mut encoder = BrotliEncoder::new(Vec::new(), 3); + let mut encoder = BrotliEncoder::new( + Vec::new(), + 8 * 1024, // 32 KiB buffer + 3, // BROTLI_PARAM_QUALITY + 22, // BROTLI_PARAM_LGWIN + ); encoder.write_all(bytes.as_ref()).unwrap(); - encoder.finish().unwrap() + encoder.flush().unwrap(); + encoder.into_inner() } pub fn decode(bytes: impl AsRef<[u8]>) -> Vec { - let mut decoder = BrotliDecoder::new(bytes.as_ref()); + let mut decoder = BrotliDecoder::new(bytes.as_ref(), 8_096); let mut buf = Vec::new(); decoder.read_to_end(&mut buf).unwrap(); buf