Skip to content

Commit

Permalink
Replace brotli with brotli2 actix#1224
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 20, 2019
1 parent 8c54054 commit 1d12ba9
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 139 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ open-ssl = { version="0.10", package = "openssl", optional = true }
rust-tls = { version = "0.16.0", package = "rustls", optional = true }

[dev-dependencies]
actix = "0.9.0-alpha.1"
actix = "0.9.0-alpha.2"
rand = "0.7"
env_logger = "0.6"
serde_derive = "1.0"
brotli = "3.3.0"
brotli2 = "0.3.2"
flate2 = "1.0.13"

[profile.release]
Expand Down
4 changes: 3 additions & 1 deletion actix-http/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Changes

## [1.0.1] - 2019-12-xx
## [1.0.1] - 2019-12-20

### Fixed

* Poll upgrade service's readiness from HTTP service handlers

* Replace brotli with brotli2 #1224

## [1.0.0] - 2019-12-13

### Added
Expand Down
6 changes: 3 additions & 3 deletions actix-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-http"
version = "1.0.0"
version = "1.0.1"
authors = ["Nikolay Kim <[email protected]>"]
description = "Actix http primitives"
readme = "README.md"
Expand Down Expand Up @@ -31,7 +31,7 @@ openssl = ["actix-tls/openssl", "actix-connect/openssl"]
rustls = ["actix-tls/rustls", "actix-connect/rustls"]

# enable compressison support
compress = ["flate2", "brotli"]
compress = ["flate2", "brotli2"]

# failure integration. actix does not use failure anymore
failure = ["fail-ure"]
Expand Down Expand Up @@ -83,7 +83,7 @@ time = "0.1.42"
ring = { version = "0.16.9", optional = true }

# compression
brotli = { version = "3.3.0", optional = true }
brotli2 = { version="0.3.2", optional = true }
flate2 = { version = "1.0.13", optional = true }

# optional deps
Expand Down
6 changes: 3 additions & 3 deletions actix-http/src/encoding/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

use actix_threadpool::{run, CpuFuture};
use brotli::DecompressorWriter;
use brotli2::write::BrotliDecoder;
use bytes::Bytes;
use flate2::write::{GzDecoder, ZlibDecoder};
use futures_core::{ready, Stream};
Expand All @@ -31,7 +31,7 @@ where
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
let decoder = match encoding {
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
DecompressorWriter::new(Writer::new(), 0),
BrotliDecoder::new(Writer::new()),
))),
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
ZlibDecoder::new(Writer::new()),
Expand Down Expand Up @@ -137,7 +137,7 @@ where
enum ContentDecoder {
Deflate(Box<ZlibDecoder<Writer>>),
Gzip(Box<GzDecoder<Writer>>),
Br(Box<DecompressorWriter<Writer>>),
Br(Box<BrotliDecoder<Writer>>),
}

impl ContentDecoder {
Expand Down
24 changes: 11 additions & 13 deletions actix-http/src/encoding/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

use actix_threadpool::{run, CpuFuture};
use brotli::CompressorWriter;
use brotli2::write::BrotliEncoder;
use bytes::Bytes;
use flate2::write::{GzEncoder, ZlibEncoder};
use futures_core::ready;
Expand All @@ -17,7 +17,7 @@ use crate::{Error, ResponseHead};

use super::Writer;

const INPLACE: usize = 2049;
const INPLACE: usize = 1024;

pub struct Encoder<B> {
eof: bool,
Expand Down Expand Up @@ -174,7 +174,7 @@ fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
enum ContentEncoder {
Deflate(ZlibEncoder<Writer>),
Gzip(GzEncoder<Writer>),
Br(Box<CompressorWriter<Writer>>),
Br(BrotliEncoder<Writer>),
}

impl ContentEncoder {
Expand All @@ -188,30 +188,28 @@ impl ContentEncoder {
Writer::new(),
flate2::Compression::fast(),
))),
ContentEncoding::Br => Some(ContentEncoder::Br(Box::new(
CompressorWriter::new(Writer::new(), 0, 3, 0),
))),
ContentEncoding::Br => {
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
}
_ => None,
}
}

#[inline]
pub(crate) fn take(&mut self) -> Bytes {
match *self {
ContentEncoder::Br(ref mut encoder) => {
let mut encoder_new =
Box::new(CompressorWriter::new(Writer::new(), 0, 3, 0));
std::mem::swap(encoder, &mut encoder_new);
encoder_new.into_inner().freeze()
}
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
}
}

fn finish(self) -> Result<Bytes, io::Error> {
match self {
ContentEncoder::Br(encoder) => Ok(encoder.into_inner().buf.freeze()),
ContentEncoder::Br(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
},
ContentEncoder::Gzip(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
Expand Down
5 changes: 2 additions & 3 deletions actix-http/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ impl Writer {
buf: BytesMut::with_capacity(8192),
}
}

fn take(&mut self) -> Bytes {
self.buf.split().freeze()
}
fn freeze(self) -> Bytes {
self.buf.freeze()
}
}

impl io::Write for Writer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.extend_from_slice(buf);
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions actix-http/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::str::Utf8Error;
use std::string::FromUtf8Error;
use std::{fmt, io, result};

use actix_codec::{Decoder, Encoder};
pub use actix_threadpool::BlockingError;
use actix_utils::framed::DispatcherError as FramedDispatcherError;
use actix_utils::timeout::TimeoutError;
use bytes::BytesMut;
use derive_more::{Display, From};
Expand Down Expand Up @@ -463,6 +465,14 @@ impl ResponseError for ContentTypeError {
}
}

impl<E, U: Encoder + Decoder> ResponseError for FramedDispatcherError<E, U>
where
E: fmt::Debug + fmt::Display,
<U as Encoder>::Error: fmt::Debug,
<U as Decoder>::Error: fmt::Debug,
{
}

/// Helper type that can wrap any error and generate custom response.
///
/// In following example any `io::Error` will be converted into "BAD REQUEST"
Expand Down
2 changes: 1 addition & 1 deletion actix-http/src/h1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod rustls {
Request = (Request, Framed<TlsStream<TcpStream>, Codec>),
Response = (),
>,
U::Error: fmt::Display,
U::Error: fmt::Display + Into<Error>,
U::InitError: fmt::Debug,
{
/// Create rustls based service
Expand Down
2 changes: 1 addition & 1 deletion actix-http/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ mod rustls {
Request = (Request, Framed<TlsStream<TcpStream>, h1::Codec>),
Response = (),
>,
U::Error: fmt::Display,
U::Error: fmt::Display + Into<Error>,
U::InitError: fmt::Debug,
<U::Service as Service>::Future: 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ actix-http-test = { version = "1.0.0", features=["openssl"] }
actix-utils = "1.0.3"
actix-server = "1.0.0"
actix-tls = { version = "1.0.0", features=["openssl", "rustls"] }
brotli = "3.3.0"
brotli2 = "0.3.2"
flate2 = "1.0.13"
futures = "0.3.1"
env_logger = "0.6"
Expand Down
10 changes: 5 additions & 5 deletions awc/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;

use brotli::CompressorWriter;
use brotli2::write::BrotliEncoder;
use bytes::Bytes;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
Expand Down Expand Up @@ -500,9 +500,9 @@ async fn test_client_gzip_encoding_large_random() {
async fn test_client_brotli_encoding() {
let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(&data).unwrap();
let data = e.into_inner();
let data = e.finish().unwrap();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
Expand All @@ -527,9 +527,9 @@ async fn test_client_brotli_encoding_large_random() {

let srv = test::start(|| {
App::new().service(web::resource("/").route(web::to(|data: Bytes| {
let mut e = CompressorWriter::new(Vec::new(), 0, 5, 0);
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(&data).unwrap();
let data = e.into_inner();
let data = e.finish().unwrap();
HttpResponse::Ok()
.header("content-encoding", "br")
.body(data)
Expand Down
Loading

0 comments on commit 1d12ba9

Please sign in to comment.