Skip to content

Commit

Permalink
Internal Http SystemProxyInfo object should never be null (dotnet/cor…
Browse files Browse the repository at this point in the history
…efx#37306)

In order to provide a consistent experience with the HttpClient IWebProxy objects,
the returned internal proxy object which represent the system/platform proxy settings
should never be null. If the platform's settings indicate that no proxy is being used,
then return an instance of the internal HttpNoProxy object.

Note that even if the platform settings indicate that a proxy could be used, any
particular Http request might still not go thru a proxy. The final determination of
what proxy is being used for a request is still governed by the return of the
IWebProxy.IsBypassed and IWebProxy.GetProxy methods.

Contributes to dotnet/corefx#36553


Commit migrated from dotnet/corefx@830bf7f
  • Loading branch information
davidsh authored May 1, 2019
1 parent 9b9c690 commit 3b63495
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
</Compile>
</ItemGroup>
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' And '$(TargetsOSX)' != 'true' And '$(TargetGroup)' == 'netcoreapp'">
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpNoProxy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SystemProxyInfo.Unix.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' And '$(TargetGroup)' == 'netcoreapp'">
Expand Down Expand Up @@ -306,6 +307,7 @@
<Compile Include="System\Net\Http\SocketsHttpHandler\SystemProxyInfo.Windows.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpEnvironmentProxy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpEnvironmentProxy.Windows.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpNoProxy.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpSystemProxy.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\SChannel\Interop.SecPkgContext_ApplicationProtocol.cs">
<Link>Common\Interop\Windows\SChannel\Interop.SecPkgContext_ApplicationProtocol.cs</Link>
Expand Down
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.
// See the LICENSE file in the project root for more information.

namespace System.Net.Http
{
internal sealed class HttpNoProxy : IWebProxy
{
public ICredentials Credentials { get; set; }
public Uri GetProxy(Uri destination) => null;
public bool IsBypassed(Uri host) => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace System.Net.Http
{
internal static class SystemProxyInfo
{
// On Unix (except for OSX) we get default proxy configuration from environment variables.
// On Unix (except for OSX) we get default proxy configuration from environment variables. If the
// environment variables are not defined, we return an IWebProxy object that effectively is
// the "no proxy" object.
public static IWebProxy ConstructSystemProxy()
{
return HttpEnvironmentProxy.TryCreate(out IWebProxy proxy) ? proxy : null;
return HttpEnvironmentProxy.TryCreate(out IWebProxy proxy) ? proxy : new HttpNoProxy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static IWebProxy ConstructSystemProxy()
HttpSystemProxy.TryCreate(out proxy);
}

return proxy;
return proxy ?? new HttpNoProxy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@
<Compile Include="HttpEnvironmentProxyTest.cs" />
<Compile Include="HttpSystemProxyTest.cs" />
<Compile Include="SystemProxyInfoTest.cs" />
<Compile Include="..\..\src\System\Net\Http\SocketsHttpHandler\HttpNoProxy.cs">
<Link>ProductionCode\System\Net\Http\HttpNoProxy.cs</Link>
</Compile>
<Compile Include="..\..\src\System\Net\Http\SocketsHttpHandler\HttpSystemProxy.cs">
<Link>ProductionCode\System\Net\Http\HttpSystemProxy.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void Ctor_NoEnvironmentVariables_NotHttpEnvironmentProxy()
RemoteExecutor.Invoke(() =>
{
IWebProxy proxy = SystemProxyInfo.ConstructSystemProxy();
Assert.NotNull(proxy);

HttpEnvironmentProxy envProxy = proxy as HttpEnvironmentProxy;
Assert.Null(envProxy);

Expand Down

0 comments on commit 3b63495

Please sign in to comment.