Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed class SocketsHttpHandler : HttpMessageHandler
{
private readonly HttpConnectionSettings _settings = new HttpConnectionSettings();
private HttpMessageHandlerStage? _handler;
private Func<HttpConnectionSettings, HttpMessageHandlerStage, HttpMessageHandlerStage>? _decompressionHandlerFactory;
private bool _disposed;

private void CheckDisposedOrStarted()
Expand Down Expand Up @@ -62,6 +63,7 @@ public DecompressionMethods AutomaticDecompression
set
{
CheckDisposedOrStarted();
EnsureDecompressionHandlerFactory();
_settings._automaticDecompression = value;
}
}
Expand Down Expand Up @@ -511,7 +513,8 @@ private HttpMessageHandlerStage SetupHandlerChain()

if (settings._automaticDecompression != DecompressionMethods.None)
{
handler = new DecompressionHandler(settings._automaticDecompression, handler);
Debug.Assert(_decompressionHandlerFactory is not null);
handler = _decompressionHandlerFactory(settings, handler);
}

// Ensure a single handler is used for all requests.
Expand All @@ -523,6 +526,13 @@ private HttpMessageHandlerStage SetupHandlerChain()
return _handler;
}

// Allows for DecompressionHandler (and its compression dependencies) to be trimmed when
// AutomaticDecompression is not being used.
private void EnsureDecompressionHandlerFactory()
{
_decompressionHandlerFactory ??= (settings, handler) => new DecompressionHandler(settings._automaticDecompression, handler);

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.

Nit: just to make sure we don't accidentally create a closure

Suggested change
_decompressionHandlerFactory ??= (settings, handler) => new DecompressionHandler(settings._automaticDecompression, handler);
_decompressionHandlerFactory ??= static (settings, handler) => new DecompressionHandler(settings._automaticDecompression, handler);

}

protected internal override HttpResponseMessage Send(HttpRequestMessage request,
CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Net.Http;
using System.Runtime.Versioning;
using System.Threading.Tasks;

class Program
{
static async Task<int> Main(string[] args)
{
using HttpClient client = new();

// send a request, but ignore its result
try
{
await client.GetAsync("https://www.microsoft.com");
}
catch { }

Type decompressionHandler = GetHttpType("System.Net.Http.DecompressionHandler");

// DecompressionHandler should have been trimmed since AutomaticDecompression was not used
if (decompressionHandler is not null)
{
return -1;
}

return 100;
}

// The intention of this method is to ensure the trimmer doesn't preserve the Type.
private static Type GetHttpType(string name) =>
typeof(HttpClient).Assembly.GetType(name, throwOnError: false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<ItemGroup>
<TestConsoleAppSourceFiles Include="DecompressionHandlerTrimmedTest.cs" />
<TestConsoleAppSourceFiles Include="HttpClientTest.cs">
<SkipOnTestRuntimes>browser-wasm</SkipOnTestRuntimes>
</TestConsoleAppSourceFiles>
Expand Down