-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
RedirectHandler.cs
177 lines (148 loc) · 7.02 KB
/
RedirectHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http
{
internal sealed class RedirectHandler : HttpMessageHandlerStage
{
private readonly HttpMessageHandlerStage _initialInnerHandler; // Used for initial request
private readonly HttpMessageHandlerStage _redirectInnerHandler; // Used for redirects; this allows disabling auth
private readonly int _maxAutomaticRedirections;
public RedirectHandler(int maxAutomaticRedirections, HttpMessageHandlerStage initialInnerHandler, HttpMessageHandlerStage redirectInnerHandler)
{
Debug.Assert(initialInnerHandler != null);
Debug.Assert(redirectInnerHandler != null);
Debug.Assert(maxAutomaticRedirections > 0);
_maxAutomaticRedirections = maxAutomaticRedirections;
_initialInnerHandler = initialInnerHandler;
_redirectInnerHandler = redirectInnerHandler;
}
internal override async ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
{
HttpResponseMessage response = await _initialInnerHandler.SendAsync(request, async, cancellationToken).ConfigureAwait(false);
uint redirectCount = 0;
Uri? redirectUri;
Debug.Assert(request.RequestUri != null);
while ((redirectUri = GetUriForRedirect(request.RequestUri, response)) != null)
{
redirectCount++;
if (redirectCount > _maxAutomaticRedirections)
{
// If we exceed the maximum number of redirects
// then just return the 3xx response.
if (NetEventSource.Log.IsEnabled())
{
TraceError($"Exceeded max number of redirects. Redirect from {request.RequestUri} to {redirectUri} blocked.", request.GetHashCode());
}
break;
}
response.Dispose();
// Clear the authorization header.
request.Headers.Authorization = null;
if (HttpTelemetry.Log.IsEnabled())
{
HttpTelemetry.Log.Redirect(redirectUri.AbsoluteUri);
}
if (NetEventSource.Log.IsEnabled())
{
Trace($"Redirecting from {request.RequestUri} to {redirectUri} in response to status code {(int)response.StatusCode} '{response.StatusCode}'.", request.GetHashCode());
}
// Set up for the redirect
request.RequestUri = redirectUri;
if (RequestRequiresForceGet(response.StatusCode, request.Method))
{
if (NetEventSource.Log.IsEnabled())
{
Trace($"Modified request from {request.Method} to {HttpMethod.Get} in response to status code {(int)response.StatusCode} '{response.StatusCode}'.", request.GetHashCode());
}
request.Method = HttpMethod.Get;
request.Content = null;
if (request.Headers.TransferEncodingChunked == true)
{
request.Headers.TransferEncodingChunked = false;
}
}
request.MarkAsRedirected();
// Issue the redirected request.
response = await _redirectInnerHandler.SendAsync(request, async, cancellationToken).ConfigureAwait(false);
}
return response;
}
private Uri? GetUriForRedirect(Uri requestUri, HttpResponseMessage response)
{
switch (response.StatusCode)
{
case HttpStatusCode.Moved:
case HttpStatusCode.Found:
case HttpStatusCode.SeeOther:
case HttpStatusCode.TemporaryRedirect:
case HttpStatusCode.MultipleChoices:
case HttpStatusCode.PermanentRedirect:
break;
default:
return null;
}
Uri? location = response.Headers.Location;
if (location == null)
{
return null;
}
// Ensure the redirect location is an absolute URI.
if (!location.IsAbsoluteUri)
{
location = new Uri(requestUri, location);
}
// Per https://tools.ietf.org/html/rfc7231#section-7.1.2, a redirect location without a
// fragment should inherit the fragment from the original URI.
string requestFragment = requestUri.Fragment;
if (!string.IsNullOrEmpty(requestFragment))
{
string redirectFragment = location.Fragment;
if (string.IsNullOrEmpty(redirectFragment))
{
location = new UriBuilder(location) { Fragment = requestFragment }.Uri;
}
}
// Disallow automatic redirection from secure to non-secure schemes
if (HttpUtilities.IsSupportedSecureScheme(requestUri.Scheme) && !HttpUtilities.IsSupportedSecureScheme(location.Scheme))
{
if (NetEventSource.Log.IsEnabled())
{
TraceError($"Insecure https to http redirect from '{requestUri}' to '{location}' blocked.", response.RequestMessage!.GetHashCode());
}
return null;
}
return location;
}
private static bool RequestRequiresForceGet(HttpStatusCode statusCode, HttpMethod requestMethod)
{
switch (statusCode)
{
case HttpStatusCode.Moved:
case HttpStatusCode.Found:
case HttpStatusCode.MultipleChoices:
return requestMethod == HttpMethod.Post;
case HttpStatusCode.SeeOther:
return requestMethod != HttpMethod.Get && requestMethod != HttpMethod.Head;
default:
return false;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_initialInnerHandler.Dispose();
_redirectInnerHandler.Dispose();
}
base.Dispose(disposing);
}
internal void Trace(string message, int requestId, [CallerMemberName] string? memberName = null) =>
NetEventSource.Log.HandlerMessage(0, 0, requestId, memberName, ToString() + ": " + message);
internal void TraceError(string message, int requestId, [CallerMemberName] string? memberName = null) =>
NetEventSource.Log.HandlerMessageError(0, 0, requestId, memberName, ToString() + ": " + message);
}
}