From cae52aa4a6592cd4ca29e528fb4f05a10b04c078 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Mon, 14 Oct 2019 17:19:22 +0200 Subject: [PATCH] [System.Net.Http] Clean up HttpMessageHandler setup (#17252) * [System.Net.Http] Clean up HttpMessageHandler setup to avoid reflection and not to be recursive Superseded #15051 and #15922 * [csproj] Update project files --- external/binary-reference-assemblies | 2 +- mcs/build/profiles/wasm.make | 2 +- mcs/class/Mono.Security/Mono.Security.csproj | 4 +- .../System.Net.Http/HttpClientHandler.wasm.cs | 148 --------------------- mcs/class/System.Net.Http/Makefile | 4 + mcs/class/System.Net.Http/System.Net.Http.csproj | 16 +-- .../System.Net.Http/HttpClient.wasm.cs | 61 +-------- .../System.Net.Http/HttpClientHandler.wasm.cs | 147 ++++++++++++++++++++ .../wasm_System.Net.Http.dll.exclude.sources | 6 - .../wasm_System.Net.Http.dll.sources | 37 +++++- mcs/class/System.Security/System.Security.csproj | 8 +- mcs/class/System/System.csproj | 16 +-- 12 files changed, 216 insertions(+), 235 deletions(-) delete mode 100644 mcs/class/System.Net.Http/HttpClientHandler.wasm.cs rewrite mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs (98%) create mode 100644 mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.wasm.cs delete mode 100644 mcs/class/System.Net.Http/wasm_System.Net.Http.dll.exclude.sources rewrite mcs/class/System.Net.Http/wasm_System.Net.Http.dll.sources (62%) diff --git a/external/binary-reference-assemblies b/external/binary-reference-assemblies index 4e3d4446a3c..20704e7005d 160000 --- a/external/binary-reference-assemblies +++ b/external/binary-reference-assemblies @@ -1 +1 @@ -Subproject commit 4e3d4446a3cf8ffc7225f7aa26a96ef9cde1233d +Subproject commit 20704e7005d976ed3bd99983696a08428cf5376c diff --git a/mcs/build/profiles/wasm.make b/mcs/build/profiles/wasm.make index 3e10d8974cc..512c663bb96 100644 --- a/mcs/build/profiles/wasm.make +++ b/mcs/build/profiles/wasm.make @@ -30,7 +30,7 @@ PROFILE_MCS_FLAGS = \ -nostdlib \ $(PLATFORM_DEBUG_FLAGS) -API_BIN_PROFILE = build/monotouch +API_BIN_PROFILE = build/monowasm FRAMEWORK_VERSION = 2.1 # the tuner takes care of the install diff --git a/mcs/class/Mono.Security/Mono.Security.csproj b/mcs/class/Mono.Security/Mono.Security.csproj index 7a01b75379f..de767dabd30 100644 --- a/mcs/class/Mono.Security/Mono.Security.csproj +++ b/mcs/class/Mono.Security/Mono.Security.csproj @@ -477,9 +477,9 @@ - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.dll False diff --git a/mcs/class/System.Net.Http/HttpClientHandler.wasm.cs b/mcs/class/System.Net.Http/HttpClientHandler.wasm.cs deleted file mode 100644 index c2e16dea993..00000000000 --- a/mcs/class/System.Net.Http/HttpClientHandler.wasm.cs +++ /dev/null @@ -1,148 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using System.Net.Security; -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 - { - HttpMessageHandler wasmHandler; - - public HttpClientHandler () : this (HttpClient.CreateDefaultHandler ()) { } - - HttpClientHandler (HttpMessageHandler wasmHandler) - { - this.wasmHandler = wasmHandler; - } - - protected override void Dispose (bool disposing) - { - if (disposing) { - if (wasmHandler != null) { - wasmHandler.Dispose (); - wasmHandler = null; - } - } - base.Dispose (disposing); - } - - const string EXCEPTION_MESSAGE = "System.Net.Http.HttpClientHandler is not supported on the current platform."; - - public virtual bool SupportsAutomaticDecompression => false; - - public virtual bool SupportsProxy => false; - - public virtual bool SupportsRedirectConfiguration => false; - - public bool UseCookies { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public CookieContainer CookieContainer { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public ClientCertificateOption ClientCertificateOptions { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public X509CertificateCollection ClientCertificates { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public Func ServerCertificateCustomValidationCallback { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public bool CheckCertificateRevocationList { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public SslProtocols SslProtocols { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public DecompressionMethods AutomaticDecompression { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public bool UseProxy { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public IWebProxy Proxy { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public ICredentials DefaultProxyCredentials { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public bool PreAuthenticate { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public bool UseDefaultCredentials { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public ICredentials Credentials { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public bool AllowAutoRedirect { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public int MaxAutomaticRedirections { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public int MaxConnectionsPerServer { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public int MaxResponseHeadersLength { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public long MaxRequestContentBufferSize { - get => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - set => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - } - - public IDictionary Properties => throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - - protected internal override Task SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) - { - if (wasmHandler == null) - throw new PlatformNotSupportedException (EXCEPTION_MESSAGE); - return wasmHandler.SendAsync (request, cancellationToken); - } - } -} diff --git a/mcs/class/System.Net.Http/Makefile b/mcs/class/System.Net.Http/Makefile index 3eef96a6b2f..9e56625a14d 100644 --- a/mcs/class/System.Net.Http/Makefile +++ b/mcs/class/System.Net.Http/Makefile @@ -37,6 +37,10 @@ LIB_MCS_FLAGS += -r:$(topdir)/../external/binary-reference-assemblies/build/mono LIB_MCS_FLAGS += -d:XAMARIN_MODERN endif +ifeq (wasm,$(PROFILE)) +API_BIN_REFS := WebAssembly.Net.Http +endif + TEST_LIB_REFS = System System.Core TEST_MCS_FLAGS = diff --git a/mcs/class/System.Net.Http/System.Net.Http.csproj b/mcs/class/System.Net.Http/System.Net.Http.csproj index 68faf320aa7..b5d683ba3cf 100644 --- a/mcs/class/System.Net.Http/System.Net.Http.csproj +++ b/mcs/class/System.Net.Http/System.Net.Http.csproj @@ -251,12 +251,10 @@ - - @@ -304,7 +302,6 @@ - @@ -323,13 +320,9 @@ - - - - - + @@ -1264,6 +1257,13 @@ False + + + False + ./../../../external/binary-reference-assemblies/build/monowasm/WebAssembly.Net.Http.dll + False + + diff --git a/mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs dissimilarity index 98% index 6be7c51368e..e98dc08a7e2 100644 --- a/mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs +++ b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs @@ -1,54 +1,7 @@ -using System; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace System.Net.Http -{ - public partial class HttpClient - { - -#pragma warning disable 649 - private static Func GetHttpMessageHandler; -#pragma warning restore 649 - - internal static HttpMessageHandler CreateDefaultHandler() - { - - if (GetHttpMessageHandler == null) - { - Type type = Type.GetType("WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler, WebAssembly.Net.Http"); - if (type == null) - return GetFallback ("Invalid WebAssembly Module? Cannot find WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler"); - - MethodInfo method = type.GetMethod("GetHttpMessageHandler", BindingFlags.Static | BindingFlags.NonPublic); - if (method == null) - return GetFallback ("Your WebAssembly version does not support obtaining of the custom HttpClientHandler"); - - object ret = method.Invoke(null, null); - if (ret == null) - return GetFallback ("WebAssembly returned no custom HttpClientHandler"); - - var handler = ret as HttpMessageHandler; - if (handler == null) - return GetFallback ($"{ret?.GetType()} is not a valid HttpMessageHandler"); - - return handler; - - } - else - { - var handler = GetHttpMessageHandler(); - if (handler == null) - return GetFallback($"Custom HttpMessageHandler is not valid"); - - return handler; - } - } - - static HttpMessageHandler GetFallback(string message) - { - //Console.WriteLine(message + ". Defaulting to System.Net.Http.HttpClientHandler"); - return new HttpClientHandler(); - } - } -} +namespace System.Net.Http +{ + public partial class HttpClient + { + static HttpMessageHandler CreateDefaultHandler () => new WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler (); + } +} diff --git a/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.wasm.cs b/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.wasm.cs new file mode 100644 index 00000000000..b32de23b948 --- /dev/null +++ b/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.wasm.cs @@ -0,0 +1,147 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; + +namespace System.Net.Http +{ + public class HttpClientHandler : HttpMessageHandler + { + WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler wasmHandler; + + public HttpClientHandler () + { + wasmHandler = new WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler (); + } + + protected override void Dispose (bool disposing) + { + if (disposing) { + if (wasmHandler != null) { + wasmHandler.Dispose (); + wasmHandler = null; + } + } + base.Dispose (disposing); + } + + public virtual bool SupportsAutomaticDecompression => false; + + public virtual bool SupportsProxy => false; + + public virtual bool SupportsRedirectConfiguration => false; + + public bool UseCookies { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public CookieContainer CookieContainer { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public ClientCertificateOption ClientCertificateOptions { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public X509CertificateCollection ClientCertificates { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public Func ServerCertificateCustomValidationCallback { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public bool CheckCertificateRevocationList { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public SslProtocols SslProtocols { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public DecompressionMethods AutomaticDecompression { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public bool UseProxy { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public IWebProxy Proxy { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public ICredentials DefaultProxyCredentials { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public bool PreAuthenticate { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public bool UseDefaultCredentials { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public ICredentials Credentials { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public bool AllowAutoRedirect { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public int MaxAutomaticRedirections { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public int MaxConnectionsPerServer { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public int MaxResponseHeadersLength { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public long MaxRequestContentBufferSize { + get => throw new PlatformNotSupportedException (); + set => throw new PlatformNotSupportedException (); + } + + public IDictionary Properties => throw new PlatformNotSupportedException (); + + public static Func DangerousAcceptAnyServerCertificateValidator { get; } = delegate { return true; }; + + protected internal override Task SendAsync (HttpRequestMessage request, CancellationToken cancellationToken) + { + if (wasmHandler == null) + throw new ObjectDisposedException (GetType().ToString()); + + return wasmHandler.SendAsync (request, cancellationToken); + } + } +} diff --git a/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.exclude.sources b/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.exclude.sources deleted file mode 100644 index aa99247b7a8..00000000000 --- a/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.exclude.sources +++ /dev/null @@ -1,6 +0,0 @@ -HttpClient.DefaultHandler.cs -HttpClientHandler.cs -HttpClientHandler.DefaultHandler.cs -HttpClientHandler.SocketsHandler.cs -corefx/SocketsHttpHandler.Mono.cs -../../../external/corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/*.cs diff --git a/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.sources b/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.sources dissimilarity index 62% index e315913cc0b..9c99b180e52 100644 --- a/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.sources +++ b/mcs/class/System.Net.Http/wasm_System.Net.Http.dll.sources @@ -1,3 +1,34 @@ -#include socketshandler.sources -HttpClientHandler.wasm.cs -System.Net.Http/HttpClient.wasm.cs +Assembly/AssemblyInfo.cs + +corefx/NetEventSource.Http.cs + +System.Net.Http/HttpClientHandler.wasm.cs +System.Net.Http/HttpClient.wasm.cs +HttpRequestMessage.Mono.cs + +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/Headers/*.cs + +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/ByteArrayContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/ByteArrayHelpers.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/ClientCertificateOption.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/DelegatingHandler.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/FormUrlEncodedContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpCompletionOption.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpMessageHandler.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpMessageInvoker.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpMethod.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpParseResult.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpRequestException.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpRuleParser.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/HttpUtilities.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/MessageProcessingHandler.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/MultipartContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/MultipartFormDataContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/StreamContent.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/StreamToStreamCopy.cs +../../../external/corefx/src/System.Net.Http/src/System/Net/Http/StringContent.cs diff --git a/mcs/class/System.Security/System.Security.csproj b/mcs/class/System.Security/System.Security.csproj index 0c68242c1a8..dad6da662ae 100644 --- a/mcs/class/System.Security/System.Security.csproj +++ b/mcs/class/System.Security/System.Security.csproj @@ -1029,14 +1029,14 @@ - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Numerics.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Numerics.dll False - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Core.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Core.dll False diff --git a/mcs/class/System/System.csproj b/mcs/class/System/System.csproj index f28ed5af1b2..d681ec81ce3 100644 --- a/mcs/class/System/System.csproj +++ b/mcs/class/System/System.csproj @@ -5073,24 +5073,24 @@ - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Net.Http.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Net.Http.dll False - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Xml.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Xml.dll False - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Core.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Core.dll False - + False - ./../../../external/binary-reference-assemblies/build/monotouch/System.Numerics.dll + ./../../../external/binary-reference-assemblies/build/monowasm/System.Numerics.dll False -- 2.11.4.GIT