Skip to content

Commit 9089931

Browse files
geoffkizerGeoffrey Kizerstephentoub
authored
make EstablishProxyTunnelAsync throw on failure status code from proxy (#50763)
* make EstablishProxyTunnelAsync throw on failure status code from proxy and fix relevant test * ensure tunnelResponse is disposed on exception * Update src/libraries/System.Net.Http/src/Resources/Strings.resx Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Geoffrey Kizer <[email protected]> Co-authored-by: Stephen Toub <[email protected]>
1 parent 89498d8 commit 9089931

File tree

4 files changed

+51
-84
lines changed

4 files changed

+51
-84
lines changed

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Proxy.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,8 @@ public async Task AuthenticatedProxiedRequest_GetAsyncWithNoCreds_ProxyAuthentic
280280
}
281281
}
282282

283-
[OuterLoop("Uses external server")]
284283
[Fact]
285-
public async Task AuthenticatedProxyTunnelRequest_PostAsyncWithNoCreds_ProxyAuthenticationRequiredStatusCode()
284+
public async Task AuthenticatedProxyTunnelRequest_PostAsyncWithNoCreds_Throws()
286285
{
287286
if (IsWinHttpHandler)
288287
{
@@ -300,14 +299,13 @@ public async Task AuthenticatedProxyTunnelRequest_PostAsyncWithNoCreds_ProxyAuth
300299
handler.Proxy = new WebProxy(proxyServer.Uri);
301300
handler.ServerCertificateCustomValidationCallback = TestHelper.AllowAllCertificates;
302301
using (HttpClient client = CreateHttpClient(handler))
303-
using (HttpResponseMessage response = await client.PostAsync(Configuration.Http.SecureRemoteEchoServer, new StringContent(content)))
304302
{
305-
Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);
303+
HttpRequestException e = await Assert.ThrowsAnyAsync<HttpRequestException>(async () => await client.PostAsync("https://nosuchhost.invalid", new StringContent(content)));
304+
Assert.Contains("407", e.Message);
306305
}
307306
}
308307
}
309308

310-
311309
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsSubsystemForLinux))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/18258")]
312310
public async Task Proxy_SslProxyUnsupported_Throws()
313311
{

src/libraries/System.Net.Http/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,7 @@
606606
<data name="net_http_synchronous_reads_not_supported" xml:space="preserve">
607607
<value>Synchronous reads are not supported, use ReadAsync instead.</value>
608608
</data>
609+
<data name="net_http_proxy_tunnel_returned_failure_status_code" xml:space="preserve">
610+
<value>The proxy tunnel request to proxy '{0}' failed with status code '{1}'."</value>
611+
</data>
609612
</root>

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,7 @@ private static async Task<HttpResponseMessage> SendWithNtAuthAsync(HttpRequestMe
6363
if (response.Headers.ConnectionClose.GetValueOrDefault())
6464
{
6565
// Server is closing the connection and asking us to authenticate on a new connection.
66-
// expression returns null connection on error, was not able to use '!' for the expression
67-
#pragma warning disable CS8600
68-
(connection, response) = await connectionPool.CreateHttp11ConnectionAsync(request, async, cancellationToken).ConfigureAwait(false);
69-
#pragma warning restore CS8600
70-
if (response != null)
71-
{
72-
return response;
73-
}
74-
66+
connection = await connectionPool.CreateHttp11ConnectionAsync(request, async, cancellationToken).ConfigureAwait(false);
7567
connectionPool.IncrementConnectionCount();
7668
connection!.Acquire();
7769
isNewConnection = true;

0 commit comments

Comments
 (0)