mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
Move brotli encoding to a feature
This commit is contained in:
parent
fd0bb54469
commit
0f064db31d
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
* Send Query Parameters in client requests #120
|
* Send Query Parameters in client requests #120
|
||||||
|
|
||||||
|
* Move brotli encoding to a feature
|
||||||
|
|
||||||
|
|
||||||
## 0.4.8 (2018-03-12)
|
## 0.4.8 (2018-03-12)
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ name = "actix_web"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["session"]
|
default = ["session", "brotli"]
|
||||||
|
|
||||||
# tls
|
# tls
|
||||||
tls = ["native-tls", "tokio-tls"]
|
tls = ["native-tls", "tokio-tls"]
|
||||||
@ -38,12 +38,14 @@ alpn = ["openssl", "openssl/v102", "openssl/v110", "tokio-openssl"]
|
|||||||
# sessions
|
# sessions
|
||||||
session = ["cookie/secure"]
|
session = ["cookie/secure"]
|
||||||
|
|
||||||
|
# brotli
|
||||||
|
brotli = ["brotli2"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix = "^0.5.2"
|
actix = "^0.5.2"
|
||||||
|
|
||||||
base64 = "0.9"
|
base64 = "0.9"
|
||||||
bitflags = "1.0"
|
bitflags = "1.0"
|
||||||
brotli2 = "^0.3.2"
|
|
||||||
failure = "0.1.1"
|
failure = "0.1.1"
|
||||||
flate2 = "1.0"
|
flate2 = "1.0"
|
||||||
h2 = "0.1"
|
h2 = "0.1"
|
||||||
@ -67,6 +69,7 @@ encoding = "0.2"
|
|||||||
language-tags = "0.2"
|
language-tags = "0.2"
|
||||||
url = { version="1.7", features=["query_encoding"] }
|
url = { version="1.7", features=["query_encoding"] }
|
||||||
cookie = { version="0.10", features=["percent-encode"] }
|
cookie = { version="0.10", features=["percent-encode"] }
|
||||||
|
brotli2 = { version="^0.3.2", optional = true }
|
||||||
|
|
||||||
# io
|
# io
|
||||||
mio = "^0.6.13"
|
mio = "^0.6.13"
|
||||||
|
@ -13,10 +13,11 @@ use http::header::{HeaderValue, DATE,
|
|||||||
CONNECTION, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
CONNECTION, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING};
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
use flate2::write::{GzEncoder, DeflateEncoder};
|
use flate2::write::{GzEncoder, DeflateEncoder};
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
use brotli2::write::BrotliEncoder;
|
use brotli2::write::BrotliEncoder;
|
||||||
|
|
||||||
use body::{Body, Binary};
|
use body::{Body, Binary};
|
||||||
use headers::ContentEncoding;
|
use header::ContentEncoding;
|
||||||
use server::WriterState;
|
use server::WriterState;
|
||||||
use server::shared::SharedBytes;
|
use server::shared::SharedBytes;
|
||||||
use server::encoding::{ContentEncoder, TransferEncoding};
|
use server::encoding::{ContentEncoder, TransferEncoding};
|
||||||
@ -215,6 +216,7 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder
|
|||||||
DeflateEncoder::new(transfer, Compression::default())),
|
DeflateEncoder::new(transfer, Compression::default())),
|
||||||
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
||||||
GzEncoder::new(transfer, Compression::default())),
|
GzEncoder::new(transfer, Compression::default())),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => ContentEncoder::Br(
|
ContentEncoding::Br => ContentEncoder::Br(
|
||||||
BrotliEncoder::new(transfer, 5)),
|
BrotliEncoder::new(transfer, 5)),
|
||||||
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
|
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
|
||||||
@ -264,6 +266,7 @@ fn content_encoder(buf: SharedBytes, req: &mut ClientRequest) -> ContentEncoder
|
|||||||
DeflateEncoder::new(transfer, Compression::default())),
|
DeflateEncoder::new(transfer, Compression::default())),
|
||||||
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
||||||
GzEncoder::new(transfer, Compression::default())),
|
GzEncoder::new(transfer, Compression::default())),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => ContentEncoder::Br(
|
ContentEncoding::Br => ContentEncoder::Br(
|
||||||
BrotliEncoder::new(transfer, 5)),
|
BrotliEncoder::new(transfer, 5)),
|
||||||
ContentEncoding::Identity | ContentEncoding::Auto => ContentEncoder::Identity(transfer),
|
ContentEncoding::Identity | ContentEncoding::Auto => ContentEncoder::Identity(transfer),
|
||||||
|
@ -119,6 +119,7 @@ pub enum ContentEncoding {
|
|||||||
/// Automatically select encoding based on encoding negotiation
|
/// Automatically select encoding based on encoding negotiation
|
||||||
Auto,
|
Auto,
|
||||||
/// A format using the Brotli algorithm
|
/// A format using the Brotli algorithm
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
Br,
|
Br,
|
||||||
/// A format using the zlib structure with deflate algorithm
|
/// A format using the zlib structure with deflate algorithm
|
||||||
Deflate,
|
Deflate,
|
||||||
@ -141,6 +142,7 @@ impl ContentEncoding {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_str(&self) -> &'static str {
|
pub fn as_str(&self) -> &'static str {
|
||||||
match *self {
|
match *self {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => "br",
|
ContentEncoding::Br => "br",
|
||||||
ContentEncoding::Gzip => "gzip",
|
ContentEncoding::Gzip => "gzip",
|
||||||
ContentEncoding::Deflate => "deflate",
|
ContentEncoding::Deflate => "deflate",
|
||||||
@ -150,6 +152,7 @@ impl ContentEncoding {
|
|||||||
/// default quality value
|
/// default quality value
|
||||||
pub fn quality(&self) -> f64 {
|
pub fn quality(&self) -> f64 {
|
||||||
match *self {
|
match *self {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => 1.1,
|
ContentEncoding::Br => 1.1,
|
||||||
ContentEncoding::Gzip => 1.0,
|
ContentEncoding::Gzip => 1.0,
|
||||||
ContentEncoding::Deflate => 0.9,
|
ContentEncoding::Deflate => 0.9,
|
||||||
@ -162,6 +165,7 @@ impl ContentEncoding {
|
|||||||
impl<'a> From<&'a str> for ContentEncoding {
|
impl<'a> From<&'a str> for ContentEncoding {
|
||||||
fn from(s: &'a str) -> ContentEncoding {
|
fn from(s: &'a str) -> ContentEncoding {
|
||||||
match s.trim().to_lowercase().as_ref() {
|
match s.trim().to_lowercase().as_ref() {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
"br" => ContentEncoding::Br,
|
"br" => ContentEncoding::Br,
|
||||||
"gzip" => ContentEncoding::Gzip,
|
"gzip" => ContentEncoding::Gzip,
|
||||||
"deflate" => ContentEncoding::Deflate,
|
"deflate" => ContentEncoding::Deflate,
|
||||||
|
@ -79,6 +79,7 @@ extern crate libc;
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate flate2;
|
extern crate flate2;
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
extern crate brotli2;
|
extern crate brotli2;
|
||||||
extern crate encoding;
|
extern crate encoding;
|
||||||
extern crate percent_encoding;
|
extern crate percent_encoding;
|
||||||
|
@ -3,6 +3,7 @@ use std::io::{Read, Write};
|
|||||||
use std::fmt::Write as FmtWrite;
|
use std::fmt::Write as FmtWrite;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use bytes::{Bytes, BytesMut, BufMut};
|
||||||
use http::{Version, Method, HttpTryFrom};
|
use http::{Version, Method, HttpTryFrom};
|
||||||
use http::header::{HeaderMap, HeaderValue,
|
use http::header::{HeaderMap, HeaderValue,
|
||||||
ACCEPT_ENCODING, CONNECTION,
|
ACCEPT_ENCODING, CONNECTION,
|
||||||
@ -10,8 +11,8 @@ use http::header::{HeaderMap, HeaderValue,
|
|||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use flate2::write::{GzEncoder, DeflateDecoder, DeflateEncoder};
|
use flate2::write::{GzEncoder, DeflateDecoder, DeflateEncoder};
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
||||||
use bytes::{Bytes, BytesMut, BufMut};
|
|
||||||
|
|
||||||
use header::ContentEncoding;
|
use header::ContentEncoding;
|
||||||
use body::{Body, Binary};
|
use body::{Body, Binary};
|
||||||
@ -144,6 +145,7 @@ impl PayloadWriter for EncodedPayload {
|
|||||||
pub(crate) enum Decoder {
|
pub(crate) enum Decoder {
|
||||||
Deflate(Box<DeflateDecoder<Writer>>),
|
Deflate(Box<DeflateDecoder<Writer>>),
|
||||||
Gzip(Option<Box<GzDecoder<Wrapper>>>),
|
Gzip(Option<Box<GzDecoder<Wrapper>>>),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
Br(Box<BrotliDecoder<Writer>>),
|
Br(Box<BrotliDecoder<Writer>>),
|
||||||
Identity,
|
Identity,
|
||||||
}
|
}
|
||||||
@ -214,6 +216,7 @@ pub(crate) struct PayloadStream {
|
|||||||
impl PayloadStream {
|
impl PayloadStream {
|
||||||
pub fn new(enc: ContentEncoding) -> PayloadStream {
|
pub fn new(enc: ContentEncoding) -> PayloadStream {
|
||||||
let dec = match enc {
|
let dec = match enc {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => Decoder::Br(
|
ContentEncoding::Br => Decoder::Br(
|
||||||
Box::new(BrotliDecoder::new(Writer::new()))),
|
Box::new(BrotliDecoder::new(Writer::new()))),
|
||||||
ContentEncoding::Deflate => Decoder::Deflate(
|
ContentEncoding::Deflate => Decoder::Deflate(
|
||||||
@ -229,6 +232,7 @@ impl PayloadStream {
|
|||||||
|
|
||||||
pub fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
|
pub fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
|
||||||
match self.decoder {
|
match self.decoder {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
Decoder::Br(ref mut decoder) => {
|
Decoder::Br(ref mut decoder) => {
|
||||||
match decoder.finish() {
|
match decoder.finish() {
|
||||||
Ok(mut writer) => {
|
Ok(mut writer) => {
|
||||||
@ -278,6 +282,7 @@ impl PayloadStream {
|
|||||||
|
|
||||||
pub fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
|
pub fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
|
||||||
match self.decoder {
|
match self.decoder {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
Decoder::Br(ref mut decoder) => {
|
Decoder::Br(ref mut decoder) => {
|
||||||
match decoder.write_all(&data) {
|
match decoder.write_all(&data) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@ -346,6 +351,7 @@ impl PayloadStream {
|
|||||||
pub(crate) enum ContentEncoder {
|
pub(crate) enum ContentEncoder {
|
||||||
Deflate(DeflateEncoder<TransferEncoding>),
|
Deflate(DeflateEncoder<TransferEncoding>),
|
||||||
Gzip(GzEncoder<TransferEncoding>),
|
Gzip(GzEncoder<TransferEncoding>),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
Br(BrotliEncoder<TransferEncoding>),
|
Br(BrotliEncoder<TransferEncoding>),
|
||||||
Identity(TransferEncoding),
|
Identity(TransferEncoding),
|
||||||
}
|
}
|
||||||
@ -412,6 +418,7 @@ impl ContentEncoder {
|
|||||||
DeflateEncoder::new(transfer, Compression::default())),
|
DeflateEncoder::new(transfer, Compression::default())),
|
||||||
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
||||||
GzEncoder::new(transfer, Compression::default())),
|
GzEncoder::new(transfer, Compression::default())),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => ContentEncoder::Br(
|
ContentEncoding::Br => ContentEncoder::Br(
|
||||||
BrotliEncoder::new(transfer, 5)),
|
BrotliEncoder::new(transfer, 5)),
|
||||||
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
|
ContentEncoding::Identity => ContentEncoder::Identity(transfer),
|
||||||
@ -464,6 +471,7 @@ impl ContentEncoder {
|
|||||||
DeflateEncoder::new(transfer, Compression::default())),
|
DeflateEncoder::new(transfer, Compression::default())),
|
||||||
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
ContentEncoding::Gzip => ContentEncoder::Gzip(
|
||||||
GzEncoder::new(transfer, Compression::default())),
|
GzEncoder::new(transfer, Compression::default())),
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoding::Br => ContentEncoder::Br(
|
ContentEncoding::Br => ContentEncoder::Br(
|
||||||
BrotliEncoder::new(transfer, 5)),
|
BrotliEncoder::new(transfer, 5)),
|
||||||
ContentEncoding::Identity | ContentEncoding::Auto =>
|
ContentEncoding::Identity | ContentEncoding::Auto =>
|
||||||
@ -538,6 +546,7 @@ impl ContentEncoder {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_eof(&self) -> bool {
|
pub fn is_eof(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoder::Br(ref encoder) => encoder.get_ref().is_eof(),
|
ContentEncoder::Br(ref encoder) => encoder.get_ref().is_eof(),
|
||||||
ContentEncoder::Deflate(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(),
|
ContentEncoder::Gzip(ref encoder) => encoder.get_ref().is_eof(),
|
||||||
@ -552,6 +561,7 @@ impl ContentEncoder {
|
|||||||
self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::empty())));
|
self, ContentEncoder::Identity(TransferEncoding::eof(SharedBytes::empty())));
|
||||||
|
|
||||||
match encoder {
|
match encoder {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoder::Br(encoder) => {
|
ContentEncoder::Br(encoder) => {
|
||||||
match encoder.finish() {
|
match encoder.finish() {
|
||||||
Ok(mut writer) => {
|
Ok(mut writer) => {
|
||||||
@ -594,6 +604,7 @@ impl ContentEncoder {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn write(&mut self, data: Binary) -> Result<(), io::Error> {
|
pub fn write(&mut self, data: Binary) -> Result<(), io::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
|
#[cfg(feature="brotli")]
|
||||||
ContentEncoder::Br(ref mut encoder) => {
|
ContentEncoder::Br(ref mut encoder) => {
|
||||||
match encoder.write_all(data.as_ref()) {
|
match encoder.write_all(data.as_ref()) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
|
Loading…
Reference in New Issue
Block a user