Skip to content
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

Allow DecompressionHandler to be trimmed when the application isn't using AutomaticDecompression in HttpClient. #78198

Merged
merged 1 commit into from Nov 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
@@ -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);
}
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