Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/libraries/System.Net.Http/ref/System.Net.Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Comment thread
iremyux marked this conversation as resolved.
protected override void Dispose(bool disposing) { }
Comment on lines +11 to +15
Comment thread
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

@ManickaP ManickaP Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you generating the ref sources or editing manually?

@iremyux iremyux Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

edited here with copilot - i'll check the diff again

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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) { }
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Net.Http/ref/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Compression\ref\System.IO.Compression.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Compression.Brotli\ref\System.IO.Compression.Brotli.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Primitives\ref\System.Net.Primitives.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Sockets\ref\System.Net.Sockets.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Security\ref\System.Net.Security.csproj" />
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<Compile Include="System\Net\Http\ByteArrayContent.cs" />
<Compile Include="System\Net\Http\CancellationHelper.cs" />
<Compile Include="System\Net\Http\ClientCertificateOption.cs" />
<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 +29 to 33
<Compile Include="System\Net\Http\DiagnosticsHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandlerLoggingStrings.cs" />
Expand Down
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
Comment thread
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);

Comment thread
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);
}
Comment thread
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);
}
}
Loading
Loading