Skip to content

Commit 44655fd

Browse files
authored
Include native underlying handler support in HttpClientHandler for iOS/tvOS/MacCatalyst and Android (#55384)
Completes the plan for net6 laid out in https://github.com/dotnet/designs/blob/main/accepted/2020/mono-convergence/platform-specific-httpclient.md#the-plan-for-net-5 This change supports using either the native HttpMessageHandler types that exist in the Xamarin repos (NSUrlSessionHandler for iOS/tvOS/Catalyst and AndroidMessageHandler for Android) or SocketsHttpHandler as the underlying handler in HttpClientHandler. The behavior for new HttpClient() was established earlier in net6, but did not go all the way. For example, if the System.Net.Http.UseNativeHttpHandler feature switch was set to true, using HttpClient in different ways would lead to different underlying handlers. Before this PR: // System.Net.Http.UseNativeHttpHandler == true new HttpClient(); // Chooses the native handler as the underlying handler var handler = new HttpClientHandler(); // SocketsHttpHandler is the only choice new HttpClient(handler); The change creates a handful of partial HttpClientHandler files in order to split out the platform specific parts. As you review the PR, you'll notice a bunch of if (IsSocketHandler) blocks. The intent of these are to make use of the linker and get linked out once the linker knows which handler is the active one. get { if (IsSocketHandler) { return _socketHandler!.UseCookies; } else { return GetUseCookies(); } } Get and Set methods like GetUseCookies make it easier to tell the linker via DynamicDependency to preserve the reflection calls being made to the native underlying handler. [DynamicDependency("get_UseCookies", "Xamarin.Android.Net.AndroidMessageHandler", "Mono.Android")] private bool GetUseCookies() => (bool)InvokeNativeHandlerMethod("get_UseCookies"); It is important to point out that the underlying handler has to be derived from HttpMessageHandler. It cannot be HttpClientHandler or you'll end up with a circular dependency.
1 parent a8926ba commit 44655fd

21 files changed

+1185
-148
lines changed

eng/testing/tests.mobile.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<EventSourceSupport Condition="'$(EventSourceSupport)' == ''">false</EventSourceSupport>
2727
<HttpActivityPropagationSupport Condition="'$(HttpActivityPropagationSupport)' == ''">false</HttpActivityPropagationSupport>
2828
<UseSystemResourceKeys Condition="'$(UseSystemResourceKeys)' == ''">false</UseSystemResourceKeys>
29+
<UseNativeHttpHandler Condition="'$(UseNativeHttpHandler)' == ''">false</UseNativeHttpHandler>
2930
</PropertyGroup>
3031

3132
<PropertyGroup>

src/libraries/System.Net.Http/ref/System.Net.Http.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,87 @@ public partial class HttpClientHandler : System.Net.Http.HttpMessageHandler
114114
public HttpClientHandler() { }
115115
public bool AllowAutoRedirect { get { throw null; } set { } }
116116
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
117+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
118+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
119+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
117120
public System.Net.DecompressionMethods AutomaticDecompression { get { throw null; } set { } }
121+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
118122
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
123+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
124+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
125+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
119126
public bool CheckCertificateRevocationList { get { throw null; } set { } }
127+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
128+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
129+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
130+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
120131
public System.Net.Http.ClientCertificateOption ClientCertificateOptions { get { throw null; } set { } }
132+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
121133
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
134+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
135+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
136+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
122137
public System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates { get { throw null; } }
123138
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
124139
public System.Net.CookieContainer CookieContainer { get { throw null; } set { } }
125140
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
126141
public System.Net.ICredentials? Credentials { get { throw null; } set { } }
142+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
127143
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
144+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
145+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
146+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
128147
public static System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2?, System.Security.Cryptography.X509Certificates.X509Chain?, System.Net.Security.SslPolicyErrors, bool> DangerousAcceptAnyServerCertificateValidator { get { throw null; } }
148+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
129149
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
150+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
151+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
152+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
130153
public System.Net.ICredentials? DefaultProxyCredentials { get { throw null; } set { } }
131154
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
155+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
156+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
157+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
132158
public int MaxAutomaticRedirections { get { throw null; } set { } }
159+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
133160
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
161+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
162+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
163+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
134164
public int MaxConnectionsPerServer { get { throw null; } set { } }
135165
public long MaxRequestContentBufferSize { get { throw null; } set { } }
166+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
136167
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
168+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
169+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
170+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
137171
public int MaxResponseHeadersLength { get { throw null; } set { } }
138172
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
173+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
174+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
175+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
139176
public bool PreAuthenticate { get { throw null; } set { } }
177+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
178+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
179+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
180+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
140181
public System.Collections.Generic.IDictionary<string, object?> Properties { get { throw null; } }
141182
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
183+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
184+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
185+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
142186
public System.Net.IWebProxy? Proxy { get { throw null; } set { } }
187+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
143188
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
189+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
190+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
191+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
144192
public System.Func<System.Net.Http.HttpRequestMessage, System.Security.Cryptography.X509Certificates.X509Certificate2?, System.Security.Cryptography.X509Certificates.X509Chain?, System.Net.Security.SslPolicyErrors, bool>? ServerCertificateCustomValidationCallback { get { throw null; } set { } }
193+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
145194
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
195+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
196+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
197+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
146198
public System.Security.Authentication.SslProtocols SslProtocols { get { throw null; } set { } }
147199
public virtual bool SupportsAutomaticDecompression { get { throw null; } }
148200
public virtual bool SupportsProxy { get { throw null; } }
@@ -152,9 +204,20 @@ public HttpClientHandler() { }
152204
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
153205
public bool UseDefaultCredentials { get { throw null; } set { } }
154206
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
207+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
208+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
209+
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("maccatalyst")]
155210
public bool UseProxy { get { throw null; } set { } }
156211
protected override void Dispose(bool disposing) { }
212+
//
213+
// Attributes are commented out due to https://github.com/dotnet/arcade/issues/7585
214+
// API compat will fail until this is fixed
215+
//
216+
//[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
157217
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
218+
//[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("ios")]
219+
//[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("tvos")]
220+
//[System.Runtime.Versioning.UnsupportedOSPlatformAttributeUnsupportedOSPlatform("maccatalyst")]
158221
protected internal override System.Net.Http.HttpResponseMessage Send(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; }
159222
protected internal override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { throw null; }
160223
}

src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<type fullname="System.Net.Http.DiagnosticsHandler">
44
<method signature="System.Boolean IsGloballyEnabled()" body="stub" value="false" feature="System.Net.Http.EnableActivityPropagation" featurevalue="false" />
55
</type>
6-
<type fullname="System.Net.Http.HttpClient">
7-
<method signature="System.Boolean IsNativeHandlerEnabled()" body="stub" value="true" feature="System.Net.Http.UseNativeHttpHandler" featurevalue="true" />
6+
<type fullname="System.Net.Http.HttpClientHandler">
7+
<method signature="System.Boolean get_IsNativeHandlerEnabled()" body="stub" value="false" feature="System.Net.Http.UseNativeHttpHandler" featurevalue="false" />
88
</type>
99
</assembly>
1010
</linker>

0 commit comments

Comments
 (0)