forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContentExtensions.cs
More file actions
279 lines (253 loc) · 15.7 KB
/
HttpContentExtensions.cs
File metadata and controls
279 lines (253 loc) · 15.7 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
// 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.IO;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Net.Http
{
/// <summary>
/// Extension methods to allow strongly typed objects to be read from <see cref="HttpContent"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpContentExtensions
{
private static MediaTypeFormatterCollection _defaultMediaTypeFormatterCollection = null;
// Using the JsonMediaTypeFormatter for the first time is rather expensive (due to reflection cost
// when creating the default contract resolver). Hence we new up a static collection, such
// that the second call is much faster.
private static MediaTypeFormatterCollection DefaultMediaTypeFormatterCollection
{
get
{
if (_defaultMediaTypeFormatterCollection == null)
{
_defaultMediaTypeFormatterCollection = new MediaTypeFormatterCollection();
}
return _defaultMediaTypeFormatterCollection;
}
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance.
/// </summary>
/// <remarks>This override use the built-in collection of formatters.</remarks>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type)
{
return content.ReadAsAsync(type, DefaultMediaTypeFormatterCollection);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance.
/// </summary>
/// <remarks>This override use the built-in collection of formatters.</remarks>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type, CancellationToken cancellationToken)
{
return content.ReadAsAsync(type, DefaultMediaTypeFormatterCollection, cancellationToken);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance using one of the provided <paramref name="formatters"/>
/// to deserialize the content.
/// </summary>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters)
{
return ReadAsAsync<object>(content, type, formatters, null);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance using one of the provided <paramref name="formatters"/>
/// to deserialize the content.
/// </summary>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters,
CancellationToken cancellationToken)
{
return ReadAsAsync<object>(content, type, formatters, null, cancellationToken);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance using one of the provided <paramref name="formatters"/>
/// to deserialize the content.
/// </summary>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger)
{
return ReadAsAsync<object>(content, type, formatters, formatterLogger);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified <paramref name="type"/>
/// from the <paramref name="content"/> instance using one of the provided <paramref name="formatters"/>
/// to deserialize the content.
/// </summary>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="type">The type of the object to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<object> ReadAsAsync(this HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger, CancellationToken cancellationToken)
{
return ReadAsAsync<object>(content, type, formatters, formatterLogger, cancellationToken);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <remarks>This override use the built-in collection of formatters.</remarks>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content)
{
return content.ReadAsAsync<T>(DefaultMediaTypeFormatterCollection);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <remarks>This override use the built-in collection of formatters.</remarks>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content, CancellationToken cancellationToken)
{
return content.ReadAsAsync<T>(DefaultMediaTypeFormatterCollection, cancellationToken);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content, IEnumerable<MediaTypeFormatter> formatters)
{
return ReadAsAsync<T>(content, typeof(T), formatters, null);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content, IEnumerable<MediaTypeFormatter> formatters,
CancellationToken cancellationToken)
{
return ReadAsAsync<T>(content, typeof(T), formatters, null, cancellationToken);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger)
{
return ReadAsAsync<T>(content, typeof(T), formatters, formatterLogger);
}
/// <summary>
/// Returns a <see cref="Task"/> that will yield an object of the specified
/// type <typeparamref name="T"/> from the <paramref name="content"/> instance.
/// </summary>
/// <typeparam name="T">The type of the object to read.</typeparam>
/// <param name="content">The <see cref="HttpContent"/> instance from which to read.</param>
/// <param name="formatters">The collection of <see cref="MediaTypeFormatter"/> instances to use.</param>
/// <param name="formatterLogger">The <see cref="IFormatterLogger"/> to log events to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task object representing reading the content as an object of the specified type.</returns>
public static Task<T> ReadAsAsync<T>(this HttpContent content, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger, CancellationToken cancellationToken)
{
return ReadAsAsync<T>(content, typeof(T), formatters, formatterLogger, cancellationToken);
}
private static Task<T> ReadAsAsync<T>(HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger)
{
return ReadAsAsync<T>(content, type, formatters, formatterLogger, CancellationToken.None);
}
// There are many helper overloads for ReadAs*(). Provide one worker function to ensure the logic is shared.
//
// For loosely typed, T = Object, type = specific class.
// For strongly typed, T == type.GetType()
private static Task<T> ReadAsAsync<T>(HttpContent content, Type type, IEnumerable<MediaTypeFormatter> formatters,
IFormatterLogger formatterLogger, CancellationToken cancellationToken)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
if (type == null)
{
throw Error.ArgumentNull("type");
}
if (formatters == null)
{
throw Error.ArgumentNull("formatters");
}
ObjectContent objectContent = content as ObjectContent;
if (objectContent != null && objectContent.Value != null && type.IsAssignableFrom(objectContent.Value.GetType()))
{
return Task.FromResult((T)objectContent.Value);
}
MediaTypeFormatter formatter = null;
// Default to "application/octet-stream" if there is no content-type in accordance with section 7.2.1 of the HTTP spec
MediaTypeHeaderValue mediaType = content.Headers.ContentType ?? MediaTypeConstants.ApplicationOctetStreamMediaType;
formatter = new MediaTypeFormatterCollection(formatters).FindReader(type, mediaType);
if (formatter == null)
{
if (content.Headers.ContentLength == 0)
{
T defaultValue = (T)MediaTypeFormatter.GetDefaultValueForType(type);
return Task.FromResult<T>(defaultValue);
}
throw new UnsupportedMediaTypeException(
Error.Format(Properties.Resources.NoReadSerializerAvailable, type.Name, mediaType.MediaType),
mediaType);
}
return ReadAsAsyncCore<T>(content, type, formatterLogger, formatter, cancellationToken);
}
private static async Task<T> ReadAsAsyncCore<T>(HttpContent content, Type type, IFormatterLogger formatterLogger,
MediaTypeFormatter formatter, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Stream stream = await content.ReadAsStreamAsync();
object result = await formatter.ReadFromStreamAsync(type, stream, content, formatterLogger, cancellationToken);
return (T)result;
}
}
}