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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http.Headers;
namespace System.Net.Http
{
/// <summary>
/// Extension methods to provide convenience methods for finding <see cref="HttpContent"/> items
/// within a <see cref="IEnumerable{HttpContent}"/> collection.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpContentCollectionExtensions
{
private const string ContentID = @"Content-ID";
/// <summary>
/// Returns the first <see cref="HttpContent"/> in a sequence that has a <see cref="ContentDispositionHeaderValue"/> header field
/// with a <see cref="ContentDispositionHeaderValue.DispositionType"/> property equal to <paramref name="dispositionType"/>.
/// </summary>
/// <param name="contents">The contents to evaluate</param>
/// <param name="dispositionType">The disposition type to look for.</param>
/// <returns>The first <see cref="HttpContent"/> in the sequence with a matching disposition type.</returns>
public static HttpContent FirstDispositionType(this IEnumerable<HttpContent> contents, string dispositionType)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(dispositionType))
{
throw new ArgumentNullException("dispositionType");
}
return contents.First((item) =>
{
return HttpContentCollectionExtensions.FirstDispositionType(item, dispositionType);
});
}
/// <summary>
/// Returns the first <see cref="HttpContent"/> in a sequence that has a <see cref="ContentDispositionHeaderValue"/> header field
/// with a <see cref="ContentDispositionHeaderValue.DispositionType"/> property equal to <paramref name="dispositionType"/>.
/// </summary>
/// <param name="contents">The contents to evaluate</param>
/// <param name="dispositionType">The disposition type to look for.</param>
/// <returns>null if source is empty or if no element matches; otherwise the first <see cref="HttpContent"/> in
/// the sequence with a matching disposition type.</returns>
public static HttpContent FirstDispositionTypeOrDefault(this IEnumerable<HttpContent> contents, string dispositionType)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(dispositionType))
{
throw new ArgumentNullException("dispositionType");
}
return contents.FirstOrDefault((item) =>
{
return HttpContentCollectionExtensions.FirstDispositionType(item, dispositionType);
});
}
/// <summary>
/// Returns the first <see cref="HttpContent"/> in a sequence that has a <see cref="ContentDispositionHeaderValue"/> header field
/// with a <see cref="ContentDispositionHeaderValue.Name"/> property equal to <paramref name="dispositionName"/>.
/// </summary>
/// <param name="contents">The contents to evaluate</param>
/// <param name="dispositionName">The disposition name to look for.</param>
/// <returns>The first <see cref="HttpContent"/> in the sequence with a matching disposition name.</returns>
public static HttpContent FirstDispositionName(this IEnumerable<HttpContent> contents, string dispositionName)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(dispositionName))
{
throw new ArgumentNullException("dispositionName");
}
return contents.First((item) =>
{
return HttpContentCollectionExtensions.FirstDispositionName(item, dispositionName);
});
}
/// <summary>
/// Returns the first <see cref="HttpContent"/> in a sequence that has a <see cref="ContentDispositionHeaderValue"/> header field
/// with a <see cref="ContentDispositionHeaderValue.Name"/> property equal to <paramref name="dispositionName"/>.
/// </summary>
/// <param name="contents">The contents to evaluate</param>
/// <param name="dispositionName">The disposition name to look for.</param>
/// <returns>null if source is empty or if no element matches; otherwise the first <see cref="HttpContent"/> in
/// the sequence with a matching disposition name.</returns>
public static HttpContent FirstDispositionNameOrDefault(this IEnumerable<HttpContent> contents, string dispositionName)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(dispositionName))
{
throw new ArgumentNullException("dispositionName");
}
return contents.FirstOrDefault((item) =>
{
return HttpContentCollectionExtensions.FirstDispositionName(item, dispositionName);
});
}
/// <summary>
/// Returns the <c>start</c> multipart body part. The <c>start</c> is used to identify the main body
/// in <c>multipart/related</c> content (see RFC 2387).
/// </summary>
/// <param name="contents">The contents to evaluate.</param>
/// <param name="start">The <c>start</c> value to look for.
/// A match is found if a <see cref="HttpContent"/> has a <c>Content-ID</c>
/// header field with the given value.</param>
/// <returns>The first <see cref="HttpContent"/> in the sequence with a matching value.</returns>
public static HttpContent FirstStart(this IEnumerable<HttpContent> contents, string start)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(start))
{
throw new ArgumentNullException("start");
}
return contents.First((item) =>
{
return HttpContentCollectionExtensions.FirstStart(item, start);
});
}
/// <summary>
/// Returns the first <see cref="HttpContent"/> in a sequence that has a <see cref="ContentDispositionHeaderValue"/> header field
/// parameter equal to <paramref name="start"/>. This parameter is typically used in connection with <c>multipart/related</c>
/// content (see RFC 2387).
/// </summary>
/// <param name="contents">The contents to evaluate.</param>
/// <param name="start">The start value to look for. A match is found if a <see cref="HttpContent"/> has a <c>Content-ID</c>
/// header field with the given value.</param>
/// <returns>null if source is empty or if no element matches; otherwise the first <see cref="HttpContent"/> in
/// the sequence with a matching value.</returns>
public static HttpContent FirstStartOrDefault(this IEnumerable<HttpContent> contents, string start)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (String.IsNullOrWhiteSpace(start))
{
throw new ArgumentNullException("start");
}
return contents.FirstOrDefault((item) =>
{
return HttpContentCollectionExtensions.FirstStart(item, start);
});
}
/// <summary>
/// Returns all instances of <see cref="HttpContent"/> in a sequence that has a <see cref="MediaTypeHeaderValue"/> header field
/// with a <see cref="MediaTypeHeaderValue.MediaType"/> property equal to the provided <paramref name="contentType"/>.
/// </summary>
/// <param name="contents">The content to evaluate</param>
/// <param name="contentType">The media type to look for.</param>
/// <returns>null if source is empty or if no element matches; otherwise the first <see cref="HttpContent"/> in
/// the sequence with a matching media type.</returns>
public static IEnumerable<HttpContent> FindAllContentType(this IEnumerable<HttpContent> contents, string contentType)
{
if (String.IsNullOrWhiteSpace(contentType))
{
throw new ArgumentNullException("contentType");
}
return HttpContentCollectionExtensions.FindAllContentType(contents, new MediaTypeHeaderValue(contentType));
}
/// <summary>
/// Returns all instances of <see cref="HttpContent"/> in a sequence that has a <see cref="MediaTypeHeaderValue"/> header field
/// with a <see cref="MediaTypeHeaderValue.MediaType"/> property equal to the provided <paramref name="contentType"/>.
/// </summary>
/// <param name="contents">The content to evaluate</param>
/// <param name="contentType">The media type to look for.</param>
/// <returns>null if source is empty or if no element matches; otherwise the first <see cref="HttpContent"/> in
/// the sequence with a matching media type.</returns>
public static IEnumerable<HttpContent> FindAllContentType(this IEnumerable<HttpContent> contents, MediaTypeHeaderValue contentType)
{
if (contents == null)
{
throw new ArgumentNullException("contents");
}
if (contentType == null)
{
throw new ArgumentNullException("contentType");
}
return contents.Where((item) =>
{
return HttpContentCollectionExtensions.FindAllContentType(item, contentType);
});
}
private static bool FirstStart(HttpContent content, string start)
{
Contract.Assert(content != null, "content cannot be null");
Contract.Assert(start != null, "start cannot be null");
if (content.Headers != null)
{
IEnumerable<string> values;
if (content.Headers.TryGetValues(ContentID, out values))
{
return String.Equals(
FormattingUtilities.UnquoteToken(values.ElementAt(0)),
FormattingUtilities.UnquoteToken(start),
StringComparison.OrdinalIgnoreCase);
}
}
return false;
}
private static bool FirstDispositionType(HttpContent content, string dispositionType)
{
Contract.Assert(content != null, "content cannot be null");
Contract.Assert(dispositionType != null, "dispositionType cannot be null");
return content.Headers != null && content.Headers.ContentDisposition != null &&
String.Equals(
FormattingUtilities.UnquoteToken(content.Headers.ContentDisposition.DispositionType),
FormattingUtilities.UnquoteToken(dispositionType),
StringComparison.OrdinalIgnoreCase);
}
private static bool FirstDispositionName(HttpContent content, string dispositionName)
{
Contract.Assert(content != null, "content cannot be null");
Contract.Assert(dispositionName != null, "dispositionName cannot be null");
return content.Headers != null && content.Headers.ContentDisposition != null &&
String.Equals(
FormattingUtilities.UnquoteToken(content.Headers.ContentDisposition.Name),
FormattingUtilities.UnquoteToken(dispositionName),
StringComparison.OrdinalIgnoreCase);
}
private static bool FindAllContentType(HttpContent content, MediaTypeHeaderValue contentType)
{
Contract.Assert(content != null, "content cannot be null");
Contract.Assert(contentType != null, "contentType cannot be null");
return content.Headers != null && content.Headers.ContentType != null &&
String.Equals(
content.Headers.ContentType.MediaType,
contentType.MediaType,
StringComparison.OrdinalIgnoreCase);
}
}
}
|