Skip to content

Http Request Compression#130082

Open
iremyux wants to merge 16 commits into
dotnet:mainfrom
iremyux:46944-request-compression
Open

Http Request Compression#130082
iremyux wants to merge 16 commits into
dotnet:mainfrom
iremyux:46944-request-compression

Conversation

@iremyux

@iremyux iremyux commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Adds built-in support for compressing HTTP request content by introducing three new HttpContent types. 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

Copilot AI review requested due to automatic review settings July 1, 2026 14:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and ZstandardCompressedContent public HttpContent types.
  • Factors common header/serialization logic into an internal CompressedContentCore.
  • Updates System.Net.Http source/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.

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs Outdated
Comment thread src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs Outdated
Comment thread src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs Outdated
Copilot AI review requested due to automatic review settings July 7, 2026 11:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs Outdated
Copilot AI review requested due to automatic review settings July 7, 2026 21:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs Outdated
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) { }
@iremyux iremyux marked this pull request as ready for review July 8, 2026 18:16
Copilot AI review requested due to automatic review settings July 8, 2026 18:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.

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 thread src/libraries/System.Net.Http/tests/FunctionalTests/CompressedContentTest.cs Outdated
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) { }
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 9, 2026 10:34
This reverts commit fb074ac.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/ZstandardCompressedContent.cs Outdated
Comment thread src/libraries/System.Net.Http/ref/System.Net.Http.cs
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; }
}
Copilot AI review requested due to automatic review settings July 9, 2026 10:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

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) { }
Copilot AI review requested due to automatic review settings July 9, 2026 21:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

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'" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants