Expand description
HTTP response compression middleware for Tower.
This crate provides a Tower layer that automatically compresses HTTP response
bodies using Zstd, Brotli, Gzip, or Deflate based on the client’s Accept-Encoding header.
§Example
ⓘ
use http_response_compression::CompressionLayer;
use tower::ServiceBuilder;
let service = ServiceBuilder::new()
.layer(CompressionLayer::new())
.service(my_service);§Compression Rules
The middleware will not compress responses when:
- No supported
Accept-Encodingis present in the request Content-Encodingheader is already setContent-Rangeheader is present (range responses)Content-Typestarts withimage/(exceptimage/svg+xml)Content-Typestarts withapplication/grpc(exceptapplication/grpc-web)Content-Lengthis below the minimum size threshold (default: 860 bytes)
The middleware will always flush after each chunk when:
X-Accel-Buffering: noheader is presentContent-Typeistext/event-streamContent-Typestarts withapplication/grpc-web
§Response Modifications
When compression is applied:
Content-Encodingheader is set to the codec usedContent-Lengthheader is removed (compressed size is unknown)Accept-Rangesheader is removedVaryheader includesAccept-Encoding
Structs§
- Compression
Layer - A Tower layer that compresses HTTP response bodies.
- Compression
Service - A Tower service that compresses HTTP response bodies.
- Response
Future - Future for compression service responses.
Enums§
- Compression
Body - A response body that may be compressed.