Skip to content

Commit

Permalink
Feature gate brotli2 encoder (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Mar 28, 2024
1 parent d3499fe commit 0e21f46
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ jobs:
run: cargo llvm-cov clean --workspace

- name: Code coverage (glommio)
run: cargo +nightly llvm-cov --no-report --all --no-default-features --features="glommio,cookie,url,compress,openssl,rustls,ws"
run: cargo +nightly llvm-cov --no-report --all --no-default-features --features="glommio,cookie,url,compress,openssl,rustls,ws,brotli"

- name: Code coverage
run: cargo +nightly llvm-cov --no-report --all --doctests --no-default-features --features="tokio,cookie,url,compress,openssl,rustls,ws"
run: cargo +nightly llvm-cov --no-report --all --doctests --no-default-features --features="tokio,cookie,url,compress,openssl,rustls,ws,brotli"

- name: Generate coverage report
run: cargo +nightly llvm-cov report --lcov --output-path lcov.info --ignore-filename-regex="ntex-tokio|ntex-glommio|ntex-async-std"
Expand Down
20 changes: 13 additions & 7 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,30 @@ jobs:
with:
command: generate-lockfile

# - name: Cache vcpkg
# uses: actions/cache@v1
# with:
# path: C:\vcpkg\installed\x64-windows\
# key: x86_64-pc-windows-msvc-openssl
- name: Cache vcpkg
uses: actions/cache@v4
with:
path: C:\vcpkg\installed\x64-windows\
key: x86_64-pc-windows-msvc-openssl

- name: Cache cargo registry
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ matrix.version }}-x86_64-pc-windows-msvc-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo index
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ matrix.version }}-x86_64-pc-windows-msvc-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}

- name: Install cmake
uses: lukka/get-cmake@latest

- name: Install nasm
uses: ilammy/setup-nasm@v1

- name: Install OpenSSL
run: |
vcpkg integrate install
Expand Down
6 changes: 6 additions & 0 deletions ntex/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## [1.2.1] - 2024-03-28

* Feature gate websocket support #320

* Feature gate brotli2 encoder

## [1.2.0] - 2024-03-24

* Refactor server workers management
Expand Down
25 changes: 14 additions & 11 deletions ntex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex"
version = "1.2.0"
version = "1.2.1"
authors = ["ntex contributors <[email protected]>"]
description = "Framework for composable network services"
readme = "README.md"
Expand All @@ -17,14 +17,14 @@ license = "MIT OR Apache-2.0"
edition = "2021"

[package.metadata.docs.rs]
features = ["tokio", "openssl", "rustls", "compress", "cookie"]
features = ["tokio", "openssl", "rustls", "compress", "cookie", "ws"]

[lib]
name = "ntex"
path = "src/lib.rs"

[features]
default = []
default = ["ws"]

# openssl
openssl = ["tls-openssl", "ntex-tls/openssl"]
Expand All @@ -33,7 +33,7 @@ openssl = ["tls-openssl", "ntex-tls/openssl"]
rustls = ["tls-rustls", "webpki-roots", "ntex-tls/rustls"]

# enable compressison support
compress = ["flate2", "brotli2"]
compress = ["flate2"]

# enable cookie support
cookie = ["coo-kie", "coo-kie/percent-encode"]
Expand All @@ -53,6 +53,9 @@ async-std = ["ntex-net/async-std"]
# websocket support
ws = ["dep:sha-1"]

# brotli2 support
brotli = ["dep:brotli2"]

[dependencies]
ntex-codec = "0.6.2"
ntex-http = "0.1.12"
Expand All @@ -62,24 +65,24 @@ ntex-macros = "0.1.3"
ntex-util = "1.0.1"
ntex-bytes = "0.1.24"
ntex-server = "1.0.0"
ntex-h2 = "0.5.1"
ntex-rt = "0.4.11"
ntex-h2 = "0.5.2"
ntex-rt = "0.4.12"
ntex-io = "1.0.1"
ntex-net = "1.0.0"
ntex-tls = "1.0.0"
ntex-tls = "1.1.0"

base64 = "0.22"
bitflags = "2"
log = "0.4"
nanorand = { version = "0.7", default-features = false, features = [
"std",
"wyrand",
] }
pin-project-lite = "0.2"
regex = { version = "1.10", default-features = false, features = ["std"] }
serde = { version = "1.0", features = ["derive"] }
sha-1 = { version = "0.10", optional = true }
thiserror = "1.0"
nanorand = { version = "0.7", default-features = false, features = [
"std",
"wyrand",
] }

# http/web framework
httparse = "1.8"
Expand Down
5 changes: 5 additions & 0 deletions ntex/src/http/encoding/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{future::Future, io, io::Write, pin::Pin, task::Context, task::Poll};

#[cfg(feature = "brotli")]
use brotli2::write::BrotliDecoder;
use flate2::write::{GzDecoder, ZlibDecoder};

Expand All @@ -26,6 +27,7 @@ where
#[inline]
pub fn new(stream: S, encoding: ContentEncoding) -> Decoder<S> {
let decoder = match encoding {
#[cfg(feature = "brotli")]
ContentEncoding::Br => Some(ContentDecoder::Br(Box::new(BrotliDecoder::new(
Writer::new(),
)))),
Expand Down Expand Up @@ -135,12 +137,14 @@ where
enum ContentDecoder {
Deflate(Box<ZlibDecoder<Writer>>),
Gzip(Box<GzDecoder<Writer>>),
#[cfg(feature = "brotli")]
Br(Box<BrotliDecoder<Writer>>),
}

impl ContentDecoder {
fn feed_eof(&mut self) -> io::Result<Option<Bytes>> {
match self {
#[cfg(feature = "brotli")]
ContentDecoder::Br(ref mut decoder) => match decoder.flush() {
Ok(()) => {
let b = decoder.get_mut().take();
Expand Down Expand Up @@ -179,6 +183,7 @@ impl ContentDecoder {

fn feed_data(&mut self, data: Bytes) -> io::Result<Option<Bytes>> {
match self {
#[cfg(feature = "brotli")]
ContentDecoder::Br(ref mut decoder) => match decoder.write_all(&data) {
Ok(_) => {
decoder.flush()?;
Expand Down
22 changes: 18 additions & 4 deletions ntex/src/http/encoding/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Stream encoder
use std::{fmt, future::Future, io, io::Write, pin::Pin, task::Context, task::Poll};

#[cfg(feature = "brotli")]
use brotli2::write::BrotliEncoder;
use flate2::write::{GzEncoder, ZlibEncoder};

Expand Down Expand Up @@ -190,15 +191,23 @@ fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
enum ContentEncoder {
Deflate(ZlibEncoder<Writer>),
Gzip(GzEncoder<Writer>),
#[cfg(feature = "brotli")]
Br(BrotliEncoder<Writer>),
}

impl ContentEncoder {
fn can_encode(encoding: ContentEncoding) -> bool {
matches!(
encoding,
ContentEncoding::Deflate | ContentEncoding::Gzip | ContentEncoding::Br
)
#[cfg(feature = "brotli")]
{
matches!(
encoding,
ContentEncoding::Deflate | ContentEncoding::Gzip | ContentEncoding::Br
)
}
#[cfg(not(feature = "brotli"))]
{
matches!(encoding, ContentEncoding::Deflate | ContentEncoding::Gzip)
}
}

fn encoder(encoding: ContentEncoding) -> Option<Self> {
Expand All @@ -211,6 +220,7 @@ impl ContentEncoder {
Writer::new(),
flate2::Compression::fast(),
))),
#[cfg(feature = "brotli")]
ContentEncoding::Br => {
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
}
Expand All @@ -220,6 +230,7 @@ impl ContentEncoder {

fn take(&mut self) -> Bytes {
match *self {
#[cfg(feature = "brotli")]
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(),
Expand All @@ -228,6 +239,7 @@ impl ContentEncoder {

fn finish(self) -> Result<Bytes, io::Error> {
match self {
#[cfg(feature = "brotli")]
ContentEncoder::Br(encoder) => match encoder.finish() {
Ok(writer) => Ok(writer.buf.freeze()),
Err(err) => Err(err),
Expand All @@ -245,6 +257,7 @@ impl ContentEncoder {

fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
match *self {
#[cfg(feature = "brotli")]
ContentEncoder::Br(ref mut encoder) => match encoder.write_all(data) {
Ok(_) => Ok(()),
Err(err) => {
Expand Down Expand Up @@ -275,6 +288,7 @@ impl fmt::Debug for ContentEncoder {
match self {
ContentEncoder::Deflate(_) => write!(f, "ContentEncoder::Deflate"),
ContentEncoder::Gzip(_) => write!(f, "ContentEncoder::Gzip"),
#[cfg(feature = "brotli")]
ContentEncoder::Br(_) => write!(f, "ContentEncoder::Br"),
}
}
Expand Down

0 comments on commit 0e21f46

Please sign in to comment.