From a3af365f10fa97132a5225859e6c355f8dbcfb3e Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Wed, 9 Jun 2021 00:26:12 -0400 Subject: [PATCH 01/13] Refactor HttpClient / HttpClientHandler usage on iOS, tvOS, MacCatalyst --- .../src/System.Net.Http.csproj | 13 +- ...HttpClient.CreateDefaultHandler.Android.cs | 29 -- ...Client.CreateDefaultHandler.MacCatalyst.cs | 29 -- .../Http/HttpClient.CreateDefaultHandler.cs | 13 - .../HttpClient.CreateDefaultHandler.iOS.cs | 29 -- .../HttpClient.CreateDefaultHandler.tvOS.cs | 29 -- .../src/System/Net/Http/HttpClient.cs | 2 +- .../Net/Http/HttpClientHandler.AnyMobile.cs | 23 + .../Net/Http/HttpClientHandler.Apple.cs | 418 ++++++++++++++++++ .../Net/Http/HttpClientHandler.MacCatalyst.cs | 21 + .../System/Net/Http/HttpClientHandler.iOS.cs | 21 + .../System/Net/Http/HttpClientHandler.tvOS.cs | 21 + 12 files changed, 513 insertions(+), 135 deletions(-) delete mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.Android.cs delete mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.MacCatalyst.cs delete mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.cs delete mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.iOS.cs delete mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.tvOS.cs create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index 0ce5ec1aa171ef..645f0d0727009c 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -34,11 +34,14 @@ - - - - - + + + + + + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.Android.cs deleted file mode 100644 index f770bbe22a2ac6..00000000000000 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.Android.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Reflection; - -namespace System.Net.Http -{ - public partial class HttpClient - { - private static MethodInfo? handlerMethod; - - private static HttpMessageHandler CreateDefaultHandler() - { - // Default is to use the Android native handler - if (!IsNativeHandlerEnabled()) - { - return new HttpClientHandler(); - } - - if (handlerMethod == null) - { - Type? androidEnv = Type.GetType("Android.Runtime.AndroidEnvironment, Mono.Android"); - handlerMethod = androidEnv!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - } - - return (HttpMessageHandler)handlerMethod!.Invoke(null, null)!; - } - } -} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.MacCatalyst.cs deleted file mode 100644 index dc19e0ed35d87a..00000000000000 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.MacCatalyst.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Reflection; - -namespace System.Net.Http -{ - public partial class HttpClient - { - private static MethodInfo? handlerMethod; - - private static HttpMessageHandler CreateDefaultHandler() - { - // Default is to use the iOS native handler - if (!IsNativeHandlerEnabled()) - { - return new HttpClientHandler(); - } - - if (handlerMethod == null) - { - Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst"); - handlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - } - - return (HttpMessageHandler)handlerMethod!.Invoke(null, null)!; - } - } -} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.cs deleted file mode 100644 index b1327d9cd0fded..00000000000000 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Net.Http -{ - public partial class HttpClient - { - private static HttpMessageHandler CreateDefaultHandler() - { - return new HttpClientHandler(); - } - } -} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.iOS.cs deleted file mode 100644 index 03f4f945c9f276..00000000000000 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.iOS.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Reflection; - -namespace System.Net.Http -{ - public partial class HttpClient - { - private static MethodInfo? handlerMethod; - - private static HttpMessageHandler CreateDefaultHandler() - { - // Default is to use the iOS native handler - if (!IsNativeHandlerEnabled()) - { - return new HttpClientHandler(); - } - - if (handlerMethod == null) - { - Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.iOS"); - handlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - } - - return (HttpMessageHandler)handlerMethod!.Invoke(null, null)!; - } - } -} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.tvOS.cs deleted file mode 100644 index e2dfbe0ffc1072..00000000000000 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.CreateDefaultHandler.tvOS.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Reflection; - -namespace System.Net.Http -{ - public partial class HttpClient - { - private static MethodInfo? handlerMethod; - - private static HttpMessageHandler CreateDefaultHandler() - { - // Default is to use the tvOS native handler - if (!IsNativeHandlerEnabled()) - { - return new HttpClientHandler(); - } - - if (handlerMethod == null) - { - Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.TVOS"); - handlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - } - - return (HttpMessageHandler)handlerMethod!.Invoke(null, null)!; - } - } -} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs index b09850ec9a4b0d..e2fe7b6ca3f595 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs @@ -127,7 +127,7 @@ public long MaxResponseContentBufferSize #region Constructors - public HttpClient() : this(CreateDefaultHandler()) + public HttpClient() : this(new HttpClientHandler()) { } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs new file mode 100644 index 00000000000000..91d89b101744b0 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + private static bool IsSocketHandler => IsNativeHandlerEnabled(); + + // check to see if this is linker friendly or not. + private static bool IsNativeHandlerEnabled() + { + if (!AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool isEnabled)) + { + return false; + } + + return isEnabled; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs new file mode 100644 index 00000000000000..9a1cf065abd592 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -0,0 +1,418 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Globalization; +using System.Net.Security; +using System.Collections.Generic; +using System.Runtime.Versioning; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + private static MethodInfo? _underlyingHandlerMethod; + + private readonly SocketsHttpHandler _socketHandler; + private readonly object? _appleHandler; + + private readonly DiagnosticsHandler? _diagnosticsHandler; + + private volatile bool _disposed; + + public HttpClientHandler() + { + HttpMessageHandler handler; + + if (IsSocketHandler) + { + _socketHandler = new SocketsHttpHandler(); + handler = _socketHandler; + } + else + { + _appleHandler = CreateNativeHandler(); + handler = (HttpMessageHandler)_appleHandler; + } + + if (DiagnosticsHandler.IsGloballyEnabled()) + { + _diagnosticsHandler = new DiagnosticsHandler(handler); + } + } + + protected override void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _disposed = true; + + if (IsSocketHandler) + { + _socketHandler.Dispose(); + } + else + { + (HttpMessageHandler)_appleHandler.Dispose(); + } + } + + base.Dispose(disposing); + } + + // not sure + public virtual bool SupportsAutomaticDecompression => false; + public virtual bool SupportsProxy => false; + public virtual bool SupportsRedirectConfiguration => true; + + public bool UseCookies + { + get + { + if (IsSocketHandler) + { + return _socketHandler.UseCookies; + } + else + { + return (bool)GetNativeHandlerProp("UseCookies"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler.UseCookies = value; + } + else + { + SetNativeHandlerProp("UseCookies", value); + } + } + } + + public CookieContainer CookieContainer + { + get + { + if (IsSocketHandler) + { + return _socketHandler.CookieContainer; + } + else + { + return (CookieContainer)GetNativeHandlerProp("CookieContainer"); + } + } + set + { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (IsSocketHandler) + { + _socketHandler.CookieContainer = value; + } + else + { + SetNativeHandlerProp("CookieContainer", value); + } + } + } + + [UnsupportedOSPlatform("ios")] + public DecompressionMethods AutomaticDecompression + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public bool UseProxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public IWebProxy? Proxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public ICredentials? DefaultProxyCredentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public bool PreAuthenticate + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public bool UseDefaultCredentials + { + // SocketsHttpHandler doesn't have a separate UseDefaultCredentials property. There + // is just a Credentials property. So, we need to map the behavior. + // Same with the native handler. + get + { + ICredentials creds; + if (IsSocketHandler) + { + creds = _socketHandler.Credentials; + } + else + { + creds = (ICredentials)GetNativeHandlerProp("Credentials"); + } + + return creds == CredentialCache.DefaultCredentials; + } + set + { + if (value) + { + if (IsSocketHandler) + { + _socketHandler.Credentials = CredentialCache.DefaultCredentials; + } + else + { + SetNativeHandlerProp("Credentials", CredentialCache.DefaultCredentials); + } + } + else + { + if (_underlyingHandler.Credentials == CredentialCache.DefaultCredentials) + { + // Only clear out the Credentials property if it was a DefaultCredentials. + if (IsSocketHandler) + { + _socketHandler.Credentials = null; + } + else + { + SetNativeHandlerProp("Credentials", null); + } + } + } + } + } + + public ICredentials? Credentials + { + get + { + if (IsSocketHandler) + { + return _socketHandler.Credentials; + } + else + { + return (ICredentials)GetNativeHandlerProp("Credentials"); + } + + } + set + { + if (IsSocketHandler) + { + _socketHandler.Credentials = value; + } + else + { + SetNativeHandlerProp("Credentials", value); + } + } + } + + public bool AllowAutoRedirect + { + get + { + if (IsSocketHandler) + { + return _socketHandler.AllowAutoRedirect; + } + else + { + return (bool)GetNativeHandlerProp("AllowAutoRedirect"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler.AllowAutoRedirect = value; + } + else + { + SetNativeHandlerProp("AllowAutoRedirect", value); + } + } + } + + [UnsupportedOSPlatform("ios")] + public int MaxAutomaticRedirections + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public int MaxConnectionsPerServer + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public long MaxRequestContentBufferSize + { + // This property is not supported. In the .NET Framework it was only used when the handler needed to + // automatically buffer the request content. That only happened if neither 'Content-Length' nor + // 'Transfer-Encoding: chunked' request headers were specified. So, the handler thus needed to buffer + // in the request content to determine its length and then would choose 'Content-Length' semantics when + // POST'ing. In .NET Core, the handler will resolve the ambiguity by always choosing + // 'Transfer-Encoding: chunked'. The handler will never automatically buffer in the request content. + get + { + return 0; // Returning zero is appropriate since in .NET Framework it means no limit. + } + + set + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(nameof(value)); + } + + if (value > HttpContent.MaxBufferSize) + { + throw new ArgumentOutOfRangeException(nameof(value), value, + SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_limit, + HttpContent.MaxBufferSize)); + } + + CheckDisposed(); + + // No-op on property setter. + } + } + + [UnsupportedOSPlatform("ios")] + public int MaxResponseHeadersLength + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public ClientCertificateOption ClientCertificateOptions + { + get => throw new PlatformNotSupportedException(); + set + { + throw new PlatformNotSupportedException(); + } + } + + [UnsupportedOSPlatform("ios")] + public X509CertificateCollection ClientCertificates + { + get + { + throw new PlatformNotSupportedException(); + } + } + + // this may be able to map somehow to NSUrlSessionHandlerTrustOverrideCallback? + [UnsupportedOSPlatform("ios")] + public Func? ServerCertificateCustomValidationCallback + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public bool CheckCertificateRevocationList + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public SslProtocols SslProtocols + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("ios")] + public IDictionary Properties => throw new PlatformNotSupportedException(); + + [UnsupportedOSPlatform("ios")] + protected internal override HttpResponseMessage Send(HttpRequestMessage request, + CancellationToken cancellationToken) + { + throw new PlatformNotSupportedException(); + } + + protected internal override Task SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) + { + if (DiagnosticsHandler.IsEnabled() && _diagnosticsHandler != null) + { + return _diagnosticsHandler.SendAsync(request, cancellationToken); + } + + if (IsSocketHandler) + { + return _socketHandler.SendAsync(request, cancellationToken); + } + else + { + return (Task)InvokeNativeHandlerMethod("SendAsync", request, cancellationToken); + } + } + + [UnsupportedOSPlatform("ios")] + public static Func DangerousAcceptAnyServerCertificateValidator => + throw new PlatformNotSupportedException(); + + // move these to a common place + private object GetNativeHandlerProp(string name) + { + return _appleHandler!.GetType().GetProperty(name).GetValue(_appleHandler, null); + } + + private void SetNativeHandlerProp(string name, object value) + { + _appleHandler!.GetType().GetProperty(name).SetValue(_appleHandler, value); + } + + private object InvokeNativeHandlerMethod(string name, params object[] parameters) + { + return _appleHandler!.Invoke(_appleHandler, parameters)!; + } + + private static bool IsSocketHandler => true/false; + + private void CheckDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(GetType().ToString()); + } + } + } +} diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs new file mode 100644 index 00000000000000..285aa4234561c1 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + private object CreateNativeHandler() + { + if (_underlyingHandlerMethod == null) + { + Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst"); + _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + } + + return _underlyingHandlerMethod!.Invoke(null, null)!; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs new file mode 100644 index 00000000000000..0e673a45d59c9f --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + private object CreateNativeHandler() + { + if (_underlyingHandlerMethod == null) + { + Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.iOS"); + _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + } + + return _underlyingHandlerMethod!.Invoke(null, null)!; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs new file mode 100644 index 00000000000000..76adafa2a04942 --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + private object CreateNativeHandler() + { + if (_underlyingHandlerMethod == null) + { + Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.TVOS"); + _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + } + + return _underlyingHandlerMethod!.Invoke(null, null)!; + } + } +} \ No newline at end of file From 8c0541cc03a167b6a98ca812ca02defb837da2cb Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Thu, 10 Jun 2021 13:44:43 -0400 Subject: [PATCH 02/13] Fixup build / add unsupported attributes --- .../System.Net.Http/ref/System.Net.Http.cs | 15 ++++ .../src/System.Net.Http.csproj | 3 +- .../Net/Http/HttpClientHandler.Apple.cs | 75 ++++++++++++------- .../Net/Http/HttpClientHandler.MacCatalyst.cs | 1 + .../src/System/Net/Http/HttpClientHandler.cs | 15 ++++ .../System/Net/Http/HttpClientHandler.iOS.cs | 1 + .../System/Net/Http/HttpClientHandler.tvOS.cs | 1 + 7 files changed, 83 insertions(+), 28 deletions(-) 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 02486ab68b11b8..8925caa128cb8b 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -109,35 +109,48 @@ public partial class HttpClientHandler : System.Net.Http.HttpMessageHandler public HttpClientHandler() { } public bool AllowAutoRedirect { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public bool CheckCertificateRevocationList { get { throw null; } set { } } public System.Net.Http.ClientCertificateOption ClientCertificateOptions { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.CookieContainer CookieContainer { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.ICredentials? Credentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public static System.Func DangerousAcceptAnyServerCertificateValidator { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Net.ICredentials? DefaultProxyCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public int MaxAutomaticRedirections { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public int MaxConnectionsPerServer { get { throw null; } set { } } public long MaxRequestContentBufferSize { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public int MaxResponseHeadersLength { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public bool PreAuthenticate { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Collections.Generic.IDictionary Properties { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Net.IWebProxy? Proxy { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Func? ServerCertificateCustomValidationCallback { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public System.Security.Authentication.SslProtocols SslProtocols { get { throw null; } set { } } public virtual bool SupportsAutomaticDecompression { get { throw null; } } public virtual bool SupportsProxy { get { throw null; } } @@ -147,9 +160,11 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public bool UseDefaultCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] protected internal override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; } protected internal override System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; } } diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index 645f0d0727009c..408eedcad90e15 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -42,7 +42,8 @@ - + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs index 9a1cf065abd592..513ae20c7a88bc 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Net.Security; using System.Collections.Generic; +using System.Reflection; using System.Runtime.Versioning; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; @@ -16,7 +17,7 @@ public partial class HttpClientHandler : HttpMessageHandler { private static MethodInfo? _underlyingHandlerMethod; - private readonly SocketsHttpHandler _socketHandler; + private readonly SocketsHttpHandler? _socketHandler; private readonly object? _appleHandler; private readonly DiagnosticsHandler? _diagnosticsHandler; @@ -52,11 +53,11 @@ protected override void Dispose(bool disposing) if (IsSocketHandler) { - _socketHandler.Dispose(); + _socketHandler!.Dispose(); } else { - (HttpMessageHandler)_appleHandler.Dispose(); + ((HttpMessageHandler)_appleHandler!)!.Dispose(); } } @@ -68,13 +69,14 @@ protected override void Dispose(bool disposing) public virtual bool SupportsProxy => false; public virtual bool SupportsRedirectConfiguration => true; + [UnsupportedOSPlatform("browser")] public bool UseCookies { get { if (IsSocketHandler) { - return _socketHandler.UseCookies; + return _socketHandler!.UseCookies; } else { @@ -85,7 +87,7 @@ public bool UseCookies { if (IsSocketHandler) { - _socketHandler.UseCookies = value; + _socketHandler!.UseCookies = value; } else { @@ -94,13 +96,14 @@ public bool UseCookies } } + [UnsupportedOSPlatform("browser")] public CookieContainer CookieContainer { get { if (IsSocketHandler) { - return _socketHandler.CookieContainer; + return _socketHandler!.CookieContainer; } else { @@ -116,7 +119,7 @@ public CookieContainer CookieContainer if (IsSocketHandler) { - _socketHandler.CookieContainer = value; + _socketHandler!.CookieContainer = value; } else { @@ -125,6 +128,7 @@ public CookieContainer CookieContainer } } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public DecompressionMethods AutomaticDecompression { @@ -132,6 +136,7 @@ public DecompressionMethods AutomaticDecompression set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public bool UseProxy { @@ -139,6 +144,7 @@ public bool UseProxy set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public IWebProxy? Proxy { @@ -146,6 +152,7 @@ public IWebProxy? Proxy set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public ICredentials? DefaultProxyCredentials { @@ -153,6 +160,7 @@ public ICredentials? DefaultProxyCredentials set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public bool PreAuthenticate { @@ -160,6 +168,7 @@ public bool PreAuthenticate set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] public bool UseDefaultCredentials { // SocketsHttpHandler doesn't have a separate UseDefaultCredentials property. There @@ -167,10 +176,10 @@ public bool UseDefaultCredentials // Same with the native handler. get { - ICredentials creds; + ICredentials? creds; if (IsSocketHandler) { - creds = _socketHandler.Credentials; + creds = _socketHandler!.Credentials; } else { @@ -185,7 +194,7 @@ public bool UseDefaultCredentials { if (IsSocketHandler) { - _socketHandler.Credentials = CredentialCache.DefaultCredentials; + _socketHandler!.Credentials = CredentialCache.DefaultCredentials; } else { @@ -194,14 +203,18 @@ public bool UseDefaultCredentials } else { - if (_underlyingHandler.Credentials == CredentialCache.DefaultCredentials) + if (IsSocketHandler) { - // Only clear out the Credentials property if it was a DefaultCredentials. - if (IsSocketHandler) + if (_socketHandler!.Credentials == CredentialCache.DefaultCredentials) { - _socketHandler.Credentials = null; + _socketHandler!.Credentials = null; } - else + } + else + { + ICredentials? creds = (ICredentials)GetNativeHandlerProp("Credentials"); + + if (creds == CredentialCache.DefaultCredentials) { SetNativeHandlerProp("Credentials", null); } @@ -210,25 +223,26 @@ public bool UseDefaultCredentials } } + [UnsupportedOSPlatform("browser")] public ICredentials? Credentials { get { if (IsSocketHandler) { - return _socketHandler.Credentials; + return _socketHandler!.Credentials; } else { return (ICredentials)GetNativeHandlerProp("Credentials"); } - + } set { if (IsSocketHandler) { - _socketHandler.Credentials = value; + _socketHandler!.Credentials = value; } else { @@ -243,7 +257,7 @@ public bool AllowAutoRedirect { if (IsSocketHandler) { - return _socketHandler.AllowAutoRedirect; + return _socketHandler!.AllowAutoRedirect; } else { @@ -254,7 +268,7 @@ public bool AllowAutoRedirect { if (IsSocketHandler) { - _socketHandler.AllowAutoRedirect = value; + _socketHandler!.AllowAutoRedirect = value; } else { @@ -263,6 +277,7 @@ public bool AllowAutoRedirect } } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public int MaxAutomaticRedirections { @@ -270,6 +285,7 @@ public int MaxAutomaticRedirections set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public int MaxConnectionsPerServer { @@ -310,6 +326,7 @@ public long MaxRequestContentBufferSize } } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public int MaxResponseHeadersLength { @@ -326,6 +343,7 @@ public ClientCertificateOption ClientCertificateOptions } } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public X509CertificateCollection ClientCertificates { @@ -336,6 +354,7 @@ public X509CertificateCollection ClientCertificates } // this may be able to map somehow to NSUrlSessionHandlerTrustOverrideCallback? + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public Func? ServerCertificateCustomValidationCallback { @@ -343,6 +362,7 @@ public X509CertificateCollection ClientCertificates set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public bool CheckCertificateRevocationList { @@ -350,6 +370,7 @@ public bool CheckCertificateRevocationList set => throw new PlatformNotSupportedException(); } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public SslProtocols SslProtocols { @@ -360,6 +381,7 @@ public SslProtocols SslProtocols [UnsupportedOSPlatform("ios")] public IDictionary Properties => throw new PlatformNotSupportedException(); + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) @@ -377,7 +399,7 @@ protected internal override Task SendAsync(HttpRequestMessa if (IsSocketHandler) { - return _socketHandler.SendAsync(request, cancellationToken); + return _socketHandler!.SendAsync(request, cancellationToken); } else { @@ -385,6 +407,7 @@ protected internal override Task SendAsync(HttpRequestMessa } } + [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public static Func DangerousAcceptAnyServerCertificateValidator => throw new PlatformNotSupportedException(); @@ -392,21 +415,19 @@ protected internal override Task SendAsync(HttpRequestMessa // move these to a common place private object GetNativeHandlerProp(string name) { - return _appleHandler!.GetType().GetProperty(name).GetValue(_appleHandler, null); + return _appleHandler!.GetType()!.GetProperty(name)!.GetValue(_appleHandler, null)!; } - private void SetNativeHandlerProp(string name, object value) + private void SetNativeHandlerProp(string name, object? value) { - _appleHandler!.GetType().GetProperty(name).SetValue(_appleHandler, value); + _appleHandler!.GetType()!.GetProperty(name)!.SetValue(_appleHandler, value!); } private object InvokeNativeHandlerMethod(string name, params object[] parameters) { - return _appleHandler!.Invoke(_appleHandler, parameters)!; + return _appleHandler!.GetType()!.GetMethod(name)!.Invoke(_appleHandler, parameters)!; } - private static bool IsSocketHandler => true/false; - private void CheckDisposed() { if (_disposed) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index 285aa4234561c1..36bde1b5aed80d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Reflection; namespace System.Net.Http { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index 2e3289643cfbed..d27e76237afef6 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -73,6 +73,7 @@ public CookieContainer CookieContainer } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public DecompressionMethods AutomaticDecompression { get => _underlyingHandler.AutomaticDecompression; @@ -80,6 +81,7 @@ public DecompressionMethods AutomaticDecompression } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public bool UseProxy { get => _underlyingHandler.UseProxy; @@ -87,6 +89,7 @@ public bool UseProxy } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public IWebProxy? Proxy { get => _underlyingHandler.Proxy; @@ -94,6 +97,7 @@ public IWebProxy? Proxy } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public ICredentials? DefaultProxyCredentials { get => _underlyingHandler.DefaultProxyCredentials; @@ -101,6 +105,7 @@ public ICredentials? DefaultProxyCredentials } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public bool PreAuthenticate { get => _underlyingHandler.PreAuthenticate; @@ -144,6 +149,7 @@ public bool AllowAutoRedirect } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public int MaxAutomaticRedirections { get => _underlyingHandler.MaxAutomaticRedirections; @@ -151,6 +157,7 @@ public int MaxAutomaticRedirections } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public int MaxConnectionsPerServer { get => _underlyingHandler.MaxConnectionsPerServer; @@ -191,6 +198,7 @@ public long MaxRequestContentBufferSize } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public int MaxResponseHeadersLength { get => _underlyingHandler.MaxResponseHeadersLength; @@ -231,6 +239,7 @@ public ClientCertificateOption ClientCertificateOptions } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public X509CertificateCollection ClientCertificates { get @@ -246,6 +255,7 @@ public X509CertificateCollection ClientCertificates } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public Func? ServerCertificateCustomValidationCallback { #if TARGET_BROWSER @@ -264,6 +274,7 @@ public X509CertificateCollection ClientCertificates } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public bool CheckCertificateRevocationList { get => _underlyingHandler.SslOptions.CertificateRevocationCheckMode == X509RevocationMode.Online; @@ -275,6 +286,7 @@ public bool CheckCertificateRevocationList } [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public SslProtocols SslProtocols { get => _underlyingHandler.SslOptions.EnabledSslProtocols; @@ -285,9 +297,11 @@ public SslProtocols SslProtocols } } + [UnsupportedOSPlatform("ios")] public IDictionary Properties => _underlyingHandler.Properties; [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) { @@ -307,6 +321,7 @@ protected internal override Task SendAsync(HttpRequestMessa // lazy-load the validator func so it can be trimmed by the ILLinker if it isn't used. private static Func? s_dangerousAcceptAnyServerCertificateValidator; [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] public static Func DangerousAcceptAnyServerCertificateValidator => Volatile.Read(ref s_dangerousAcceptAnyServerCertificateValidator) ?? Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index 0e673a45d59c9f..a95d7c15f4bad9 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Reflection; namespace System.Net.Http { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index 76adafa2a04942..439005608c9e78 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Reflection; namespace System.Net.Http { From a74c91e84f9a9ce7cb7b42d4c73d557062af9d16 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Tue, 29 Jun 2021 22:14:31 -0400 Subject: [PATCH 03/13] Restructure HttpClientHandler for iOS and Android in order to support the reflection based underlying native handlers --- .../System.Net.Http/ref/System.Net.Http.cs | 1 + .../src/System.Net.Http.csproj | 2 +- .../Net/Http/HttpClientHandler.Android.cs | 143 ++++++ .../Net/Http/HttpClientHandler.AnyMobile.cs | 412 +++++++++++++++++- .../Net/Http/HttpClientHandler.Apple.cs | 395 +---------------- .../Net/Http/HttpClientHandler.MacCatalyst.cs | 3 + .../src/System/Net/Http/HttpClientHandler.cs | 1 + .../System/Net/Http/HttpClientHandler.iOS.cs | 3 + .../System/Net/Http/HttpClientHandler.tvOS.cs | 3 + 9 files changed, 568 insertions(+), 395 deletions(-) create mode 100644 src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs 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 8925caa128cb8b..854aafb48922a2 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -163,6 +163,7 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] protected internal override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; } diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index 408eedcad90e15..75c0c6685d4b0a 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -41,7 +41,7 @@ - + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs new file mode 100644 index 00000000000000..317f144829f7ca --- /dev/null +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs @@ -0,0 +1,143 @@ +// 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.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Runtime.Versioning; + +namespace System.Net.Http +{ + public partial class HttpClientHandler : HttpMessageHandler + { + // not sure + public virtual bool SupportsAutomaticDecompression => true; + public virtual bool SupportsProxy => true; + public virtual bool SupportsRedirectConfiguration => true; + + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public DecompressionMethods AutomaticDecompression + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.AutomaticDecompression; + } + else + { + return (DecompressionMethods)GetNativeHandlerProp("AutomaticDecompression"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.AutomaticDecompression = value; + } + else + { + SetNativeHandlerProp("AutomaticDecompression", value); + } + } + } + + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public bool UseProxy + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.UseProxy; + } + else + { + return (bool)GetNativeHandlerProp("UseProxy"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.UseProxy = value; + } + else + { + SetNativeHandlerProp("UseProxy", value); + } + } + } + + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public bool PreAuthenticate + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.PreAuthenticate; + } + else + { + return (bool)GetNativeHandlerProp("PreAuthenticate"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.PreAuthenticate = value; + } + else + { + SetNativeHandlerProp("PreAuthenticate", value); + } + } + } + + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public int MaxAutomaticRedirections + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.MaxAutomaticRedirections; + } + else + { + return (int)GetNativeHandlerProp("MaxAutomaticRedirections"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.MaxAutomaticRedirections = value; + } + else + { + SetNativeHandlerProp("MaxAutomaticRedirections", value); + } + } + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] + private object CreateNativeHandler() + { + if (_underlyingHandlerMethod == null) + { + Type? androidEnv = Type.GetType("Android.Runtime.AndroidEnvironment, Mono.Android"); + _underlyingHandlerMethod = androidEnv!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + } + + return _underlyingHandlerMethod!.Invoke(null, null)!; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 91d89b101744b0..2c89cc9b1cd681 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -2,12 +2,422 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Net.Security; +using System.Reflection; +using System.Runtime.Versioning; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static bool IsSocketHandler => IsNativeHandlerEnabled(); + private readonly SocketsHttpHandler? _socketHandler; + private readonly DiagnosticsHandler? _diagnosticsHandler; + + private readonly object? _underlyingHandler; + private static MethodInfo? _underlyingHandlerMethod; + + private volatile bool _disposed; + + public HttpClientHandler() + { + HttpMessageHandler handler; + + if (IsSocketHandler) + { + _socketHandler = new SocketsHttpHandler(); + handler = _socketHandler; + } + else + { + _underlyingHandler = CreateNativeHandler(); + handler = (HttpMessageHandler)_underlyingHandler; + } + + if (DiagnosticsHandler.IsGloballyEnabled()) + { + _diagnosticsHandler = new DiagnosticsHandler(handler); + } + } + + protected override void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _disposed = true; + + if (IsSocketHandler) + { + _socketHandler!.Dispose(); + } + else + { + ((HttpMessageHandler)_underlyingHandler!)!.Dispose(); + } + } + + base.Dispose(disposing); + } + + protected static bool IsSocketHandler => IsNativeHandlerEnabled(); + + [UnsupportedOSPlatform("browser")] + public bool UseCookies + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.UseCookies; + } + else + { + return (bool)GetNativeHandlerProp("UseCookies"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.UseCookies = value; + } + else + { + SetNativeHandlerProp("UseCookies", value); + } + } + } + + [UnsupportedOSPlatform("browser")] + public CookieContainer CookieContainer + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.CookieContainer; + } + else + { + return (CookieContainer)GetNativeHandlerProp("CookieContainer"); + } + } + set + { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (IsSocketHandler) + { + _socketHandler!.CookieContainer = value; + } + else + { + SetNativeHandlerProp("CookieContainer", value); + } + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public IWebProxy? Proxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public ICredentials? DefaultProxyCredentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("browser")] + public bool UseDefaultCredentials + { + // SocketsHttpHandler doesn't have a separate UseDefaultCredentials property. There + // is just a Credentials property. So, we need to map the behavior. + // Same with the native handler. + get + { + ICredentials? creds; + if (IsSocketHandler) + { + creds = _socketHandler!.Credentials; + } + else + { + creds = (ICredentials)GetNativeHandlerProp("Credentials"); + } + + return creds == CredentialCache.DefaultCredentials; + } + set + { + if (value) + { + if (IsSocketHandler) + { + _socketHandler!.Credentials = CredentialCache.DefaultCredentials; + } + else + { + SetNativeHandlerProp("Credentials", CredentialCache.DefaultCredentials); + } + } + else + { + if (IsSocketHandler) + { + if (_socketHandler!.Credentials == CredentialCache.DefaultCredentials) + { + _socketHandler!.Credentials = null; + } + } + else + { + ICredentials? creds = (ICredentials)GetNativeHandlerProp("Credentials"); + + if (creds == CredentialCache.DefaultCredentials) + { + SetNativeHandlerProp("Credentials", null); + } + } + } + } + } + + [UnsupportedOSPlatform("browser")] + public ICredentials? Credentials + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.Credentials; + } + else + { + return (ICredentials)GetNativeHandlerProp("Credentials"); + } + + } + set + { + if (IsSocketHandler) + { + _socketHandler!.Credentials = value; + } + else + { + SetNativeHandlerProp("Credentials", value); + } + } + } + + public bool AllowAutoRedirect + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.AllowAutoRedirect; + } + else + { + return (bool)GetNativeHandlerProp("AllowAutoRedirect"); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.AllowAutoRedirect = value; + } + else + { + SetNativeHandlerProp("AllowAutoRedirect", value); + } + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public int MaxConnectionsPerServer + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public long MaxRequestContentBufferSize + { + // This property is not supported. In the .NET Framework it was only used when the handler needed to + // automatically buffer the request content. That only happened if neither 'Content-Length' nor + // 'Transfer-Encoding: chunked' request headers were specified. So, the handler thus needed to buffer + // in the request content to determine its length and then would choose 'Content-Length' semantics when + // POST'ing. In .NET Core, the handler will resolve the ambiguity by always choosing + // 'Transfer-Encoding: chunked'. The handler will never automatically buffer in the request content. + get + { + return 0; // Returning zero is appropriate since in .NET Framework it means no limit. + } + + set + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(nameof(value)); + } + + if (value > HttpContent.MaxBufferSize) + { + throw new ArgumentOutOfRangeException(nameof(value), value, + SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_limit, + HttpContent.MaxBufferSize)); + } + + CheckDisposed(); + + // No-op on property setter. + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public int MaxResponseHeadersLength + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("ios")] + public ClientCertificateOption ClientCertificateOptions + { + get => throw new PlatformNotSupportedException(); + set + { + throw new PlatformNotSupportedException(); + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public X509CertificateCollection ClientCertificates + { + get + { + throw new PlatformNotSupportedException(); + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public Func? ServerCertificateCustomValidationCallback + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public bool CheckCertificateRevocationList + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public SslProtocols SslProtocols + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("ios")] + public IDictionary Properties => throw new PlatformNotSupportedException(); + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + protected internal override HttpResponseMessage Send(HttpRequestMessage request, + CancellationToken cancellationToken) + { + throw new PlatformNotSupportedException(); + } + + protected internal override Task SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) + { + if (DiagnosticsHandler.IsEnabled() && _diagnosticsHandler != null) + { + return _diagnosticsHandler.SendAsync(request, cancellationToken); + } + + if (IsSocketHandler) + { + return _socketHandler!.SendAsync(request, cancellationToken); + } + else + { + return (Task)InvokeNativeHandlerMethod("SendAsync", request, cancellationToken); + } + } + + [UnsupportedOSPlatform("android")] + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + public static Func DangerousAcceptAnyServerCertificateValidator => + throw new PlatformNotSupportedException(); + + private void CheckDisposed() + { + if (_disposed) + { + throw new ObjectDisposedException(GetType().ToString()); + } + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] + private object GetNativeHandlerProp(string name) + { + return _underlyingHandler!.GetType()!.GetProperty(name)!.GetValue(_underlyingHandler, null)!; + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] + private void SetNativeHandlerProp(string name, object? value) + { + _underlyingHandler!.GetType()!.GetProperty(name)!.SetValue(_underlyingHandler, value!); + } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] + private object InvokeNativeHandlerMethod(string name, params object[] parameters) + { + return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; + } // check to see if this is linker friendly or not. private static bool IsNativeHandlerEnabled() diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs index 513ae20c7a88bc..8246cdc35ca0a2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -1,133 +1,18 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Globalization; -using System.Net.Security; -using System.Collections.Generic; -using System.Reflection; +using System; using System.Runtime.Versioning; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static MethodInfo? _underlyingHandlerMethod; - - private readonly SocketsHttpHandler? _socketHandler; - private readonly object? _appleHandler; - - private readonly DiagnosticsHandler? _diagnosticsHandler; - - private volatile bool _disposed; - - public HttpClientHandler() - { - HttpMessageHandler handler; - - if (IsSocketHandler) - { - _socketHandler = new SocketsHttpHandler(); - handler = _socketHandler; - } - else - { - _appleHandler = CreateNativeHandler(); - handler = (HttpMessageHandler)_appleHandler; - } - - if (DiagnosticsHandler.IsGloballyEnabled()) - { - _diagnosticsHandler = new DiagnosticsHandler(handler); - } - } - - protected override void Dispose(bool disposing) - { - if (disposing && !_disposed) - { - _disposed = true; - - if (IsSocketHandler) - { - _socketHandler!.Dispose(); - } - else - { - ((HttpMessageHandler)_appleHandler!)!.Dispose(); - } - } - - base.Dispose(disposing); - } - // not sure public virtual bool SupportsAutomaticDecompression => false; public virtual bool SupportsProxy => false; public virtual bool SupportsRedirectConfiguration => true; - [UnsupportedOSPlatform("browser")] - public bool UseCookies - { - get - { - if (IsSocketHandler) - { - return _socketHandler!.UseCookies; - } - else - { - return (bool)GetNativeHandlerProp("UseCookies"); - } - } - set - { - if (IsSocketHandler) - { - _socketHandler!.UseCookies = value; - } - else - { - SetNativeHandlerProp("UseCookies", value); - } - } - } - - [UnsupportedOSPlatform("browser")] - public CookieContainer CookieContainer - { - get - { - if (IsSocketHandler) - { - return _socketHandler!.CookieContainer; - } - else - { - return (CookieContainer)GetNativeHandlerProp("CookieContainer"); - } - } - set - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - if (IsSocketHandler) - { - _socketHandler!.CookieContainer = value; - } - else - { - SetNativeHandlerProp("CookieContainer", value); - } - } - } - [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public DecompressionMethods AutomaticDecompression @@ -144,139 +29,6 @@ public bool UseProxy set => throw new PlatformNotSupportedException(); } - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public IWebProxy? Proxy - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public ICredentials? DefaultProxyCredentials - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public bool PreAuthenticate - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("browser")] - public bool UseDefaultCredentials - { - // SocketsHttpHandler doesn't have a separate UseDefaultCredentials property. There - // is just a Credentials property. So, we need to map the behavior. - // Same with the native handler. - get - { - ICredentials? creds; - if (IsSocketHandler) - { - creds = _socketHandler!.Credentials; - } - else - { - creds = (ICredentials)GetNativeHandlerProp("Credentials"); - } - - return creds == CredentialCache.DefaultCredentials; - } - set - { - if (value) - { - if (IsSocketHandler) - { - _socketHandler!.Credentials = CredentialCache.DefaultCredentials; - } - else - { - SetNativeHandlerProp("Credentials", CredentialCache.DefaultCredentials); - } - } - else - { - if (IsSocketHandler) - { - if (_socketHandler!.Credentials == CredentialCache.DefaultCredentials) - { - _socketHandler!.Credentials = null; - } - } - else - { - ICredentials? creds = (ICredentials)GetNativeHandlerProp("Credentials"); - - if (creds == CredentialCache.DefaultCredentials) - { - SetNativeHandlerProp("Credentials", null); - } - } - } - } - } - - [UnsupportedOSPlatform("browser")] - public ICredentials? Credentials - { - get - { - if (IsSocketHandler) - { - return _socketHandler!.Credentials; - } - else - { - return (ICredentials)GetNativeHandlerProp("Credentials"); - } - - } - set - { - if (IsSocketHandler) - { - _socketHandler!.Credentials = value; - } - else - { - SetNativeHandlerProp("Credentials", value); - } - } - } - - public bool AllowAutoRedirect - { - get - { - if (IsSocketHandler) - { - return _socketHandler!.AllowAutoRedirect; - } - else - { - return (bool)GetNativeHandlerProp("AllowAutoRedirect"); - } - } - set - { - if (IsSocketHandler) - { - _socketHandler!.AllowAutoRedirect = value; - } - else - { - SetNativeHandlerProp("AllowAutoRedirect", value); - } - } - } - [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] public int MaxAutomaticRedirections @@ -287,153 +39,10 @@ public int MaxAutomaticRedirections [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] - public int MaxConnectionsPerServer - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - public long MaxRequestContentBufferSize - { - // This property is not supported. In the .NET Framework it was only used when the handler needed to - // automatically buffer the request content. That only happened if neither 'Content-Length' nor - // 'Transfer-Encoding: chunked' request headers were specified. So, the handler thus needed to buffer - // in the request content to determine its length and then would choose 'Content-Length' semantics when - // POST'ing. In .NET Core, the handler will resolve the ambiguity by always choosing - // 'Transfer-Encoding: chunked'. The handler will never automatically buffer in the request content. - get - { - return 0; // Returning zero is appropriate since in .NET Framework it means no limit. - } - - set - { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value)); - } - - if (value > HttpContent.MaxBufferSize) - { - throw new ArgumentOutOfRangeException(nameof(value), value, - SR.Format(CultureInfo.InvariantCulture, SR.net_http_content_buffersize_limit, - HttpContent.MaxBufferSize)); - } - - CheckDisposed(); - - // No-op on property setter. - } - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public int MaxResponseHeadersLength - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - public ClientCertificateOption ClientCertificateOptions - { - get => throw new PlatformNotSupportedException(); - set - { - throw new PlatformNotSupportedException(); - } - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public X509CertificateCollection ClientCertificates - { - get - { - throw new PlatformNotSupportedException(); - } - } - - // this may be able to map somehow to NSUrlSessionHandlerTrustOverrideCallback? - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public Func? ServerCertificateCustomValidationCallback - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public bool CheckCertificateRevocationList - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public SslProtocols SslProtocols + public bool PreAuthenticate { get => throw new PlatformNotSupportedException(); set => throw new PlatformNotSupportedException(); } - - [UnsupportedOSPlatform("ios")] - public IDictionary Properties => throw new PlatformNotSupportedException(); - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - protected internal override HttpResponseMessage Send(HttpRequestMessage request, - CancellationToken cancellationToken) - { - throw new PlatformNotSupportedException(); - } - - protected internal override Task SendAsync(HttpRequestMessage request, - CancellationToken cancellationToken) - { - if (DiagnosticsHandler.IsEnabled() && _diagnosticsHandler != null) - { - return _diagnosticsHandler.SendAsync(request, cancellationToken); - } - - if (IsSocketHandler) - { - return _socketHandler!.SendAsync(request, cancellationToken); - } - else - { - return (Task)InvokeNativeHandlerMethod("SendAsync", request, cancellationToken); - } - } - - [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] - public static Func DangerousAcceptAnyServerCertificateValidator => - throw new PlatformNotSupportedException(); - - // move these to a common place - private object GetNativeHandlerProp(string name) - { - return _appleHandler!.GetType()!.GetProperty(name)!.GetValue(_appleHandler, null)!; - } - - private void SetNativeHandlerProp(string name, object? value) - { - _appleHandler!.GetType()!.GetProperty(name)!.SetValue(_appleHandler, value!); - } - - private object InvokeNativeHandlerMethod(string name, params object[] parameters) - { - return _appleHandler!.GetType()!.GetMethod(name)!.Invoke(_appleHandler, parameters)!; - } - - private void CheckDisposed() - { - if (_disposed) - { - throw new ObjectDisposedException(GetType().ToString()); - } - } } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index 36bde1b5aed80d..83c6e388a0232d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -2,12 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] private object CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index d27e76237afef6..2d86dcc7085fb3 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -300,6 +300,7 @@ public SslProtocols SslProtocols [UnsupportedOSPlatform("ios")] public IDictionary Properties => _underlyingHandler.Properties; + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] [UnsupportedOSPlatform("ios")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index a95d7c15f4bad9..ca38e6b6dfbecb 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -2,12 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] private object CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index 439005608c9e78..007148dcc2ed54 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -2,12 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", + Justification = "Unused fields don't make a difference for hashcode quality")] private object CreateNativeHandler() { if (_underlyingHandlerMethod == null) From 7fc9474cdd39ffe136e152ea9a2686bd70ac1ae5 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Wed, 30 Jun 2021 21:29:24 -0400 Subject: [PATCH 04/13] Bit more cleanup --- .../System/Net/Http/HttpClientHandler.AnyMobile.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 2c89cc9b1cd681..410f53a9b850d2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -40,7 +40,7 @@ public HttpClientHandler() handler = (HttpMessageHandler)_underlyingHandler; } - if (DiagnosticsHandler.IsGloballyEnabled()) + if (DiagnosticsHandler.IsGloballyEnabled) { _diagnosticsHandler = new DiagnosticsHandler(handler); } @@ -369,9 +369,9 @@ protected internal override HttpResponseMessage Send(HttpRequestMessage request, protected internal override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - if (DiagnosticsHandler.IsEnabled() && _diagnosticsHandler != null) + if (DiagnosticsHandler.IsGloballyEnabled && _diagnosticsHandler != null) { - return _diagnosticsHandler.SendAsync(request, cancellationToken); + return _diagnosticsHandler!.SendAsync(request, cancellationToken); } if (IsSocketHandler) @@ -398,6 +398,11 @@ private void CheckDisposed() } } + protected object? GetUnderlyingHandler() + { + return _underlyingHandler!; + } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] private object GetNativeHandlerProp(string name) From d0879c9b8476a6764acb52b20504cf76ecba8ec4 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Thu, 1 Jul 2021 23:36:34 -0400 Subject: [PATCH 05/13] Few adjustments --- .../System.Net.Http/ref/System.Net.Http.cs | 2 + .../Net/Http/HttpClientHandler.Android.cs | 13 +++--- .../Net/Http/HttpClientHandler.AnyMobile.cs | 43 ++++++++----------- .../Net/Http/HttpClientHandler.Apple.cs | 1 - .../Net/Http/HttpClientHandler.MacCatalyst.cs | 4 +- .../System/Net/Http/HttpClientHandler.iOS.cs | 4 +- .../System/Net/Http/HttpClientHandler.tvOS.cs | 4 +- 7 files changed, 33 insertions(+), 38 deletions(-) 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 0f0bde634edc1d..4cc76ff87c963e 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -163,6 +163,8 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } + protected static bool IsSocketHandler { get { throw null; } } + protected HttpMessageHandler? GetUnderlyingHandler() { throw null; } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs index 317f144829f7ca..aff1afe3b02d23 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs @@ -12,7 +12,6 @@ public partial class HttpClientHandler : HttpMessageHandler { // not sure public virtual bool SupportsAutomaticDecompression => true; - public virtual bool SupportsProxy => true; public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] @@ -38,7 +37,7 @@ public DecompressionMethods AutomaticDecompression } else { - SetNativeHandlerProp("AutomaticDecompression", value); + InvokeNativeHandlerMethod("set_AutomaticDecompression", value); } } } @@ -66,7 +65,7 @@ public bool UseProxy } else { - SetNativeHandlerProp("UseProxy", value); + InvokeNativeHandlerMethod("set_UseProxy", value); } } } @@ -94,7 +93,7 @@ public bool PreAuthenticate } else { - SetNativeHandlerProp("PreAuthenticate", value); + InvokeNativeHandlerMethod("set_PreAuthenticate", value); } } } @@ -122,14 +121,14 @@ public int MaxAutomaticRedirections } else { - SetNativeHandlerProp("MaxAutomaticRedirections", value); + InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); } } } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] - private object CreateNativeHandler() + private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) { @@ -137,7 +136,7 @@ private object CreateNativeHandler() _underlyingHandlerMethod = androidEnv!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return _underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 410f53a9b850d2..35aee974e585f7 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -20,7 +20,7 @@ public partial class HttpClientHandler : HttpMessageHandler private readonly SocketsHttpHandler? _socketHandler; private readonly DiagnosticsHandler? _diagnosticsHandler; - private readonly object? _underlyingHandler; + private readonly HttpMessageHandler? _underlyingHandler; private static MethodInfo? _underlyingHandlerMethod; private volatile bool _disposed; @@ -37,7 +37,7 @@ public HttpClientHandler() else { _underlyingHandler = CreateNativeHandler(); - handler = (HttpMessageHandler)_underlyingHandler; + handler = _underlyingHandler; } if (DiagnosticsHandler.IsGloballyEnabled) @@ -58,14 +58,16 @@ protected override void Dispose(bool disposing) } else { - ((HttpMessageHandler)_underlyingHandler!)!.Dispose(); + _underlyingHandler!.Dispose(); } } base.Dispose(disposing); } - protected static bool IsSocketHandler => IsNativeHandlerEnabled(); + public virtual bool SupportsProxy => false; + + protected static bool IsSocketHandler => IsSocketHandlerEnabled(); [UnsupportedOSPlatform("browser")] public bool UseCookies @@ -89,7 +91,7 @@ public bool UseCookies } else { - SetNativeHandlerProp("UseCookies", value); + InvokeNativeHandlerMethod("set_UseCookies", value); } } } @@ -121,7 +123,7 @@ public CookieContainer CookieContainer } else { - SetNativeHandlerProp("CookieContainer", value); + InvokeNativeHandlerMethod("set_CookieContainer", value); } } } @@ -174,7 +176,7 @@ public bool UseDefaultCredentials } else { - SetNativeHandlerProp("Credentials", CredentialCache.DefaultCredentials); + InvokeNativeHandlerMethod("set_Credentials", CredentialCache.DefaultCredentials); } } else @@ -192,7 +194,7 @@ public bool UseDefaultCredentials if (creds == CredentialCache.DefaultCredentials) { - SetNativeHandlerProp("Credentials", null); + InvokeNativeHandlerMethod("set_Credentials", null!); } } } @@ -222,7 +224,7 @@ public ICredentials? Credentials } else { - SetNativeHandlerProp("Credentials", value); + InvokeNativeHandlerMethod("set_Credentials", value!); } } } @@ -248,7 +250,7 @@ public bool AllowAutoRedirect } else { - SetNativeHandlerProp("AllowAutoRedirect", value); + InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); } } } @@ -380,7 +382,7 @@ protected internal override Task SendAsync(HttpRequestMessa } else { - return (Task)InvokeNativeHandlerMethod("SendAsync", request, cancellationToken); + return _underlyingHandler!.SendAsync(request, cancellationToken); } } @@ -398,7 +400,7 @@ private void CheckDisposed() } } - protected object? GetUnderlyingHandler() + protected HttpMessageHandler? GetUnderlyingHandler() { return _underlyingHandler!; } @@ -412,27 +414,20 @@ private object GetNativeHandlerProp(string name) [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] - private void SetNativeHandlerProp(string name, object? value) - { - _underlyingHandler!.GetType()!.GetProperty(name)!.SetValue(_underlyingHandler, value!); - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] - private object InvokeNativeHandlerMethod(string name, params object[] parameters) + private object InvokeNativeHandlerMethod(string name, params object?[] parameters) { return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; } // check to see if this is linker friendly or not. - private static bool IsNativeHandlerEnabled() + private static bool IsSocketHandlerEnabled() { - if (!AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool isEnabled)) + if (!AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool isNativeHandlerEnabled)) { - return false; + return true; } - return isEnabled; + return !isNativeHandlerEnabled; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs index 8246cdc35ca0a2..93e61cbe292822 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -10,7 +10,6 @@ public partial class HttpClientHandler : HttpMessageHandler { // not sure public virtual bool SupportsAutomaticDecompression => false; - public virtual bool SupportsProxy => false; public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index 83c6e388a0232d..881ec29f1e7fe7 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -11,7 +11,7 @@ public partial class HttpClientHandler : HttpMessageHandler { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] - private object CreateNativeHandler() + private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) { @@ -19,7 +19,7 @@ private object CreateNativeHandler() _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return _underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index ca38e6b6dfbecb..fd4c3f713b0fde 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -11,7 +11,7 @@ public partial class HttpClientHandler : HttpMessageHandler { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] - private object CreateNativeHandler() + private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) { @@ -19,7 +19,7 @@ private object CreateNativeHandler() _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return _underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index 007148dcc2ed54..f604312d903f2d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -11,7 +11,7 @@ public partial class HttpClientHandler : HttpMessageHandler { [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Unused fields don't make a difference for hashcode quality")] - private object CreateNativeHandler() + private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) { @@ -19,7 +19,7 @@ private object CreateNativeHandler() _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return _underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file From b15997385c8ac8b2c03bad7584808bdf82fe98c6 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Wed, 7 Jul 2021 09:51:20 -0400 Subject: [PATCH 06/13] Latest round of updates --- .../System.Net.Http/ref/System.Net.Http.cs | 78 +++++++++++---- .../Net/Http/HttpClientHandler.Android.cs | 90 ++++++++++++++--- .../Net/Http/HttpClientHandler.AnyMobile.cs | 99 ++++++++++--------- .../Net/Http/HttpClientHandler.Apple.cs | 16 ++- .../Net/Http/HttpClientHandler.MacCatalyst.cs | 28 +++++- .../src/System/Net/Http/HttpClientHandler.cs | 74 +++++++++++--- .../System/Net/Http/HttpClientHandler.iOS.cs | 28 +++++- .../System/Net/Http/HttpClientHandler.tvOS.cs | 28 +++++- 8 files changed, 342 insertions(+), 99 deletions(-) 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 4cc76ff87c963e..e4f59ecf1e20d0 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -109,48 +109,88 @@ public partial class HttpClientHandler : System.Net.Http.HttpMessageHandler public HttpClientHandler() { } public bool AllowAutoRedirect { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public bool CheckCertificateRevocationList { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Net.Http.ClientCertificateOption ClientCertificateOptions { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.CookieContainer CookieContainer { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.ICredentials? Credentials { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public static System.Func DangerousAcceptAnyServerCertificateValidator { get { throw null; } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Net.ICredentials? DefaultProxyCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public int MaxConnectionsPerServer { get { throw null; } set { } } public long MaxRequestContentBufferSize { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public int MaxResponseHeadersLength { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get { throw null; } set { } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Collections.Generic.IDictionary Properties { get { throw null; } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Net.IWebProxy? Proxy { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Func? ServerCertificateCustomValidationCallback { get { throw null; } set { } } + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public System.Security.Authentication.SslProtocols SslProtocols { get { throw null; } set { } } public virtual bool SupportsAutomaticDecompression { get { throw null; } } public virtual bool SupportsProxy { get { throw null; } } @@ -160,14 +200,16 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public bool UseDefaultCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } - protected static bool IsSocketHandler { get { throw null; } } - protected HttpMessageHandler? GetUnderlyingHandler() { throw null; } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] + //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] protected internal override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; } protected internal override System.Threading.Tasks.Task SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs index aff1afe3b02d23..5b6fb0c08c3b49 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs @@ -10,12 +10,16 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + private static MethodInfo? _underlyingHandlerMethod; + // not sure public virtual bool SupportsAutomaticDecompression => true; public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get @@ -26,8 +30,13 @@ public DecompressionMethods AutomaticDecompression } else { - return (DecompressionMethods)GetNativeHandlerProp("AutomaticDecompression"); + return GetAutomaticDecompression(); } + + //[DynamicDependency("get_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", + // Justification = "Xamarin dependencies are not available during libraries build")] + DecompressionMethods GetAutomaticDecompression() => (DecompressionMethods)InvokeNativeHandlerMethod("get_AutomaticDecompression"); } set { @@ -37,13 +46,18 @@ public DecompressionMethods AutomaticDecompression } else { - InvokeNativeHandlerMethod("set_AutomaticDecompression", value); + SetAutomaticDecompression(value); } + + //[DynamicDependency("set_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + void SetAutomaticDecompression(DecompressionMethods value) => InvokeNativeHandlerMethod("set_AutomaticDecompression", value); } } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get @@ -54,8 +68,11 @@ public bool UseProxy } else { - return (bool)GetNativeHandlerProp("UseProxy"); + return GetUseProxy(); } + + //[DynamicDependency("get_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + bool GetUseProxy() => (bool)InvokeNativeHandlerMethod("get_UseProxy"); } set { @@ -65,13 +82,18 @@ public bool UseProxy } else { - InvokeNativeHandlerMethod("set_UseProxy", value); + SetUseProxy(value); } + + //[DynamicDependency("set_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + void SetUseProxy(bool value) => InvokeNativeHandlerMethod("set_UseProxy", value); } } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get @@ -82,8 +104,13 @@ public bool PreAuthenticate } else { - return (bool)GetNativeHandlerProp("PreAuthenticate"); + return GetPreAuthenticate(); } + + //[DynamicDependency("get_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", + // Justification = "Xamarin dependencies are not available during libraries build")] + bool GetPreAuthenticate() => (bool)InvokeNativeHandlerMethod("get_PreAuthenticate"); } set { @@ -93,13 +120,18 @@ public bool PreAuthenticate } else { - InvokeNativeHandlerMethod("set_PreAuthenticate", value); + SetPreAuthenticate(value); } + + //[DynamicDependency("set_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + void SetPreAuthenticate(bool value) => InvokeNativeHandlerMethod("set_PreAuthenticate", value); } } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get @@ -110,8 +142,13 @@ public int MaxAutomaticRedirections } else { - return (int)GetNativeHandlerProp("MaxAutomaticRedirections"); + return GetMaxAutomaticRedirections(); } + + //[DynamicDependency("get_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", + // Justification = "Xamarin dependencies are not available during libraries build")] + int GetMaxAutomaticRedirections() => (int)InvokeNativeHandlerMethod("get_MaxAutomaticRedirections"); } set { @@ -121,13 +158,40 @@ public int MaxAutomaticRedirections } else { - InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); + SetMaxAutomaticRedirections(value); } + + //[DynamicDependency("set_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + void SetMaxAutomaticRedirections(int value) => InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); } } + //[DynamicDependency("get_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); + + //[DynamicDependency("set_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); + + //[DynamicDependency("get_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); + + //[DynamicDependency("set_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); + + //[DynamicDependency("get_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); + + //[DynamicDependency("set_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + + //[DynamicDependency("get_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); + + //[DynamicDependency("set_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] + Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 35aee974e585f7..97562c67d5268b 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -21,7 +21,6 @@ public partial class HttpClientHandler : HttpMessageHandler private readonly DiagnosticsHandler? _diagnosticsHandler; private readonly HttpMessageHandler? _underlyingHandler; - private static MethodInfo? _underlyingHandlerMethod; private volatile bool _disposed; @@ -67,8 +66,6 @@ protected override void Dispose(bool disposing) public virtual bool SupportsProxy => false; - protected static bool IsSocketHandler => IsSocketHandlerEnabled(); - [UnsupportedOSPlatform("browser")] public bool UseCookies { @@ -80,7 +77,7 @@ public bool UseCookies } else { - return (bool)GetNativeHandlerProp("UseCookies"); + return GetUseCookies(); } } set @@ -91,7 +88,7 @@ public bool UseCookies } else { - InvokeNativeHandlerMethod("set_UseCookies", value); + SetUseCookies(value); } } } @@ -107,7 +104,7 @@ public CookieContainer CookieContainer } else { - return (CookieContainer)GetNativeHandlerProp("CookieContainer"); + return GetCookieContainer(); } } set @@ -123,14 +120,16 @@ public CookieContainer CookieContainer } else { - InvokeNativeHandlerMethod("set_CookieContainer", value); + SetCookieContainer(value); } } } [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public IWebProxy? Proxy { get => throw new PlatformNotSupportedException(); @@ -139,7 +138,9 @@ public IWebProxy? Proxy [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public ICredentials? DefaultProxyCredentials { get => throw new PlatformNotSupportedException(); @@ -161,7 +162,7 @@ public bool UseDefaultCredentials } else { - creds = (ICredentials)GetNativeHandlerProp("Credentials"); + creds = GetCredentials(); } return creds == CredentialCache.DefaultCredentials; @@ -176,7 +177,7 @@ public bool UseDefaultCredentials } else { - InvokeNativeHandlerMethod("set_Credentials", CredentialCache.DefaultCredentials); + SetCredentials(CredentialCache.DefaultCredentials); } } else @@ -190,11 +191,11 @@ public bool UseDefaultCredentials } else { - ICredentials? creds = (ICredentials)GetNativeHandlerProp("Credentials"); + ICredentials? creds = GetCredentials(); if (creds == CredentialCache.DefaultCredentials) { - InvokeNativeHandlerMethod("set_Credentials", null!); + SetCredentials(null!); } } } @@ -212,7 +213,7 @@ public ICredentials? Credentials } else { - return (ICredentials)GetNativeHandlerProp("Credentials"); + return GetCredentials(); } } @@ -224,7 +225,7 @@ public ICredentials? Credentials } else { - InvokeNativeHandlerMethod("set_Credentials", value!); + SetCredentials(value!); } } } @@ -239,7 +240,7 @@ public bool AllowAutoRedirect } else { - return (bool)GetNativeHandlerProp("AllowAutoRedirect"); + return GetAllowAutoRedirect(); } } set @@ -250,14 +251,16 @@ public bool AllowAutoRedirect } else { - InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + SetAllowAutoRedirect(value); } } } [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxConnectionsPerServer { get => throw new PlatformNotSupportedException(); @@ -299,7 +302,9 @@ public long MaxRequestContentBufferSize [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxResponseHeadersLength { get => throw new PlatformNotSupportedException(); @@ -307,19 +312,20 @@ public int MaxResponseHeadersLength } [UnsupportedOSPlatform("android")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public ClientCertificateOption ClientCertificateOptions { get => throw new PlatformNotSupportedException(); - set - { - throw new PlatformNotSupportedException(); - } + set => throw new PlatformNotSupportedException(); } [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public X509CertificateCollection ClientCertificates { get @@ -330,7 +336,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public Func? ServerCertificateCustomValidationCallback { get => throw new PlatformNotSupportedException(); @@ -339,7 +347,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool CheckCertificateRevocationList { get => throw new PlatformNotSupportedException(); @@ -348,7 +358,9 @@ public bool CheckCertificateRevocationList [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public SslProtocols SslProtocols { get => throw new PlatformNotSupportedException(); @@ -356,12 +368,16 @@ public SslProtocols SslProtocols } [UnsupportedOSPlatform("android")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public IDictionary Properties => throw new PlatformNotSupportedException(); - [UnsupportedOSPlatform("android")] + //[UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) { @@ -388,7 +404,9 @@ protected internal override Task SendAsync(HttpRequestMessa [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public static Func DangerousAcceptAnyServerCertificateValidator => throw new PlatformNotSupportedException(); @@ -400,26 +418,15 @@ private void CheckDisposed() } } - protected HttpMessageHandler? GetUnderlyingHandler() - { - return _underlyingHandler!; - } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] - private object GetNativeHandlerProp(string name) - { - return _underlyingHandler!.GetType()!.GetProperty(name)!.GetValue(_underlyingHandler, null)!; - } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] + Justification = "Xamarin dependencies are not available during libraries build")] private object InvokeNativeHandlerMethod(string name, params object?[] parameters) { return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; } - // check to see if this is linker friendly or not. + private static bool IsSocketHandler => IsSocketHandlerEnabled(); + private static bool IsSocketHandlerEnabled() { if (!AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool isNativeHandlerEnabled)) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs index 93e61cbe292822..5a82c26531d380 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -13,7 +13,9 @@ public partial class HttpClientHandler : HttpMessageHandler public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get => throw new PlatformNotSupportedException(); @@ -21,7 +23,9 @@ public DecompressionMethods AutomaticDecompression } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get => throw new PlatformNotSupportedException(); @@ -29,7 +33,9 @@ public bool UseProxy } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get => throw new PlatformNotSupportedException(); @@ -37,7 +43,9 @@ public int MaxAutomaticRedirections } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get => throw new PlatformNotSupportedException(); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index 881ec29f1e7fe7..de2ddcd4c03ef0 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -9,8 +9,34 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + private static MethodInfo? _underlyingHandlerMethod; + + [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); + + [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); + + [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); + + [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); + + [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); + + [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + + [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); + + [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] + Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index 0b5b0d9821de2c..97db29cf004722 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -73,7 +73,9 @@ public CookieContainer CookieContainer } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get => _underlyingHandler.AutomaticDecompression; @@ -81,23 +83,31 @@ public DecompressionMethods AutomaticDecompression } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get => _underlyingHandler.UseProxy; set => _underlyingHandler.UseProxy = value; } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public IWebProxy? Proxy { get => _underlyingHandler.Proxy; set => _underlyingHandler.Proxy = value; } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public ICredentials? DefaultProxyCredentials { get => _underlyingHandler.DefaultProxyCredentials; @@ -105,7 +115,9 @@ public ICredentials? DefaultProxyCredentials } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get => _underlyingHandler.PreAuthenticate; @@ -149,15 +161,20 @@ public bool AllowAutoRedirect } [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get => _underlyingHandler.MaxAutomaticRedirections; set => _underlyingHandler.MaxAutomaticRedirections = value; } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxConnectionsPerServer { get => _underlyingHandler.MaxConnectionsPerServer; @@ -197,14 +214,21 @@ public long MaxRequestContentBufferSize } } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public int MaxResponseHeadersLength { get => _underlyingHandler.MaxResponseHeadersLength; set => _underlyingHandler.MaxResponseHeadersLength = value; } + [UnsupportedOSPlatform("android")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public ClientCertificateOption ClientCertificateOptions { get => _clientCertificateOptions; @@ -238,8 +262,11 @@ public ClientCertificateOption ClientCertificateOptions } } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public X509CertificateCollection ClientCertificates { get @@ -254,8 +281,11 @@ public X509CertificateCollection ClientCertificates } } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public Func? ServerCertificateCustomValidationCallback { #if TARGET_BROWSER @@ -273,8 +303,11 @@ public X509CertificateCollection ClientCertificates #endif } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public bool CheckCertificateRevocationList { get => _underlyingHandler.SslOptions.CertificateRevocationCheckMode == X509RevocationMode.Online; @@ -285,8 +318,11 @@ public bool CheckCertificateRevocationList } } + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public SslProtocols SslProtocols { get => _underlyingHandler.SslOptions.EnabledSslProtocols; @@ -297,12 +333,17 @@ public SslProtocols SslProtocols } } - [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("android")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public IDictionary Properties => _underlyingHandler.Properties; [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) => _handler.Send(request, cancellationToken); @@ -311,8 +352,11 @@ protected internal override Task SendAsync(HttpRequestMessa // lazy-load the validator func so it can be trimmed by the ILLinker if it isn't used. private static Func? s_dangerousAcceptAnyServerCertificateValidator; + [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - [UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("ios")] + //[UnsupportedOSPlatform("tvos")] + //[UnsupportedOSPlatform("maccatalyst")] public static Func DangerousAcceptAnyServerCertificateValidator => Volatile.Read(ref s_dangerousAcceptAnyServerCertificateValidator) ?? Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index fd4c3f713b0fde..f0b37b3da71c60 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -9,8 +9,34 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + private static MethodInfo? _underlyingHandlerMethod; + + [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); + + [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); + + [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); + + [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); + + [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); + + [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + + [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); + + [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] + Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index f604312d903f2d..fd9708df632564 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -9,8 +9,34 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { + private static MethodInfo? _underlyingHandlerMethod; + + [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); + + [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); + + [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); + + [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); + + [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); + + [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + + [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); + + [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Unused fields don't make a difference for hashcode quality")] + Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) From 154648196cb1afd5ada1fec77e5e9d8b29575765 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Thu, 8 Jul 2021 23:07:20 -0400 Subject: [PATCH 07/13] Added linker suppressions to the native handler method calls. Re-enabled unsupported attributes as only Send is impacted by the api compat bug --- .../System.Net.Http/ref/System.Net.Http.cs | 91 ++++++------ .../ILLink.Suppressions.LibraryBuild.xml | 135 +++++++++++++++++- .../Net/Http/HttpClientHandler.Android.cs | 132 ++++++++++------- .../Net/Http/HttpClientHandler.AnyMobile.cs | 79 +++++----- .../Net/Http/HttpClientHandler.Apple.cs | 36 +++-- .../Net/Http/HttpClientHandler.MacCatalyst.cs | 4 +- .../src/System/Net/Http/HttpClientHandler.cs | 97 +++++++------ .../System/Net/Http/HttpClientHandler.iOS.cs | 4 +- .../System/Net/Http/HttpClientHandler.tvOS.cs | 4 +- 9 files changed, 369 insertions(+), 213 deletions(-) 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 e4f59ecf1e20d0..145a5dde51f2fc 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -109,26 +109,26 @@ public partial class HttpClientHandler : System.Net.Http.HttpMessageHandler public HttpClientHandler() { } public bool AllowAutoRedirect { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public bool CheckCertificateRevocationList { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Net.Http.ClientCertificateOption ClientCertificateOptions { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public System.Net.CookieContainer CookieContainer { get { throw null; } set { } } @@ -136,61 +136,60 @@ public HttpClientHandler() { } public System.Net.ICredentials? Credentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public static System.Func DangerousAcceptAnyServerCertificateValidator { get { throw null; } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Net.ICredentials? DefaultProxyCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public int MaxAutomaticRedirections { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public int MaxConnectionsPerServer { get { throw null; } set { } } public long MaxRequestContentBufferSize { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public int MaxResponseHeadersLength { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public bool PreAuthenticate { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Collections.Generic.IDictionary Properties { get { throw null; } } - [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Net.IWebProxy? Proxy { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Func? ServerCertificateCustomValidationCallback { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public System.Security.Authentication.SslProtocols SslProtocols { get { throw null; } set { } } public virtual bool SupportsAutomaticDecompression { get { throw null; } } public virtual bool SupportsProxy { get { throw null; } } @@ -200,9 +199,9 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public bool UseDefaultCredentials { get { throw null; } set { } } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")] - //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } //[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] diff --git a/src/libraries/System.Net.Http/src/ILLink/ILLink.Suppressions.LibraryBuild.xml b/src/libraries/System.Net.Http/src/ILLink/ILLink.Suppressions.LibraryBuild.xml index e11daf0bd241f9..dbeca2aa0a94d8 100644 --- a/src/libraries/System.Net.Http/src/ILLink/ILLink.Suppressions.LibraryBuild.xml +++ b/src/libraries/System.Net.Http/src/ILLink/ILLink.Suppressions.LibraryBuild.xml @@ -5,7 +5,140 @@ ILLink IL2075 member - M:System.Net.Http.HttpClient.CreateDefaultHandler() + M:System.Net.Http.HttpClientHandler.CreateNativeHandler() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2075 + member + M:System.Net.Http.HttpClientHandler.InvokeNativeHandlerMethod(System.String,System.Object[]) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetUseCookies() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetUseCookies(System.Boolean) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetCookieContainer() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetCookieContainer(System.Net.CookieContainer) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetAllowAutoRedirect() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetAllowAutoRedirect(System.Boolean) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetCredentials() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetCredentials(System.Net.ICredentials) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetAutomaticDecompression() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetAutomaticDecompression(System.Net.DecompressionMethods) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetUseProxy() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetUseProxy(System.Boolean) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetProxy() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetProxy(System.Net.IWebProxy) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetPreAuthenticate() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetPreAuthenticate(System.Boolean) + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.GetMaxAutomaticRedirections() + The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. + + + ILLink + IL2035 + member + M:System.Net.Http.HttpClientHandler.SetMaxAutomaticRedirections(System.Int32) The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app. diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs index 5b6fb0c08c3b49..f35c45a66cd3bf 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs @@ -12,14 +12,14 @@ public partial class HttpClientHandler : HttpMessageHandler { private static MethodInfo? _underlyingHandlerMethod; - // not sure public virtual bool SupportsAutomaticDecompression => true; + public virtual bool SupportsProxy => true; public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get @@ -32,11 +32,6 @@ public DecompressionMethods AutomaticDecompression { return GetAutomaticDecompression(); } - - //[DynamicDependency("get_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", - // Justification = "Xamarin dependencies are not available during libraries build")] - DecompressionMethods GetAutomaticDecompression() => (DecompressionMethods)InvokeNativeHandlerMethod("get_AutomaticDecompression"); } set { @@ -48,16 +43,13 @@ public DecompressionMethods AutomaticDecompression { SetAutomaticDecompression(value); } - - //[DynamicDependency("set_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - void SetAutomaticDecompression(DecompressionMethods value) => InvokeNativeHandlerMethod("set_AutomaticDecompression", value); } } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get @@ -70,9 +62,6 @@ public bool UseProxy { return GetUseProxy(); } - - //[DynamicDependency("get_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - bool GetUseProxy() => (bool)InvokeNativeHandlerMethod("get_UseProxy"); } set { @@ -84,16 +73,43 @@ public bool UseProxy { SetUseProxy(value); } + } + } - //[DynamicDependency("set_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - void SetUseProxy(bool value) => InvokeNativeHandlerMethod("set_UseProxy", value); + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] + public IWebProxy? Proxy + { + get + { + if (IsSocketHandler) + { + return _socketHandler!.Proxy; + } + else + { + return GetProxy(); + } + } + set + { + if (IsSocketHandler) + { + _socketHandler!.Proxy = value; + } + else + { + SetProxy(value!); + } } } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get @@ -106,11 +122,6 @@ public bool PreAuthenticate { return GetPreAuthenticate(); } - - //[DynamicDependency("get_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", - // Justification = "Xamarin dependencies are not available during libraries build")] - bool GetPreAuthenticate() => (bool)InvokeNativeHandlerMethod("get_PreAuthenticate"); } set { @@ -122,16 +133,13 @@ public bool PreAuthenticate { SetPreAuthenticate(value); } - - //[DynamicDependency("set_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - void SetPreAuthenticate(bool value) => InvokeNativeHandlerMethod("set_PreAuthenticate", value); } } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get @@ -144,11 +152,6 @@ public int MaxAutomaticRedirections { return GetMaxAutomaticRedirections(); } - - //[DynamicDependency("get_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - //[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2035:Unresolved External Assemblies", - // Justification = "Xamarin dependencies are not available during libraries build")] - int GetMaxAutomaticRedirections() => (int)InvokeNativeHandlerMethod("get_MaxAutomaticRedirections"); } set { @@ -160,38 +163,63 @@ public int MaxAutomaticRedirections { SetMaxAutomaticRedirections(value); } - - //[DynamicDependency("set_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] - void SetMaxAutomaticRedirections(int value) => InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); } } - //[DynamicDependency("get_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private int GetMaxAutomaticRedirections() => (int)InvokeNativeHandlerMethod("get_MaxAutomaticRedirections"); + + [DynamicDependency("set_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetMaxAutomaticRedirections(int value) => InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); + + [DynamicDependency("get_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private bool GetPreAuthenticate() => (bool)InvokeNativeHandlerMethod("get_PreAuthenticate"); + + [DynamicDependency("set_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetPreAuthenticate(bool value) => InvokeNativeHandlerMethod("set_PreAuthenticate", value); + + [DynamicDependency("get_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private bool GetUseProxy() => (bool)InvokeNativeHandlerMethod("get_UseProxy"); + + [DynamicDependency("set_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetUseProxy(bool value) => InvokeNativeHandlerMethod("set_UseProxy", value); + + [DynamicDependency("get_Proxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private IWebProxy GetProxy() => (IWebProxy)InvokeNativeHandlerMethod("get_Proxy"); + + [DynamicDependency("set_Proxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetProxy(IWebProxy value) => InvokeNativeHandlerMethod("set_Proxy", value); + + [DynamicDependency("get_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private DecompressionMethods GetAutomaticDecompression() => (DecompressionMethods)InvokeNativeHandlerMethod("get_AutomaticDecompression"); + + [DynamicDependency("set_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + private void SetAutomaticDecompression(DecompressionMethods value) => InvokeNativeHandlerMethod("set_AutomaticDecompression", value); + + [DynamicDependency("get_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); - //[DynamicDependency("set_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); - //[DynamicDependency("get_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); - //[DynamicDependency("set_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); - //[DynamicDependency("get_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); - //[DynamicDependency("set_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); - //[DynamicDependency("get_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); - //[DynamicDependency("set_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 97562c67d5268b..7d1b58574a63f4 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -64,8 +64,6 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - public virtual bool SupportsProxy => false; - [UnsupportedOSPlatform("browser")] public bool UseCookies { @@ -127,20 +125,9 @@ public CookieContainer CookieContainer [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] - public IWebProxy? Proxy - { - get => throw new PlatformNotSupportedException(); - set => throw new PlatformNotSupportedException(); - } - - [UnsupportedOSPlatform("android")] - [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public ICredentials? DefaultProxyCredentials { get => throw new PlatformNotSupportedException(); @@ -258,9 +245,9 @@ public bool AllowAutoRedirect [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxConnectionsPerServer { get => throw new PlatformNotSupportedException(); @@ -302,9 +289,9 @@ public long MaxRequestContentBufferSize [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxResponseHeadersLength { get => throw new PlatformNotSupportedException(); @@ -312,9 +299,9 @@ public int MaxResponseHeadersLength } [UnsupportedOSPlatform("android")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public ClientCertificateOption ClientCertificateOptions { get => throw new PlatformNotSupportedException(); @@ -323,9 +310,9 @@ public ClientCertificateOption ClientCertificateOptions [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public X509CertificateCollection ClientCertificates { get @@ -336,9 +323,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public Func? ServerCertificateCustomValidationCallback { get => throw new PlatformNotSupportedException(); @@ -347,9 +334,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool CheckCertificateRevocationList { get => throw new PlatformNotSupportedException(); @@ -358,9 +345,9 @@ public bool CheckCertificateRevocationList [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public SslProtocols SslProtocols { get => throw new PlatformNotSupportedException(); @@ -368,11 +355,15 @@ public SslProtocols SslProtocols } [UnsupportedOSPlatform("android")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public IDictionary Properties => throw new PlatformNotSupportedException(); + // + // Attributes are commented out due to https://github.com/dotnet/arcade/issues/7585 + // API compat will fail until this is fixed + // //[UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] //[UnsupportedOSPlatform("ios")] @@ -404,9 +395,9 @@ protected internal override Task SendAsync(HttpRequestMessa [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public static Func DangerousAcceptAnyServerCertificateValidator => throw new PlatformNotSupportedException(); @@ -418,8 +409,6 @@ private void CheckDisposed() } } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Xamarin dependencies are not available during libraries build")] private object InvokeNativeHandlerMethod(string name, params object?[] parameters) { return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs index 5a82c26531d380..8569f35116c714 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Apple.cs @@ -8,14 +8,14 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - // not sure public virtual bool SupportsAutomaticDecompression => false; + public virtual bool SupportsProxy => false; public virtual bool SupportsRedirectConfiguration => true; [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get => throw new PlatformNotSupportedException(); @@ -23,9 +23,9 @@ public DecompressionMethods AutomaticDecompression } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get => throw new PlatformNotSupportedException(); @@ -33,9 +33,19 @@ public bool UseProxy } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] + public IWebProxy? Proxy + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get => throw new PlatformNotSupportedException(); @@ -43,9 +53,9 @@ public int MaxAutomaticRedirections } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get => throw new PlatformNotSupportedException(); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index de2ddcd4c03ef0..3776955f8feb2d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -27,7 +27,7 @@ public partial class HttpClientHandler : HttpMessageHandler private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] - void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); @@ -35,8 +35,6 @@ public partial class HttpClientHandler : HttpMessageHandler [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs index 97db29cf004722..b37ebf94fc1185 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs @@ -73,9 +73,9 @@ public CookieContainer CookieContainer } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public DecompressionMethods AutomaticDecompression { get => _underlyingHandler.AutomaticDecompression; @@ -83,20 +83,19 @@ public DecompressionMethods AutomaticDecompression } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool UseProxy { get => _underlyingHandler.UseProxy; set => _underlyingHandler.UseProxy = value; } - [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public IWebProxy? Proxy { get => _underlyingHandler.Proxy; @@ -105,9 +104,9 @@ public IWebProxy? Proxy [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public ICredentials? DefaultProxyCredentials { get => _underlyingHandler.DefaultProxyCredentials; @@ -115,9 +114,9 @@ public ICredentials? DefaultProxyCredentials } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool PreAuthenticate { get => _underlyingHandler.PreAuthenticate; @@ -161,9 +160,9 @@ public bool AllowAutoRedirect } [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxAutomaticRedirections { get => _underlyingHandler.MaxAutomaticRedirections; @@ -172,9 +171,9 @@ public int MaxAutomaticRedirections [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxConnectionsPerServer { get => _underlyingHandler.MaxConnectionsPerServer; @@ -216,9 +215,9 @@ public long MaxRequestContentBufferSize [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public int MaxResponseHeadersLength { get => _underlyingHandler.MaxResponseHeadersLength; @@ -226,9 +225,9 @@ public int MaxResponseHeadersLength } [UnsupportedOSPlatform("android")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public ClientCertificateOption ClientCertificateOptions { get => _clientCertificateOptions; @@ -264,9 +263,9 @@ public ClientCertificateOption ClientCertificateOptions [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public X509CertificateCollection ClientCertificates { get @@ -283,9 +282,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public Func? ServerCertificateCustomValidationCallback { #if TARGET_BROWSER @@ -305,9 +304,9 @@ public X509CertificateCollection ClientCertificates [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public bool CheckCertificateRevocationList { get => _underlyingHandler.SslOptions.CertificateRevocationCheckMode == X509RevocationMode.Online; @@ -320,9 +319,9 @@ public bool CheckCertificateRevocationList [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public SslProtocols SslProtocols { get => _underlyingHandler.SslOptions.EnabledSslProtocols; @@ -334,12 +333,16 @@ public SslProtocols SslProtocols } [UnsupportedOSPlatform("android")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public IDictionary Properties => _underlyingHandler.Properties; - [UnsupportedOSPlatform("android")] + // + // Attributes are commented out due to https://github.com/dotnet/arcade/issues/7585 + // API compat will fail until this is fixed + // + //[UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] //[UnsupportedOSPlatform("ios")] //[UnsupportedOSPlatform("tvos")] @@ -354,9 +357,9 @@ protected internal override Task SendAsync(HttpRequestMessa private static Func? s_dangerousAcceptAnyServerCertificateValidator; [UnsupportedOSPlatform("android")] [UnsupportedOSPlatform("browser")] - //[UnsupportedOSPlatform("ios")] - //[UnsupportedOSPlatform("tvos")] - //[UnsupportedOSPlatform("maccatalyst")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] + [UnsupportedOSPlatform("maccatalyst")] public static Func DangerousAcceptAnyServerCertificateValidator => Volatile.Read(ref s_dangerousAcceptAnyServerCertificateValidator) ?? Interlocked.CompareExchange(ref s_dangerousAcceptAnyServerCertificateValidator, delegate { return true; }, null) ?? diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index f0b37b3da71c60..66438cbd8fd623 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -27,7 +27,7 @@ public partial class HttpClientHandler : HttpMessageHandler private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] - void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); @@ -35,8 +35,6 @@ public partial class HttpClientHandler : HttpMessageHandler [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index fd9708df632564..ed1f52d8898ed5 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -27,7 +27,7 @@ public partial class HttpClientHandler : HttpMessageHandler private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] - void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); + private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); @@ -35,8 +35,6 @@ public partial class HttpClientHandler : HttpMessageHandler [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", - Justification = "Xamarin dependencies are not available during libraries build")] private HttpMessageHandler CreateNativeHandler() { if (_underlyingHandlerMethod == null) From 7eb859fded6b66175d8158db655bb28c64c20ec1 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Thu, 8 Jul 2021 23:37:53 -0400 Subject: [PATCH 08/13] Switch IsGloballyEnabled call back --- .../src/System/Net/Http/HttpClientHandler.AnyMobile.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 7d1b58574a63f4..e62c8dc1e4ea6c 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -39,7 +39,7 @@ public HttpClientHandler() handler = _underlyingHandler; } - if (DiagnosticsHandler.IsGloballyEnabled) + if (DiagnosticsHandler.IsGloballyEnabled()) { _diagnosticsHandler = new DiagnosticsHandler(handler); } @@ -378,7 +378,7 @@ protected internal override HttpResponseMessage Send(HttpRequestMessage request, protected internal override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - if (DiagnosticsHandler.IsGloballyEnabled && _diagnosticsHandler != null) + if (DiagnosticsHandler.IsGloballyEnabled() && _diagnosticsHandler != null) { return _diagnosticsHandler!.SendAsync(request, cancellationToken); } From 5f1b02a17cbd90c157af87392ec5dc9511b8eb7d Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Fri, 9 Jul 2021 08:14:25 -0400 Subject: [PATCH 09/13] Remove no longer needed files from unit tests. Add comment about api compat to ref --- src/libraries/System.Net.Http/ref/System.Net.Http.cs | 4 ++++ .../tests/UnitTests/System.Net.Http.Unit.Tests.csproj | 6 ------ 2 files changed, 4 insertions(+), 6 deletions(-) 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 4ded308b4ba90d..e6ae32e87beafe 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -204,6 +204,10 @@ public HttpClientHandler() { } [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")] public bool UseProxy { get { throw null; } set { } } protected override void Dispose(bool disposing) { } + // + // Attributes are commented out due to https://github.com/dotnet/arcade/issues/7585 + // API compat will fail until this is fixed + // //[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")] [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] //[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj index 3b950f2a36c9f6..58e0e5af7fb160 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj +++ b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj @@ -184,12 +184,6 @@ Link="ProductionCode\System\Net\Http\Headers\WarningHeaderValue.cs" /> - - Date: Fri, 9 Jul 2021 14:32:07 -0400 Subject: [PATCH 10/13] Make the linker happy --- eng/testing/tests.mobile.targets | 1 + .../src/ILLink/ILLink.Substitutions.xml | 4 ++-- .../Net/Http/HttpClientHandler.AnyMobile.cs | 14 ++++---------- .../SocketsHttpHandler/RuntimeSettingParser.cs | 16 ++++++++++++++++ src/tests/FunctionalTests/Directory.Build.props | 6 +++--- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/eng/testing/tests.mobile.targets b/eng/testing/tests.mobile.targets index 517ebf719850c1..970832be1cea1a 100644 --- a/eng/testing/tests.mobile.targets +++ b/eng/testing/tests.mobile.targets @@ -22,6 +22,7 @@ false false false + false diff --git a/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml index 314469c96d7775..fbe1cd7c307bb6 100644 --- a/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml +++ b/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml @@ -3,8 +3,8 @@ - - + + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index e62c8dc1e4ea6c..3ee72e2a254b59 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -414,16 +414,10 @@ private object InvokeNativeHandlerMethod(string name, params object?[] parameter return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; } - private static bool IsSocketHandler => IsSocketHandlerEnabled(); + private static bool IsSocketHandler => !IsNativeHandlerEnabled(); - private static bool IsSocketHandlerEnabled() - { - if (!AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool isNativeHandlerEnabled)) - { - return true; - } - - return !isNativeHandlerEnabled; - } + private static bool IsNativeHandlerEnabled() => RuntimeSettingParser.QueryRuntimeSettingSwitch( + "System.Net.Http.UseNativeHttpHandler", + false); } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RuntimeSettingParser.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RuntimeSettingParser.cs index 2fc23028f9b58c..e950e218552a09 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RuntimeSettingParser.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RuntimeSettingParser.cs @@ -35,6 +35,22 @@ public static bool QueryRuntimeSettingSwitch(string appCtxSettingName, string en return defaultValue; } + /// + /// Parse a value from an AppContext switch. + /// + public static bool QueryRuntimeSettingSwitch(string appCtxSettingName, bool defaultValue) + { + bool value; + + // First check for the AppContext switch, giving it priority over the environment variable. + if (AppContext.TryGetSwitch(appCtxSettingName, out value)) + { + return value; + } + + return defaultValue; + } + /// /// Parse an environment variable for an value. /// diff --git a/src/tests/FunctionalTests/Directory.Build.props b/src/tests/FunctionalTests/Directory.Build.props index 5dea1a35e0ccc8..a85fa69ca8edda 100644 --- a/src/tests/FunctionalTests/Directory.Build.props +++ b/src/tests/FunctionalTests/Directory.Build.props @@ -21,7 +21,7 @@ false false - true + false @@ -31,7 +31,7 @@ - + false true false @@ -39,7 +39,7 @@ false true false - true + false From ea461dc899213ab6b55b1c8d9a24d6038fa484eb Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Tue, 13 Jul 2021 16:49:42 -0400 Subject: [PATCH 11/13] Feedback items --- .../src/ILLink/ILLink.Substitutions.xml | 2 +- .../Net/Http/HttpClientHandler.Android.cs | 107 ++++++++-------- .../Net/Http/HttpClientHandler.AnyMobile.cs | 116 ++++++++++-------- .../Net/Http/HttpClientHandler.MacCatalyst.cs | 27 ++-- .../System/Net/Http/HttpClientHandler.iOS.cs | 27 ++-- .../System/Net/Http/HttpClientHandler.tvOS.cs | 27 ++-- 6 files changed, 164 insertions(+), 142 deletions(-) diff --git a/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml index fbe1cd7c307bb6..8af1dcbdfa8a6a 100644 --- a/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml +++ b/src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml @@ -4,7 +4,7 @@ - + diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs index f35c45a66cd3bf..cf9069a9f69b50 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs @@ -10,7 +10,10 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static MethodInfo? _underlyingHandlerMethod; + private static MethodInfo? _nativeHandlerMethod; + + private const string NativeHandlerType = "Xamarin.Android.Net.AndroidMessageHandler"; + private const string AssemblyName = "Mono.Android"; public virtual bool SupportsAutomaticDecompression => true; public virtual bool SupportsProxy => true; @@ -24,24 +27,24 @@ public DecompressionMethods AutomaticDecompression { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.AutomaticDecompression; + return GetAutomaticDecompression(); } else { - return GetAutomaticDecompression(); + return _socketHandler!.AutomaticDecompression; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.AutomaticDecompression = value; + SetAutomaticDecompression(value); } else { - SetAutomaticDecompression(value); + _socketHandler!.AutomaticDecompression = value; } } } @@ -54,24 +57,24 @@ public bool UseProxy { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.UseProxy; + return GetUseProxy(); } else { - return GetUseProxy(); + return _socketHandler!.UseProxy; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.UseProxy = value; + SetUseProxy(value); } else { - SetUseProxy(value); + _socketHandler!.UseProxy = value; } } } @@ -84,24 +87,24 @@ public IWebProxy? Proxy { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.Proxy; + return GetProxy(); } else { - return GetProxy(); + return _socketHandler!.Proxy; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.Proxy = value; + SetProxy(value!); } else { - SetProxy(value!); + _socketHandler!.Proxy = value; } } } @@ -114,24 +117,24 @@ public bool PreAuthenticate { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.PreAuthenticate; + return GetPreAuthenticate(); } else { - return GetPreAuthenticate(); + return _socketHandler!.PreAuthenticate; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.PreAuthenticate = value; + SetPreAuthenticate(value); } else { - SetPreAuthenticate(value); + _socketHandler!.PreAuthenticate = value; } } } @@ -144,91 +147,91 @@ public int MaxAutomaticRedirections { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.MaxAutomaticRedirections; + return GetMaxAutomaticRedirections(); } else { - return GetMaxAutomaticRedirections(); + return _socketHandler!.MaxAutomaticRedirections; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.MaxAutomaticRedirections = value; + SetMaxAutomaticRedirections(value); } else { - SetMaxAutomaticRedirections(value); + _socketHandler!.MaxAutomaticRedirections = value; } } } - [DynamicDependency("get_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_MaxAutomaticRedirections", NativeHandlerType, AssemblyName)] private int GetMaxAutomaticRedirections() => (int)InvokeNativeHandlerMethod("get_MaxAutomaticRedirections"); - [DynamicDependency("set_MaxAutomaticRedirections", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_MaxAutomaticRedirections", NativeHandlerType, AssemblyName)] private void SetMaxAutomaticRedirections(int value) => InvokeNativeHandlerMethod("set_MaxAutomaticRedirections", value); - [DynamicDependency("get_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_PreAuthenticate", NativeHandlerType, AssemblyName)] private bool GetPreAuthenticate() => (bool)InvokeNativeHandlerMethod("get_PreAuthenticate"); - [DynamicDependency("set_PreAuthenticate", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_PreAuthenticate", NativeHandlerType, AssemblyName)] private void SetPreAuthenticate(bool value) => InvokeNativeHandlerMethod("set_PreAuthenticate", value); - [DynamicDependency("get_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_UseProxy", NativeHandlerType, AssemblyName)] private bool GetUseProxy() => (bool)InvokeNativeHandlerMethod("get_UseProxy"); - [DynamicDependency("set_UseProxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_UseProxy", NativeHandlerType, AssemblyName)] private void SetUseProxy(bool value) => InvokeNativeHandlerMethod("set_UseProxy", value); - [DynamicDependency("get_Proxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_Proxy", NativeHandlerType, AssemblyName)] private IWebProxy GetProxy() => (IWebProxy)InvokeNativeHandlerMethod("get_Proxy"); - [DynamicDependency("set_Proxy", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_Proxy", NativeHandlerType, AssemblyName)] private void SetProxy(IWebProxy value) => InvokeNativeHandlerMethod("set_Proxy", value); - [DynamicDependency("get_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_AutomaticDecompression", NativeHandlerType, AssemblyName)] private DecompressionMethods GetAutomaticDecompression() => (DecompressionMethods)InvokeNativeHandlerMethod("get_AutomaticDecompression"); - [DynamicDependency("set_AutomaticDecompression", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_AutomaticDecompression", NativeHandlerType, AssemblyName)] private void SetAutomaticDecompression(DecompressionMethods value) => InvokeNativeHandlerMethod("set_AutomaticDecompression", value); - [DynamicDependency("get_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_UseCookies", NativeHandlerType, AssemblyName)] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); - [DynamicDependency("set_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_UseCookies", NativeHandlerType, AssemblyName)] private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); - [DynamicDependency("get_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_CookieContainer", NativeHandlerType, AssemblyName)] private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); - [DynamicDependency("set_CookieContainer", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_CookieContainer", NativeHandlerType, AssemblyName)] private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); - [DynamicDependency("get_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); - [DynamicDependency("set_AllowAutoRedirect", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); - [DynamicDependency("get_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("get_Credentials", NativeHandlerType, AssemblyName)] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); - [DynamicDependency("set_Credentials", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] + [DynamicDependency("set_Credentials", NativeHandlerType, AssemblyName)] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); private HttpMessageHandler CreateNativeHandler() { - if (_underlyingHandlerMethod == null) + if (_nativeHandlerMethod == null) { Type? androidEnv = Type.GetType("Android.Runtime.AndroidEnvironment, Mono.Android"); - _underlyingHandlerMethod = androidEnv!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + _nativeHandlerMethod = androidEnv!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_nativeHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 3ee72e2a254b59..b17784018bc91a 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -20,7 +21,10 @@ public partial class HttpClientHandler : HttpMessageHandler private readonly SocketsHttpHandler? _socketHandler; private readonly DiagnosticsHandler? _diagnosticsHandler; - private readonly HttpMessageHandler? _underlyingHandler; + private readonly HttpMessageHandler? _nativeHandler; + + private static readonly ConcurrentDictionary s_cachedMethods = + new ConcurrentDictionary(); private volatile bool _disposed; @@ -28,15 +32,15 @@ public HttpClientHandler() { HttpMessageHandler handler; - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler = new SocketsHttpHandler(); - handler = _socketHandler; + _nativeHandler = CreateNativeHandler(); + handler = _nativeHandler; } else { - _underlyingHandler = CreateNativeHandler(); - handler = _underlyingHandler; + _socketHandler = new SocketsHttpHandler(); + handler = _socketHandler; } if (DiagnosticsHandler.IsGloballyEnabled()) @@ -51,13 +55,13 @@ protected override void Dispose(bool disposing) { _disposed = true; - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.Dispose(); + _nativeHandler!.Dispose(); } else { - _underlyingHandler!.Dispose(); + _socketHandler!.Dispose(); } } @@ -69,24 +73,24 @@ public bool UseCookies { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.UseCookies; + return GetUseCookies(); } else { - return GetUseCookies(); + return _socketHandler!.UseCookies; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.UseCookies = value; + SetUseCookies(value); } else { - SetUseCookies(value); + _socketHandler!.UseCookies = value; } } } @@ -96,13 +100,13 @@ public CookieContainer CookieContainer { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.CookieContainer; + return GetCookieContainer(); } else { - return GetCookieContainer(); + return _socketHandler!.CookieContainer; } } set @@ -112,13 +116,13 @@ public CookieContainer CookieContainer throw new ArgumentNullException(nameof(value)); } - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.CookieContainer = value; + SetCookieContainer(value); } else { - SetCookieContainer(value); + _socketHandler!.CookieContainer = value; } } } @@ -143,13 +147,13 @@ public bool UseDefaultCredentials get { ICredentials? creds; - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - creds = _socketHandler!.Credentials; + creds = GetCredentials(); } else { - creds = GetCredentials(); + creds = _socketHandler!.Credentials; } return creds == CredentialCache.DefaultCredentials; @@ -158,31 +162,31 @@ public bool UseDefaultCredentials { if (value) { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.Credentials = CredentialCache.DefaultCredentials; + SetCredentials(CredentialCache.DefaultCredentials); } else { - SetCredentials(CredentialCache.DefaultCredentials); + _socketHandler!.Credentials = CredentialCache.DefaultCredentials; } } else { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - if (_socketHandler!.Credentials == CredentialCache.DefaultCredentials) + ICredentials? creds = GetCredentials(); + + if (creds == CredentialCache.DefaultCredentials) { - _socketHandler!.Credentials = null; + SetCredentials(null!); } } else { - ICredentials? creds = GetCredentials(); - - if (creds == CredentialCache.DefaultCredentials) + if (_socketHandler!.Credentials == CredentialCache.DefaultCredentials) { - SetCredentials(null!); + _socketHandler!.Credentials = null; } } } @@ -194,25 +198,25 @@ public ICredentials? Credentials { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.Credentials; + return GetCredentials(); } else { - return GetCredentials(); + return _socketHandler!.Credentials; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.Credentials = value; + SetCredentials(value!); } else { - SetCredentials(value!); + _socketHandler!.Credentials = value; } } } @@ -221,24 +225,24 @@ public bool AllowAutoRedirect { get { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.AllowAutoRedirect; + return GetAllowAutoRedirect(); } else { - return GetAllowAutoRedirect(); + return _socketHandler!.AllowAutoRedirect; } } set { - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - _socketHandler!.AllowAutoRedirect = value; + SetAllowAutoRedirect(value); } else { - SetAllowAutoRedirect(value); + _socketHandler!.AllowAutoRedirect = value; } } } @@ -383,13 +387,13 @@ protected internal override Task SendAsync(HttpRequestMessa return _diagnosticsHandler!.SendAsync(request, cancellationToken); } - if (IsSocketHandler) + if (IsNativeHandlerEnabled) { - return _socketHandler!.SendAsync(request, cancellationToken); + return _nativeHandler!.SendAsync(request, cancellationToken); } else { - return _underlyingHandler!.SendAsync(request, cancellationToken); + return _socketHandler!.SendAsync(request, cancellationToken); } } @@ -411,12 +415,18 @@ private void CheckDisposed() private object InvokeNativeHandlerMethod(string name, params object?[] parameters) { - return _underlyingHandler!.GetType()!.GetMethod(name)!.Invoke(_underlyingHandler, parameters)!; - } + MethodInfo? method; - private static bool IsSocketHandler => !IsNativeHandlerEnabled(); + if (!s_cachedMethods.TryGetValue(name, out method)) + { + method = _nativeHandler!.GetType()!.GetMethod(name); + s_cachedMethods[name] = method; + } + + return method!.Invoke(_nativeHandler, parameters)!; + } - private static bool IsNativeHandlerEnabled() => RuntimeSettingParser.QueryRuntimeSettingSwitch( + private static bool IsNativeHandlerEnabled => RuntimeSettingParser.QueryRuntimeSettingSwitch( "System.Net.Http.UseNativeHttpHandler", false); } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs index 3776955f8feb2d..886e9cd80c3e61 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs @@ -9,41 +9,44 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static MethodInfo? _underlyingHandlerMethod; + private static MethodInfo? _nativeHandlerMethod; - [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler"; + private const string AssemblyName = "Xamarin.MacCatalyst"; + + [DynamicDependency("get_UseCookies", NativeHandlerType, AssemblyName)] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); - [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("set_UseCookies", NativeHandlerType, AssemblyName)] private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); - [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("get_CookieContainer", NativeHandlerType, AssemblyName)] private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); - [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("set_CookieContainer", NativeHandlerType, AssemblyName)] private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); - [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("get_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); - [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("set_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); - [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("get_Credentials", NativeHandlerType, AssemblyName)] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); - [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.MacCatalyst")] + [DynamicDependency("set_Credentials", NativeHandlerType, AssemblyName)] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); private HttpMessageHandler CreateNativeHandler() { - if (_underlyingHandlerMethod == null) + if (_nativeHandlerMethod == null) { Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst"); - _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + _nativeHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_nativeHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs index 66438cbd8fd623..ff9e9108d28511 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.iOS.cs @@ -9,41 +9,44 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static MethodInfo? _underlyingHandlerMethod; + private static MethodInfo? _nativeHandlerMethod; - [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler"; + private const string AssemblyName = "Xamarin.iOS"; + + [DynamicDependency("get_UseCookies", NativeHandlerType, AssemblyName)] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); - [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("set_UseCookies", NativeHandlerType, AssemblyName)] private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); - [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("get_CookieContainer", NativeHandlerType, AssemblyName)] private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); - [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("set_CookieContainer", NativeHandlerType, AssemblyName)] private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); - [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("get_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); - [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("set_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); - [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("get_Credentials", NativeHandlerType, AssemblyName)] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); - [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.iOS")] + [DynamicDependency("set_Credentials", NativeHandlerType, AssemblyName)] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); private HttpMessageHandler CreateNativeHandler() { - if (_underlyingHandlerMethod == null) + if (_nativeHandlerMethod == null) { Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.iOS"); - _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + _nativeHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_nativeHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs index ed1f52d8898ed5..4eaa6a9ad43c06 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.tvOS.cs @@ -9,41 +9,44 @@ namespace System.Net.Http { public partial class HttpClientHandler : HttpMessageHandler { - private static MethodInfo? _underlyingHandlerMethod; + private static MethodInfo? _nativeHandlerMethod; - [DynamicDependency("get_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler"; + private const string AssemblyName = "Xamarin.TVOS"; + + [DynamicDependency("get_UseCookies", NativeHandlerType, AssemblyName)] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); - [DynamicDependency("set_UseCookies", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("set_UseCookies", NativeHandlerType, AssemblyName)] private void SetUseCookies(bool value) => InvokeNativeHandlerMethod("set_UseCookies", value); - [DynamicDependency("get_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("get_CookieContainer", NativeHandlerType, AssemblyName)] private CookieContainer GetCookieContainer() => (CookieContainer)InvokeNativeHandlerMethod("get_CookieContainer"); - [DynamicDependency("set_CookieContainer", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("set_CookieContainer", NativeHandlerType, AssemblyName)] private void SetCookieContainer(CookieContainer value) => InvokeNativeHandlerMethod("set_CookieContainer", value); - [DynamicDependency("get_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("get_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private bool GetAllowAutoRedirect() => (bool)InvokeNativeHandlerMethod("get_AllowAutoRedirect"); - [DynamicDependency("set_AllowAutoRedirect", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("set_AllowAutoRedirect", NativeHandlerType, AssemblyName)] private void SetAllowAutoRedirect(bool value) => InvokeNativeHandlerMethod("set_AllowAutoRedirect", value); - [DynamicDependency("get_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("get_Credentials", NativeHandlerType, AssemblyName)] private ICredentials GetCredentials() => (ICredentials)InvokeNativeHandlerMethod("get_Credentials"); - [DynamicDependency("set_Credentials", "System.Net.Http.NSUrlSessionHandler", "Xamarin.TVOS")] + [DynamicDependency("set_Credentials", NativeHandlerType, AssemblyName)] private void SetCredentials(ICredentials? value) => InvokeNativeHandlerMethod("set_Credentials", value); private HttpMessageHandler CreateNativeHandler() { - if (_underlyingHandlerMethod == null) + if (_nativeHandlerMethod == null) { Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.TVOS"); - _underlyingHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); + _nativeHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); } - return (HttpMessageHandler)_underlyingHandlerMethod!.Invoke(null, null)!; + return (HttpMessageHandler)_nativeHandlerMethod!.Invoke(null, null)!; } } } \ No newline at end of file From 369dbd7223566015fd5a26684140e01459905355 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Wed, 14 Jul 2021 17:02:27 -0400 Subject: [PATCH 12/13] Fix diagnostics handler creation --- .../src/System/Net/Http/HttpClientHandler.AnyMobile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index b17784018bc91a..d0edadae478d98 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -45,7 +45,7 @@ public HttpClientHandler() if (DiagnosticsHandler.IsGloballyEnabled()) { - _diagnosticsHandler = new DiagnosticsHandler(handler); + _diagnosticsHandler = new DiagnosticsHandler(handler, DistributedContextPropagator.Current); } } From 7eea245ced2c3e84c312ed900407f1e39e20274e Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Wed, 14 Jul 2021 17:06:40 -0400 Subject: [PATCH 13/13] missing using --- .../src/System/Net/Http/HttpClientHandler.AnyMobile.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index d0edadae478d98..309e60f65a9156 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Net.Security;