Skip to content

Commit

Permalink
Replace flate2-xxx features with compress
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 12, 2019
1 parent b4b3350 commit fa07415
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 109 deletions.
19 changes: 8 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"]
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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" }

Expand Down
14 changes: 7 additions & 7 deletions actix-framed/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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!(),
};
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions actix-http/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions actix-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]
Expand Down Expand Up @@ -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 }
Expand Down
13 changes: 0 additions & 13 deletions actix-http/src/encoding/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()),
))),
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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()?;
Expand All @@ -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()?;
Expand All @@ -229,7 +217,6 @@ impl ContentDecoder {
}
Err(e) => Err(e),
},
_ => Ok(Some(data)),
}
}
}
11 changes: 0 additions & 11 deletions actix-http/src/encoding/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -172,22 +171,18 @@ 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>>),
}

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(),
Expand All @@ -208,22 +203,18 @@ 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(),
}
}

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),
Expand All @@ -240,15 +231,13 @@ 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) => {
trace!("Error decoding gzip encoding: {}", err);
Err(err)
}
},
#[cfg(any(feature = "flate2-zlib", feature = "flate2-rust"))]
ContentEncoder::Deflate(ref mut encoder) => match encoder.write_all(data) {
Ok(_) => Ok(()),
Err(err) => {
Expand Down
1 change: 1 addition & 0 deletions actix-http/src/h1/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
3 changes: 2 additions & 1 deletion actix-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -16,6 +16,7 @@ mod builder;
pub mod client;
mod cloneable;
mod config;
#[cfg(feature = "compress")]
pub mod encoding;
mod extensions;
mod header;
Expand Down
Loading

0 comments on commit fa07415

Please sign in to comment.