forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPcl.NameValueCollectionWrapper.cs
More file actions
216 lines (193 loc) · 7.06 KB
/
Pcl.NameValueCollectionWrapper.cs
File metadata and controls
216 lines (193 loc) · 7.06 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
//
// System.Web.HttpUtility
//
// Authors:
// Patrik Torstensson (Patrik.Torstensson@labs2.com)
// Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)
// Tim Coleman (tim@timcoleman.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using ServiceStack.Text;
using ServiceStack.Web;
using System.Text;
using System.Collections.Specialized;
#if NETSTANDARD2_0
//namespace System.Collections.Specialized
namespace ServiceStack.Pcl
{
using System;
using System.Net;
using System.Text;
using ServiceStack.Text;
public class HttpUtility
{
private sealed class HttpQSCollection : NameValueCollection
{
public override string ToString()
{
int count = Count;
if (count == 0)
return "";
var sb = StringBuilderCache.Allocate();
string[] keys = AllKeys;
for (int i = 0; i < count; i++)
{
sb.AppendFormat("{0}={1}&", keys[i], this[keys[i]]);
}
if (sb.Length > 0)
sb.Length--;
return StringBuilderCache.ReturnAndFree(sb);
}
}
public static NameValueCollection ParseQueryString(string query) => ParseQueryString(query, Encoding.UTF8);
public static NameValueCollection ParseQueryString(string query, Encoding encoding)
{
if (query == null)
throw new ArgumentNullException("query");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
return new HttpQSCollection();
if (query[0] == '?')
query = query.Substring(1);
NameValueCollection result = new HttpQSCollection();
ParseQueryString(query, encoding, result);
return result;
}
internal static void ParseQueryString(string query, Encoding encoding, NameValueCollection result)
{
if (query.Length == 0)
return;
string decoded = PclExportClient.Instance.HtmlDecode(query);
int decodedLength = decoded.Length;
int namePos = 0;
bool first = true;
while (namePos <= decodedLength)
{
int valuePos = -1, valueEnd = -1;
for (int q = namePos; q < decodedLength; q++)
{
if (valuePos == -1 && decoded[q] == '=')
{
valuePos = q + 1;
}
else if (decoded[q] == '&')
{
valueEnd = q;
break;
}
}
if (first)
{
first = false;
if (decoded[namePos] == '?')
namePos++;
}
string name, value;
if (valuePos == -1)
{
name = null;
valuePos = namePos;
}
else
{
name = PclExportClient.Instance.UrlDecode(decoded.Substring(namePos, valuePos - namePos - 1));
}
if (valueEnd < 0)
{
namePos = -1;
valueEnd = decoded.Length;
}
else
{
namePos = valueEnd + 1;
}
value = PclExportClient.Instance.UrlDecode(decoded.Substring(valuePos, valueEnd - valuePos));
result.Add(name, value);
if (namePos == -1)
break;
}
}
}
}
#endif
namespace ServiceStack
{
public static class NameValueCollectionWrapperExtensions
{
public static Dictionary<string, string> ToDictionary(this NameValueCollection nameValues)
{
if (nameValues == null) return new Dictionary<string, string>();
var map = new Dictionary<string, string>();
foreach (var key in nameValues.AllKeys)
{
if (key == null)
{
//occurs when no value is specified, e.g. 'path/to/page?debug'
//throw new ArgumentNullException("key", "nameValues: " + nameValues);
continue;
}
var values = nameValues.GetValues(key);
if (values != null && values.Length > 0)
{
map[key] = string.Join(",", values);
}
}
return map;
}
public static NameValueCollection ToNameValueCollection(this Dictionary<string, string> map)
{
if (map == null) return new NameValueCollection();
var nameValues = new NameValueCollection();
foreach (var item in map)
{
nameValues.Add(item.Key, item.Value);
}
return nameValues;
}
public static string ToFormUrlEncoded(this NameValueCollection queryParams)
{
var sb = StringBuilderCache.Allocate();
foreach (string key in queryParams.AllKeys)
{
var values = queryParams.GetValues(key);
AppendKeyValue(sb, key, values);
}
return StringBuilderCache.ReturnAndFree(sb);
}
private static void AppendKeyValue(StringBuilder sb, string key, string[] values)
{
if (values == null) return;
foreach (var value in values)
{
if (sb.Length > 0)
sb.Append('&');
sb.Append($"{key.UrlEncode()}={value.UrlEncode()}");
}
}
}
}