Repository navigation
Expand file tree
/
Copy pathManagedHandler.cs
More file actions
308 lines (271 loc) · 11.5 KB
/
Copy pathManagedHandler.cs
File metadata and controls
308 lines (271 loc) · 11.5 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Net.Http.Client
{
public class ManagedHandler : HttpMessageHandler
{
public ManagedHandler()
{
MaxAutomaticRedirects = 20;
RedirectMode = RedirectMode.NoDowngrade;
}
public Uri ProxyAddress
{
// TODO: Validate that only an absolute http address is specified. Path, query, and fragment are ignored
get; set;
}
public int MaxAutomaticRedirects { get; set; }
public RedirectMode RedirectMode { get; set; }
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
HttpResponseMessage response = null;
int redirectCount = 0;
bool retry;
do
{
retry = false;
response = await ProcessRequestAsync(request, cancellationToken);
if (redirectCount < MaxAutomaticRedirects && IsAllowedRedirectResponse(request, response))
{
redirectCount++;
retry = true;
}
} while (retry);
return response;
}
private bool IsAllowedRedirectResponse(HttpRequestMessage request, HttpResponseMessage response)
{
// Are redirects enabled?
if (RedirectMode == RedirectMode.None)
{
return false;
}
// Status codes 301 and 302
if (response.StatusCode != HttpStatusCode.Redirect && response.StatusCode != HttpStatusCode.Moved)
{
return false;
}
Uri location = response.Headers.Location;
if (location == null)
{
return false;
}
if (!location.IsAbsoluteUri)
{
request.RequestUri = location;
request.SetPathAndQueryProperty(null);
request.SetAddressLineProperty(null);
request.Headers.Authorization = null;
return true;
}
// Check if redirect from https to http is allowed
if (request.IsHttps() && string.Equals("http", location.Scheme, StringComparison.OrdinalIgnoreCase)
&& RedirectMode == RedirectMode.NoDowngrade)
{
return false;
}
// Reset fields calculated from the URI.
request.RequestUri = location;
request.SetSchemeProperty(null);
request.Headers.Host = null;
request.Headers.Authorization = null;
request.SetHostProperty(null);
request.SetConnectionHostProperty(null);
request.SetPortProperty(null);
request.SetConnectionPortProperty(null);
request.SetPathAndQueryProperty(null);
request.SetAddressLineProperty(null);
return true;
}
private async Task<HttpResponseMessage> ProcessRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ProcessUrl(request);
ProcessHostHeader(request);
request.Headers.ConnectionClose = true; // TODO: Connection re-use is not supported.
if (request.Method != HttpMethod.Get)
{
throw new NotImplementedException(request.Method.Method); // TODO: POST
}
ProxyMode proxyMode = DetermineProxyModeAndAddressLine(request);
ApmStream transport = await ConnectAsync(request, cancellationToken);
if (proxyMode == ProxyMode.Tunnel)
{
await TunnelThroughProxyAsync(request, transport, cancellationToken);
}
System.Diagnostics.Debug.Assert(!(proxyMode == ProxyMode.Http && request.IsHttps()));
if (request.IsHttps())
{
SslStream sslStream = new SslStream(transport);
await sslStream.AuthenticateAsClientAsync(request.GetHostProperty());
transport = new ApmStreamWrapper(sslStream);
}
var bufferedReadStream = new BufferedReadStream(transport);
var connection = new HttpConnection(bufferedReadStream);
return await connection.SendAsync(request, cancellationToken);
}
// Data comes from either the request.RequestUri or from the request.Properties
private void ProcessUrl(HttpRequestMessage request)
{
string scheme = request.GetSchemeProperty();
if (string.IsNullOrWhiteSpace(scheme))
{
if (!request.RequestUri.IsAbsoluteUri)
{
throw new InvalidOperationException("Missing URL Scheme");
}
scheme = request.RequestUri.Scheme;
request.SetSchemeProperty(scheme);
}
if (!(request.IsHttp() || request.IsHttps()))
{
throw new InvalidOperationException("Only HTTP or HTTPS are supported, not: " + request.RequestUri.Scheme);
}
string host = request.GetHostProperty();
if (string.IsNullOrWhiteSpace(host))
{
if (!request.RequestUri.IsAbsoluteUri)
{
throw new InvalidOperationException("Missing URL Scheme");
}
host = request.RequestUri.DnsSafeHost;
request.SetHostProperty(host);
}
string connectionHost = request.GetConnectionHostProperty();
if (string.IsNullOrWhiteSpace(connectionHost))
{
request.SetConnectionHostProperty(host);
}
int? port = request.GetPortProperty();
if (!port.HasValue)
{
if (!request.RequestUri.IsAbsoluteUri)
{
throw new InvalidOperationException("Missing URL Scheme");
}
port = request.RequestUri.Port;
request.SetPortProperty(port);
}
int? connectionPort = request.GetConnectionPortProperty();
if (!connectionPort.HasValue)
{
request.SetConnectionPortProperty(port);
}
string pathAndQuery = request.GetPathAndQueryProperty();
if (string.IsNullOrWhiteSpace(pathAndQuery))
{
if (request.RequestUri.IsAbsoluteUri)
{
pathAndQuery = request.RequestUri.PathAndQuery;
}
else
{
pathAndQuery = Uri.EscapeUriString(request.RequestUri.ToString());
}
request.SetPathAndQueryProperty(pathAndQuery);
}
}
private void ProcessHostHeader(HttpRequestMessage request)
{
if (string.IsNullOrWhiteSpace(request.Headers.Host))
{
string host = request.GetHostProperty();
int port = request.GetPortProperty().Value;
if (host.Contains(':'))
{
// IPv6
host = '[' + host + ']';
}
request.Headers.Host = host + ":" + port.ToString(CultureInfo.InvariantCulture);
}
}
private ProxyMode DetermineProxyModeAndAddressLine(HttpRequestMessage request)
{
string scheme = request.GetSchemeProperty();
string host = request.GetHostProperty();
int? port = request.GetPortProperty();
string pathAndQuery = request.GetPathAndQueryProperty();
string addressLine = request.GetAddressLineProperty();
if (string.IsNullOrEmpty(addressLine))
{
request.SetAddressLineProperty(pathAndQuery);
}
if (ProxyAddress == null)
{
return ProxyMode.None;
}
if (request.IsHttp())
{
if (string.IsNullOrEmpty(addressLine))
{
addressLine = scheme + "://" + host + ":" + port.Value + pathAndQuery;
request.SetAddressLineProperty(addressLine);
}
request.SetConnectionHostProperty(ProxyAddress.DnsSafeHost);
request.SetConnectionPortProperty(ProxyAddress.Port);
return ProxyMode.Http;
}
// Tunneling generates a completely seperate request, don't alter the original, just the connection address.
request.SetConnectionHostProperty(ProxyAddress.DnsSafeHost);
request.SetConnectionPortProperty(ProxyAddress.Port);
return ProxyMode.Tunnel;
}
private async Task<ApmStream> ConnectAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
TcpClient client = new TcpClient();
try
{
await client.ConnectAsync(request.GetConnectionHostProperty(), request.GetConnectionPortProperty().Value);
return new ApmStreamWrapper(client.GetStream());
}
catch (SocketException sox)
{
((IDisposable)client).Dispose();
throw new HttpRequestException("Request failed", sox);
}
}
private async Task TunnelThroughProxyAsync(HttpRequestMessage request, ApmStream transport, CancellationToken cancellationToken)
{
// Send a Connect request:
// CONNECT server.example.com:80 HTTP / 1.1
// Host: server.example.com:80
var connectReqeuest = new HttpRequestMessage();
connectReqeuest.Headers.ProxyAuthorization = request.Headers.ProxyAuthorization;
connectReqeuest.Method = new HttpMethod("CONNECT");
// TODO: IPv6 hosts
string authority = request.GetHostProperty() + ":" + request.GetPortProperty().Value;
connectReqeuest.SetAddressLineProperty(authority);
connectReqeuest.Headers.Host = authority;
HttpConnection connection = new HttpConnection(new BufferedReadStream(transport));
HttpResponseMessage connectResponse;
try
{
connectResponse = await connection.SendAsync(connectReqeuest, cancellationToken);
// TODO:? await connectResponse.Content.LoadIntoBufferAsync(); // Drain any body
// There's no danger of accidently consuming real response data because the real request hasn't been sent yet.
}
catch (Exception ex)
{
transport.Dispose();
throw new HttpRequestException("SSL Tunnel failed to initialize", ex);
}
// Listen for a response. Any 2XX is considered success, anything else is considered a failure.
if ((int)connectResponse.StatusCode < 200 || 300 <= (int)connectResponse.StatusCode)
{
transport.Dispose();
throw new HttpRequestException("Failed to negotiate the proxy tunnel: " + connectResponse.ToString());
}
}
}
}