forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpValueCollection.cs
More file actions
282 lines (248 loc) · 8.78 KB
/
Copy pathHttpValueCollection.cs
File metadata and controls
282 lines (248 loc) · 8.78 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
280
281
282
// 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.
#if NETFX_CORE
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Web.Http;
#else
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http.Internal;
using System.Runtime.Serialization;
using System.Text;
using System.Web.Http;
#endif
#if NETFX_CORE
namespace System.Net.Http.Formatting
#else
namespace System.Net.Http.Formatting.Internal
#endif
{
/// <summary>
/// NameValueCollection to represent form data and to generate form data output.
/// </summary>
#if NETFX_CORE
public class HttpValueCollection : IEnumerable<KeyValuePair<string, string>>
#else
[Serializable]
internal class HttpValueCollection : NameValueCollection
#endif
{
#if NETFX_CORE
internal readonly HashSet<string> Names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
internal readonly List<KeyValuePair<string, string>> List = new List<KeyValuePair<string, string>>();
/// <summary>
/// Creates a new <see cref="System.Net.Http.Formatting.HttpValueCollection"/> instance
/// </summary>
public HttpValueCollection()
{
}
#else
protected HttpValueCollection(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
private HttpValueCollection()
: base(StringComparer.OrdinalIgnoreCase) // case-insensitive keys
{
}
#endif
// Use a builder function instead of a ctor to avoid virtual calls from the ctor.
// The above condition is only important in the Full .NET fx implementation.
internal static HttpValueCollection Create()
{
return new HttpValueCollection();
}
internal static HttpValueCollection Create(IEnumerable<KeyValuePair<string, string>> pairs)
{
Contract.Assert(pairs != null);
var hvc = new HttpValueCollection();
// Ordering example:
// k=A&j=B&k=C --> k:[A,C];j=[B].
foreach (KeyValuePair<string, string> kvp in pairs)
{
hvc.Add(kvp.Key, kvp.Value);
}
#if !NETFX_CORE
hvc.IsReadOnly = false;
#endif
return hvc;
}
/// <summary>
/// Adds a name-value pair to the collection.
/// </summary>
/// <param name="name">The name to be added as a case insensitive string.</param>
/// <param name="value">The value to be added.</param>
public
#if !NETFX_CORE
override
#endif
void Add(string name, string value)
{
ThrowIfMaxHttpCollectionKeysExceeded(Count);
name = name ?? String.Empty;
value = value ?? String.Empty;
#if NETFX_CORE
Names.Add(name);
List.Add(new KeyValuePair<string, string>(name, value));
#else
base.Add(name, value);
#endif
}
/// <summary>
/// Converts the content of this instance to its equivalent string representation.
/// </summary>
/// <returns>The string representation of the value of this instance, multiple values with a single key are comma separated.</returns>
public override string ToString()
{
return ToString(true);
}
private static void ThrowIfMaxHttpCollectionKeysExceeded(int count)
{
if (count >= MediaTypeFormatter.MaxHttpCollectionKeys)
{
throw Error.InvalidOperation(System.Net.Http.Properties.Resources.MaxHttpCollectionKeyLimitReached, MediaTypeFormatter.MaxHttpCollectionKeys, typeof(MediaTypeFormatter));
}
}
private string ToString(bool urlEncode)
{
if (Count == 0)
{
return String.Empty;
}
StringBuilder builder = new StringBuilder();
bool first = true;
#if NETFX_CORE
foreach (string name in Names)
#else
foreach (string name in this)
#endif
{
string[] values = GetValues(name);
if (values == null || values.Length == 0)
{
first = AppendNameValuePair(builder, first, urlEncode, name, String.Empty);
}
else
{
foreach (string value in values)
{
first = AppendNameValuePair(builder, first, urlEncode, name, value);
}
}
}
return builder.ToString();
}
private static bool AppendNameValuePair(StringBuilder builder, bool first, bool urlEncode, string name, string value)
{
string effectiveName = name ?? String.Empty;
string encodedName = urlEncode ? UriQueryUtility.UrlEncode(effectiveName) : effectiveName;
string effectiveValue = value ?? String.Empty;
string encodedValue = urlEncode ? UriQueryUtility.UrlEncode(effectiveValue) : effectiveValue;
if (first)
{
first = false;
}
else
{
builder.Append("&");
}
builder.Append(encodedName);
if (!String.IsNullOrEmpty(encodedValue))
{
builder.Append("=");
builder.Append(encodedValue);
}
return first;
}
#if NETFX_CORE
/// <summary>
/// Gets the values associated with the specified name
/// combined into one comma-separated list.
/// </summary>
/// <param name="name">The name of the entry that contains the values to get. The name can be null.</param>
/// <returns>A <see cref="System.String"/> that contains a comma-separated list of url encoded values associated
/// with the specified name if found; otherwise, null. The values are Url encoded.</returns>
public string this[string name]
{
get
{
return Get(name);
}
}
/// <summary>
/// Gets the number of names in the collection.
/// </summary>
public int Count
{
get
{
return Names.Count;
}
}
/// <summary>
/// Gets the values associated with the specified name
/// combined into one comma-separated list.
/// </summary>
/// <param name="name">The name of the entry that contains the values to get. The name can be null.</param>
/// <returns>
/// A <see cref="System.String"/> that contains a comma-separated list of url encoded values associated
/// with the specified name if found; otherwise, null. The values are Url encoded.
/// </returns>
public string Get(string name)
{
name = name ?? String.Empty;
if (!Names.Contains(name))
{
return null;
}
List<string> values = GetValuesInternal(name);
Contract.Assert(values != null && values.Count > 0);
return String.Join(",", values);
}
/// <summary>
/// Gets the values associated with the specified name.
/// </summary>
/// <param name="name">The <see cref="System.String"/></param>
/// <returns>A <see cref="System.String"/> that contains url encoded values associated with the name, or null if the name does not exist.</returns>
public string[] GetValues(string name)
{
name = name ?? String.Empty;
if (!Names.Contains(name))
{
return null;
}
return GetValuesInternal(name).ToArray();
}
// call this when only when there are values available.
private List<string> GetValuesInternal(string name)
{
List<string> values = new List<string>();
for (int i = 0; i < List.Count; i++)
{
KeyValuePair<string, string> kvp = List[i];
if (String.Equals(kvp.Key, name, StringComparison.OrdinalIgnoreCase))
{
values.Add(kvp.Value);
}
}
return values;
}
/// <inheritdoc />
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return List.GetEnumerator();
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}
#endif
}
}