forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipartFormDataStreamProviderHelper.cs
More file actions
71 lines (63 loc) · 2.88 KB
/
MultipartFormDataStreamProviderHelper.cs
File metadata and controls
71 lines (63 loc) · 2.88 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
// 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.ObjectModel;
using System.Collections.Specialized;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Net.Http
{
internal static class MultipartFormDataStreamProviderHelper
{
public static bool IsFileContent(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null)
{
throw Error.ArgumentNull("parent");
}
if (headers == null)
{
throw Error.ArgumentNull("headers");
}
// For form data, Content-Disposition header is a requirement.
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition == null)
{
// If no Content-Disposition header was present.
throw Error.InvalidOperation(Properties.Resources.MultipartFormDataStreamProviderNoContentDisposition,
"Content-Disposition");
}
// The file name's existence indicates it is a file data.
if (!String.IsNullOrEmpty(contentDisposition.FileName))
{
return true;
}
return false;
}
/// <summary>
/// Read the non-file contents as form data.
/// </summary>
/// <returns>A <see cref="Task"/> representing the post processing.</returns>
public static async Task ReadFormDataAsync(Collection<HttpContent> contents,
NameValueCollection formData, CancellationToken cancellationToken)
{
// Find instances of HttpContent for which we created a memory stream and read them asynchronously
// to get the string content and then add that as form data
foreach (HttpContent content in contents)
{
ContentDispositionHeaderValue contentDisposition = content.Headers.ContentDisposition;
// If FileName is null or empty, the content is form data and will be processed.
if (String.IsNullOrEmpty(contentDisposition.FileName))
{
// Extract name from Content-Disposition header. We know from earlier that the header is present.
string formFieldName = FormattingUtilities.UnquoteToken(contentDisposition.Name) ?? String.Empty;
// Read the contents as string data and add to form data
cancellationToken.ThrowIfCancellationRequested();
string formFieldValue = await content.ReadAsStringAsync();
formData.Add(formFieldName, formFieldValue);
}
}
}
}
}