-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Http Request Compression #130082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Http Request Compression #130082
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4d78ef3
Introduce CompressedContent class CompressionMethod enum
iremyux c1bfd59
Change the enum values to match DecompressionMethods
iremyux 8c99417
Change the enum values to match DecompressionMethods
iremyux e573485
Apply API changes
iremyux 2227ece
Remove _contentConsumed check
iremyux 887e18a
Optimize Content-Encoding header append
iremyux 5ed6d1a
Annotate Zstd and BrotliCompressedContent as unsupported on browser a…
iremyux f4c002a
Add tests
iremyux e3150d8
Add constructors with CompressionLevel
iremyux 268e1e6
Propagate context to the inner HttpContent in SerializeToStreamAsync
iremyux fb074ac
Remove endregion
iremyux cc09490
Revert "Remove endregion"
iremyux 97bc66a
Remove platform support precheck
iremyux 4c41953
Remove regions
iremyux c2512a3
Remove the standalone ctor
iremyux d6d2109
Alphabetical order on ref file
iremyux c515199
Merge branch 'dotnet:main' into 46944-request-compression
iremyux c4e7998
Generated ref file changes
iremyux d3bda4e
Address copilot code reviews
iremyux b1c72a2
Always use TryAddWithoutValidation
iremyux c6584a8
Make CompressedContentCore static
iremyux 81d7b77
Explicit check for CompressionLEvel
iremyux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
90 changes: 90 additions & 0 deletions
90
src/libraries/System.Net.Http/src/System/Net/Http/BrotliCompressedContent.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using System.IO.Compression; | ||
| using System.Runtime.Versioning; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| /// <summary> | ||
| /// Provides HTTP content that compresses the inner content using the Brotli content coding. | ||
| /// </summary> | ||
| [UnsupportedOSPlatform("browser")] | ||
| [UnsupportedOSPlatform("wasi")] | ||
| public sealed class BrotliCompressedContent : HttpContent | ||
| { | ||
| private const string Encoding = "br"; | ||
|
|
||
| private readonly HttpContent _content; | ||
| private readonly BrotliCompressionOptions? _compressionOptions; | ||
| private readonly CompressionLevel _compressionLevel; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the | ||
| /// provided content using the Brotli content coding at the specified compression level. | ||
| /// </summary> | ||
| /// <param name="content">The HTTP content to compress.</param> | ||
| /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or compression efficiency.</param> | ||
| public BrotliCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(content); | ||
| CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel)); | ||
|
|
||
| _compressionLevel = compressionLevel; | ||
| _content = content; | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the | ||
| /// provided content using the Brotli content coding and the specified compression options. | ||
| /// </summary> | ||
| /// <param name="content">The HTTP content to compress.</param> | ||
| /// <param name="compressionOptions">The options used to fine-tune the compression.</param> | ||
| public BrotliCompressedContent(HttpContent content, BrotliCompressionOptions compressionOptions) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(content); | ||
| ArgumentNullException.ThrowIfNull(compressionOptions); | ||
|
|
||
| _compressionOptions = compressionOptions; | ||
| _content = content; | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToken); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => | ||
| CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, CancellationToken.None); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellationToken); | ||
|
|
||
| protected internal override bool TryComputeLength(out long length) | ||
| { | ||
| // Compressed size is not known without actually compressing. | ||
| length = 0; | ||
| return false; | ||
| } | ||
|
|
||
| internal override bool AllowDuplex => false; | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _content.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private BrotliStream CreateCompressionStream(Stream outputStream) => | ||
| _compressionOptions is null | ||
| ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/libraries/System.Net.Http/src/System/Net/Http/CompressedContentCore.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using System.IO.Compression; | ||
| using System.Net.Http.Headers; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| /// <summary> | ||
| /// Shared helpers for the per-algorithm compressed content types. Provides header initialization, | ||
| /// argument validation, and serialization that copies the inner content through a compression stream. | ||
| /// </summary> | ||
| internal static class CompressedContentCore | ||
| { | ||
| public static void ValidateCompressionLevel(CompressionLevel compressionLevel, string paramName) | ||
| { | ||
| // Validate up front so an invalid enum value fails fast at construction time rather than | ||
| // deferring the exception to the serialization path when the request is being sent. | ||
| if (compressionLevel is not (CompressionLevel.Optimal or CompressionLevel.Fastest or CompressionLevel.NoCompression or CompressionLevel.SmallestSize)) | ||
| { | ||
| throw new ArgumentOutOfRangeException(paramName); | ||
| } | ||
| } | ||
|
|
||
| public static void InitializeHeaders(HttpContent target, HttpContent source, string encoding) | ||
| { | ||
| // Copy headers from the original content. | ||
| target.Headers.AddHeaders(source.Headers); | ||
|
|
||
|
iremyux marked this conversation as resolved.
|
||
| // Append our encoding to the Content-Encoding header (supports stacking per HTTP spec). | ||
| // Always use TryAddWithoutValidation so we append the new encoding without re-parsing or | ||
| // validating any existing Content-Encoding values the inner content may have added. | ||
| target.Headers.TryAddWithoutValidation(KnownHeaders.ContentEncoding.Descriptor, encoding); | ||
|
|
||
| // Remove Content-Length since we don't know the compressed size upfront. | ||
| target.Headers.ContentLength = null; | ||
| } | ||
|
|
||
| public static void SerializeToStream(HttpContent originalContent, Stream compressionStream, TransportContext? context, CancellationToken cancellationToken) | ||
| { | ||
| using (compressionStream) | ||
| { | ||
| originalContent.CopyTo(compressionStream, context, cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| public static async Task SerializeToStreamAsync(HttpContent originalContent, Stream compressionStream, TransportContext? context, CancellationToken cancellationToken) | ||
| { | ||
| await using (compressionStream.ConfigureAwait(false)) | ||
| { | ||
| await originalContent.CopyToAsync(compressionStream, context, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
src/libraries/System.Net.Http/src/System/Net/Http/GZipCompressedContent.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.IO; | ||
| using System.IO.Compression; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| /// <summary> | ||
| /// Provides HTTP content that compresses the inner content using the gzip content coding. | ||
| /// </summary> | ||
| public sealed class GZipCompressedContent : HttpContent | ||
| { | ||
| private const string Encoding = "gzip"; | ||
|
|
||
| private readonly HttpContent _content; | ||
| private readonly ZLibCompressionOptions? _compressionOptions; | ||
| private readonly CompressionLevel _compressionLevel; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GZipCompressedContent"/> class that compresses the | ||
| /// provided content using the gzip content coding at the specified compression level. | ||
| /// </summary> | ||
| /// <param name="content">The HTTP content to compress.</param> | ||
| /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or compression efficiency.</param> | ||
| public GZipCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal) | ||
| { | ||
|
iremyux marked this conversation as resolved.
|
||
| ArgumentNullException.ThrowIfNull(content); | ||
| CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel)); | ||
|
|
||
| _compressionLevel = compressionLevel; | ||
| _content = content; | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GZipCompressedContent"/> class that compresses the | ||
| /// provided content using the gzip content coding and the specified compression options. | ||
| /// </summary> | ||
| /// <param name="content">The HTTP content to compress.</param> | ||
| /// <param name="compressionOptions">The options used to fine-tune the compression.</param> | ||
| public GZipCompressedContent(HttpContent content, ZLibCompressionOptions compressionOptions) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(content); | ||
| ArgumentNullException.ThrowIfNull(compressionOptions); | ||
|
|
||
| _compressionOptions = compressionOptions; | ||
| _content = content; | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToken); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => | ||
| CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, CancellationToken.None); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellationToken); | ||
|
|
||
| protected internal override bool TryComputeLength(out long length) | ||
| { | ||
| // Compressed size is not known without actually compressing. | ||
| length = 0; | ||
| return false; | ||
| } | ||
|
|
||
| internal override bool AllowDuplex => false; | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _content.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private GZipStream CreateCompressionStream(Stream outputStream) => | ||
| _compressionOptions is null | ||
| ? new GZipStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new GZipStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.