From 2fe518995491da430c17316f3b9a2741cacf22cd Mon Sep 17 00:00:00 2001 From: Paul <135009186+phooijenga@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:57:19 +0100 Subject: [PATCH 1/2] Do not encode zero-sized response bodies (#3199) * Do not encode zero-sized response bodies * Test empty response remains empty after compression --- actix-http/CHANGES.md | 4 +++ actix-http/src/encoding/encoder.rs | 2 +- actix-web/src/middleware/compress.rs | 45 +++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 318df5a4d..fc49bc931 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,6 +6,10 @@ - Updated `zstd` dependency to `0.13`. +### Fixed + +- Do not encode zero-sized response bodies + ## 3.4.0 ### Added diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index 527bfebaa..e084aa564 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -52,7 +52,7 @@ impl Encoder { pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self { // no need to compress an empty body - if matches!(body.size(), BodySize::None) { + if matches!(body.size(), BodySize::None | BodySize::Sized(0)) { return Self::none(); } diff --git a/actix-web/src/middleware/compress.rs b/actix-web/src/middleware/compress.rs index a55b46264..8ff518cd3 100644 --- a/actix-web/src/middleware/compress.rs +++ b/actix-web/src/middleware/compress.rs @@ -373,7 +373,7 @@ mod tests { .default_service(web::to(move || { HttpResponse::Ok() .insert_header((header::VARY, "x-test")) - .finish() + .body(TEXT_DATA) })) }) .await; @@ -429,4 +429,47 @@ mod tests { assert_successful_identity_res_with_content_type(&res, "image/jpeg"); assert_eq!(test::read_body(res).await, TEXT_DATA.as_bytes()); } + + #[actix_rt::test] + async fn prevents_compression_empty() { + let app = test::init_service({ + App::new() + .wrap(Compress::default()) + .default_service(web::to(move || HttpResponse::Ok().finish())) + }) + .await; + + let req = test::TestRequest::default() + .insert_header((header::ACCEPT_ENCODING, "gzip")) + .to_request(); + let res = test::call_service(&app, req).await; + assert_eq!(res.status(), StatusCode::OK); + assert!(!res.headers().contains_key(header::CONTENT_ENCODING)); + assert!(test::read_body(res).await.is_empty()); + } +} + +#[cfg(feature = "compress-brotli")] +#[cfg(test)] +mod tests_brotli { + use super::*; + use crate::{test, web, App}; + + #[actix_rt::test] + async fn prevents_compression_empty() { + let app = test::init_service({ + App::new() + .wrap(Compress::default()) + .default_service(web::to(move || HttpResponse::Ok().finish())) + }) + .await; + + let req = test::TestRequest::default() + .insert_header((header::ACCEPT_ENCODING, "br")) + .to_request(); + let res = test::call_service(&app, req).await; + assert_eq!(res.status(), StatusCode::OK); + assert!(!res.headers().contains_key(header::CONTENT_ENCODING)); + assert!(test::read_body(res).await.is_empty()); + } } From e95c8fe5a6d466ba674ea2ea35c0c4d18e1bb9d0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 02:54:42 +0000 Subject: [PATCH 2/2] build(deps): bump taiki-e/install-action from 2.21.17 to 2.21.19 (#3205) Bumps [taiki-e/install-action](https://github.com/taiki-e/install-action) from 2.21.17 to 2.21.19. - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/taiki-e/install-action/compare/v2.21.17...v2.21.19) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-post-merge.yml | 6 +++--- .github/workflows/ci.yml | 2 +- .github/workflows/coverage.yml | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci-post-merge.yml b/.github/workflows/ci-post-merge.yml index 86a09e0cd..bff9908e5 100644 --- a/.github/workflows/ci-post-merge.yml +++ b/.github/workflows/ci-post-merge.yml @@ -45,7 +45,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install cargo-hack - uses: taiki-e/install-action@v2.21.17 + uses: taiki-e/install-action@v2.21.19 with: tool: cargo-hack @@ -85,7 +85,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.5.0 - name: Install cargo-hack - uses: taiki-e/install-action@v2.21.17 + uses: taiki-e/install-action@v2.21.19 with: tool: cargo-hack @@ -106,7 +106,7 @@ jobs: uses: actions-rust-lang/setup-rust-toolchain@v1.5.0 - name: Install nextest - uses: taiki-e/install-action@v2.21.17 + uses: taiki-e/install-action@v2.21.19 with: tool: nextest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc8026f96..f9e93dddf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,7 @@ jobs: toolchain: ${{ matrix.version.version }} - name: Install cargo-hack - uses: taiki-e/install-action@v2.21.17 + uses: taiki-e/install-action@v2.21.19 with: tool: cargo-hack diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 4e02735c7..015cc7968 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -23,7 +23,7 @@ jobs: components: llvm-tools-preview - name: Install cargo-llvm-cov - uses: taiki-e/install-action@v2.21.17 + uses: taiki-e/install-action@v2.21.19 with: tool: cargo-llvm-cov