From 0f064db31d564a923519d4c7bb4c82c48e083b78 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 13 Mar 2018 17:21:22 -0700 Subject: [PATCH] Move brotli encoding to a feature --- CHANGES.md | 2 ++ Cargo.toml | 7 +++++-- src/client/writer.rs | 5 ++++- src/header/mod.rs | 4 ++++ src/lib.rs | 1 + src/server/encoding.rs | 13 ++++++++++++- 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 21a81d3d6..cfd234642 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ * Send Query Parameters in client requests #120 +* Move brotli encoding to a feature + ## 0.4.8 (2018-03-12) diff --git a/Cargo.toml b/Cargo.toml index 90653a152..a2d4fca79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ name = "actix_web" path = "src/lib.rs" [features] -default = ["session"] +default = ["session", "brotli"] # tls tls = ["native-tls", "tokio-tls"] @@ -38,12 +38,14 @@ alpn = ["openssl", "openssl/v102", "openssl/v110", "tokio-openssl"] # sessions session = ["cookie/secure"] +# brotli +brotli = ["brotli2"] + [dependencies] actix = "^0.5.2" base64 = "0.9" bitflags = "1.0" -brotli2 = "^0.3.2" failure = "0.1.1" flate2 = "1.0" h2 = "0.1" @@ -67,6 +69,7 @@ encoding = "0.2" language-tags = "0.2" url = { version="1.7", features=["query_encoding"] } cookie = { version="0.10", features=["percent-encode"] } +brotli2 = { version="^0.3.2", optional = true } # io mio = "^0.6.13" diff --git a/src/client/writer.rs b/src/client/writer.rs index a3692358f..be60c1c0a 100644 --- a/src/client/writer.rs +++ b/src/client/writer.rs @@ -13,10 +13,11 @@ use http::header::{HeaderValue, DATE, CONNECTION, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING}; use flate2::Compression; use flate2::write::{GzEncoder, DeflateEncoder}; +#[cfg(feature="brotli")] use brotli2::write::BrotliEncoder; use body::{Body, Binary}; -use headers::ContentEncoding; +use header::ContentEncoding; use server::WriterState; use server::shared::SharedBytes; use server::encoding::{ContentEncoder, TransferEncoding}; @@ -215,6 +216,7 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder DeflateEncoder::new(transfer, Compression::default())), ContentEncoding::Gzip => ContentEncoder::Gzip( GzEncoder::new(transfer, Compression::default())), + #[cfg(feature="brotli")] ContentEncoding::Br => ContentEncoder::Br( BrotliEncoder::new(transfer, 5)), ContentEncoding::Identity => ContentEncoder::Identity(transfer), @@ -264,6 +266,7 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder DeflateEncoder::new(transfer, Compression::default())), ContentEncoding::Gzip => ContentEncoder::Gzip( GzEncoder::new(transfer, Compression::default())), + #[cfg(feature="brotli")] ContentEncoding::Br => ContentEncoder::Br( BrotliEncoder::new(transfer, 5)), ContentEncoding::Identity | ContentEncoding::Auto => ContentEncoder::Identity(transfer), diff --git a/src/header/mod.rs b/src/header/mod.rs index d02ca9778..29fa9e134 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -119,6 +119,7 @@ pub enum ContentEncoding { /// Automatically select encoding based on encoding negotiation Auto, /// A format using the Brotli algorithm + #[cfg(feature="brotli")] Br, /// A format using the zlib structure with deflate algorithm Deflate, @@ -141,6 +142,7 @@ impl ContentEncoding { #[inline] pub fn as_str(&self) -> &'static str { match *self { + #[cfg(feature="brotli")] ContentEncoding::Br => "br", ContentEncoding::Gzip => "gzip", ContentEncoding::Deflate => "deflate", @@ -150,6 +152,7 @@ impl ContentEncoding { /// default quality value pub fn quality(&self) -> f64 { match *self { + #[cfg(feature="brotli")] ContentEncoding::Br => 1.1, ContentEncoding::Gzip => 1.0, ContentEncoding::Deflate => 0.9, @@ -162,6 +165,7 @@ impl ContentEncoding { impl<'a> From<&'a str> for ContentEncoding { fn from(s: &'a str) -> ContentEncoding { match s.trim().to_lowercase().as_ref() { + #[cfg(feature="brotli")] "br" => ContentEncoding::Br, "gzip" => ContentEncoding::Gzip, "deflate" => ContentEncoding::Deflate, diff --git a/src/lib.rs b/src/lib.rs index 93cfe2662..05552fdc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,6 +79,7 @@ extern crate libc; extern crate serde; extern crate serde_json; extern crate flate2; +#[cfg(feature="brotli")] extern crate brotli2; extern crate encoding; extern crate percent_encoding; diff --git a/src/server/encoding.rs b/src/server/encoding.rs index a7eaf1c00..6ba232b0c 100644 --- a/src/server/encoding.rs +++ b/src/server/encoding.rs @@ -3,6 +3,7 @@ use std::io::{Read, Write}; use std::fmt::Write as FmtWrite; use std::str::FromStr; +use bytes::{Bytes, BytesMut, BufMut}; use http::{Version, Method, HttpTryFrom}; use http::header::{HeaderMap, HeaderValue, ACCEPT_ENCODING, CONNECTION, @@ -10,8 +11,8 @@ use http::header::{HeaderMap, HeaderValue, use flate2::Compression; use flate2::read::GzDecoder; use flate2::write::{GzEncoder, DeflateDecoder, DeflateEncoder}; +#[cfg(feature="brotli")] use brotli2::write::{BrotliDecoder, BrotliEncoder}; -use bytes::{Bytes, BytesMut, BufMut}; use header::ContentEncoding; use body::{Body, Binary}; @@ -144,6 +145,7 @@ impl PayloadWriter for EncodedPayload { pub(crate) enum Decoder { Deflate(Box>), Gzip(Option>>), + #[cfg(feature="brotli")] Br(Box>), Identity, } @@ -214,6 +216,7 @@ pub(crate) struct PayloadStream { impl PayloadStream { pub fn new(enc: ContentEncoding) -> PayloadStream { let dec = match enc { + #[cfg(feature="brotli")] ContentEncoding::Br => Decoder::Br( Box::new(BrotliDecoder::new(Writer::new()))), ContentEncoding::Deflate => Decoder::Deflate( @@ -229,6 +232,7 @@ impl PayloadStream { pub fn feed_eof(&mut self) -> io::Result> { match self.decoder { + #[cfg(feature="brotli")] Decoder::Br(ref mut decoder) => { match decoder.finish() { Ok(mut writer) => { @@ -278,6 +282,7 @@ impl PayloadStream { pub fn feed_data(&mut self, data: Bytes) -> io::Result> { match self.decoder { + #[cfg(feature="brotli")] Decoder::Br(ref mut decoder) => { match decoder.write_all(&data) { Ok(_) => { @@ -346,6 +351,7 @@ impl PayloadStream { pub(crate) enum ContentEncoder { Deflate(DeflateEncoder), Gzip(GzEncoder), + #[cfg(feature="brotli")] Br(BrotliEncoder), Identity(TransferEncoding), } @@ -412,6 +418,7 @@ impl ContentEncoder { DeflateEncoder::new(transfer, Compression::default())), ContentEncoding::Gzip => ContentEncoder::Gzip( GzEncoder::new(transfer, Compression::default())), + #[cfg(feature="brotli")] ContentEncoding::Br => ContentEncoder::Br( BrotliEncoder::new(transfer, 5)), ContentEncoding::Identity => ContentEncoder::Identity(transfer), @@ -464,6 +471,7 @@ impl ContentEncoder { DeflateEncoder::new(transfer, Compression::default())), ContentEncoding::Gzip => ContentEncoder::Gzip( GzEncoder::new(transfer, Compression::default())), + #[cfg(feature="brotli")] ContentEncoding::Br => ContentEncoder::Br( BrotliEncoder::new(transfer, 5)), ContentEncoding::Identity | ContentEncoding::Auto => @@ -538,6 +546,7 @@ impl ContentEncoder { #[inline] pub fn is_eof(&self) -> bool { match *self { + #[cfg(feature="brotli")] ContentEncoder::Br(ref encoder) => encoder.get_ref().is_eof(), ContentEncoder::Deflate(ref encoder) => encoder.get_ref().is_eof(), ContentEncoder::Gzip(ref encoder) => encoder.get_ref().is_eof(), @@ -552,6 +561,7 @@ impl ContentEncoder { self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::empty()))); match encoder { + #[cfg(feature="brotli")] ContentEncoder::Br(encoder) => { match encoder.finish() { Ok(mut writer) => { @@ -594,6 +604,7 @@ impl ContentEncoder { #[inline(always)] pub fn write(&mut self, data: Binary) -> Result<(), io::Error> { match *self { + #[cfg(feature="brotli")] ContentEncoder::Br(ref mut encoder) => { match encoder.write_all(data.as_ref()) { Ok(_) => Ok(()),