Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Erhardt <eric.erhardt@microsoft.com>2022-11-11 14:24:34 +0300
committerGitHub <noreply@github.com>2022-11-11 14:24:34 +0300
commit35bec28c611a75647b8895fd6c892ea46030dca6 (patch)
tree8b9e5c6fb05185abf78b9326baa229149e272ce2
parent1980c7b5e836296a37000440f4a3903576d11311 (diff)
Allow DecompressionHandler to be trimmed when the application isn't using AutomaticDecompression in HttpClient. (#78198)
This allow for the Brotli compression code to be trimmed in a NativeAOT app that uses HttpClient, which is about 1 MB of size savings on Linux.
-rw-r--r--src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs12
-rw-r--r--src/libraries/System.Net.Http/tests/TrimmingTests/DecompressionHandlerTrimmedTest.cs37
-rw-r--r--src/libraries/System.Net.Http/tests/TrimmingTests/System.Net.Http.TrimmingTests.proj1
3 files changed, 49 insertions, 1 deletions
diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs
index 1e89bfc19de..3553e089055 100644
--- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs
+++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs
@@ -18,6 +18,7 @@ namespace System.Net.Http
{
private readonly HttpConnectionSettings _settings = new HttpConnectionSettings();
private HttpMessageHandlerStage? _handler;
+ private Func<HttpConnectionSettings, HttpMessageHandlerStage, HttpMessageHandlerStage>? _decompressionHandlerFactory;
private bool _disposed;
private void CheckDisposedOrStarted()
@@ -62,6 +63,7 @@ namespace System.Net.Http
set
{
CheckDisposedOrStarted();
+ EnsureDecompressionHandlerFactory();
_settings._automaticDecompression = value;
}
}
@@ -511,7 +513,8 @@ namespace System.Net.Http
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.
@@ -523,6 +526,13 @@ namespace System.Net.Http
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);
+ }
+
protected internal override HttpResponseMessage Send(HttpRequestMessage request,
CancellationToken cancellationToken)
{
diff --git a/src/libraries/System.Net.Http/tests/TrimmingTests/DecompressionHandlerTrimmedTest.cs b/src/libraries/System.Net.Http/tests/TrimmingTests/DecompressionHandlerTrimmedTest.cs
new file mode 100644
index 00000000000..56df6f10052
--- /dev/null
+++ b/src/libraries/System.Net.Http/tests/TrimmingTests/DecompressionHandlerTrimmedTest.cs
@@ -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);
+}
diff --git a/src/libraries/System.Net.Http/tests/TrimmingTests/System.Net.Http.TrimmingTests.proj b/src/libraries/System.Net.Http/tests/TrimmingTests/System.Net.Http.TrimmingTests.proj
index d1de6e68e34..ca55774a5ac 100644
--- a/src/libraries/System.Net.Http/tests/TrimmingTests/System.Net.Http.TrimmingTests.proj
+++ b/src/libraries/System.Net.Http/tests/TrimmingTests/System.Net.Http.TrimmingTests.proj
@@ -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>