From 429485cfc62766ffa143df5ea4de1d73ffbe6dbb Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 12 May 2021 22:13:40 -0700 Subject: [PATCH 1/7] Use platform guard attributes --- eng/Versions.props | 2 +- .../src/ConsoleLoggerProvider.cs | 10 ++++++++-- .../SocketsHttpHandler/HttpConnectionPool.cs | 20 +++++++++---------- .../Tasks/ThreadPoolTaskScheduler.cs | 2 -- .../src/System/Threading/Thread.cs | 1 + 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index ae34e9f29851a..2b9e23ccb0ca6 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -47,7 +47,7 @@ 3.8.0 - 6.0.0-preview5.21219.2 + 6.0.0-preview5.21262.4 3.10.0-2.final 3.10.0-2.final diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index a7ac0f7f86d87..bb24ed8f28aac 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -47,8 +47,8 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu _optionsReloadToken = _options.OnChange(ReloadLoggerOptions); _messageQueue = new ConsoleLoggerProcessor(); - // TODO update when https://github.com/dotnet/runtime/issues/44922 implemented - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || DoesWindowsConsoleSupportAnsi()) + + if (DoesWindowsConsoleSupportAnsi()) { _messageQueue.Console = new AnsiLogConsole(); _messageQueue.ErrorConsole = new AnsiLogConsole(stdErr: true); @@ -60,8 +60,14 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu } } + [UnsupportedOSPlatformGuard("windows")] private static bool DoesWindowsConsoleSupportAnsi() { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return true; + } + // for Windows, check the console mode var stdOutHandle = Interop.Kernel32.GetStdHandle(Interop.Kernel32.STD_OUTPUT_HANDLE); if (!Interop.Kernel32.GetConsoleMode(stdOutHandle, out int consoleMode)) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 16272cbaa4336..40d9798212759 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -76,6 +76,9 @@ internal sealed class HttpConnectionPool : IDisposable private byte[]? _http2AltSvcOriginUri; internal readonly byte[]? _http2EncodedAuthorityHostHeader; + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] private readonly bool _http3Enabled; private Http3Connection? _http3Connection; private SemaphoreSlim? _http3ConnectionCreateLock; @@ -122,8 +125,8 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK } _http2Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version20; - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + + if (_http3Enabled) { _http3Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version30 && (_poolManager.Settings._quicImplementationProvider ?? QuicImplementationProviders.Default).IsSupported; } @@ -261,14 +264,10 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); } - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (_http3Enabled) { - if (_http3Enabled) - { - _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); - _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; - } + _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); + _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; } } @@ -866,8 +865,7 @@ private async ValueTask DetermineVersionAndSendAsync(HttpRe { HttpResponseMessage? response; - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (_http3Enabled) { response = await TrySendUsingHttp3Async(request, async, doRequestAuth, cancellationToken).ConfigureAwait(false); if (response is not null) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs index 692e39bb6b015..e1469c5cc8b80 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs @@ -45,13 +45,11 @@ protected internal override void QueueTask(Task task) if (Thread.IsThreadStartSupported && (options & TaskCreationOptions.LongRunning) != 0) { // Run LongRunning tasks on their own dedicated thread. -#pragma warning disable CA1416 // TODO: https://github.com/dotnet/runtime/issues/44922 new Thread(s_longRunningThreadWork) { IsBackground = true, Name = ".NET Long Running Task" }.UnsafeStart(task); -#pragma warning restore CA1416 } else { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index aa6e8c6f8d582..cb16c8b99d4f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -164,6 +164,7 @@ public Thread(ParameterizedThreadStart start, int maxStackSize) } #if !TARGET_BROWSER + [UnsupportedOSPlatformGuard("browser")] internal const bool IsThreadStartSupported = true; /// Causes the operating system to change the state of the current instance to , and optionally supplies an object containing data to be used by the method the thread executes. From ef2408216a4e37fbbf11e028d035741dee9cd9db Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 12 May 2021 23:13:56 -0700 Subject: [PATCH 2/7] Move attribute outside of ifdef --- .../System.Private.CoreLib/src/System/Threading/Thread.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index cb16c8b99d4f1..88071eb455551 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -163,8 +163,8 @@ public Thread(ParameterizedThreadStart start, int maxStackSize) Initialize(); } -#if !TARGET_BROWSER [UnsupportedOSPlatformGuard("browser")] +#if !TARGET_BROWSER internal const bool IsThreadStartSupported = true; /// Causes the operating system to change the state of the current instance to , and optionally supplies an object containing data to be used by the method the thread executes. From 175e3d9eca2c062418bb4c3b4fbc5fbab3761e84 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Wed, 12 May 2021 23:47:48 -0700 Subject: [PATCH 3/7] Add annotation in Thread.Browser.Mono.cs --- .../src/System/Threading/Thread.Browser.Mono.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs index a456c5d8207ad..dd792d18fd74f 100644 --- a/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs @@ -7,6 +7,7 @@ namespace System.Threading { public partial class Thread { + [UnsupportedOSPlatformGuard("browser")] internal const bool IsThreadStartSupported = false; [UnsupportedOSPlatform("browser")] From e2e3218a1ec97faeb54f9077871b79b52f0f4c0a Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 13 May 2021 12:01:57 -0700 Subject: [PATCH 4/7] Apply feedbacks --- .../src/ConsoleLoggerProvider.cs | 4 ++-- src/libraries/System.Net.Http/ref/System.Net.Http.cs | 1 + .../Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs | 1 + .../Net/Http/SocketsHttpHandler/HttpConnectionPool.cs | 4 ++-- .../Http/SocketsHttpHandler/HttpConnectionSettings.cs | 8 ++++++-- .../Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs | 1 + .../tests/TrimmingTests/HttpClientTest.cs | 11 ++++++++--- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs index bb24ed8f28aac..9f4c45a6f9589 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/src/ConsoleLoggerProvider.cs @@ -48,7 +48,7 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu _messageQueue = new ConsoleLoggerProcessor(); - if (DoesWindowsConsoleSupportAnsi()) + if (DoesConsoleSupportAnsi()) { _messageQueue.Console = new AnsiLogConsole(); _messageQueue.ErrorConsole = new AnsiLogConsole(stdErr: true); @@ -61,7 +61,7 @@ public ConsoleLoggerProvider(IOptionsMonitor options, IEnu } [UnsupportedOSPlatformGuard("windows")] - private static bool DoesWindowsConsoleSupportAnsi() + private static bool DoesConsoleSupportAnsi() { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { diff --git a/src/libraries/System.Net.Http/ref/System.Net.Http.cs b/src/libraries/System.Net.Http/ref/System.Net.Http.cs index c05f0e0e9aead..9fa9bdc34287e 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -352,6 +352,7 @@ protected override void SerializeToStream(System.IO.Stream stream, System.Net.Tr public sealed partial class SocketsHttpHandler : System.Net.Http.HttpMessageHandler { public SocketsHttpHandler() { } + [System.Runtime.Versioning.UnsupportedOSPlatformGuardAttribute("browser")] public static bool IsSupported { get { throw null; } } public bool AllowAutoRedirect { get { throw null; } set { } } public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs index 0fb0ca57b099a..f85f0a3521e82 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/BrowserHttpHandler/SocketsHttpHandler.cs @@ -16,6 +16,7 @@ namespace System.Net.Http [UnsupportedOSPlatform("browser")] public sealed class SocketsHttpHandler : HttpMessageHandler { + [UnsupportedOSPlatformGuard("browser")] public static bool IsSupported => false; public bool UseCookies diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 40d9798212759..1ee818108ef9e 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -79,7 +79,7 @@ internal sealed class HttpConnectionPool : IDisposable [SupportedOSPlatformGuard("linux")] [SupportedOSPlatformGuard("macOS")] [SupportedOSPlatformGuard("Windows")] - private readonly bool _http3Enabled; + private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); private Http3Connection? _http3Connection; private SemaphoreSlim? _http3ConnectionCreateLock; internal readonly byte[]? _http3EncodedAuthorityHostHeader; @@ -286,7 +286,7 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK private static List CreateHttp3ApplicationProtocols() { - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished + // TODO: Cannot use instance field _http3Enabled in static method if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) { // TODO: Once the HTTP/3 versions are part of SslApplicationProtocol, see https://github.com/dotnet/runtime/issues/1293, move this back to field initialization. diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs index cf81a5d5a2929..a0d4e4c532b68 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs @@ -59,6 +59,11 @@ internal sealed class HttpConnectionSettings internal bool _enableMultipleHttp2Connections; + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] + private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); + internal Func>? _connectCallback; internal Func>? _plaintextStreamFilter; @@ -122,9 +127,8 @@ public HttpConnectionSettings CloneAndNormalize() _plaintextStreamFilter = _plaintextStreamFilter }; - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished // TODO: Remove if/when QuicImplementationProvider is removed from System.Net.Quic. - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (_http3Enabled) { settings._quicImplementationProvider = _quicImplementationProvider; } 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 8e0b508e6a485..31a9e6ac2cc1e 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 @@ -42,6 +42,7 @@ private void CheckDisposedOrStarted() /// /// Gets a value that indicates whether the handler is supported on the current platform. /// + [UnsupportedOSPlatformGuard("browser")] public static bool IsSupported => true; public bool UseCookies diff --git a/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs index c05cac77920e6..f6ec0a315f233 100644 --- a/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Net.Http; +using System.Runtime.Versioning; using System.Threading.Tasks; class Program @@ -18,8 +19,12 @@ static async Task Main(string[] args) const string quicDll = "System.Net.Quic.dll"; var quicDllExists = File.Exists(Path.Combine(AppContext.BaseDirectory, quicDll)); - // TODO: Replace with Platform-Guard Assertion Annotations once https://github.com/dotnet/runtime/issues/44922 is finished - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] + private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); + + if (_http3Enabled) { Console.WriteLine($"Expected {quicDll} is {(quicDllExists ? "present - OK" : "missing - BAD")}."); return quicDllExists ? 100 : -1; @@ -30,4 +35,4 @@ static async Task Main(string[] args) return quicDllExists ? -1 : 100; } } -} \ No newline at end of file +} From 7e0a13c97ecd0cf4f32b8096a781d06834fba032 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Thu, 13 May 2021 17:49:06 -0700 Subject: [PATCH 5/7] Move back the annotation inside ifdef --- .../System.Private.CoreLib/src/System/Threading/Thread.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index 88071eb455551..cb16c8b99d4f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -163,8 +163,8 @@ public Thread(ParameterizedThreadStart start, int maxStackSize) Initialize(); } - [UnsupportedOSPlatformGuard("browser")] #if !TARGET_BROWSER + [UnsupportedOSPlatformGuard("browser")] internal const bool IsThreadStartSupported = true; /// Causes the operating system to change the state of the current instance to , and optionally supplies an object containing data to be used by the method the thread executes. From f4644e06183cfa360c5acdac7ef0313e89ce7ea9 Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Fri, 14 May 2021 18:03:28 -0700 Subject: [PATCH 6/7] Change const field into readonly property, apply feedback --- .../SocketsHttpHandler/HttpConnectionPool.cs | 25 +++++++++++-------- .../HttpConnectionSettings.cs | 7 +----- .../src/System/Threading/Thread.cs | 2 +- .../System/Threading/Thread.Browser.Mono.cs | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 1ee818108ef9e..3acfe7f189f4a 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -76,10 +76,7 @@ internal sealed class HttpConnectionPool : IDisposable private byte[]? _http2AltSvcOriginUri; internal readonly byte[]? _http2EncodedAuthorityHostHeader; - [SupportedOSPlatformGuard("linux")] - [SupportedOSPlatformGuard("macOS")] - [SupportedOSPlatformGuard("Windows")] - private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); + private readonly bool _http3Enabled; private Http3Connection? _http3Connection; private SemaphoreSlim? _http3ConnectionCreateLock; internal readonly byte[]? _http3EncodedAuthorityHostHeader; @@ -126,7 +123,7 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK _http2Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version20; - if (_http3Enabled) + if (IsHttp3Supported()) { _http3Enabled = _poolManager.Settings._maxHttpVersion >= HttpVersion.Version30 && (_poolManager.Settings._quicImplementationProvider ?? QuicImplementationProviders.Default).IsSupported; } @@ -264,10 +261,13 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK _http3EncodedAuthorityHostHeader = QPackEncoder.EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(H3StaticTable.Authority, hostHeader); } - if (_http3Enabled) + if (IsHttp3Supported()) { - _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); - _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; + if (_http3Enabled) + { + _sslOptionsHttp3 = ConstructSslOptions(poolManager, sslHostName); + _sslOptionsHttp3.ApplicationProtocols = s_http3ApplicationProtocols; + } } } @@ -280,14 +280,17 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK if (NetEventSource.Log.IsEnabled()) Trace($"{this}"); } + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] + internal static bool IsHttp3Supported() => (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); private static readonly List s_http3ApplicationProtocols = CreateHttp3ApplicationProtocols(); private static readonly List s_http2ApplicationProtocols = new List() { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 }; private static readonly List s_http2OnlyApplicationProtocols = new List() { SslApplicationProtocol.Http2 }; private static List CreateHttp3ApplicationProtocols() { - // TODO: Cannot use instance field _http3Enabled in static method - if ((OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) + if (IsHttp3Supported()) { // TODO: Once the HTTP/3 versions are part of SslApplicationProtocol, see https://github.com/dotnet/runtime/issues/1293, move this back to field initialization. return new List() { Http3Connection.Http3ApplicationProtocol31, Http3Connection.Http3ApplicationProtocol30, Http3Connection.Http3ApplicationProtocol29 }; @@ -865,7 +868,7 @@ private async ValueTask DetermineVersionAndSendAsync(HttpRe { HttpResponseMessage? response; - if (_http3Enabled) + if (IsHttp3Supported()) { response = await TrySendUsingHttp3Async(request, async, doRequestAuth, cancellationToken).ConfigureAwait(false); if (response is not null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs index a0d4e4c532b68..1ccbc3d8673d1 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionSettings.cs @@ -59,11 +59,6 @@ internal sealed class HttpConnectionSettings internal bool _enableMultipleHttp2Connections; - [SupportedOSPlatformGuard("linux")] - [SupportedOSPlatformGuard("macOS")] - [SupportedOSPlatformGuard("Windows")] - private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); - internal Func>? _connectCallback; internal Func>? _plaintextStreamFilter; @@ -128,7 +123,7 @@ public HttpConnectionSettings CloneAndNormalize() }; // TODO: Remove if/when QuicImplementationProvider is removed from System.Net.Quic. - if (_http3Enabled) + if (HttpConnectionPool.IsHttp3Supported()) { settings._quicImplementationProvider = _quicImplementationProvider; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index cb16c8b99d4f1..d6b20c461a5ce 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -165,7 +165,7 @@ public Thread(ParameterizedThreadStart start, int maxStackSize) #if !TARGET_BROWSER [UnsupportedOSPlatformGuard("browser")] - internal const bool IsThreadStartSupported = true; + internal static bool IsThreadStartSupported => true; /// Causes the operating system to change the state of the current instance to , and optionally supplies an object containing data to be used by the method the thread executes. /// An object that contains data to be used by the method the thread executes. diff --git a/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs index dd792d18fd74f..435a155c4ed16 100644 --- a/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Threading/Thread.Browser.Mono.cs @@ -8,7 +8,7 @@ namespace System.Threading public partial class Thread { [UnsupportedOSPlatformGuard("browser")] - internal const bool IsThreadStartSupported = false; + internal static bool IsThreadStartSupported => false; [UnsupportedOSPlatform("browser")] public void Start() => throw new PlatformNotSupportedException(); From 8bdea1403cf0540df51c55d76cdcfa05c066eaef Mon Sep 17 00:00:00 2001 From: Buyaa Namnan Date: Sun, 16 May 2021 23:20:27 -0700 Subject: [PATCH 7/7] Apply feedback, move a field out of method --- .../Http/SocketsHttpHandler/HttpConnectionPool.cs | 3 +++ .../tests/TrimmingTests/HttpClientTest.cs | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs index 3acfe7f189f4a..1a3b68c55c711 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs @@ -76,6 +76,9 @@ internal sealed class HttpConnectionPool : IDisposable private byte[]? _http2AltSvcOriginUri; internal readonly byte[]? _http2EncodedAuthorityHostHeader; + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] private readonly bool _http3Enabled; private Http3Connection? _http3Connection; private SemaphoreSlim? _http3ConnectionCreateLock; diff --git a/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs index f6ec0a315f233..7ded3b5b65be8 100644 --- a/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/TrimmingTests/HttpClientTest.cs @@ -9,6 +9,11 @@ class Program { + [SupportedOSPlatformGuard("linux")] + [SupportedOSPlatformGuard("macOS")] + [SupportedOSPlatformGuard("Windows")] + private static bool IsHttp3Supported => (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); + static async Task Main(string[] args) { using var client = new HttpClient(); @@ -19,12 +24,7 @@ static async Task Main(string[] args) const string quicDll = "System.Net.Quic.dll"; var quicDllExists = File.Exists(Path.Combine(AppContext.BaseDirectory, quicDll)); - [SupportedOSPlatformGuard("linux")] - [SupportedOSPlatformGuard("macOS")] - [SupportedOSPlatformGuard("Windows")] - private readonly bool _http3Enabled = (OperatingSystem.IsLinux() && !OperatingSystem.IsAndroid()) || OperatingSystem.IsWindows() || OperatingSystem.IsMacOS(); - - if (_http3Enabled) + if (IsHttp3Supported) { Console.WriteLine($"Expected {quicDll} is {(quicDllExists ? "present - OK" : "missing - BAD")}."); return quicDllExists ? 100 : -1;