Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("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 thread
iremyux marked this conversation as resolved.
Comment thread
iremyux marked this conversation as resolved.
Comment thread
iremyux marked this conversation as resolved.
Comment thread
iremyux marked this conversation as resolved.
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 thread
iremyux marked this conversation as resolved.
}
Comment thread
iremyux marked this conversation as resolved.
public partial class ByteArrayContent : System.Net.Http.HttpContent
Comment thread
iremyux marked this conversation as resolved.
{
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.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { }
public GZipCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.ZLibCompressionOptions compressionOptions) { }
protected override void Dispose(bool disposing) { }
Comment thread
iremyux marked this conversation as resolved.
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.UnsupportedOSPlatformAttribute("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("wasi")]
public sealed partial class ZstandardCompressedContent : System.Net.Http.HttpContent
{
public ZstandardCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.CompressionLevel compressionLevel = System.IO.Compression.CompressionLevel.Optimal) { }
public ZstandardCompressedContent(System.Net.Http.HttpContent content, System.IO.Compression.ZstandardCompressionOptions compressionOptions) { }
Comment thread
iremyux marked this conversation as resolved.
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 @@ -23,9 +23,11 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\Net\Http\BrotliCompressedContent.cs" />
<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\CompressedContentCore.cs" />
<Compile Include="System\Net\Http\DelegatingHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandler.cs" />
<Compile Include="System\Net\Http\DiagnosticsHandlerLoggingStrings.cs" />
Expand All @@ -34,6 +36,7 @@
<Compile Include="System\Net\Http\EmptyReadStream.cs" />
<Compile Include="System\Net\Http\FormUrlEncodedContent.cs" />
<Compile Include="System\Net\Http\GlobalHttpSettings.cs" />
<Compile Include="System\Net\Http\GZipCompressedContent.cs" />
<Compile Include="System\Net\Http\HeaderEncodingSelector.cs" />
<Compile Include="System\Net\Http\Headers\KnownHeader.cs" />
<Compile Include="System\Net\Http\Headers\HttpHeaderType.cs" />
Expand Down Expand Up @@ -75,6 +78,7 @@
<Compile Include="System\Net\Http\StreamContent.cs" />
<Compile Include="System\Net\Http\StreamToStreamCopy.cs" />
<Compile Include="System\Net\Http\StringContent.cs" />
<Compile Include="System\Net\Http\ZstandardCompressedContent.cs" />
<Compile Include="System\Net\Http\Headers\AuthenticationHeaderValue.cs" />
<Compile Include="System\Net\Http\Headers\BaseHeaderParser.cs" />
<Compile Include="System\Net\Http\Headers\ByteArrayHeaderParser.cs" />
Expand Down
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);
}
}
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);

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