forked from aws/aws-sdk-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpWebRequestFactory.cs
583 lines (531 loc) · 24.3 KB
/
HttpWebRequestFactory.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using Amazon.Runtime.Internal.Transform;
using Amazon.Runtime.Internal.Util;
using Amazon.Util;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
#if AWS_ASYNC_API
using System.Threading;
using System.Threading.Tasks;
#endif
namespace Amazon.Runtime.Internal
{
/// <summary>
/// The request factory for System.Net.HttpWebRequest.
/// </summary>
public class HttpWebRequestFactory : IHttpRequestFactory<Stream>
{
/// <summary>
/// Creates an HTTP request for the given URI.
/// </summary>
/// <param name="requestUri">The request URI.</param>
/// <returns>An HTTP request.</returns>
public IHttpRequest<Stream> CreateHttpRequest(Uri requestUri)
{
return new HttpRequest(requestUri);
}
/// <summary>
/// Disposes the HttpWebRequestFactory.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
/// <summary>
/// HTTP request wrapper for System.Net.HttpWebRequest.
/// </summary>
public class HttpRequest : IHttpRequest<Stream>
{
private bool _isAborted = false;
private HttpWebRequest _request;
/// <summary>
/// Constructor for HttpRequest.
/// </summary>
/// <param name="requestUri">The request URI.</param>
public HttpRequest(Uri requestUri)
{
_request = WebRequest.Create(requestUri) as HttpWebRequest;
}
/// <summary>
/// The underlying HTTP web request.
/// </summary>
public HttpWebRequest Request
{
get { return _request; }
}
/// <summary>
/// The HTTP method or verb.
/// </summary>
public string Method
{
get { return _request.Method; }
set { _request.Method = value; }
}
/// <summary>
/// The request URI.
/// </summary>
public Uri RequestUri
{
get { return _request.RequestUri; }
}
/// <summary>
/// Returns the HTTP response.
/// </summary>
/// <returns>The HTTP response.</returns>
public virtual IWebResponseData GetResponse()
{
try
{
var response = _request.GetResponse() as HttpWebResponse;
return new HttpWebRequestResponseData(response);
}
catch (WebException webException)
{
var errorResponse = webException.Response as HttpWebResponse;
if (errorResponse != null)
{
throw new HttpErrorResponseException(webException.Message,
webException,
new HttpWebRequestResponseData(errorResponse));
}
throw;
}
}
/// <summary>
/// Gets a handle to the request content.
/// </summary>
/// <returns>The request content.</returns>
public Stream GetRequestContent()
{
return _request.GetRequestStream();
}
/// <summary>
/// Writes a stream to the request body.
/// </summary>
/// <param name="requestContent">The destination where the content stream is written.</param>
/// <param name="contentStream">The content stream to be written.</param>
/// <param name="contentHeaders">HTTP content headers.</param>
/// <param name="requestContext">The request context.</param>
public void WriteToRequestBody(Stream requestContent, Stream contentStream,
IDictionary<string, string> contentHeaders, IRequestContext requestContext)
{
bool gotException = false;
try
{
var buffer = new byte[requestContext.ClientConfig.BufferSize];
int bytesRead = 0;
int bytesToRead = buffer.Length;
while ((bytesRead = contentStream.Read(buffer, 0, bytesToRead)) > 0)
{
requestContent.Write(buffer, 0, bytesRead);
}
}
catch
{
gotException = true;
// If an exception occurred while reading the input stream,
// Abort the request to signal failure to the server and prevent
// potentially writing an incomplete stream to the server.
this.Abort();
throw;
}
finally
{
// Only bubble up exception from the close method if we haven't already got an exception
// reading and writing from the streams.
try
{
requestContent.Close();
}
catch
{
if (!gotException)
throw;
}
}
}
/// <summary>
/// Writes a byte array to the request body.
/// </summary>
/// <param name="requestContent">The destination where the content stream is written.</param>
/// <param name="content">The content stream to be written.</param>
/// <param name="contentHeaders">HTTP content headers.</param>
public void WriteToRequestBody(Stream requestContent, byte[] content, IDictionary<string, string> contentHeaders)
{
using (requestContent)
{
requestContent.Write(content, 0, content.Length);
}
}
/// <summary>
/// Aborts the HTTP request.
/// </summary>
public void Abort()
{
if (!_isAborted)
{
_request.Abort();
_isAborted = true;
}
}
#if AWS_ASYNC_API
/// <summary>
/// Writes a stream to the request body.
/// </summary>
/// <param name="requestContent">The destination where the content stream is written.</param>
/// <param name="contentStream">The content stream to be written.</param>
/// <param name="contentHeaders">HTTP content headers.</param>
/// <param name="requestContext">The request context.</param>
public async Task WriteToRequestBodyAsync(Stream requestContent, Stream contentStream,
IDictionary<string, string> contentHeaders, IRequestContext requestContext)
{
bool gotException = false;
try
{
var buffer = new byte[requestContext.ClientConfig.BufferSize];
int bytesRead = 0;
int bytesToRead = buffer.Length;
while ((bytesRead = await contentStream.ReadAsync(buffer, 0, bytesToRead, requestContext.CancellationToken).ConfigureAwait(false)) > 0)
{
requestContext.CancellationToken.ThrowIfCancellationRequested();
await requestContent.WriteAsync(buffer, 0, bytesRead, requestContext.CancellationToken).ConfigureAwait(false);
}
}
catch
{
gotException = true;
// If an exception occurred while reading the input stream,
// Abort the request to signal failure to the server and prevent
// potentially writing an incomplete stream to the server.
this.Abort();
throw;
}
finally
{
// Only bubble up exception from the close method if we haven't already got an exception
// reading and writing from the streams.
try
{
requestContent.Close();
}
catch
{
if (!gotException)
throw;
}
}
}
/// <summary>
/// Writes a byte array to the request body.
/// </summary>
/// <param name="requestContent">The destination where the content stream is written.</param>
/// <param name="content">The content stream to be written.</param>
/// <param name="contentHeaders">HTTP content headers.</param>
public async Task WriteToRequestBodyAsync(Stream requestContent, byte[] content, IDictionary<string, string> contentHeaders, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
using (requestContent)
{
await requestContent.WriteAsync(content, 0, content.Length, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets a handle to the request content.
/// </summary>
/// <returns></returns>
public async Task<Stream> GetRequestContentAsync()
{
return await GetRequestContentAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Gets a handle to the request content.
/// </summary>
/// <param name="cancellationToken">Used to cancel the request on demand</param>
/// <returns></returns>
public async Task<Stream> GetRequestContentAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
using (cancellationToken.Register(() => this.Abort(), useSynchronizationContext:false))
{
try
{
return await _request.GetRequestStreamAsync().ConfigureAwait(false);
}
catch (WebException webException)
{
// After HttpWebRequest.Abort() is called, GetRequestStreamAsync throws a WebException.
// If request has been cancelled using cancellationToken, wrap the
// WebException in an OperationCancelledException.
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(webException.Message, webException, cancellationToken);
}
var errorResponse = webException.Response as HttpWebResponse;
if (errorResponse != null)
{
throw new HttpErrorResponseException(webException.Message,
webException,
new HttpWebRequestResponseData(errorResponse));
}
throw;
}
}
}
/// <summary>
/// Returns the HTTP response.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param>
/// <returns></returns>
public virtual async Task<IWebResponseData> GetResponseAsync(System.Threading.CancellationToken cancellationToken)
{
using (var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
using (linkedTokenSource.Token.Register(() => this.Abort(), useSynchronizationContext: false))
{
linkedTokenSource.CancelAfter(_request.Timeout);
try
{
var response = await _request.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse;
return new HttpWebRequestResponseData(response);
}
catch (WebException webException)
{
// After HttpWebRequest.Abort() is called, GetResponseAsync throws a WebException.
// If request has been cancelled using cancellationToken, wrap the
// WebException in an OperationCancelledException.
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(webException.Message, webException, cancellationToken);
}
if (linkedTokenSource.Token.IsCancellationRequested)
{
throw new OperationCanceledException(webException.Message, webException, linkedTokenSource.Token);
}
var errorResponse = webException.Response as HttpWebResponse;
if (errorResponse != null)
{
throw new HttpErrorResponseException(webException.Message,
webException,
new HttpWebRequestResponseData(errorResponse));
}
throw;
}
}
}
#elif AWS_APM_API
/// <summary>
/// Initiates the operation to gets a handle to the request content.
/// </summary>
/// <param name="callback">The async callback invoked when the operation completes.</param>
/// <param name="state">The state object to be passed to the async callback.</param>
/// <returns>IAsyncResult that represents an async operation.</returns>
public IAsyncResult BeginGetRequestContent(AsyncCallback callback, object state)
{
return _request.BeginGetRequestStream(callback, state);
}
/// <summary>
/// Ends the operation to gets a handle to the request content.
/// </summary>
/// <param name="asyncResult">IAsyncResult that represents an async operation.</param>
/// <returns>The request content.</returns>
public Stream EndGetRequestContent(IAsyncResult asyncResult)
{
return _request.EndGetRequestStream(asyncResult);
}
/// <summary>
/// Initiates the operation to Returns the HTTP response.
/// </summary>
/// <param name="callback">The async callback invoked when the operation completes.</param>
/// <param name="state">The state object to be passed to the async callback.</param>
/// <returns>IAsyncResult that represents an async operation.</returns>
public IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
return _request.BeginGetResponse(callback, state);
}
/// <summary>
/// Ends the operation to Returns the HTTP response.
/// </summary>
/// <param name="asyncResult">IAsyncResult that represents an async operation.</param>
/// <returns>The HTTP response.</returns>
public virtual IWebResponseData EndGetResponse(IAsyncResult asyncResult)
{
try
{
var response = _request.EndGetResponse(asyncResult) as HttpWebResponse;
return new HttpWebRequestResponseData(response);
}
catch (WebException webException)
{
var errorResponse = webException.Response as HttpWebResponse;
if (errorResponse != null)
{
throw new HttpErrorResponseException(webException.Message,
webException,
new HttpWebRequestResponseData(errorResponse));
}
throw;
}
}
#endif
/// <summary>
/// Configures a request as per the request context.
/// </summary>
/// <param name="requestContext">The request context.</param>
public virtual void ConfigureRequest(IRequestContext requestContext)
{
// Set HttpWebRequest specific properties which are
// not exposed in the IHttpRequest interface.
var clientConfig = requestContext.ClientConfig;
var originalRequest = requestContext.OriginalRequest;
// If System.Net.WebRequest.AllowAutoRedirect is set to true (default value),
// redirects for GET requests are automatically followed and redirects for POST
// requests are thrown back as exceptions.
// If System.Net.WebRequest.AllowAutoRedirect is set to false (e.g. S3),
// redirects are returned as responses.
_request.AllowAutoRedirect = clientConfig.AllowAutoRedirect;
// Configure timeouts.
if (requestContext.Request.ContentStream != null)
{
_request.Timeout = int.MaxValue;
_request.ReadWriteTimeout = int.MaxValue;
_request.AllowWriteStreamBuffering = false;
}
// Override the Timeout and ReadWriteTimeout values if set at the request or config level.
// Public Timeout and ReadWriteTimeout properties are present on client config objects.
var timeout = ClientConfig.GetTimeoutValue(clientConfig.Timeout, originalRequest.TimeoutInternal);
var readWriteTimeout = ClientConfig.GetTimeoutValue(clientConfig.ReadWriteTimeout, originalRequest.ReadWriteTimeoutInternal);
if (timeout != null)
{
_request.Timeout = (int)timeout.Value.TotalMilliseconds;
}
if (readWriteTimeout != null)
{
_request.ReadWriteTimeout = (int)readWriteTimeout.Value.TotalMilliseconds;
}
// Set proxy related properties
IWebProxy proxy = requestContext.ClientConfig.GetWebProxy();
if (proxy != null)
{
requestContext.Metrics.AddProperty(Metric.ProxyHost, requestContext.ClientConfig.ProxyHost);
requestContext.Metrics.AddProperty(Metric.ProxyPort, requestContext.ClientConfig.ProxyPort);
_request.Proxy = proxy;
}
else if (_request.RequestUri.Scheme == Uri.UriSchemeHttp)
{
_request.Proxy = requestContext.ClientConfig.GetHttpProxy();
}
else if (_request.RequestUri.Scheme == Uri.UriSchemeHttps)
{
_request.Proxy = requestContext.ClientConfig.GetHttpsProxy();
}
// Set service point properties.
_request.ServicePoint.ConnectionLimit = clientConfig.ConnectionLimit;
_request.ServicePoint.UseNagleAlgorithm = clientConfig.UseNagleAlgorithm;
_request.ServicePoint.MaxIdleTime = clientConfig.MaxIdleTime;
_request.ServicePoint.Expect100Continue = originalRequest.GetExpect100Continue();
var tcpKeepAlive = clientConfig.TcpKeepAlive;
_request.ServicePoint.SetTcpKeepAlive(
tcpKeepAlive.Enabled,
(int) tcpKeepAlive.Timeout.Value.TotalMilliseconds,
(int) tcpKeepAlive.Interval.Value.TotalMilliseconds);
}
/// <summary>
/// Sets the headers on the request.
/// </summary>
/// <param name="headers">A dictionary of header names and values.</param>
public void SetRequestHeaders(IDictionary<string, string> headers)
{
AddHeaders(_request, headers);
}
private static System.Reflection.MethodInfo _addWithoutValidateHeadersMethod =
typeof(WebHeaderCollection).GetMethod("AddWithoutValidate", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
// As per MSDN documentation (http://msdn.microsoft.com/en-us/library/system.net.webheadercollection%28v=VS.80%29.aspx)
// some headers are restricted, cannot be set through the request.Headers property and must be
// set through properties on the HttpWebRequest
internal static void AddHeaders(HttpWebRequest request, IDictionary<string, string> headersToAdd)
{
var headers = request.Headers;
foreach (var kvp in headersToAdd)
{
if (WebHeaderCollection.IsRestricted(kvp.Key) || string.Equals(kvp.Key, HeaderKeys.RangeHeader, StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(kvp.Key, HeaderKeys.AcceptHeader, StringComparison.OrdinalIgnoreCase))
request.Accept = kvp.Value;
else if (string.Equals(kvp.Key, HeaderKeys.ContentTypeHeader, StringComparison.OrdinalIgnoreCase))
request.ContentType = kvp.Value;
else if (string.Equals(kvp.Key, HeaderKeys.ContentLengthHeader, StringComparison.OrdinalIgnoreCase))
request.ContentLength = long.Parse(kvp.Value, CultureInfo.InvariantCulture);
else if (string.Equals(kvp.Key, HeaderKeys.UserAgentHeader, StringComparison.OrdinalIgnoreCase))
request.UserAgent = kvp.Value;
else if (string.Equals(kvp.Key, HeaderKeys.TransferEncodingHeader, StringComparison.OrdinalIgnoreCase)
&& string.Equals(kvp.Value, "chunked", StringComparison.OrdinalIgnoreCase))
request.SendChunked = true;
// Date accessor is only present in .NET 4.0, so using reflection
else if (string.Equals(kvp.Key, HeaderKeys.DateHeader, StringComparison.OrdinalIgnoreCase))
_addWithoutValidateHeadersMethod.Invoke(request.Headers, new[] { HeaderKeys.DateHeader, kvp.Value });
// Host accessor is only present in .NET 4.0, so using reflection
else if (string.Equals(kvp.Key, HeaderKeys.HostHeader, StringComparison.OrdinalIgnoreCase))
_addWithoutValidateHeadersMethod.Invoke(request.Headers, new[] { HeaderKeys.HostHeader, kvp.Value });
else if (string.Equals(kvp.Key, HeaderKeys.RangeHeader, StringComparison.OrdinalIgnoreCase))
_addWithoutValidateHeadersMethod.Invoke(request.Headers, new[] { HeaderKeys.RangeHeader, kvp.Value });
else if (string.Equals(kvp.Key, HeaderKeys.IfModifiedSinceHeader, StringComparison.OrdinalIgnoreCase))
_addWithoutValidateHeadersMethod.Invoke(request.Headers, new[] { kvp.Key, kvp.Value });
else
throw new NotSupportedException("Header with name " + kvp.Key + " is not supported");
}
else
{
headers[kvp.Key] = kvp.Value;
}
}
}
/// <summary>
/// Disposes the HttpRequest.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// NOP since HttWebRequest does not implement IDisposable.
}
/// <summary>
/// Sets up the progress listeners
/// </summary>
/// <param name="originalStream">The content stream</param>
/// <param name="progressUpdateInterval">The interval at which progress needs to be published</param>
/// <param name="sender">The objects which is trigerring the progress changes</param>
/// <param name="callback">The callback which will be invoked when the progress changed event is trigerred</param>
/// <returns>an <see cref="EventStream"/> object, incase the progress is setup, else returns the original stream</returns>
public Stream SetupProgressListeners(Stream originalStream, long progressUpdateInterval, object sender, EventHandler<StreamTransferProgressArgs> callback)
{
var eventStream = new EventStream(originalStream, true);
var tracker = new StreamReadTracker(sender, callback, originalStream.Length,
progressUpdateInterval);
eventStream.OnRead += tracker.ReadProgress;
return eventStream;
}
}
}