Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android / iOS / tvOS specific HttpClientHandler #47083

Merged
merged 16 commits into from
Mar 2, 2021
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<WindowsRID>win</WindowsRID>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);HTTP_DLL</DefineConstants>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris;$(NetCoreAppCurrent)-Android</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetsOSX)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
@@ -37,6 +37,8 @@
<Compile Include="System\Net\Http\Headers\KnownHeaders.cs" />
<Compile Include="System\Net\Http\HttpBaseStream.cs" />
<Compile Include="System\Net\Http\HttpClient.cs" />
<Compile Condition="'$(TargetsAndroid)' != 'true'" Include="System\Net\Http\HttpClient.CreateDefaultHandler.cs" />
<Compile Condition="'$(TargetsAndroid)' == 'true'" Include="System\Net\Http\HttpClient.CreateDefaultHandler.Android.cs" />
marek-safar marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="System\Net\Http\HttpClientHandler.cs" />
<Compile Include="System\Net\Http\HttpCompletionOption.cs" />
<Compile Include="System\Net\Http\HttpContent.cs" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.Reflection;
using System.Diagnostics.CodeAnalysis;

namespace System.Net.Http
{
public partial class HttpClient
{
private const string CustomHandlerTypeEnvVar = "XA_HTTP_CLIENT_HANDLER_TYPE";
steveisok marked this conversation as resolved.
Show resolved Hide resolved

private static Type? s_customHandlerType;

private static HttpMessageHandler CreateDefaultHandler()
{
if (s_customHandlerType != null)
{
#pragma warning disable IL2057
return (HttpMessageHandler)Activator.CreateInstance(s_customHandlerType)!;
#pragma warning restore IL2057
}

string? envVar = Environment.GetEnvironmentVariable(CustomHandlerTypeEnvVar)?.Trim();
if (!string.IsNullOrEmpty(envVar))
{
#pragma warning disable IL2072
steveisok marked this conversation as resolved.
Show resolved Hide resolved
Type? handlerType = Type.GetType(envVar, false);
steveisok marked this conversation as resolved.
Show resolved Hide resolved
if (handlerType == null && !envVar.Contains(", "))
{
// Look for custom handlers in Mono.Android by default if assembly name is not specified.
handlerType = Type.GetType(envVar + ", Mono.Android", false);
}
#pragma warning restore IL2072
if (handlerType != null && Activator.CreateInstance(handlerType) is HttpMessageHandler handler)
{
// Create instance or fallback to default one if the type is invalid (current XA behavior).
s_customHandlerType = handlerType;
return handler;
}
}
return new HttpClientHandler();
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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()
steveisok marked this conversation as resolved.
Show resolved Hide resolved
{
return new HttpClientHandler();
}
}
}
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

namespace System.Net.Http
{
public class HttpClient : HttpMessageInvoker
public partial class HttpClient : HttpMessageInvoker
{
#region Fields

@@ -127,7 +127,7 @@ public long MaxResponseContentBufferSize

#region Constructors

public HttpClient() : this(new HttpClientHandler())
public HttpClient() : this(CreateDefaultHandler())
{
}

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Android</TargetFrameworks>
<Nullable>annotations</Nullable>
</PropertyGroup>
<!-- Do not reference these assemblies from the TargetingPack since we are building part of the source code for tests. -->
@@ -190,6 +190,12 @@
Link="ProductionCode\System\Net\Http\Headers\WarningHeaderValue.cs" />
<Compile Include="..\..\src\System\Net\Http\HttpClient.cs"
Link="ProductionCode\System\Net\Http\HttpClient.cs" />
<Compile Condition="'$(TargetsAndroid)' != 'true'"
Include="..\..\src\System\Net\Http\HttpClient.CreateDefaultHandler.cs"
Link="ProductionCode\System\Net\Http\HttpClient.CreateDefaultHandler.cs" />
<Compile Condition="'$(TargetsAndroid)' == 'true'"
Include="..\..\src\System\Net\Http\HttpClient.CreateDefaultHandler.Android.cs"
Link="ProductionCode\System\Net\Http\HttpClient.CreateDefaultHandler.Android.cs" />
<Compile Include="..\..\src\System\Net\Http\HttpHandlerDefaults.cs"
Link="ProductionCode\System\Net\Http\HttpHandlerDefaults.cs" />
<Compile Include="..\..\src\System\Net\Http\HttpCompletionOption.cs"