Skip to content

Commit

Permalink
prevent hang when compressing Sized(0) bodies
Browse files Browse the repository at this point in the history
fixes #3229
  • Loading branch information
robjtede committed Dec 25, 2023
1 parent f4851b3 commit d14e98b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions actix-http/CHANGES.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## Fixed

- Prevent hang when returning zero-sized response bodies through compression layer.

## 3.5.0

### Changed
Expand Down
17 changes: 14 additions & 3 deletions actix-http/src/encoding/encoder.rs
Expand Up @@ -50,10 +50,21 @@ impl<B: MessageBody> Encoder<B> {
}
}

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)
Expand Down

0 comments on commit d14e98b

Please sign in to comment.