forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proxy environment variables support for Windows
Changed the Windows version of SocketsHttpHandler so that it will honor the same environment variables similar to the Linux and OSX versions. If the environment variables are not set then it reverts back to the Windows WinInet/IE settings behavior. I added several kinds of unit and end-to-end tests to verify the SystemProxyInfo class is making the correct choice for all the different platforms regarding whether the HttpEnvironmentProxy or platform proxy (HttpSystemProxy for Windows and MacProxy for OSX) is used. Fixes #37187
- Loading branch information
Showing
14 changed files
with
284 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.Unix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 partial class HttpEnvironmentProxy : IWebProxy | ||
{ | ||
public static bool TryCreate(out IWebProxy proxy) | ||
{ | ||
// Get environment variables. Protocol specific take precedence over | ||
// general all_*, lower case variable has precedence over upper case. | ||
// Note that curl uses HTTPS_PROXY but not HTTP_PROXY. | ||
|
||
Uri httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyLC)); | ||
if (httpProxy == null && Environment.GetEnvironmentVariable(EnvCGI) == null) | ||
{ | ||
httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyUC)); | ||
} | ||
|
||
Uri httpsProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyLC)) ?? | ||
GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyUC)); | ||
|
||
if (httpProxy == null || httpsProxy == null) | ||
{ | ||
Uri allProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyLC)) ?? | ||
GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyUC)); | ||
|
||
if (httpProxy == null) | ||
{ | ||
httpProxy = allProxy; | ||
} | ||
|
||
if (httpsProxy == null) | ||
{ | ||
httpsProxy = allProxy; | ||
} | ||
} | ||
|
||
// Do not instantiate if nothing is set. | ||
// Caller may pick some other proxy type. | ||
if (httpProxy == null && httpsProxy == null) | ||
{ | ||
proxy = null; | ||
return false; | ||
} | ||
|
||
string noProxy = Environment.GetEnvironmentVariable(EnvNoProxyLC) ?? | ||
Environment.GetEnvironmentVariable(EnvNoProxyUC); | ||
proxy = new HttpEnvironmentProxy(httpProxy, httpsProxy, noProxy); | ||
|
||
return true; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 partial class HttpEnvironmentProxy : IWebProxy | ||
{ | ||
public static bool TryCreate(out IWebProxy proxy) | ||
{ | ||
// Get environment variables. Protocol specific take precedence over | ||
// general all_*. On Windows, environment variables are case insensitive. | ||
|
||
Uri httpProxy = null; | ||
if (Environment.GetEnvironmentVariable(EnvCGI) == null) | ||
{ | ||
httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyUC)); | ||
} | ||
|
||
Uri httpsProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyUC)); | ||
|
||
if (httpProxy == null || httpsProxy == null) | ||
{ | ||
Uri allProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyUC)); | ||
|
||
if (httpProxy == null) | ||
{ | ||
httpProxy = allProxy; | ||
} | ||
|
||
if (httpsProxy == null) | ||
{ | ||
httpsProxy = allProxy; | ||
} | ||
} | ||
|
||
// Do not instantiate if nothing is set. | ||
// Caller may pick some other proxy type. | ||
if (httpProxy == null && httpsProxy == null) | ||
{ | ||
proxy = null; | ||
return false; | ||
} | ||
|
||
string noProxy = Environment.GetEnvironmentVariable(EnvNoProxyUC); | ||
proxy = new HttpEnvironmentProxy(httpProxy, httpsProxy, noProxy); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
<Project DefaultTargets="Build"> | ||
<PropertyGroup> | ||
<BuildConfigurations> | ||
netcoreapp; | ||
netcoreapp-OSX; | ||
netcoreapp-Unix; | ||
netcoreapp-Windows_NT; | ||
</BuildConfigurations> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 MacProxy : IWebProxy | ||
{ | ||
public ICredentials Credentials | ||
{ | ||
get => throw NotImplemented.ByDesignWithMessage("Mac Proxy not implemented"); | ||
set => throw NotImplemented.ByDesignWithMessage("Mac Proxy not implemented"); | ||
} | ||
|
||
public Uri GetProxy(Uri targetUri) | ||
{ | ||
throw NotImplemented.ByDesignWithMessage("Mac Proxy not implemented"); | ||
} | ||
|
||
public bool IsBypassed(Uri targetUri) | ||
{ | ||
throw NotImplemented.ByDesignWithMessage("Mac Proxy not implemented"); | ||
} | ||
} | ||
} |
Oops, something went wrong.