diff options
author | Egor Bogatov <egorbo@gmail.com> | 2020-02-14 15:04:46 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-14 15:04:46 +0300 |
commit | b5df4be189767e10bec076b5b7b9b0022a541e0a (patch) | |
tree | b9867fcdd0f7f5a888c4232022430429aeb86e9c /mcs | |
parent | 549a38cd4d88d8394a533b67e682eeef0104545c (diff) |
Make MonoWebRequestHandler linker friendly (#18832)
Create it using reflection so it will be always linked out. However if it's set via `XA_HTTP_CLIENT_HANDLER_TYPE` linker will preserve it in a special substep: https://github.com/xamarin/xamarin-android/blob/master/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveHttpAndroidClientHandler.cs#L72-L80
I had to make its ctor **public** because Linker instantiate it via `Activator.CreateInstance(string)` ([here](https://github.com/xamarin/xamarin-android/blob/60363ef45cd6994a322adac1b73d4271b0658c1e/src/Mono.Android/Android.Runtime/AndroidEnvironment.cs#L369)). But the whole type remains internal so it's not a real API change.
Fixes issue from https://github.com/mono/mono/pull/18818#issuecomment-585611647
Diffstat (limited to 'mcs')
3 files changed, 20 insertions, 15 deletions
diff --git a/mcs/class/System.Net.Http/HttpClientHandler.SocketsHandler.Android.cs b/mcs/class/System.Net.Http/HttpClientHandler.SocketsHandler.Android.cs index c483c28f03b..9b4fadc19f1 100644 --- a/mcs/class/System.Net.Http/HttpClientHandler.SocketsHandler.Android.cs +++ b/mcs/class/System.Net.Http/HttpClientHandler.SocketsHandler.Android.cs @@ -1,3 +1,5 @@ +using System.Reflection; + namespace System.Net.Http { partial class HttpClientHandler : HttpMessageHandler @@ -6,7 +8,12 @@ namespace System.Net.Http { string envvar = Environment.GetEnvironmentVariable ("XA_HTTP_CLIENT_HANDLER_TYPE")?.Trim (); if (envvar?.StartsWith("System.Net.Http.MonoWebRequestHandler", StringComparison.InvariantCulture) == true) - return new MonoWebRequestHandler (); + { + Type monoWrhType = Type.GetType (envvar, false); + if (monoWrhType != null) + return (IMonoHttpClientHandler) Activator.CreateInstance (monoWrhType); + } + // Ignore other types of handlers here (e.g. AndroidHttpHandler) to keep the old behavior // and always create SocketsHttpHandler for code like this if MonoWebRequestHandler was not specified: // diff --git a/mcs/class/System.Net.Http/MonoWebRequestHandler.cs b/mcs/class/System.Net.Http/MonoWebRequestHandler.cs index 023343f01f4..27c904adfdf 100644 --- a/mcs/class/System.Net.Http/MonoWebRequestHandler.cs +++ b/mcs/class/System.Net.Http/MonoWebRequestHandler.cs @@ -70,7 +70,7 @@ namespace System.Net.Http TimeSpan? timeout; bool disposed; - internal MonoWebRequestHandler () + public MonoWebRequestHandler () { allowAutoRedirect = true; maxAutomaticRedirections = 50; diff --git a/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs index 9b899bcffd8..8ea04ac4880 100644 --- a/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs +++ b/mcs/class/System.Net.Http/System.Net.Http/HttpClient.android.cs @@ -11,10 +11,16 @@ namespace System.Net.Http { string envvar = Environment.GetEnvironmentVariable ("XA_HTTP_CLIENT_HANDLER_TYPE")?.Trim (); if (string.IsNullOrEmpty (envvar)) - return GetFallback ($"XA_HTTP_CLIENT_HANDLER_TYPE is empty"); + return new HttpClientHandler (); - if (envvar?.StartsWith("System.Net.Http.MonoWebRequestHandler", StringComparison.InvariantCulture) == true) - return new HttpClientHandler (new MonoWebRequestHandler ()); + if (envvar?.StartsWith ("System.Net.Http.MonoWebRequestHandler", StringComparison.InvariantCulture) == true) + { + Type monoWrhType = Type.GetType (envvar, false); + if (monoWrhType != null) + return new HttpClientHandler ((IMonoHttpClientHandler) Activator.CreateInstance (monoWrhType)); + + return new HttpClientHandler (); + } Type handlerType = Type.GetType (envvar, false); if (handlerType == null && !envvar.Contains (", ")) @@ -25,19 +31,11 @@ namespace System.Net.Http { } if (handlerType == null) - return GetFallback ($"'{envvar}' type was not found"); - - object handlerObj = Activator.CreateInstance (handlerType); + return new HttpClientHandler (); - if (handlerObj is HttpMessageHandler msgHandler) + if (Activator.CreateInstance (handlerType) is HttpMessageHandler msgHandler) return msgHandler; - return GetFallback ($"{handlerObj?.GetType ()} is not a valid HttpMessageHandler or MonoWebRequestHandler"); - } - - static HttpMessageHandler GetFallback (string message) - { - Console.WriteLine (message + ". Defaulting to System.Net.Http.HttpClientHandler"); return new HttpClientHandler (); } } |