Http Request Compression#130082
Open
iremyux wants to merge 16 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds new public HttpContent wrappers in System.Net.Http that compress the request body using gzip, Brotli, or Zstandard, with a shared internal helper to handle header copying and serialization.
Changes:
- Introduces
GZipCompressedContent,BrotliCompressedContent, andZstandardCompressedContentpublicHttpContenttypes. - Factors common header/serialization logic into an internal
CompressedContentCore. - Updates
System.Net.Httpsource/ref projects and ref surface to include the new APIs and compression dependencies.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Net.Http/src/System/Net/Http/BrotliCompressedContent.cs | New HttpContent wrapper that applies Brotli request compression. |
| src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs | Internal helper for header initialization and (a)synchronous serialization via compression streams. |
| src/libraries/System.Net.Http/src/System/Net/Http/GZipCompressedContent.cs | New HttpContent wrapper that applies gzip request compression. |
| src/libraries/System.Net.Http/src/System/Net/Http/ZstandardCompressedContent.cs | New HttpContent wrapper that applies Zstandard request compression. |
| src/libraries/System.Net.Http/src/System.Net.Http.csproj | Includes the new source files in the build. |
| src/libraries/System.Net.Http/ref/System.Net.Http.csproj | Adds ref-time dependencies on compression ref projects needed by the new public API signatures. |
| src/libraries/System.Net.Http/ref/System.Net.Http.cs | Adds the new public API surface for the three compressed content types. |
MihaZupan
reviewed
Jul 2, 2026
MihaZupan
reviewed
Jul 2, 2026
Comment on lines
+22
to
+27
| public sealed partial class BrotliCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.BrotliCompressionOptions compressionOptions) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } | ||
| protected override void Dispose(bool disposing) { } |
Comment on lines
+31
to
+41
| // Append our encoding to the Content-Encoding header (supports stacking per HTTP spec). | ||
| // The common case is that the inner content has no Content-Encoding, so take the | ||
| // allocation-free fast path and only materialize the collection when we need to stack. | ||
| if (target.Headers.Contains(KnownHeaders.ContentEncoding.Descriptor)) | ||
| { | ||
| target.Headers.ContentEncoding.Add(encoding); | ||
| } | ||
| else | ||
| { | ||
| target.Headers.TryAddWithoutValidation(KnownHeaders.ContentEncoding.Descriptor, encoding); | ||
| } |
Comment on lines
+97
to
+107
| private BrotliStream CreateCompressionStream(Stream outputStream) | ||
| { | ||
| if (OperatingSystem.IsBrowser() || OperatingSystem.IsWasi()) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| } | ||
|
|
||
| return _compressionOptions is null | ||
| ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } |
Comment on lines
+97
to
+107
| private ZstandardStream CreateCompressionStream(Stream outputStream) | ||
| { | ||
| if (OperatingSystem.IsBrowser() || OperatingSystem.IsWasi()) | ||
| { | ||
| throw new PlatformNotSupportedException(); | ||
| } | ||
|
|
||
| return _compressionOptions is null | ||
| ? new ZstandardStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new ZstandardStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } |
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) => Task.CompletedTask; |
| } | ||
| } | ||
|
|
||
| #region Helpers |
Comment on lines
+22
to
+27
| public sealed partial class BrotliCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.BrotliCompressionOptions compressionOptions) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } | ||
| protected override void Dispose(bool disposing) { } |
This was referenced Jul 9, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This reverts commit fb074ac.
Comment on lines
+33
to
+56
| public sealed partial class GZipCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| public GZipCompressedContent(System.Net.Http.HttpContent content) { } | ||
| public GZipCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.ZLibCompressionOptions compressionOptions) { } | ||
| public GZipCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } | ||
| protected override void Dispose(bool disposing) { } | ||
| protected override void SerializeToStream(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context) { throw null; } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { throw null; } | ||
| protected internal override bool TryComputeLength(out long length) { throw null; } | ||
| } | ||
| [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] | ||
| [System.Runtime.Versioning.UnsupportedOSPlatform("wasi")] | ||
| public sealed partial class ZstandardCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| public ZstandardCompressedContent(System.Net.Http.HttpContent content) { } | ||
| public ZstandardCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.ZstandardCompressionOptions compressionOptions) { } | ||
| public ZstandardCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } | ||
| protected override void Dispose(bool disposing) { } | ||
| protected override void SerializeToStream(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context) { throw null; } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { throw null; } | ||
| protected internal override bool TryComputeLength(out long length) { throw null; } | ||
| } |
Comment on lines
+20
to
+26
| [System.Runtime.Versioning.UnsupportedOSPlatform("browser")] | ||
| [System.Runtime.Versioning.UnsupportedOSPlatform("wasi")] | ||
| public sealed partial class BrotliCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.BrotliCompressionOptions compressionOptions) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } |
Comment on lines
+29
to
33
| <Compile Include="System\Net\Http\BrotliCompressedContent.cs" /> | ||
| <Compile Include="System\Net\Http\CompressedContentCore.cs" /> | ||
| <Compile Include="System\Net\Http\GZipCompressedContent.cs" /> | ||
| <Compile Include="System\Net\Http\ZstandardCompressedContent.cs" /> | ||
| <Compile Include="System\Net\Http\DelegatingHandler.cs" /> |
Comment on lines
+13
to
+15
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.BrotliCompressionOptions compressionOptions) { } | ||
| public BrotliCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { } | ||
| protected override void Dispose(bool disposing) { } |
| Link="Common\System\Threading\Tasks\TaskTimeoutExtensions.cs" /> | ||
| <Compile Include="AssemblyInfo.cs" /> | ||
| <Compile Include="ByteArrayContentTest.cs" /> | ||
| <Compile Include="CompressedContentTest.cs" Condition="'$(TargetPlatformIdentifier)' != 'browser' and '$(TargetPlatformIdentifier)' != 'wasi'" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds built-in support for compressing HTTP request content by introducing three new
HttpContenttypes. Every type offers a default constructor plus an options-based overload for tuning compression, with shared serialization logic factored into an internal helper.Implements #46944