From d14e98b62b866e75c84baa19e36bf2d0bb7d49e7 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 25 Dec 2023 02:27:51 +0000 Subject: [PATCH] prevent hang when compressing Sized(0) bodies fixes #3229 --- actix-http/CHANGES.md | 4 ++++ actix-http/src/encoding/encoder.rs | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 3068b4ce2..053c4e512 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +## Fixed + +- Prevent hang when returning zero-sized response bodies through compression layer. + ## 3.5.0 ### Changed diff --git a/actix-http/src/encoding/encoder.rs b/actix-http/src/encoding/encoder.rs index e084aa564..180927ac6 100644 --- a/actix-http/src/encoding/encoder.rs +++ b/actix-http/src/encoding/encoder.rs @@ -50,10 +50,21 @@ impl Encoder { } } + fn empty() -> Self { + Encoder { + body: EncoderBody::Full { body: Bytes::new() }, + encoder: None, + fut: None, + eof: true, + } + } + pub fn response(encoding: ContentEncoding, head: &mut ResponseHead, body: B) -> Self { - // no need to compress an empty body - if matches!(body.size(), BodySize::None | BodySize::Sized(0)) { - return Self::none(); + // no need to compress empty bodies + match body.size() { + BodySize::None => return Self::none(), + BodySize::Sized(0) => return Self::empty(), + _ => {} } let should_encode = !(head.headers().contains_key(&CONTENT_ENCODING)