Skip to content

[API Proposal]: Configure Zstandard automatic decompression options on SocketsHttpHandler/HttpClientHandler #129986

Closed

Description

@rzikm

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

  1. HTTP-owned options bag that does not expose System.IO.Compression types and omits the HTTP-irrelevant Dictionary:

    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; hides Dictionary, which has no meaning for the standard zstd HTTP 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 ZstandardDecompressionOptions directly and the libraries are already coupled.
  2. 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.
  3. Open questions for review:

    • ZstandardDecompressionOptions.Dictionary is 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 HttpClientHandler also expose this? Proposed: yes — HttpClientHandler.ZstandardDecompressionOptions forwards to the underlying SocketsHttpHandler, mirroring AutomaticDecompression. (HttpClientHandler.AutomaticDecompression already 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.Http on System.IO.Compression (today only the implementation references it). Accepted intentionally.
  • Dictionary on the reused options type is meaningless for HTTP and could confuse users (see open question above).
  • Behavior only applies on SocketsHttpHandler platforms 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.GetDecompressedStream would pass the configured MaxWindowLog into the ZstandardStream/ZstandardDecompressionOptions. Options would be threaded through HttpConnectionSettings and the _decompressionHandlerFactory in SocketsHttpHandler.cs.

  • src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs — new ZstandardDecompressionOptions property forwarding to _underlyingHandler (the SocketsHttpHandler), mirroring AutomaticDecompression.

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

  1. added
    api-suggestion
    on Jun 29, 2026
  2. added
    untriaged
    on Jun 29, 2026
  3. added
    api-suggestion
    and removed
    api-suggestion
    untriaged
    on Jun 29, 2026
  4. added theissue type on Jun 29, 2026
  5. added this to the 11.0.0 milestone on Jun 29, 2026
  6. dotnet-policy-service commented on Jun 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  7. fabiorubim740 commented on Jun 29, 2026

    @fabiorubim740
  8. modified the milestones: 11.0.0, Future on Jun 30, 2026
  9. rzikm commented on Jun 30, 2026

    @rzikm
    MemberAuthor

    Moving to future since RFC 9659 says that

    To ensure interoperability, when using the "zstd" content coding, decoders MUST support a Window_Size of up to and including 8 MB, and encoders MUST NOT generate frames requiring a Window_Size larger than 8 MB.

    Which makes API mostly unnecessary. I edited the proposal to reflect this

  10. rzikm commented on Jun 30, 2026

    @rzikm
    MemberAuthor

    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

  11. locked and limited conversation to collaborators on Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Net.Http

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions