1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

migrate to brotli crate

This commit is contained in:
Rob Ede 2022-01-21 21:16:23 +00:00
parent 0669ed0f06
commit ad7e3c06e7
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
7 changed files with 35 additions and 28 deletions

View File

@ -115,7 +115,7 @@ actix-http = { version = "2.1.0", features = ["actors"] }
rand = "0.7" rand = "0.7"
env_logger = "0.8" env_logger = "0.8"
serde_derive = "1.0" serde_derive = "1.0"
brotli2 = "0.3.2" brotli = "3.3.3"
flate2 = "1.0.13" flate2 = "1.0.13"
criterion = "0.3" criterion = "0.3"

View File

@ -31,7 +31,7 @@ openssl = ["actix-tls/openssl", "actix-connect/openssl"]
rustls = ["actix-tls/rustls", "actix-connect/rustls"] rustls = ["actix-tls/rustls", "actix-connect/rustls"]
# enable compressison support # enable compressison support
compress = ["flate2", "brotli2"] compress = ["flate2", "brotli"]
# support for secure cookies # support for secure cookies
secure-cookies = ["cookie/secure"] secure-cookies = ["cookie/secure"]
@ -82,7 +82,7 @@ serde_urlencoded = "0.7"
time = { version = "0.2.7", default-features = false, features = ["std"] } time = { version = "0.2.7", default-features = false, features = ["std"] }
# compression # compression
brotli2 = { version="0.3.2", optional = true } brotli = { version = "3.3.3", optional = true }
flate2 = { version = "1.0.13", optional = true } flate2 = { version = "1.0.13", optional = true }
[dev-dependencies] [dev-dependencies]

View File

@ -4,7 +4,7 @@ use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture}; use actix_threadpool::{run, CpuFuture};
use brotli2::write::BrotliDecoder; use brotli::DecompressorWriter as BrotliDecoder;
use bytes::Bytes; use bytes::Bytes;
use flate2::write::{GzDecoder, ZlibDecoder}; use flate2::write::{GzDecoder, ZlibDecoder};
use futures_core::{ready, Stream}; use futures_core::{ready, Stream};
@ -31,7 +31,7 @@ where
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> { pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
let decoder = match encoding { let decoder = match encoding {
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new( ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
BrotliDecoder::new(Writer::new()), BrotliDecoder::new(Writer::new(), 8 * 1024),
))), ))),
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new( ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
ZlibDecoder::new(Writer::new()), ZlibDecoder::new(Writer::new()),

View File

@ -5,7 +5,7 @@ use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture}; use actix_threadpool::{run, CpuFuture};
use brotli2::write::BrotliEncoder; use brotli::CompressorWriter as BrotliEncoder;
use bytes::Bytes; use bytes::Bytes;
use flate2::write::{GzEncoder, ZlibEncoder}; use flate2::write::{GzEncoder, ZlibEncoder};
use futures_core::ready; use futures_core::ready;
@ -212,9 +212,12 @@ impl ContentEncoder {
Writer::new(), Writer::new(),
flate2::Compression::fast(), flate2::Compression::fast(),
))), ))),
ContentEncoding::Br => { ContentEncoding::Br => Some(ContentEncoder::Br(BrotliEncoder::new(
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3))) Writer::new(),
} 32 * 1024,
3,
22,
))),
_ => None, _ => None,
} }
} }
@ -230,8 +233,8 @@ impl ContentEncoder {
fn finish(self) -> Result<Bytes, io::Error> { fn finish(self) -> Result<Bytes, io::Error> {
match self { match self {
ContentEncoder::Br(encoder) => match encoder.finish() { ContentEncoder::Br(mut encoder) => match encoder.flush() {
Ok(writer) => Ok(writer.buf.freeze()), Ok(()) => Ok(encoder.into_inner().buf.freeze()),
Err(err) => Err(err), Err(err) => Err(err),
}, },
ContentEncoder::Gzip(encoder) => match encoder.finish() { ContentEncoder::Gzip(encoder) => match encoder.finish() {

View File

@ -65,7 +65,7 @@ actix-http-test = { version = "2.0.0", features = ["openssl"] }
actix-utils = "2.0.0" actix-utils = "2.0.0"
actix-server = "1.0.0" actix-server = "1.0.0"
actix-tls = { version = "2.0.0", features = ["openssl", "rustls"] } actix-tls = { version = "2.0.0", features = ["openssl", "rustls"] }
brotli2 = "0.3.2" brotli = "3.3.3"
flate2 = "1.0.13" flate2 = "1.0.13"
futures-util = { version = "0.3.5", default-features = false } futures-util = { version = "0.3.5", default-features = false }
env_logger = "0.7" env_logger = "0.7"

View File

@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use brotli2::write::BrotliEncoder; use brotli::CompressorWriter as BrotliEncoder;
use bytes::Bytes; use bytes::Bytes;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use flate2::write::GzEncoder; use flate2::write::GzEncoder;
@ -506,9 +506,10 @@ async fn test_client_gzip_encoding_large_random() {
async fn test_client_brotli_encoding() { async fn test_client_brotli_encoding() {
let srv = test::start(|| { let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| { App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = BrotliEncoder::new(Vec::new(), 5); let mut e = BrotliEncoder::new(Vec::new(), 8096, 5, 22);
e.write_all(&data).unwrap(); e.write_all(&data).unwrap();
let data = e.finish().unwrap(); e.flush().unwrap();
let data = e.into_inner();
HttpResponse::Ok() HttpResponse::Ok()
.header("content-encoding", "br") .header("content-encoding", "br")
.body(data) .body(data)
@ -533,9 +534,9 @@ async fn test_client_brotli_encoding_large_random() {
let srv = test::start(|| { let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| { App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = BrotliEncoder::new(Vec::new(), 5); let mut e = BrotliEncoder::new(Vec::new(), 8096, 5, 22);
e.write_all(&data).unwrap(); e.write_all(&data).unwrap();
let data = e.finish().unwrap(); let data = e.into_inner();
HttpResponse::Ok() HttpResponse::Ok()
.header("content-encoding", "br") .header("content-encoding", "br")
.body(data) .body(data)

View File

@ -7,7 +7,8 @@ use actix_http::http::header::{
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH, ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH,
TRANSFER_ENCODING, TRANSFER_ENCODING,
}; };
use brotli2::write::{BrotliDecoder, BrotliEncoder};
use brotli::{CompressorWriter as BrotliEncoder, DecompressorWriter as BrotliDecoder};
use bytes::Bytes; use bytes::Bytes;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder}; use flate2::write::{GzEncoder, ZlibDecoder, ZlibEncoder};
@ -340,9 +341,9 @@ async fn test_body_br_streaming() {
println!("TEST: {:?}", bytes.len()); println!("TEST: {:?}", bytes.len());
// decode br // decode br
let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); let mut e = BrotliDecoder::new(Vec::with_capacity(2048), 8096);
e.write_all(bytes.as_ref()).unwrap(); e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap(); let dec = e.into_inner().unwrap();
println!("T: {:?}", Bytes::copy_from_slice(&dec)); println!("T: {:?}", Bytes::copy_from_slice(&dec));
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref())); assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
} }
@ -439,9 +440,9 @@ async fn test_body_brotli() {
let bytes = response.body().await.unwrap(); let bytes = response.body().await.unwrap();
// decode brotli // decode brotli
let mut e = BrotliDecoder::new(Vec::with_capacity(2048)); let mut e = BrotliDecoder::new(Vec::with_capacity(2048), 8096);
e.write_all(bytes.as_ref()).unwrap(); e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap(); let dec = e.into_inner().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref())); assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
} }
@ -650,9 +651,10 @@ async fn test_brotli_encoding() {
) )
}); });
let mut e = BrotliEncoder::new(Vec::new(), 5); let mut e = BrotliEncoder::new(Vec::new(), 8096, 5, 22);
e.write_all(STR.as_ref()).unwrap(); e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap(); e.flush().unwrap();
let enc = e.into_inner();
// client request // client request
let request = srv let request = srv
@ -684,9 +686,10 @@ async fn test_brotli_encoding_large() {
) )
}); });
let mut e = BrotliEncoder::new(Vec::new(), 5); let mut e = BrotliEncoder::new(Vec::new(), 8096, 5, 22);
e.write_all(data.as_ref()).unwrap(); e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap(); e.flush().unwrap();
let enc = e.into_inner();
// client request // client request
let request = srv let request = srv
@ -724,9 +727,9 @@ async fn test_brotli_encoding_large_openssl() {
}); });
// body // body
let mut e = BrotliEncoder::new(Vec::new(), 3); let mut e = BrotliEncoder::new(Vec::new(), 8096, 5, 22);
e.write_all(data.as_ref()).unwrap(); e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap(); let enc = e.into_inner();
// client request // client request
let mut response = srv let mut response = srv