Repository navigation
Description
Background and motivation
#123531 added DecompressionMethods.Zstandard, enabling automatic decompression of HTTP responses with Content-Encoding: zstd. The decompression streams are created internally with default settings; there is no way for an application to configure them.
Unlike gzip/deflate (32 KB window) and Brotli (≤ 16 MB window), Zstandard frames declare their own window size, and the decoder must allocate a back-reference buffer of that size. RFC 9659 caps this limit to 8 MB for zstd encoded http content.
The same knob already exists on the low-level System.IO.Compression API: ZstandardDecompressionOptions.MaxWindowLog and ZstandardStream(Stream, ZstandardDecompressionOptions, ...). The HTTP layer just needs to surface it for automatic decompression.
Existing workaround
Disable AutomaticDecompression for zstd, read the raw response stream, strip/track the Content-Encoding header manually, and wrap the stream in new ZstandardStream(responseStream, new ZstandardDecompressionOptions { MaxWindowLog = n }) yourself. This reimplements the content-coding handling (multiple/last encoding, header fixup) that DecompressionHandler already does, and loses the ergonomics of automatic decompression. It is sufficient functionally but is boilerplate-heavy and easy to get wrong.
Related: the automatic-decompression entry point is SocketsHttpHandler.AutomaticDecompression (DecompressionMethods flags), mirrored on HttpClientHandler.
API Proposal
Reuse the existing System.IO.Compression.ZstandardDecompressionOptions type (already used internally by DecompressionHandler), exposed via a new property on SocketsHttpHandler. This takes a new public dependency from the System.Net.Http ref assembly on System.IO.Compression, which is acceptable because the two libraries are already coupled and many users configure ZstandardDecompressionOptions directly.
namespace System.Net.Http;
public partial class SocketsHttpHandler
{
// EXISTING
// public DecompressionMethods AutomaticDecompression { get; set; }
// Options applied when automatically decompressing zstd-encoded responses
// (DecompressionMethods.Zstandard). null (default) uses default decompression settings.
public System.IO.Compression.ZstandardDecompressionOptions? ZstandardDecompressionOptions { get; set; }
}
public partial class HttpClientHandler
{
// EXISTING
// public DecompressionMethods AutomaticDecompression { get; set; }
// Forwards to the underlying SocketsHttpHandler. Can be omitted if we want this SocketsHttpHandler-only
public System.IO.Compression.ZstandardDecompressionOptions? ZstandardDecompressionOptions { get; set; }
}For reference, the reused type (already public, shipped in System.IO.Compression):
namespace System.IO.Compression;
public sealed partial class ZstandardDecompressionOptions
{
public ZstandardDecompressionOptions();
public int MaxWindowLog { get; set; } // 0 = default (windowLog 27 = 128 MB)
public ZstandardDictionary? Dictionary { get; set; }
}Prototype: not yet built (pending design sign-off).
API Usage
// Harden against decompression-bomb / large-allocation DoS from untrusted servers:
// cap the accepted zstd window at 2^24 = 16 MB.
var handler = new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.All,
ZstandardDecompressionOptions = new ZstandardDecompressionOptions
{
MaxWindowLog = 24
}
};
using var client = new HttpClient(handler);// Accept content compressed with a very large window (zstd --long=30):
var handler = new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.Zstandard,
ZstandardDecompressionOptions = new ZstandardDecompressionOptions { MaxWindowLog = 30 }
};Alternative Designs
-
HTTP-owned options bag that does not expose
System.IO.Compressiontypes and omits the HTTP-irrelevantDictionary:namespace System.Net.Http; public sealed class DecompressionOptions { public int ZstandardMaxWindowLog { get; set; } } public partial class SocketsHttpHandler { public DecompressionOptions? AutomaticDecompressionOptions { get; set; } }
- Pro: no public dependency on
System.IO.Compression; hidesDictionary, which has no meaning for the standardzstdHTTP content-encoding (no dictionary negotiation). - Con: new type to maintain; duplicates a concept the compression library already models. Rejected in favor of reuse, since users already work with
ZstandardDecompressionOptionsdirectly and the libraries are already coupled.
- Pro: no public dependency on
-
Scalar property on the handler:
public partial class SocketsHttpHandler { public int ZstandardMaxWindowLog { get; set; } }
- Pro: minimal; consistent with existing scalar limits (
MaxResponseHeadersLength,MaxResponseDrainSize). - Con: algorithm-specific name on the handler; not extensible.
- Pro: minimal; consistent with existing scalar limits (
-
Open questions for review:
ZstandardDecompressionOptions.Dictionaryis exposed but has no effect for HTTP automatic decompression (no dictionary content-encoding). Should setting it throw, be ignored, or be honored if/when a dictionary content-coding is supported? Proposed: ignored for now (documented).- Should
HttpClientHandleralso expose this? Proposed: yes —HttpClientHandler.ZstandardDecompressionOptionsforwards to the underlyingSocketsHttpHandler, mirroringAutomaticDecompression. (HttpClientHandler.AutomaticDecompressionalready delegates to_underlyingHandler.)
Risks
- Adding a public property is binary-compatible. No source-breaking overload-resolution risk.
- Adds a public ref dependency from
System.Net.HttponSystem.IO.Compression(today only the implementation references it). Accepted intentionally. Dictionaryon the reused options type is meaningless for HTTP and could confuse users (see open question above).- Behavior only applies on
SocketsHttpHandlerplatforms where zstd decompression is supported (not browser/wasi).
Usage in dotnet/runtime
-
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/DecompressionHandler.cs—ZstandardDecompressedContent.GetDecompressedStreamwould pass the configuredMaxWindowLoginto theZstandardStream/ZstandardDecompressionOptions. Options would be threaded throughHttpConnectionSettingsand the_decompressionHandlerFactoryinSocketsHttpHandler.cs. -
src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs— newZstandardDecompressionOptionsproperty forwarding to_underlyingHandler(theSocketsHttpHandler), mirroringAutomaticDecompression.
No other in-repo adoption sites (this is a new configuration surface, not a replacement for an existing pattern).
Note
This proposal was drafted with the assistance of GitHub Copilot.
Activity
dotnet-policy-service commentedon Jun 29, 2026
Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
fabiorubim740 commentedon Jun 29, 2026
rzikm commentedon Jun 30, 2026
Moving to future since RFC 9659 says that
Which makes API mostly unnecessary. I edited the proposal to reflect this
rzikm commentedon Jun 30, 2026
Since the only scenarios this would enable are outside of RFC, we don't plan to implement. Users who want non-RFC compliant behavior can still workaround it by decompressing it manually
Configure ZstandardStream decompression with MaxWindowLog=23 per RFC …
Configure ZstandardStream decompression with MaxWindowLog=23 per RFC …