Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6e3a414

Browse files
authored
Change behavior of SocketsHttpHandler for redirects (#28008)
When exceeding the max number of redirecs, .NET Framework will not throw an exception. Instead, it will return the 3xx response. This PR changes the behavior of SocketsHttpHandler to match. Fixes #27285
1 parent 6cd1708 commit 6e3a414

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,6 @@
372372
<data name="net_http_feature_UWPClientCertSupportRequiresCertInPersonalCertificateStore" xml:space="preserve">
373373
<value>Client certificate was not found in the personal (\"MY\") certificate store. In UWP, client certificates are only supported if they have been added to that certificate store.</value>
374374
</data>
375-
<data name="net_http_max_redirects" xml:space="preserve">
376-
<value>The maximum number of redirects was exceeded.</value>
377-
</data>
378375
<data name="net_http_invalid_proxy_scheme" xml:space="preserve">
379376
<value>Only the 'http' scheme is allowed for proxies.</value>
380377
</data>

src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ protected internal override async Task<HttpResponseMessage> SendAsync(HttpReques
3333
Uri redirectUri;
3434
while ((redirectUri = GetUriForRedirect(request.RequestUri, response)) != null)
3535
{
36-
response.Dispose();
37-
3836
redirectCount++;
37+
3938
if (redirectCount > _maxAutomaticRedirections)
4039
{
41-
throw new HttpRequestException(SR.net_http_max_redirects);
40+
// If we exceed the maximum number of redirects
41+
// then just return the 3xx response.
42+
if (NetEventSource.IsEnabled)
43+
{
44+
NetEventSource.Info(this, $"Exceeded max number of redirects. Redirect from {request.RequestUri} to {redirectUri} blocked.");
45+
}
46+
47+
break;
4248
}
4349

50+
response.Dispose();
51+
4452
// Clear the authorization header.
4553
request.Headers.Authorization = null;
4654

src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,17 @@ public async Task GetAsync_MaxAutomaticRedirectionsNServerHops_ThrowsIfTooMany(i
872872
}
873873
else
874874
{
875-
await Assert.ThrowsAsync<HttpRequestException>(() => t);
875+
if (UseSocketsHttpHandler)
876+
{
877+
using (HttpResponseMessage response = await t)
878+
{
879+
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
880+
}
881+
}
882+
else
883+
{
884+
await Assert.ThrowsAsync<HttpRequestException>(() => t);
885+
}
876886
}
877887
}
878888
}

0 commit comments

Comments
 (0)