-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathHttpUtility.cs
More file actions
233 lines (190 loc) · 9.67 KB
/
Copy pathHttpUtility.cs
File metadata and controls
233 lines (190 loc) · 9.67 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// 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.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Util;
namespace System.Web
{
public sealed class HttpUtility
{
private sealed class HttpQSCollection : NameValueCollection
{
internal HttpQSCollection()
: base(StringComparer.OrdinalIgnoreCase)
{
}
public override string ToString()
{
int count = Count;
if (count == 0)
{
return "";
}
StringBuilder sb = new StringBuilder();
string?[] keys = AllKeys;
for (int i = 0; i < count; i++)
{
string? key = keys[i];
string[]? values = GetValues(key);
if (values != null)
{
foreach (string value in values)
{
if (!string.IsNullOrEmpty(key))
{
sb.Append(key).Append('=');
}
sb.Append(UrlEncode(value)).Append('&');
}
}
}
return sb.Length > 0 ? sb.ToString(0, sb.Length - 1) : "";
}
}
public static NameValueCollection ParseQueryString(string query) => ParseQueryString(query, Encoding.UTF8);
public static NameValueCollection ParseQueryString(string query, Encoding encoding)
{
ArgumentNullException.ThrowIfNull(query);
ArgumentNullException.ThrowIfNull(encoding);
HttpQSCollection result = new HttpQSCollection();
int queryLength = query.Length;
int namePos = query.StartsWith('?') ? 1 : 0;
if (queryLength == namePos)
{
return result;
}
while (namePos <= queryLength)
{
int valuePos = -1, valueEnd = -1;
for (int q = namePos; q < queryLength; q++)
{
if (valuePos == -1 && query[q] == '=')
{
valuePos = q + 1;
}
else if (query[q] == '&')
{
valueEnd = q;
break;
}
}
string? name;
if (valuePos == -1)
{
name = null;
valuePos = namePos;
}
else
{
name = UrlDecode(query.Substring(namePos, valuePos - namePos - 1), encoding);
}
if (valueEnd < 0)
{
valueEnd = query.Length;
}
namePos = valueEnd + 1;
string value = UrlDecode(query.Substring(valuePos, valueEnd - valuePos), encoding);
result.Add(name, value);
}
return result;
}
[return: NotNullIfNotNull(nameof(s))]
public static string? HtmlDecode(string? s) => HttpEncoder.HtmlDecode(s);
public static void HtmlDecode(string? s, TextWriter output) => HttpEncoder.HtmlDecode(s, output);
[return: NotNullIfNotNull(nameof(s))]
public static string? HtmlEncode(string? s) => HttpEncoder.HtmlEncode(s);
[return: NotNullIfNotNull(nameof(value))]
public static string? HtmlEncode(object? value) =>
value == null ? null : HtmlEncode(Convert.ToString(value, CultureInfo.CurrentCulture) ?? string.Empty);
public static void HtmlEncode(string? s, TextWriter output) => HttpEncoder.HtmlEncode(s, output);
[return: NotNullIfNotNull(nameof(s))]
public static string? HtmlAttributeEncode(string? s) => HttpEncoder.HtmlAttributeEncode(s);
public static void HtmlAttributeEncode(string? s, TextWriter output) => HttpEncoder.HtmlAttributeEncode(s, output);
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlEncode(string? str) => UrlEncode(str, Encoding.UTF8);
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlPathEncode(string? str) => HttpEncoder.UrlPathEncode(str);
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlEncode(string? str, Encoding e) =>
str == null ? null : Encoding.ASCII.GetString(UrlEncodeToBytes(str, e));
[return: NotNullIfNotNull(nameof(bytes))]
public static string? UrlEncode(byte[]? bytes) => bytes == null ? null : Encoding.ASCII.GetString(UrlEncodeToBytes(bytes));
[return: NotNullIfNotNull(nameof(bytes))]
public static string? UrlEncode(byte[]? bytes, int offset, int count) => bytes == null ? null : Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, offset, count));
[return: NotNullIfNotNull(nameof(str))]
public static byte[]? UrlEncodeToBytes(string? str) => UrlEncodeToBytes(str, Encoding.UTF8);
[return: NotNullIfNotNull(nameof(bytes))]
public static byte[]? UrlEncodeToBytes(byte[]? bytes) => bytes == null ? null : UrlEncodeToBytes(bytes, 0, bytes.Length);
[Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncodeToBytes(String).")]
[return: NotNullIfNotNull(nameof(str))]
public static byte[]? UrlEncodeUnicodeToBytes(string? str) => str == null ? null : Encoding.ASCII.GetBytes(UrlEncodeUnicode(str));
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlDecode(string? str) => UrlDecode(str, Encoding.UTF8);
[return: NotNullIfNotNull(nameof(bytes))]
public static string? UrlDecode(byte[]? bytes, Encoding e) => bytes == null ? null : UrlDecode(bytes, 0, bytes.Length, e);
[return: NotNullIfNotNull(nameof(str))]
public static byte[]? UrlDecodeToBytes(string? str) => UrlDecodeToBytes(str, Encoding.UTF8);
[return: NotNullIfNotNull(nameof(str))]
public static byte[]? UrlDecodeToBytes(string? str, Encoding e) => str == null ? null : UrlDecodeToBytes(e.GetBytes(str));
[return: NotNullIfNotNull(nameof(bytes))]
public static byte[]? UrlDecodeToBytes(byte[]? bytes) => bytes == null ? null : UrlDecodeToBytes(bytes, 0, bytes.Length);
[return: NotNullIfNotNull(nameof(str))]
public static byte[]? UrlEncodeToBytes(string? str, Encoding e)
{
if (str == null)
{
return null;
}
byte[] bytes = e.GetBytes(str);
return HttpEncoder.UrlEncode(bytes, 0, bytes.Length, alwaysCreateNewReturnValue: false);
}
[return: NotNullIfNotNull(nameof(bytes))]
public static byte[]? UrlEncodeToBytes(byte[]? bytes, int offset, int count) => HttpEncoder.UrlEncode(bytes, offset, count, alwaysCreateNewReturnValue: true);
[Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncode(String).")]
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlEncodeUnicode(string? str) => HttpEncoder.UrlEncodeUnicode(str);
[return: NotNullIfNotNull(nameof(str))]
public static string? UrlDecode(string? str, Encoding e) => HttpEncoder.UrlDecode(str, e);
[return: NotNullIfNotNull(nameof(bytes))]
public static string? UrlDecode(byte[]? bytes, int offset, int count, Encoding e) =>
HttpEncoder.UrlDecode(bytes, offset, count, e);
[return: NotNullIfNotNull(nameof(bytes))]
public static byte[]? UrlDecodeToBytes(byte[]? bytes, int offset, int count) => HttpEncoder.UrlDecode(bytes, offset, count);
public static string JavaScriptStringEncode(string? value) => HttpEncoder.JavaScriptStringEncode(value);
public static string JavaScriptStringEncode(string? value, bool addDoubleQuotes)
{
string encoded = HttpEncoder.JavaScriptStringEncode(value);
return addDoubleQuotes ? "\"" + encoded + "\"" : encoded;
}
}
}