1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

Replace brotli with brotli2 #1224

This commit is contained in:
Nikolay Kim
2019-12-20 13:50:07 +06:00
parent 8c54054844
commit 1d12ba9d5f
12 changed files with 108 additions and 139 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
[package]
name = "actix-http"
version = "1.0.0"
version = "1.0.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http primitives"
readme = "README.md"
@ -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"]
@ -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

View File

@ -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};
@ -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()),
@ -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 {

View File

@ -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;
@ -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,
@ -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 {
@ -188,9 +188,9 @@ 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,
}
}
@ -198,12 +198,7 @@ impl ContentEncoder {
#[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(),
}
@ -211,7 +206,10 @@ impl ContentEncoder {
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),

View File

@ -19,12 +19,10 @@ 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 {
@ -32,6 +30,7 @@ impl io::Write for Writer {
self.buf.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}

View File

@ -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};
@ -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"

View File

@ -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

View File

@ -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,
{