forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpContentFormDataExtensions.cs
More file actions
82 lines (74 loc) · 3.81 KB
/
Copy pathHttpContentFormDataExtensions.cs
File metadata and controls
82 lines (74 loc) · 3.81 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
// 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.Specialized;
using System.ComponentModel;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
#if NETFX_CORE
using NameValueCollection = System.Net.Http.Formatting.HttpValueCollection;
#endif
namespace System.Net.Http
{
/// <summary>
/// Extension methods to allow HTML form URL-encoded data, also known as <c>application/x-www-form-urlencoded</c>,
/// to be read from <see cref="HttpContent"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpContentFormDataExtensions
{
private const string ApplicationFormUrlEncoded = "application/x-www-form-urlencoded";
/// <summary>
/// Determines whether the specified content is HTML form URL-encoded data, also known as <c>application/x-www-form-urlencoded</c> data.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the specified content is HTML form URL-encoded data; otherwise, <c>false</c>.
/// </returns>
public static bool IsFormData(this HttpContent content)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
MediaTypeHeaderValue contentType = content.Headers.ContentType;
return contentType != null && String.Equals(ApplicationFormUrlEncoded, contentType.MediaType, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns a <see cref="Task{T}"/> that will yield a <see cref="NameValueCollection"/> instance containing the form data
/// parsed as HTML form URL-encoded from the <paramref name="content"/> instance.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>A <see cref="Task{T}"/> which will provide the result. If the data can not be read
/// as HTML form URL-encoded data then the result is null.</returns>
public static Task<NameValueCollection> ReadAsFormDataAsync(this HttpContent content)
{
return ReadAsFormDataAsync(content, CancellationToken.None);
}
/// <summary>
/// Returns a <see cref="Task{T}"/> that will yield a <see cref="NameValueCollection"/> instance containing the form data
/// parsed as HTML form URL-encoded from the <paramref name="content"/> instance.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task{T}"/> which will provide the result. If the data can not be read
/// as HTML form URL-encoded data then the result is null.</returns>
public static Task<NameValueCollection> ReadAsFormDataAsync(this HttpContent content, CancellationToken cancellationToken)
{
if (content == null)
{
throw Error.ArgumentNull("content");
}
MediaTypeFormatter[] formatters = new MediaTypeFormatter[1] { new FormUrlEncodedMediaTypeFormatter() };
return ReadAsAsyncCore(content, formatters, cancellationToken);
}
private static async Task<NameValueCollection> ReadAsAsyncCore(HttpContent content, MediaTypeFormatter[] formatters,
CancellationToken cancellationToken)
{
FormDataCollection formData = await content.ReadAsAsync<FormDataCollection>(formatters, cancellationToken);
return formData == null ? null : formData.ReadAsNameValueCollection();
}
}
}