-
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
base: main
Are you sure you want to change the base?
Http Request Compression #130082
Changes from all commits
4d78ef3
c1bfd59
8c99417
e573485
2227ece
887e18a
5ed6d1a
f4c002a
e3150d8
268e1e6
fb074ac
cc09490
97bc66a
4c41953
c2512a3
d6d2109
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,18 @@ | |
|
|
||
| namespace System.Net.Http | ||
| { | ||
| [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, 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
+11
to
+15
iremyux marked this conversation as resolved.
Comment on lines
+13
to
+15
|
||
| 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; } | ||
| } | ||
| public partial class ByteArrayContent : System.Net.Http.HttpContent | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you generating the ref sources or editing manually?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. edited here with copilot - i'll check the diff again
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be generated with the tool we have for this, see https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/updating-ref-source.md Editting manually (or with AI) just causes more diffs for anyone who will use the ref source generator after you. |
||
| { | ||
| public ByteArrayContent(byte[] content) { } | ||
|
|
@@ -42,6 +54,16 @@ public FormUrlEncodedContent( | |
| >> nameValueCollection) : base (default(byte[])) { } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { throw null; } | ||
| } | ||
| public sealed partial class GZipCompressedContent : System.Net.Http.HttpContent | ||
| { | ||
| 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; } | ||
| } | ||
| public delegate System.Text.Encoding? HeaderEncodingSelector<TContext>(string headerName, TContext context); | ||
| public partial class HttpClient : System.Net.Http.HttpMessageInvoker | ||
| { | ||
|
|
@@ -501,6 +523,18 @@ public StringContent(string content, System.Text.Encoding? encoding, System.Net. | |
| public StringContent(string content, System.Text.Encoding? encoding, string? mediaType) : base (default(byte[])) { } | ||
| protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { 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, 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; } | ||
| } | ||
| } | ||
| namespace System.Net.Http.Headers | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // 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 CompressedContentCore _core; | ||
| 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); | ||
|
|
||
| _compressionLevel = compressionLevel; | ||
| _core = new CompressedContentCore(content, CreateCompressionStream); | ||
| 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; | ||
| _core = new CompressedContentCore(content, CreateCompressionStream); | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| _core.SerializeToStream(stream, context, cancellationToken); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => | ||
| _core.SerializeToStreamAsync(stream, context, CancellationToken.None); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| _core.SerializeToStreamAsync(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) | ||
| { | ||
| _core.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private BrotliStream CreateCompressionStream(Stream outputStream) => | ||
| _compressionOptions is null | ||
| ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // 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.Net.Http.Headers; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace System.Net.Http | ||
| { | ||
| /// <summary> | ||
| /// Shared implementation for the per-algorithm compressed content types. Holds the inner content and | ||
| /// drives serialization through a caller-provided factory that wraps the output stream in a compression stream. | ||
| /// </summary> | ||
| internal sealed class CompressedContentCore | ||
|
iremyux marked this conversation as resolved.
|
||
| { | ||
| private readonly HttpContent _originalContent; | ||
| private readonly Func<Stream, Stream> _createCompressionStream; | ||
|
|
||
| public CompressedContentCore(HttpContent originalContent, Func<Stream, Stream> createCompressionStream) | ||
| { | ||
| _originalContent = originalContent; | ||
| _createCompressionStream = createCompressionStream; | ||
| } | ||
|
|
||
| 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). | ||
| // 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); | ||
| } | ||
|
|
||
| // Remove Content-Length since we don't know the compressed size upfront. | ||
| target.Headers.ContentLength = null; | ||
| } | ||
|
|
||
| public void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) | ||
| { | ||
| using Stream compressionStream = _createCompressionStream(stream); | ||
| _originalContent.CopyTo(compressionStream, context, cancellationToken); | ||
| } | ||
|
|
||
| public async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) | ||
| { | ||
| Stream compressionStream = _createCompressionStream(stream); | ||
| await using (compressionStream.ConfigureAwait(false)) | ||
| { | ||
| await _originalContent.CopyToAsync(compressionStream, context, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
iremyux marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public void Dispose() => _originalContent.Dispose(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // 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 CompressedContentCore _core; | ||
| 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) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(content); | ||
|
|
||
| _compressionLevel = compressionLevel; | ||
| _core = new CompressedContentCore(content, CreateCompressionStream); | ||
| 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; | ||
| _core = new CompressedContentCore(content, CreateCompressionStream); | ||
| CompressedContentCore.InitializeHeaders(this, content, Encoding); | ||
| } | ||
|
|
||
| protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| _core.SerializeToStream(stream, context, cancellationToken); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) => | ||
| _core.SerializeToStreamAsync(stream, context, CancellationToken.None); | ||
|
|
||
| protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) => | ||
| _core.SerializeToStreamAsync(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) | ||
| { | ||
| _core.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private GZipStream CreateCompressionStream(Stream outputStream) => | ||
| _compressionOptions is null | ||
| ? new GZipStream(outputStream, _compressionLevel, leaveOpen: true) | ||
| : new GZipStream(outputStream, _compressionOptions, leaveOpen: true); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.