-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathHttpContentMessageExtensions.cs
More file actions
546 lines (485 loc) · 28.1 KB
/
HttpContentMessageExtensions.cs
File metadata and controls
546 lines (485 loc) · 28.1 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
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http.Formatting.Parsers;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Net.Http
{
/// <summary>
/// Extension methods to read <see cref="HttpRequestMessage"/> and <see cref="HttpResponseMessage"/> entities from <see cref="HttpContent"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpContentMessageExtensions
{
private const int MinBufferSize = 256;
private const int DefaultBufferSize = 32 * 1024;
/// <summary>
/// Determines whether the specified content is HTTP request message content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the specified content is HTTP message content; otherwise, <c>false</c>.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to false.")]
public static bool IsHttpRequestMessageContent(this HttpContent content)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
try
{
return HttpMessageContent.ValidateHttpMessageContent(content, true, false);
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Determines whether the specified content is HTTP response message content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the specified content is HTTP message content; otherwise, <c>false</c>.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to false.")]
public static bool IsHttpResponseMessageContent(this HttpContent content)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
try
{
return HttpMessageContent.ValidateHttpMessageContent(content, false, false);
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content)
{
return ReadAsHttpRequestMessageAsync(content, "http", DefaultBufferSize);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, CancellationToken cancellationToken)
{
return ReadAsHttpRequestMessageAsync(content, "http", DefaultBufferSize, cancellationToken);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme)
{
return ReadAsHttpRequestMessageAsync(content, uriScheme, DefaultBufferSize);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme,
CancellationToken cancellationToken)
{
return ReadAsHttpRequestMessageAsync(content, uriScheme, DefaultBufferSize, cancellationToken);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI (the
/// URI scheme is not actually part of the HTTP Request URI and so must be provided externally).</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme, int bufferSize)
{
return ReadAsHttpRequestMessageAsync(content, uriScheme, bufferSize, HttpRequestHeaderParser.DefaultMaxHeaderSize);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI (the
/// URI scheme is not actually part of the HTTP Request URI and so must be provided externally).</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme,
int bufferSize, CancellationToken cancellationToken)
{
return ReadAsHttpRequestMessageAsync(content, uriScheme, bufferSize,
HttpRequestHeaderParser.DefaultMaxHeaderSize, cancellationToken);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI (the
/// URI scheme is not actually part of the HTTP Request URI and so must be provided externally).</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="maxHeaderSize">The max length of the HTTP header.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to parser state.")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme,
int bufferSize, int maxHeaderSize)
{
return ReadAsHttpRequestMessageAsync(content, uriScheme, bufferSize, maxHeaderSize, CancellationToken.None);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpRequestMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="uriScheme">The URI scheme to use for the request URI (the
/// URI scheme is not actually part of the HTTP Request URI and so must be provided externally).</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="maxHeaderSize">The max length of the HTTP header.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpRequestMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "This is not a full URI but only the URI scheme")]
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to parser state.")]
public static Task<HttpRequestMessage> ReadAsHttpRequestMessageAsync(this HttpContent content, string uriScheme,
int bufferSize, int maxHeaderSize, CancellationToken cancellationToken)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
if (uriScheme == null)
{
throw Error.ArgumentNull("uriScheme");
}
if (!Uri.CheckSchemeName(uriScheme))
{
throw Error.Argument("uriScheme", Properties.Resources.HttpMessageParserInvalidUriScheme, uriScheme, typeof(Uri).Name);
}
if (bufferSize < MinBufferSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
}
if (maxHeaderSize < InternetMessageFormatHeaderParser.MinHeaderSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("maxHeaderSize", maxHeaderSize, InternetMessageFormatHeaderParser.MinHeaderSize);
}
HttpMessageContent.ValidateHttpMessageContent(content, true, true);
return content.ReadAsHttpRequestMessageAsyncCore(uriScheme, bufferSize, maxHeaderSize, cancellationToken);
}
private static async Task<HttpRequestMessage> ReadAsHttpRequestMessageAsyncCore(this HttpContent content,
string uriScheme, int bufferSize, int maxHeaderSize, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Stream stream = await content.ReadAsStreamAsync();
HttpUnsortedRequest httpRequest = new HttpUnsortedRequest();
HttpRequestHeaderParser parser = new HttpRequestHeaderParser(httpRequest,
Math.Max(HttpRequestHeaderParser.DefaultMaxRequestLineSize, maxHeaderSize), maxHeaderSize);
ParserState parseStatus;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
int headerConsumed = 0;
while (true)
{
try
{
bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
}
catch (Exception e)
{
throw new IOException(Properties.Resources.HttpMessageErrorReading, e);
}
try
{
parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed);
}
catch (Exception)
{
parseStatus = ParserState.Invalid;
}
if (parseStatus == ParserState.Done)
{
return CreateHttpRequestMessage(uriScheme, httpRequest, stream, bytesRead - headerConsumed);
}
else if (parseStatus != ParserState.NeedMoreData)
{
throw Error.InvalidOperation(Properties.Resources.HttpMessageParserError, headerConsumed, buffer);
}
else if (bytesRead == 0)
{
throw new IOException(Properties.Resources.ReadAsHttpMessageUnexpectedTermination);
}
}
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpResponseMessage"/>.</returns>
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content)
{
return ReadAsHttpResponseMessageAsync(content, DefaultBufferSize);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpResponseMessage"/>.</returns>
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, CancellationToken cancellationToken)
{
return ReadAsHttpResponseMessageAsync(content, DefaultBufferSize, cancellationToken);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpResponseMessage"/>.</returns>
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize)
{
return ReadAsHttpResponseMessageAsync(content, bufferSize, HttpResponseHeaderParser.DefaultMaxHeaderSize);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpResponseMessage"/>.</returns>
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize,
CancellationToken cancellationToken)
{
return ReadAsHttpResponseMessageAsync(content, bufferSize, HttpResponseHeaderParser.DefaultMaxHeaderSize, cancellationToken);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="maxHeaderSize">The max length of the HTTP header.</param>
/// <returns>A task object representing reading the content as an <see cref="HttpResponseMessage"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to parser state.")]
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize,
int maxHeaderSize)
{
return ReadAsHttpResponseMessageAsync(content, bufferSize, maxHeaderSize, CancellationToken.None);
}
/// <summary>
/// Read the <see cref="HttpContent"/> as an <see cref="HttpResponseMessage"/>.
/// </summary>
/// <param name="content">The content to read.</param>
/// <param name="bufferSize">Size of the buffer.</param>
/// <param name="maxHeaderSize">The max length of the HTTP header.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The parsed <see cref="HttpResponseMessage"/> instance.</returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception translates to parser state.")]
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize,
int maxHeaderSize, CancellationToken cancellationToken)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
if (bufferSize < MinBufferSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("bufferSize", bufferSize, MinBufferSize);
}
if (maxHeaderSize < InternetMessageFormatHeaderParser.MinHeaderSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("maxHeaderSize", maxHeaderSize, InternetMessageFormatHeaderParser.MinHeaderSize);
}
HttpMessageContent.ValidateHttpMessageContent(content, false, true);
return content.ReadAsHttpResponseMessageAsyncCore(bufferSize, maxHeaderSize, cancellationToken);
}
private static async Task<HttpResponseMessage> ReadAsHttpResponseMessageAsyncCore(this HttpContent content,
int bufferSize, int maxHeaderSize, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Stream stream = await content.ReadAsStreamAsync();
HttpUnsortedResponse httpResponse = new HttpUnsortedResponse();
HttpResponseHeaderParser parser = new HttpResponseHeaderParser(httpResponse, HttpResponseHeaderParser.DefaultMaxStatusLineSize, maxHeaderSize);
ParserState parseStatus;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
int headerConsumed = 0;
while (true)
{
try
{
bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
}
catch (Exception e)
{
throw new IOException(Properties.Resources.HttpMessageErrorReading, e);
}
try
{
parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed);
}
catch (Exception)
{
parseStatus = ParserState.Invalid;
}
if (parseStatus == ParserState.Done)
{
// Create and return parsed HttpResponseMessage
return CreateHttpResponseMessage(httpResponse, stream, bytesRead - headerConsumed);
}
else if (parseStatus != ParserState.NeedMoreData)
{
throw Error.InvalidOperation(Properties.Resources.HttpMessageParserError, headerConsumed, buffer);
}
else if (bytesRead == 0)
{
throw new IOException(Properties.Resources.ReadAsHttpMessageUnexpectedTermination);
}
}
}
/// <summary>
/// Creates the request URI by combining scheme (provided) with parsed values of
/// host and path.
/// </summary>
/// <param name="uriScheme">The URI scheme to use for the request URI.</param>
/// <param name="httpRequest">The unsorted HTTP request.</param>
/// <returns>A fully qualified request URI.</returns>
private static Uri CreateRequestUri(string uriScheme, HttpUnsortedRequest httpRequest)
{
Contract.Assert(httpRequest != null, "httpRequest cannot be null.");
Contract.Assert(uriScheme != null, "uriScheme cannot be null");
IEnumerable<string> hostValues;
if (httpRequest.HttpHeaders.TryGetValues(FormattingUtilities.HttpHostHeader, out hostValues))
{
int hostCount = hostValues.Count();
if (hostCount != 1)
{
throw Error.InvalidOperation(Properties.Resources.HttpMessageParserInvalidHostCount, FormattingUtilities.HttpHostHeader, hostCount);
}
}
else
{
throw Error.InvalidOperation(Properties.Resources.HttpMessageParserInvalidHostCount, FormattingUtilities.HttpHostHeader, 0);
}
// We don't use UriBuilder as hostValues.ElementAt(0) contains 'host:port' and UriBuilder needs these split out into separate host and port.
string requestUri = String.Format(CultureInfo.InvariantCulture, "{0}://{1}{2}", uriScheme, hostValues.ElementAt(0), httpRequest.RequestUri);
return new Uri(requestUri);
}
/// <summary>
/// Copies the unsorted header fields to a sorted collection.
/// </summary>
/// <param name="source">The unsorted source headers</param>
/// <param name="destination">The destination <see cref="HttpRequestHeaders"/> or <see cref="HttpResponseHeaders"/>.</param>
/// <param name="contentStream">The input <see cref="Stream"/> used to form any <see cref="HttpContent"/> being part of this HTTP request.</param>
/// <param name="rewind">Start location of any request entity within the <paramref name="contentStream"/>.</param>
/// <returns>An <see cref="HttpContent"/> instance if header fields contained and <see cref="HttpContentHeaders"/>.</returns>
private static HttpContent CreateHeaderFields(HttpHeaders source, HttpHeaders destination, Stream contentStream, int rewind)
{
Contract.Assert(source != null, "source headers cannot be null");
Contract.Assert(destination != null, "destination headers cannot be null");
Contract.Assert(contentStream != null, "contentStream must be non null");
HttpContentHeaders contentHeaders = null;
HttpContent content;
// Set the header fields
foreach (KeyValuePair<string, IEnumerable<string>> header in source)
{
if (!destination.TryAddWithoutValidation(header.Key, header.Value))
{
if (contentHeaders == null)
{
contentHeaders = FormattingUtilities.CreateEmptyContentHeaders();
}
contentHeaders.TryAddWithoutValidation(header.Key, header.Value);
}
}
// If we have content headers then create an HttpContent for this Response
if (contentHeaders != null)
{
// Need to rewind the input stream to be at the position right after the HTTP header
// which we may already have parsed as we read the content stream.
if (!contentStream.CanSeek)
{
throw Error.InvalidOperation(Properties.Resources.HttpMessageContentStreamMustBeSeekable, "ContentReadStream", FormattingUtilities.HttpResponseMessageType.Name);
}
contentStream.Seek(0 - rewind, SeekOrigin.Current);
content = new StreamContent(contentStream);
contentHeaders.CopyTo(content.Headers);
}
else
{
content = new StreamContent(Stream.Null);
}
return content;
}
/// <summary>
/// Creates an <see cref="HttpRequestMessage"/> based on information provided in <see cref="HttpUnsortedRequest"/>.
/// </summary>
/// <param name="uriScheme">The URI scheme to use for the request URI.</param>
/// <param name="httpRequest">The unsorted HTTP request.</param>
/// <param name="contentStream">The input <see cref="Stream"/> used to form any <see cref="HttpContent"/> being part of this HTTP request.</param>
/// <param name="rewind">Start location of any request entity within the <paramref name="contentStream"/>.</param>
/// <returns>A newly created <see cref="HttpRequestMessage"/> instance.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "caller becomes owner.")]
private static HttpRequestMessage CreateHttpRequestMessage(string uriScheme, HttpUnsortedRequest httpRequest, Stream contentStream, int rewind)
{
Contract.Assert(uriScheme != null, "URI scheme must be non null");
Contract.Assert(httpRequest != null, "httpRequest must be non null");
Contract.Assert(contentStream != null, "contentStream must be non null");
HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
// Set method, requestURI, and version
httpRequestMessage.Method = httpRequest.Method;
httpRequestMessage.RequestUri = CreateRequestUri(uriScheme, httpRequest);
httpRequestMessage.Version = httpRequest.Version;
// Set the header fields and content if any
httpRequestMessage.Content = CreateHeaderFields(httpRequest.HttpHeaders, httpRequestMessage.Headers, contentStream, rewind);
return httpRequestMessage;
}
/// <summary>
/// Creates an <see cref="HttpResponseMessage"/> based on information provided in <see cref="HttpUnsortedResponse"/>.
/// </summary>
/// <param name="httpResponse">The unsorted HTTP Response.</param>
/// <param name="contentStream">The input <see cref="Stream"/> used to form any <see cref="HttpContent"/> being part of this HTTP Response.</param>
/// <param name="rewind">Start location of any Response entity within the <paramref name="contentStream"/>.</param>
/// <returns>A newly created <see cref="HttpResponseMessage"/> instance.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "caller becomes owner.")]
private static HttpResponseMessage CreateHttpResponseMessage(HttpUnsortedResponse httpResponse, Stream contentStream, int rewind)
{
Contract.Assert(httpResponse != null, "httpResponse must be non null");
Contract.Assert(contentStream != null, "contentStream must be non null");
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
// Set version, status code and reason phrase
httpResponseMessage.Version = httpResponse.Version;
httpResponseMessage.StatusCode = httpResponse.StatusCode;
httpResponseMessage.ReasonPhrase = httpResponse.ReasonPhrase;
// Set the header fields and content if any
httpResponseMessage.Content = CreateHeaderFields(httpResponse.HttpHeaders, httpResponseMessage.Headers, contentStream, rewind);
return httpResponseMessage;
}
}
}