Skip to content

Commit

Permalink
Don't discard the system proxy setting (#16995)
Browse files Browse the repository at this point in the history
Fixes #16990
  • Loading branch information
pakrym authored Nov 16, 2020
1 parent bd3b81f commit 78e760d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions sdk/core/Azure.Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Added
- `System.Text.Json.JsonConverter` implementation for the `ETag`

### Fixes
- System proxy settings are correctly applied on .NET Framework

## 1.6.0 (2020-10-28)

### Added
Expand Down
10 changes: 7 additions & 3 deletions sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Azure.Core.Pipeline
internal class HttpWebRequestTransport : HttpPipelineTransport
{
public static readonly HttpWebRequestTransport Shared = new HttpWebRequestTransport();
private readonly IWebProxy? _proxy;
private readonly IWebProxy? _environmentProxy;

/// <summary>
/// Creates a new instance of <see cref="HttpWebRequestTransport"/>
Expand All @@ -29,7 +29,7 @@ public HttpWebRequestTransport()
{
if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy))
{
_proxy = webProxy;
_environmentProxy = webProxy;
}
}

Expand Down Expand Up @@ -105,7 +105,11 @@ private HttpWebRequest CreateRequest(Request messageRequest)
{
var request = WebRequest.CreateHttp(messageRequest.Uri.ToUri());
request.Method = messageRequest.Method.Method;
request.Proxy = _proxy;
// Don't disable the default proxy when there is no environment proxy configured
if (_environmentProxy != null)
{
request.Proxy = _environmentProxy;
}

foreach (var messageRequestHeader in messageRequest.Headers)
{
Expand Down
39 changes: 39 additions & 0 deletions sdk/core/Azure.Core/tests/TransportFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -698,6 +699,44 @@ public async Task ProxySettingsAreReadFromEnvironment(string envVar, string url)
}
}

#if NET461 // GlobalProxySelection.Select not supported on netcoreapp
[NonParallelizable]
[Test]
public async Task DefaultProxySettingsArePreserved()
{
#pragma warning disable 618 // Use of obsolete symbol
var oldGlobalProxySelection = GlobalProxySelection.Select;
#pragma warning restore 618
try
{
using (TestServer testServer = new TestServer(async context =>
{
context.Response.Headers["Via"] = "Test-Proxy";
byte[] buffer = Encoding.UTF8.GetBytes("Hello");
await context.Response.Body.WriteAsync(buffer, 0, buffer.Length);
}))
{
#pragma warning disable 618 // Use of obsolete symbol
GlobalProxySelection.Select = new WebProxy(testServer.Address.ToString());
#pragma warning restore 618

var transport = GetTransport();
Request request = transport.CreateRequest();
request.Uri.Reset(new Uri("http://microsoft.com"));
Response response = await ExecuteRequest(request, transport);
Assert.True(response.Headers.TryGetValue("Via", out var via));
Assert.AreEqual("Test-Proxy", via);
}
}
finally
{
#pragma warning disable 618 // Use of obsolete symbol
GlobalProxySelection.Select = oldGlobalProxySelection;
#pragma warning restore 618
}
}
#endif

[Test]
public async Task ResponseHeadersAreSplit()
{
Expand Down

0 comments on commit 78e760d

Please sign in to comment.