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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net.Http.Formatting.Parsers;
using System.Threading.Tasks;
namespace System.Net.Http
{
/// <summary>
/// Extension methods to read MIME multipart entities from <see cref="HttpContent"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpContentMultipartExtensions
{
private const int MinBufferSize = 256;
private const int DefaultBufferSize = 32 * 1024;
private static readonly AsyncCallback _onMultipartReadAsyncComplete = new AsyncCallback(OnMultipartReadAsyncComplete);
private static readonly AsyncCallback _onMultipartWriteSegmentAsyncComplete = new AsyncCallback(OnMultipartWriteSegmentAsyncComplete);
/// <summary>
/// Determines whether the specified content is MIME multipart content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the specified content is MIME multipart content; otherwise, <c>false</c>.
/// </returns>
public static bool IsMimeMultipartContent(this HttpContent content)
{
if (content == null)
{
throw new ArgumentNullException("content");
}
return MimeMultipartBodyPartParser.IsMimeMultipartContent(content);
}
/// <summary>
/// Determines whether the specified content is MIME multipart content with the
/// specified subtype. For example, the subtype <c>mixed</c> would match content
/// with a content type of <c>multipart/mixed</c>.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="subtype">The MIME multipart subtype to match.</param>
/// <returns>
/// <c>true</c> if the specified content is MIME multipart content with the specified subtype; otherwise, <c>false</c>.
/// </returns>
public static bool IsMimeMultipartContent(this HttpContent content, string subtype)
{
if (String.IsNullOrWhiteSpace(subtype))
{
throw new ArgumentNullException("subtype");
}
if (IsMimeMultipartContent(content))
{
if (content.Headers.ContentType.MediaType.Equals("multipart/" + subtype, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
/// <summary>
/// Reads all body parts within a MIME multipart message and produces a set of <see cref="HttpContent"/> instances as a result.
/// </summary>
/// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param>
/// <returns>A <see cref="Task{T}"/> representing the tasks of getting the collection of <see cref="HttpContent"/> instances where each instance represents a body part.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Nesting of generic types is required with Task<T>")]
public static Task<IEnumerable<HttpContent>> ReadAsMultipartAsync(this HttpContent content)
{
return ReadAsMultipartAsync(content, MultipartMemoryStreamProvider.Instance, DefaultBufferSize);
}
/// <summary>
/// Reads all body parts within a MIME multipart message and produces a set of <see cref="HttpContent"/> instances as a result
/// using the <paramref name="streamProvider"/> instance to determine where the contents of each body part is written.
/// </summary>
/// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param>
/// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param>
/// <returns>A <see cref="Task{T}"/> representing the tasks of getting the collection of <see cref="HttpContent"/> instances where each instance represents a body part.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Nesting of generic types is required with Task<T>")]
public static Task<IEnumerable<HttpContent>> ReadAsMultipartAsync(this HttpContent content, IMultipartStreamProvider streamProvider)
{
return ReadAsMultipartAsync(content, streamProvider, DefaultBufferSize);
}
/// <summary>
/// Reads all body parts within a MIME multipart message and produces a set of <see cref="HttpContent"/> instances as a result
/// using the <paramref name="streamProvider"/> instance to determine where the contents of each body part is written and
/// <paramref name="bufferSize"/> as read buffer size.
/// </summary>
/// <param name="content">An existing <see cref="HttpContent"/> instance to use for the object's content.</param>
/// <param name="streamProvider">A stream provider providing output streams for where to write body parts as they are parsed.</param>
/// <param name="bufferSize">Size of the buffer used to read the contents.</param>
/// <returns>A <see cref="Task{T}"/> representing the tasks of getting the collection of <see cref="HttpContent"/> instances where each instance represents a body part.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "caller becomes owner.")]
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Nesting of generic types is required with Task<T>")]
public static Task<IEnumerable<HttpContent>> ReadAsMultipartAsync(this HttpContent content, IMultipartStreamProvider streamProvider, int bufferSize)
{
if (content == null)
{
throw new ArgumentNullException("content");
}
if (streamProvider == null)
{
throw new ArgumentNullException("streamProvider");
}
if (bufferSize < MinBufferSize)
{
throw new ArgumentOutOfRangeException("bufferSize", bufferSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinBufferSize));
}
return content.ReadAsStreamAsync().Then(stream =>
{
TaskCompletionSource<IEnumerable<HttpContent>> taskCompletionSource = new TaskCompletionSource<IEnumerable<HttpContent>>();
MimeMultipartBodyPartParser parser = new MimeMultipartBodyPartParser(content, streamProvider);
byte[] data = new byte[bufferSize];
MultipartAsyncContext context = new MultipartAsyncContext(stream, taskCompletionSource, parser, data);
// Start async read/write loop
MultipartReadAsync(context);
// Return task and complete later
return taskCompletionSource.Task;
});
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is propagated.")]
private static void MultipartReadAsync(MultipartAsyncContext context)
{
Contract.Assert(context != null, "context cannot be null");
IAsyncResult result = null;
try
{
result = context.ContentStream.BeginRead(context.Data, 0, context.Data.Length, _onMultipartReadAsyncComplete, context);
if (result.CompletedSynchronously)
{
MultipartReadAsyncComplete(result);
}
}
catch (Exception e)
{
Exception exception = (result != null && result.CompletedSynchronously) ? e : new IOException(Properties.Resources.ReadAsMimeMultipartErrorReading, e);
context.TaskCompletionSource.TrySetException(exception);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is propagated.")]
private static void OnMultipartReadAsyncComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
MultipartAsyncContext context = (MultipartAsyncContext)result.AsyncState;
Contract.Assert(context != null, "context cannot be null");
try
{
MultipartReadAsyncComplete(result);
}
catch (Exception e)
{
context.TaskCompletionSource.TrySetException(e);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is propagated.")]
private static void MultipartReadAsyncComplete(IAsyncResult result)
{
Contract.Assert(result != null, "result cannot be null");
MultipartAsyncContext context = (MultipartAsyncContext)result.AsyncState;
int bytesRead = 0;
try
{
bytesRead = context.ContentStream.EndRead(result);
}
catch (Exception e)
{
context.TaskCompletionSource.TrySetException(new IOException(Properties.Resources.ReadAsMimeMultipartErrorReading, e));
}
IEnumerable<MimeBodyPart> parts = context.MimeParser.ParseBuffer(context.Data, bytesRead);
context.PartsEnumerator = parts.GetEnumerator();
MoveNextPart(context);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is propagated.")]
private static void MultipartWriteSegmentAsync(MultipartAsyncContext context)
{
Contract.Assert(context != null, "context cannot be null.");
Stream output = context.PartsEnumerator.Current.GetOutputStream();
ArraySegment<byte> segment = (ArraySegment<byte>)context.SegmentsEnumerator.Current;
try
{
IAsyncResult result = output.BeginWrite(segment.Array, segment.Offset, segment.Count, _onMultipartWriteSegmentAsyncComplete, context);
if (result.CompletedSynchronously)
{
MultipartWriteSegmentAsyncComplete(result);
}
}
catch (Exception e)
{
context.PartsEnumerator.Current.Dispose();
context.TaskCompletionSource.TrySetException(new IOException(Properties.Resources.ReadAsMimeMultipartErrorWriting, e));
}
}
private static void OnMultipartWriteSegmentAsyncComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
MultipartWriteSegmentAsyncComplete(result);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is propagated.")]
private static void MultipartWriteSegmentAsyncComplete(IAsyncResult result)
{
Contract.Assert(result != null, "result cannot be null.");
MultipartAsyncContext context = (MultipartAsyncContext)result.AsyncState;
Contract.Assert(context != null, "context cannot be null");
MimeBodyPart part = context.PartsEnumerator.Current;
try
{
Stream output = context.PartsEnumerator.Current.GetOutputStream();
output.EndWrite(result);
}
catch (Exception e)
{
part.Dispose();
context.TaskCompletionSource.TrySetException(new IOException(Properties.Resources.ReadAsMimeMultipartErrorWriting, e));
}
if (!MoveNextSegment(context))
{
MoveNextPart(context);
}
}
private static void MoveNextPart(MultipartAsyncContext context)
{
Contract.Assert(context != null, "context cannot be null");
while (context.PartsEnumerator.MoveNext())
{
context.SegmentsEnumerator = context.PartsEnumerator.Current.Segments.GetEnumerator();
if (MoveNextSegment(context))
{
return;
}
}
// Read some more
MultipartReadAsync(context);
}
private static bool MoveNextSegment(MultipartAsyncContext context)
{
Contract.Assert(context != null, "context cannot be null");
if (context.SegmentsEnumerator.MoveNext())
{
MultipartWriteSegmentAsync(context);
return true;
}
else if (CheckPartCompletion(context.PartsEnumerator.Current, context.Result))
{
// We are done parsing
context.TaskCompletionSource.TrySetResult(context.Result);
return true;
}
return false;
}
private static bool CheckPartCompletion(MimeBodyPart part, List<HttpContent> result)
{
Contract.Assert(part != null, "part cannot be null.");
Contract.Assert(result != null, "result cannot be null.");
if (part.IsComplete)
{
if (part.HttpContent != null)
{
result.Add(part.HttpContent);
}
bool isFinal = part.IsFinal;
part.Dispose();
return isFinal;
}
return false;
}
/// <summary>
/// Managing state for asynchronous read and write operations
/// </summary>
private class MultipartAsyncContext
{
/// <summary>
/// Initializes a new instance of the <see cref="MultipartAsyncContext"/> class.
/// </summary>
/// <param name="contentStream">The content stream.</param>
/// <param name="taskCompletionSource">The task completion source.</param>
/// <param name="mimeParser">The MIME parser.</param>
/// <param name="data">The buffer that we read data from.</param>
public MultipartAsyncContext(Stream contentStream, TaskCompletionSource<IEnumerable<HttpContent>> taskCompletionSource, MimeMultipartBodyPartParser mimeParser, byte[] data)
{
Contract.Assert(contentStream != null, "contentStream cannot be null");
Contract.Assert(taskCompletionSource != null, "task cannot be null");
Contract.Assert(mimeParser != null, "mimeParser cannot be null");
Contract.Assert(data != null, "data cannot be null");
ContentStream = contentStream;
Result = new List<HttpContent>();
TaskCompletionSource = taskCompletionSource;
MimeParser = mimeParser;
Data = data;
}
/// <summary>
/// Gets the <see cref="Stream"/> that we read from.
/// </summary>
/// <value>
/// The content stream.
/// </value>
public Stream ContentStream { get; private set; }
/// <summary>
/// Gets the collection of parsed <see cref="HttpContent"/> instances.
/// </summary>
/// <value>
/// The result collection.
/// </value>
public List<HttpContent> Result { get; private set; }
/// <summary>
/// Gets the task completion source.
/// </summary>
/// <value>
/// The task completion source.
/// </value>
public TaskCompletionSource<IEnumerable<HttpContent>> TaskCompletionSource { get; private set; }
/// <summary>
/// Gets the data.
/// </summary>
/// <value>
/// The buffer that we read data from.
/// </value>
public byte[] Data { get; private set; }
/// <summary>
/// Gets the MIME parser.
/// </summary>
/// <value>
/// The MIME parser.
/// </value>
public MimeMultipartBodyPartParser MimeParser { get; private set; }
/// <summary>
/// Gets or sets the parts enumerator for going through the parsed parts.
/// </summary>
/// <value>
/// The parts enumerator.
/// </value>
public IEnumerator<MimeBodyPart> PartsEnumerator { get; set; }
/// <summary>
/// Gets or sets the segments enumerator for going through the segments within each part.
/// </summary>
/// <value>
/// The segments enumerator.
/// </value>
public IEnumerator SegmentsEnumerator { get; set; }
}
}
}
|