Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix HttpWebRequest when using system proxy settings (#31123)
Browse files Browse the repository at this point in the history
While investigating other HttpClient/HttpWebRequest proxy-related bugs, I discovered that
HttpWebRequest was not honoring system proxy settings as defined on Windows with IE
settings or on Linux using environment variables.

The problem is due to how HttpClient and HttpWebRequest differ in how they represent
the default behavior of using system proxy settings with the various properties. Fixed
HttpWebRequest so that it will translate the system proxy settings to the internal
HttpClient/HttpClientHandler objects.

I also removed an invalid Assert in HttpConnection. This assert was firing when using a proxy
that was defined on the loopback adapter using IPv6 literal "[::1]".  Due to issue #28863 with Uri,
the Uri.IdnHost property doesn't have the brackets for IPv6 literals. So, the Assert was
occuring.

I did not add any new CI tests because it is currently not possible to test system proxy settings
in CI since it involves changing machine configuration. But I ran manual tests.
  • Loading branch information
davidsh authored Aug 9, 2018
1 parent 6c6b536 commit f602b21
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public HttpConnectionPool(HttpConnectionPoolManager poolManager, HttpConnectionK
Debug.Assert(port != 0);
Debug.Assert(sslHostName == null);
Debug.Assert(proxyUri != null);
Debug.Assert(proxyUri.IdnHost == host && proxyUri.Port == port);
break;

default:
Expand Down
18 changes: 16 additions & 2 deletions src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,18 @@ public async Task Dispose_DisposingHandlerCancelsActiveOperationsWithoutResponse
return;
}

if (PlatformDetection.IsFullFramework)
{
// Skip test on .NET Framework. It will sometimes not throw TaskCanceledException.
// Instead it might throw the following top-level and inner exceptions depending
// on race conditions.
//
// System.Net.Http.HttpRequestException : Error while copying content to a stream.
// ---- System.IO.IOException : The read operation failed, see inner exception.
//-------- System.Net.WebException : The request was aborted: The request was canceled.
return;
}

await LoopbackServer.CreateServerAsync(async (server1, url1) =>
{
await LoopbackServer.CreateServerAsync(async (server2, url2) =>
Expand Down Expand Up @@ -2658,8 +2670,10 @@ public async Task PostAsync_Redirect_LargePayload_Helper(int statusCode, bool ex
}
}

[OuterLoop] // TODO: Issue #11345
[Theory, MemberData(nameof(EchoServers))] // NOTE: will not work for in-box System.Net.Http.dll due to disposal of request content
[OuterLoop("Uses external server")]
[Theory, MemberData(nameof(EchoServers))]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework disposes request content after send")]
[ActiveIssue(31104, TestPlatforms.AnyUnix)]
public async Task PostAsync_ReuseRequestContent_Success(Uri remoteServer)
{
const string ContentString = "This is the content string.";
Expand Down
13 changes: 12 additions & 1 deletion src/System.Net.Requests/src/System/Net/HttpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,11 +1138,22 @@ private async Task<WebResponse> SendRequest()

Debug.Assert(handler.UseProxy); // Default of handler.UseProxy is true.
Debug.Assert(handler.Proxy == null); // Default of handler.Proxy is null.

// HttpClientHandler default is to use a proxy which is the system proxy.
// This is indicated by the properties 'UseProxy == true' and 'Proxy == null'.
//
// However, HttpWebRequest doesn't have a separate 'UseProxy' property. Instead,
// the default of the 'Proxy' property is a non-null IWebProxy object which is the
// system default proxy object. If the 'Proxy' property were actually null, then
// that means don't use any proxy.
//
// So, we need to map the desired HttpWebRequest proxy settings to equivalent
// HttpClientHandler settings.
if (_proxy == null)
{
handler.UseProxy = false;
}
else
else if (!object.ReferenceEquals(_proxy, WebRequest.GetSystemWebProxy()))
{
handler.Proxy = _proxy;
}
Expand Down

0 comments on commit f602b21

Please sign in to comment.