1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-24 09:00:34 +00:00

Replace flate2-xxx features with compress

This commit is contained in:
Nikolay Kim 2019-12-12 15:08:08 +06:00
parent b4b3350b3e
commit fa07415721
13 changed files with 49 additions and 109 deletions

View file

@ -16,7 +16,7 @@ exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"
[package.metadata.docs.rs]
features = ["openssl", "flate2-zlib", "secure-cookies", "client"]
features = ["openssl", "compress", "secure-cookies", "client"]
[badges]
travis-ci = { repository = "actix/actix-web", branch = "master" }
@ -43,16 +43,13 @@ members = [
]
[features]
default = ["flate2-zlib", "client", "fail"]
default = ["compress", "client", "fail"]
# http client
client = ["awc"]
# miniz-sys backend for flate2 crate
flate2-zlib = ["actix-http/flate2-zlib", "awc/flate2-zlib"]
# rust backend for flate2 crate
flate2-rust = ["actix-http/flate2-rust", "awc/flate2-rust"]
# content-encoding support
compress = ["actix-http/compress", "awc/compress"]
# sessions feature, session require "ring" crate and c compiler
secure-cookies = ["actix-http/secure-cookies"]
@ -68,7 +65,7 @@ rustls = ["actix-tls/rustls", "awc/rustls"]
[dependencies]
actix-codec = "0.2.0"
actix-service = "1.0.0"
actix-utils = "1.0.1"
actix-utils = "1.0.3"
actix-router = "0.2.0"
actix-rt = "1.0.0"
actix-server = "1.0.0"
@ -77,8 +74,8 @@ actix-threadpool = "0.3.0"
actix-tls = "1.0.0"
actix-web-codegen = "0.2.0-alpha.2"
actix-http = "1.0.0-alpha.4"
awc = { version = "1.0.0-alpha.4", default-features = false, optional = true }
actix-http = "1.0.0"
awc = { version = "1.0.0", default-features = false, optional = true }
bytes = "0.5.2"
derive_more = "0.99.2"
@ -104,7 +101,7 @@ rand = "0.7"
env_logger = "0.6"
serde_derive = "1.0"
brotli = "3.3.0"
flate2 = "1.0.2"
flate2 = "1.0.13"
open-ssl = { version="0.10", package = "openssl" }
rust_tls = { version = "0.16.0", package = "rustls" }

View file

@ -3,7 +3,7 @@ use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
use actix_http_test::TestServer;
use actix_service::{pipeline_factory, IntoServiceFactory, ServiceFactory};
use actix_utils::framed::Dispatcher;
use bytes::BytesMut;
use bytes::Bytes;
use futures::{future, SinkExt, StreamExt};
use actix_framed::{FramedApp, FramedRequest, FramedRoute, SendError, VerifyWebSockets};
@ -29,9 +29,9 @@ async fn service(msg: ws::Frame) -> Result<ws::Message, Error> {
let msg = match msg {
ws::Frame::Ping(msg) => ws::Message::Pong(msg),
ws::Frame::Text(text) => {
ws::Message::Text(String::from_utf8_lossy(&text.unwrap()).to_string())
ws::Message::Text(String::from_utf8_lossy(&text).to_string())
}
ws::Frame::Binary(bin) => ws::Message::Binary(bin.unwrap().freeze()),
ws::Frame::Binary(bin) => ws::Message::Binary(bin),
ws::Frame::Close(reason) => ws::Message::Close(reason),
_ => panic!(),
};
@ -60,7 +60,7 @@ async fn test_simple() {
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Text(Some(BytesMut::from("text")))
ws::Frame::Text(Bytes::from_static(b"text"))
);
framed
@ -70,7 +70,7 @@ async fn test_simple() {
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Binary(Some(BytesMut::from(&b"text"[..])))
ws::Frame::Binary(Bytes::from_static(b"text"))
);
framed.send(ws::Message::Ping("text".into())).await.unwrap();
@ -126,7 +126,7 @@ async fn test_service() {
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Text(Some(BytesMut::from("text")))
ws::Frame::Text(Bytes::from_static(b"text"))
);
framed
@ -136,7 +136,7 @@ async fn test_service() {
let (item, mut framed) = framed.into_future().await;
assert_eq!(
item.unwrap().unwrap(),
ws::Frame::Binary(Some(BytesMut::from(&b"text"[..])))
ws::Frame::Binary(Bytes::from_static(b"text"))
);
framed.send(ws::Message::Ping("text".into())).await.unwrap();

View file

@ -6,6 +6,10 @@
* Add websockets continuation frame support
### Changed
* Replace `flate2-xxx` features with `compress`
## [1.0.0-alpha.5] - 2019-12-09
### Fixed

View file

@ -15,7 +15,7 @@ license = "MIT/Apache-2.0"
edition = "2018"
[package.metadata.docs.rs]
features = ["openssl", "rustls", "fail", "flate2-zlib", "secure-cookies"]
features = ["openssl", "rustls", "fail", "compress", "secure-cookies"]
[lib]
name = "actix_http"
@ -30,11 +30,8 @@ openssl = ["actix-tls/openssl", "actix-connect/openssl"]
# rustls support
rustls = ["actix-tls/rustls", "actix-connect/rustls"]
# miniz-sys backend for flate2 crate
flate2-zlib = ["flate2/miniz-sys"]
# rust backend for flate2 crate
flate2-rust = ["flate2/rust_backend"]
# enable compressison support
compress = ["flate2", "brotli"]
# failure integration. actix does not use failure anymore
fail = ["failure"]
@ -84,8 +81,8 @@ time = "0.1.42"
ring = { version = "0.16.9", optional = true }
# compression
brotli = "3.3.0"
flate2 = { version="1.0.7", optional = true, default-features = false }
brotli = { version = "3.3.0", optional = true }
flate2 = { version = "1.0.13", optional = true }
# optional deps
failure = { version = "0.1.5", optional = true }

View file

@ -6,7 +6,6 @@ use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture};
use brotli::DecompressorWriter;
use bytes::Bytes;
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
use flate2::write::{GzDecoder, ZlibDecoder};
use futures::{ready, Stream};
@ -34,11 +33,9 @@ where
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(
DecompressorWriter::new(Writer::new(), 0),
))),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoding::Deflate => Some(ContentDecoder::Deflate(Box::new(
ZlibDecoder::new(Writer::new()),
))),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoding::Gzip => Some(ContentDecoder::Gzip(Box::new(
GzDecoder::new(Writer::new()),
))),
@ -138,15 +135,12 @@ where
}
enum ContentDecoder {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Deflate(Box<ZlibDecoder<Writer>>),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Gzip(Box<GzDecoder<Writer>>),
Br(Box<DecompressorWriter<Writer>>),
}
impl ContentDecoder {
#[allow(unreachable_patterns)]
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
match self {
ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
@ -160,7 +154,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentDecoder::Gzip(ref mut decoder) => match decoder.try_finish() {
Ok(_) => {
let b = decoder.get_mut().take();
@ -172,7 +165,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentDecoder::Deflate(ref mut decoder) => match decoder.try_finish() {
Ok(_) => {
let b = decoder.get_mut().take();
@ -184,11 +176,9 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
_ => Ok(None),
}
}
#[allow(unreachable_patterns)]
fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
match self {
ContentDecoder::Br(ref mut decoder) => match decoder.write_all(&data) {
@ -203,7 +193,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentDecoder::Gzip(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
@ -216,7 +205,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentDecoder::Deflate(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
@ -229,7 +217,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
_ => Ok(Some(data)),
}
}
}

View file

@ -7,7 +7,6 @@ use std::task::{Context, Poll};
use actix_threadpool::{run, CpuFuture};
use brotli::CompressorWriter;
use bytes::Bytes;
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
use flate2::write::{GzEncoder, ZlibEncoder};
use crate::body::{Body, BodySize, MessageBody, ResponseBody};
@ -172,9 +171,7 @@ fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
}
enum ContentEncoder {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Deflate(ZlibEncoder<Writer>),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
Gzip(GzEncoder<Writer>),
Br(Box<CompressorWriter<Writer>>),
}
@ -182,12 +179,10 @@ enum ContentEncoder {
impl ContentEncoder {
fn encoder(encoding: ContentEncoding) -> Option<Self> {
match encoding {
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
Writer::new(),
flate2::Compression::fast(),
))),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoding::Gzip => Some(ContentEncoder::Gzip(GzEncoder::new(
Writer::new(),
flate2::Compression::fast(),
@ -208,9 +203,7 @@ impl ContentEncoder {
std::mem::swap(encoder, &mut encoder_new);
encoder_new.into_inner().freeze()
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
}
}
@ -218,12 +211,10 @@ impl ContentEncoder {
fn finish(self) -> Result<Bytes, io::Error> {
match self {
ContentEncoder::Br(encoder) => Ok(encoder.into_inner().buf.freeze()),
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Gzip(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Deflate(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
@ -240,7 +231,6 @@ impl ContentEncoder {
Err(err)
}
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Gzip(ref mut encoder) => match encoder.write_all(data) {
Ok(_) => Ok(()),
Err(err) => {
@ -248,7 +238,6 @@ impl ContentEncoder {
Err(err)
}
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Deflate(ref mut encoder) => match encoder.write_all(data) {
Ok(_) => Ok(()),
Err(err) => {

View file

@ -108,6 +108,7 @@ pub(crate) trait MessageType: Sized {
} else {
dst.put_slice(b"\r\ncontent-length: ");
}
#[allow(clippy::write_with_newline)]
write!(dst.writer(), "{}\r\n", len)?;
}
BodySize::None => dst.put_slice(b"\r\n"),

View file

@ -5,7 +5,7 @@
clippy::too_many_arguments,
clippy::new_without_default,
clippy::borrow_interior_mutable_const,
clippy::write_with_newline
// clippy::write_with_newline
)]
#[macro_use]
@ -16,6 +16,7 @@ mod builder;
pub mod client;
mod cloneable;
mod config;
#[cfg(feature = "compress")]
pub mod encoding;
mod extensions;
mod header;

View file

@ -1,6 +1,6 @@
[package]
name = "awc"
version = "1.0.0-alpha.4"
version = "1.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix http client."
readme = "README.md"
@ -12,19 +12,17 @@ categories = ["network-programming", "asynchronous",
"web-programming::http-client",
"web-programming::websocket"]
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018"
workspace = ".."
[lib]
name = "awc"
path = "src/lib.rs"
[package.metadata.docs.rs]
features = ["openssl", "rustls", "flate2-zlib"]
features = ["openssl", "rustls", "compress"]
[features]
default = ["flate2-zlib"]
default = ["compress"]
# openssl
openssl = ["open-ssl", "actix-http/openssl"]
@ -32,16 +30,13 @@ openssl = ["open-ssl", "actix-http/openssl"]
# rustls
rustls = ["rust-tls", "actix-http/rustls"]
# miniz-sys backend for flate2 crate
flate2-zlib = ["actix-http/flate2-zlib"]
# rust backend for flate2 crate
flate2-rust = ["actix-http/flate2-rust"]
# content-encoding support
compress = ["actix-http/compress"]
[dependencies]
actix-codec = "0.2.0"
actix-service = "1.0.0"
actix-http = "1.0.0-alpha.4"
actix-http = "1.0.0"
actix-rt = "1.0.0"
base64 = "0.11"
@ -61,12 +56,12 @@ rust-tls = { version = "0.16.0", package="rustls", optional = true, features = [
[dev-dependencies]
actix-connect = { version = "1.0.0", features=["openssl"] }
actix-web = { version = "2.0.0-alpha.3", features=["openssl"] }
actix-http = { version = "1.0.0-alpha.4", features=["openssl"] }
actix-http = { version = "1.0.0", features=["openssl"] }
actix-http-test = { version = "1.0.0-alpha.3", features=["openssl"] }
actix-utils = "1.0.0"
actix-server = "1.0.0"
actix-tls = { version = "1.0.0", features=["openssl", "rustls"] }
brotli = "3.3.0"
flate2 = { version="1.0.2" }
flate2 = "1.0.13"
env_logger = "0.6"
webpki = { version = "0.21" }
webpki = "0.21"

View file

@ -3,26 +3,17 @@ use std::io;
use actix_codec::Framed;
use actix_http::{body::BodySize, h1, ws, Error, HttpService, Request, Response};
use actix_http_test::TestServer;
use bytes::{Bytes, BytesMut};
use bytes::Bytes;
use futures::future::ok;
use futures::{SinkExt, StreamExt};
async fn ws_service(req: ws::Frame) -> Result<ws::Message, io::Error> {
match req {
ws::Frame::Ping(msg) => Ok(ws::Message::Pong(msg)),
ws::Frame::Text(text) => {
let text = if let Some(pl) = text {
String::from_utf8(Vec::from(pl.as_ref())).unwrap()
} else {
String::new()
};
Ok(ws::Message::Text(text))
}
ws::Frame::Binary(bin) => Ok(ws::Message::Binary(
bin.map(|e| e.freeze())
.unwrap_or_else(|| Bytes::from(""))
.into(),
ws::Frame::Text(text) => Ok(ws::Message::Text(
String::from_utf8(Vec::from(text.as_ref())).unwrap(),
)),
ws::Frame::Binary(bin) => Ok(ws::Message::Binary(bin)),
ws::Frame::Close(reason) => Ok(ws::Message::Close(reason)),
_ => Ok(ws::Message::Close(None)),
}
@ -56,14 +47,14 @@ async fn test_simple() {
.await
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Text(Some(BytesMut::from("text"))));
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
framed
.send(ws::Message::Binary("text".into()))
.await
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Binary(Some(BytesMut::from(&b"text"[..]))));
assert_eq!(item, ws::Frame::Binary(Bytes::from_static(b"text")));
framed.send(ws::Message::Ping("text".into())).await.unwrap();
let item = framed.next().await.unwrap().unwrap();

View file

@ -71,15 +71,11 @@
//! ## Package feature
//!
//! * `client` - enables http client (default enabled)
//! * `compress` - enables content encoding compression support (default enabled)
//! * `openssl` - enables ssl support via `openssl` crate, supports `http/2`
//! * `rustls` - enables ssl support via `rustls` crate, supports `http/2`
//! * `secure-cookies` - enables secure cookies support, includes `ring` crate as
//! dependency
//! * `flate2-zlib` - enables `gzip`, `deflate` compression support, requires
//! `c` compiler (default enabled)
//! * `flate2-rust` - experimental rust based implementation for
//! `gzip`, `deflate` compression.
//!
#![allow(clippy::type_complexity, clippy::new_without_default)]
mod app;

View file

@ -27,17 +27,17 @@ path = "src/lib.rs"
default = []
# openssl
openssl = ["open-ssl", "awc/openssl", ] # "actix-tls/openssl"]
openssl = ["open-ssl", "awc/openssl"]
[dependencies]
actix-service = "1.0.0"
actix-codec = "0.2.0"
actix-connect = "1.0.0"
actix-utils = "1.0.0"
actix-utils = "1.0.3"
actix-rt = "1.0.0"
actix-server = "1.0.0"
actix-testing = "1.0.0"
awc = "1.0.0-alpha.3"
awc = "1.0.0"
base64 = "0.11"
bytes = "0.5.2"
@ -55,5 +55,5 @@ time = "0.1"
open-ssl = { version="0.10", package="openssl", optional = true }
[dev-dependencies]
actix-web = "2.0.0-alpha.3"
actix-http = "1.0.0-alpha.3"
actix-web = "2.0.0-alpha.4"
actix-http = "1.0.0"

View file

@ -56,7 +56,6 @@ async fn test_body() {
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_gzip() {
let srv = TestServer::start(|| {
@ -86,7 +85,6 @@ async fn test_body_gzip() {
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_gzip2() {
let srv = TestServer::start(|| {
@ -118,7 +116,6 @@ async fn test_body_gzip2() {
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_encoding_override() {
let srv = TestServer::start(|| {
@ -179,7 +176,6 @@ async fn test_body_encoding_override() {
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_gzip_large() {
let data = STR.repeat(10);
@ -216,7 +212,6 @@ async fn test_body_gzip_large() {
assert_eq!(Bytes::from(dec), Bytes::from(data));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_gzip_large_random() {
let data = rand::thread_rng()
@ -257,7 +252,6 @@ async fn test_body_gzip_large_random() {
assert_eq!(Bytes::from(dec), Bytes::from(data));
}
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
#[actix_rt::test]
async fn test_body_chunked_implicit() {
let srv = TestServer::start(move || {
@ -378,7 +372,6 @@ async fn test_no_chunking() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_body_deflate() {
let srv = TestServer::start(move || {
HttpService::build()
@ -440,7 +433,6 @@ async fn test_body_brotli() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_encoding() {
let srv = TestServer::start(move || {
HttpService::build()
@ -469,7 +461,6 @@ async fn test_encoding() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_gzip_encoding() {
let srv = TestServer::start(move || {
HttpService::build()
@ -498,7 +489,6 @@ async fn test_gzip_encoding() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_gzip_encoding_large() {
let data = STR.repeat(10);
let srv = TestServer::start(move || {
@ -528,7 +518,6 @@ async fn test_gzip_encoding_large() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_reading_gzip_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
@ -563,7 +552,6 @@ async fn test_reading_gzip_encoding_large_random() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_reading_deflate_encoding() {
let srv = TestServer::start(move || {
HttpService::build()
@ -592,7 +580,6 @@ async fn test_reading_deflate_encoding() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_reading_deflate_encoding_large() {
let data = STR.repeat(10);
let srv = TestServer::start(move || {
@ -622,7 +609,6 @@ async fn test_reading_deflate_encoding_large() {
}
#[actix_rt::test]
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
async fn test_reading_deflate_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
@ -771,11 +757,7 @@ async fn test_brotli_encoding_large() {
// assert_eq!(bytes, Bytes::from(data));
// }
#[cfg(all(
feature = "rustls",
feature = "openssl",
any(feature = "flate2-zlib", feature = "flate2-rust")
))]
#[cfg(all(feature = "rustls", feature = "openssl"))]
#[actix_rt::test]
async fn test_reading_deflate_encoding_large_random_ssl() {
use open_ssl::ssl::{SslConnector, SslMethod, SslVerifyMode};