Support zstd Content-Encoding#65479
Conversation
… middleware - Add ZstdCompressionProvider implementing ICompressionProvider - Add ZstdCompressionProviderOptions with configurable compression level - Make Zstd the default first compression provider (zstd > brotli > gzip) - Add comprehensive tests for zstd compression - Update sample application to demonstrate zstd usage - Update PublicAPI.Unshipped.txt with new public APIs Zstandard provides better compression efficiency than gzip with performance comparable to Brotli, and is now available via System.IO.Compression.ZstandardStream.
…n middleware - Add ZstdDecompressionProvider implementing IDecompressionProvider - Add zstd as the default first decompression provider - Add zstd test cases to existing test suite - Update RequestDecompressionOptions to include zstd provider Zstandard decompression is now available alongside gzip, deflate, and brotli.
There was a problem hiding this comment.
Pull request overview
Adds native Zstandard (zstd) support to ASP.NET Core request decompression and response compression middleware, including new providers/options, updated defaults, and expanded test coverage to validate encoding negotiation and end-to-end compression/decompression behavior.
Changes:
- Introduces
ZstandardCompressionProvider(+ options) for response compression and makes it the first default provider (preferred when available). - Introduces
ZstandardDecompressionProviderand adds"zstd"to the default request decompression providers. - Updates samples, tests, and public API baselines to reflect the new encoding.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Middleware/ResponseCompression/test/ResponseCompressionMiddlewareTest.cs | Adds zstd negotiation tests and zstd decompression verification for response bodies. |
| src/Middleware/ResponseCompression/src/ZstandardCompressionProviderOptions.cs | Adds public options type for configuring zstd compression behavior. |
| src/Middleware/ResponseCompression/src/ZstandardCompressionProvider.cs | Adds public response compression provider implementation for Content-Encoding: zstd. |
| src/Middleware/ResponseCompression/src/ResponseCompressionProvider.cs | Changes default provider order to prefer zstd when no providers are configured. |
| src/Middleware/ResponseCompression/src/PublicAPI.Unshipped.txt | Registers new public API surface for zstd provider/options. |
| src/Middleware/ResponseCompression/sample/Startup.cs | Demonstrates configuring and registering the zstd response compression provider. |
| src/Middleware/RequestDecompression/test/RequestDecompressionOptionsTests.cs | Updates default provider assertions to include zstd. |
| src/Middleware/RequestDecompression/test/RequestDecompressionMiddlewareTests.cs | Adds end-to-end request decompression test for Content-Encoding: zstd. |
| src/Middleware/RequestDecompression/test/DefaultRequestDecompressionProviderTests.cs | Adds case-insensitivity coverage for zstd stream selection. |
| src/Middleware/RequestDecompression/src/ZstandardDecompressionProvider.cs | Adds internal provider implementation for zstd request decompression. |
| src/Middleware/RequestDecompression/src/RequestDecompressionOptions.cs | Registers "zstd" in default decompression providers dictionary. |
src/Middleware/ResponseCompression/src/ResponseCompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/ResponseCompression/src/ResponseCompressionProvider.cs
Outdated
Show resolved
Hide resolved
src/Middleware/ResponseCompression/src/ZstandardCompressionProviderOptions.cs
Outdated
Show resolved
Hide resolved
Updated comment to clarify the purpose of the factory for resolving default compression providers.
| { | ||
| private static readonly ZstandardCompressionOptions DefaultZstandardCompressionOptions = new() | ||
| { | ||
| Quality = 1 // Fastest compression level |
There was a problem hiding this comment.
I think we should just use the defaults from the underlying zstd library.
https://source.dot.net/#System.IO.Compression.Zstandard/System/IO/Compression/ZstandardCompressionOptions.cs,37
Value 0 indicates the implementation-defined default quality.
There was a problem hiding this comment.
Agreed.
Not aligned with what is done on GZip or Brotli side where the fastest compression level is used by default, but more intuitive from an option point of view.
| { | ||
| get | ||
| { | ||
| yield return new EncodingTestData("zstd", expectedBodyLength: 27); |
There was a problem hiding this comment.
We should move off of hardcoding the compressed size in tests 😆
Don't need to do it in this PR though since it's not caused by the PR.
There was a problem hiding this comment.
Good news! These sizes are not used in any test.
I have just removed them.
|
/ba-g unrelated failures |
|
Thanks @manandre! |
Support zstd Content-Encoding
This pull request adds support for the Zstandard (zstd) compression algorithm to both the request decompression and response compression middleware. It introduces new provider classes, updates configuration and test files, and ensures Zstandard is used as the default and preferred encoding when available.
Zstandard Compression and Decompression Support
ZstandardDecompressionProviderand registered it as a default provider inRequestDecompressionOptions, enabling decompression of requests withContent-Encoding: zstd.ZstandardCompressionProviderandZstandardCompressionProviderOptionsfor response compression, and registered them as default providers.Configuration and Sample Usage
Startup.csto configure Zstandard compression options and add the new provider to the response compression pipeline.Testing and Verification
Accept-Encodingheaders and wildcards.Public API Changes
Fixes #50643