Switch to a rustified version of brotli.

This commit is contained in:
daxpedda 2019-12-07 13:57:36 +01:00
parent 4921243add
commit 3d98768093
No known key found for this signature in database
GPG Key ID: 43D62A3EA388E46F
6 changed files with 27 additions and 23 deletions

View File

@ -48,7 +48,7 @@ default = ["brotli", "flate2-zlib", "client", "fail"]
# http client # http client
client = ["awc"] client = ["awc"]
# brotli encoding, requires c compiler # brotli encoding
brotli = ["actix-http/brotli", "awc/brotli"] brotli = ["actix-http/brotli", "awc/brotli"]
# miniz-sys backend for flate2 crate # miniz-sys backend for flate2 crate
@ -111,7 +111,7 @@ actix-http-test = "1.0.0-alpha.3"
rand = "0.7" rand = "0.7"
env_logger = "0.6" env_logger = "0.6"
serde_derive = "1.0" serde_derive = "1.0"
brotli2 = "0.3.2" brotli2 = { package = "brotli", version = "3.3.0" }
flate2 = "1.0.2" flate2 = "1.0.2"
[profile.release] [profile.release]

View File

@ -31,7 +31,7 @@ openssl = ["actix-tls/openssl", "actix-connect/openssl"]
# rustls support # rustls support
rustls = ["actix-tls/rustls", "actix-connect/rustls"] rustls = ["actix-tls/rustls", "actix-connect/rustls"]
# brotli encoding, requires c compiler # brotli encoding
brotli = ["brotli2"] brotli = ["brotli2"]
# miniz-sys backend for flate2 crate # miniz-sys backend for flate2 crate
@ -88,7 +88,7 @@ time = "0.1.42"
ring = { version = "0.16.9", optional = true } ring = { version = "0.16.9", optional = true }
# compression # 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 } flate2 = { version="1.0.7", optional = true, default-features = false }
# optional deps # optional deps

View File

@ -5,7 +5,7 @@ use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture}; use actix_threadpool::{run, CpuFuture};
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
use brotli2::write::BrotliDecoder; use brotli2::DecompressorWriter;
use bytes::Bytes; use bytes::Bytes;
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
use flate2::write::{GzDecoder, ZlibDecoder}; use flate2::write::{GzDecoder, ZlibDecoder};
@ -34,7 +34,7 @@ where
let decoder = match encoding { let decoder = match encoding {
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new( ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
BrotliDecoder::new(Writer::new()), DecompressorWriter::new(Writer::new(), 0),
))), ))),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new( ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
@ -145,7 +145,7 @@ enum ContentDecoder {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Gzip(Box<GzDecoder<Writer>>), Gzip(Box<GzDecoder<Writer>>),
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
Br(Box<BrotliDecoder<Writer>>), Br(Box<DecompressorWriter<Writer>>),
} }
impl ContentDecoder { impl ContentDecoder {
@ -153,9 +153,9 @@ impl ContentDecoder {
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> { fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
match self { match self {
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
ContentDecoder::Br(ref mut decoder) => match decoder.finish() { ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
Ok(mut writer) => { Ok(()) => {
let b = writer.take(); let b = decoder.get_mut().take();
if !b.is_empty() { if !b.is_empty() {
Ok(Some(b)) Ok(Some(b))
} else { } else {

View File

@ -6,7 +6,7 @@ use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture}; use actix_threadpool::{run, CpuFuture};
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
use brotli2::write::BrotliEncoder; use brotli2::CompressorWriter;
use bytes::Bytes; use bytes::Bytes;
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
use flate2::write::{GzEncoder, ZlibEncoder}; use flate2::write::{GzEncoder, ZlibEncoder};
@ -178,7 +178,7 @@ enum ContentEncoder {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Gzip(GzEncoder<Writer>), Gzip(GzEncoder<Writer>),
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
Br(BrotliEncoder<Writer>), Br(CompressorWriter<Writer>),
} }
impl ContentEncoder { impl ContentEncoder {
@ -195,9 +195,12 @@ impl ContentEncoder {
flate2::Compression::fast(), flate2::Compression::fast(),
))), ))),
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
ContentEncoding::Br => { ContentEncoding::Br => Some(ContentEncoder::Br(CompressorWriter::new(
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3))) Writer::new(),
} 0,
3,
0,
))),
_ => None, _ => None,
} }
} }
@ -206,7 +209,11 @@ impl ContentEncoder {
pub(crate) fn take(&mut self) -> Bytes { pub(crate) fn take(&mut self) -> Bytes {
match *self { match *self {
#[cfg(feature = "brotli")] #[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"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(), ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
@ -217,10 +224,7 @@ impl ContentEncoder {
fn finish(self) -> Result<Bytes, io::Error> { fn finish(self) -> Result<Bytes, io::Error> {
match self { match self {
#[cfg(feature = "brotli")] #[cfg(feature = "brotli")]
ContentEncoder::Br(encoder) => match encoder.finish() { ContentEncoder::Br(encoder) => Ok(encoder.into_inner().buf.freeze()),
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))] #[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Gzip(encoder) => match encoder.finish() { ContentEncoder::Gzip(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()), Ok(writer) => Ok(writer.buf.freeze()),

View File

@ -32,7 +32,7 @@ openssl = ["open-ssl", "actix-http/openssl"]
# rustls # rustls
rustls = ["rust-tls", "actix-http/rustls"] rustls = ["rust-tls", "actix-http/rustls"]
# brotli encoding, requires c compiler # brotli encoding
brotli = ["actix-http/brotli"] brotli = ["actix-http/brotli"]
# miniz-sys backend for flate2 crate # 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-utils = "1.0.0-alpha.3"
actix-server = { version = "1.0.0-alpha.3" } actix-server = { version = "1.0.0-alpha.3" }
actix-tls = { version = "1.0.0-alpha.3", features=["openssl", "rustls"] } 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" } flate2 = { version="1.0.2" }
env_logger = "0.6" env_logger = "0.6"
webpki = { version = "0.21" } webpki = { version = "0.21" }

View File

@ -72,7 +72,7 @@
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2` //! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as //! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
//! dependency //! dependency
//! * `brotli` - enables `brotli` compression support, requires `c` //! * `brotli` - enables `brotli` compression support
//! compiler (default enabled) //! compiler (default enabled)
//! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires //! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires
//! `c` compiler (default enabled) //! `c` compiler (default enabled)