diff --git a/Cargo.toml b/Cargo.toml index 356e4941d..97e4a1554 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ default = ["brotli", "flate2-zlib", "client", "fail"] # http client client = ["awc"] -# brotli encoding, requires c compiler +# brotli encoding brotli = ["actix-http/brotli", "awc/brotli"] # miniz-sys backend for flate2 crate @@ -111,7 +111,7 @@ actix-http-test = "1.0.0-alpha.3" rand = "0.7" env_logger = "0.6" serde_derive = "1.0" -brotli2 = "0.3.2" +brotli2 = { package = "brotli", version = "3.3.0" } flate2 = "1.0.2" [profile.release] diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 4d89e55fb..3243a1acd 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -31,7 +31,7 @@ openssl = ["actix-tls/openssl", "actix-connect/openssl"] # rustls support rustls = ["actix-tls/rustls", "actix-connect/rustls"] -# brotli encoding, requires c compiler +# brotli encoding brotli = ["brotli2"] # miniz-sys backend for flate2 crate @@ -88,7 +88,7 @@ time = "0.1.42" ring = { version = "0.16.9", optional = true } # compression -brotli2 = { version="0.3.2", optional = true } +brotli2 = { package = "brotli", version="3.3.0", optional = true } flate2 = { version="1.0.7", optional = true, default-features = false } # optional deps diff --git a/actix-http/src/encoding/decoder.rs b/actix-http/src/encoding/decoder.rs index dca774838..47eee5dcc 100644 --- a/actix-http/src/encoding/decoder.rs +++ b/actix-http/src/encoding/decoder.rs @@ -5,7 +5,7 @@ use std::task::{Context, Poll}; use actix_threadpool::{run, CpuFuture}; #[cfg(feature = "brotli")] -use brotli2::write::BrotliDecoder; +use brotli2::DecompressorWriter; use bytes::Bytes; #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] use flate2::write::{GzDecoder, ZlibDecoder}; @@ -34,7 +34,7 @@ where let decoder = match encoding { #[cfg(feature = "brotli")] ContentEncoding::Br => Some(ContentDecoder::Br(Box::new( - BrotliDecoder::new(Writer::new()), + DecompressorWriter::new(Writer::new(), 0), ))), #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new( @@ -145,7 +145,7 @@ enum ContentDecoder { #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] Gzip(Box>), #[cfg(feature = "brotli")] - Br(Box>), + Br(Box>), } impl ContentDecoder { @@ -153,9 +153,9 @@ impl ContentDecoder { fn feed_eof(&mut self) -> io::Result> { match self { #[cfg(feature = "brotli")] - ContentDecoder::Br(ref mut decoder) => match decoder.finish() { - Ok(mut writer) => { - let b = writer.take(); + ContentDecoder::Br(ref mut decoder) => match decoder.flush() { + Ok(()) => { + let b = decoder.get_mut().take(); if !b.is_empty() { Ok(Some(b)) } else { diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index d1b64bfb8..888a8f8dc 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -6,7 +6,7 @@ use std::task::{Context, Poll}; use actix_threadpool::{run, CpuFuture}; #[cfg(feature = "brotli")] -use brotli2::write::BrotliEncoder; +use brotli2::CompressorWriter; use bytes::Bytes; #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] use flate2::write::{GzEncoder, ZlibEncoder}; @@ -178,7 +178,7 @@ enum ContentEncoder { #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] Gzip(GzEncoder), #[cfg(feature = "brotli")] - Br(BrotliEncoder), + Br(CompressorWriter), } impl ContentEncoder { @@ -195,9 +195,12 @@ impl ContentEncoder { flate2::Compression::fast(), ))), #[cfg(feature = "brotli")] - ContentEncoding::Br => { - Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3))) - } + ContentEncoding::Br => Some(ContentEncoder::Br(CompressorWriter::new( + Writer::new(), + 0, + 3, + 0, + ))), _ => None, } } @@ -206,7 +209,11 @@ impl ContentEncoder { pub(crate) fn take(&mut self) -> Bytes { match *self { #[cfg(feature = "brotli")] - ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(), + ContentEncoder::Br(ref mut encoder) => { + let mut encoder_new = CompressorWriter::new(Writer::new(), 0, 3, 0); + std::mem::swap(encoder, &mut encoder_new); + encoder_new.into_inner().take() + } #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(), #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] @@ -217,10 +224,7 @@ impl ContentEncoder { fn finish(self) -> Result { match self { #[cfg(feature = "brotli")] - ContentEncoder::Br(encoder) => match encoder.finish() { - Ok(writer) => Ok(writer.buf.freeze()), - Err(err) => Err(err), - }, + ContentEncoder::Br(encoder) => Ok(encoder.into_inner().buf.freeze()), #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] ContentEncoder::Gzip(encoder) => match encoder.finish() { Ok(writer) => Ok(writer.buf.freeze()), diff --git a/awc/Cargo.toml b/awc/Cargo.toml index c4f3b7bf1..567f06db9 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -32,7 +32,7 @@ openssl = ["open-ssl", "actix-http/openssl"] # rustls rustls = ["rust-tls", "actix-http/rustls"] -# brotli encoding, requires c compiler +# brotli encoding brotli = ["actix-http/brotli"] # miniz-sys backend for flate2 crate @@ -69,7 +69,7 @@ actix-http-test = { version = "1.0.0-alpha.3", features=["openssl"] } actix-utils = "1.0.0-alpha.3" actix-server = { version = "1.0.0-alpha.3" } actix-tls = { version = "1.0.0-alpha.3", features=["openssl", "rustls"] } -brotli2 = { version="0.3.2" } +brotli2 = { package = "brotli", version="3.3.0" } flate2 = { version="1.0.2" } env_logger = "0.6" webpki = { version = "0.21" } diff --git a/src/lib.rs b/src/lib.rs index b7fd8d155..2ce2265b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,7 @@ //! * `rustls` - enables ssl support via `rustls` crate, supports `http/2` //! * `secure-cookies` - enables secure cookies support, includes `ring` crate as //! dependency -//! * `brotli` - enables `brotli` compression support, requires `c` +//! * `brotli` - enables `brotli` compression support //! compiler (default enabled) //! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires //! `c` compiler (default enabled)