-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathUriUtility.cs
More file actions
392 lines (335 loc) · 15.3 KB
/
Copy pathUriUtility.cs
File metadata and controls
392 lines (335 loc) · 15.3 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
namespace net.openstack.Core
{
using System;
using BitArray = System.Collections.BitArray;
using Encoding = System.Text.Encoding;
using MatchEvaluator = System.Text.RegularExpressions.MatchEvaluator;
using NumberStyles = System.Globalization.NumberStyles;
using Regex = System.Text.RegularExpressions.Regex;
using RegexOptions = System.Text.RegularExpressions.RegexOptions;
using StringBuilder = System.Text.StringBuilder;
/// <summary>
/// Provides static utility methods for encoding and decoding text within
/// RFC 3986 URIs.
/// </summary>
/// <seealso href="http://www.ietf.org/rfc/rfc3986">RFC 3986: URI Generic Syntax</seealso>
/// <threadsafety static="true" instance="false"/>
/// <preliminary/>
public static class UriUtility
{
private static readonly BitArray _unreservedCharacters;
private static readonly BitArray _generalDelimiters;
private static readonly BitArray _subDelimiters;
private static readonly BitArray _reservedCharacters;
private static readonly BitArray _allowedHostCharacters;
private static readonly BitArray _allowedPathCharacters;
private static readonly BitArray _allowedQueryCharacters;
private static readonly BitArray _allowedFragmentCharacters;
/// <summary>
/// This is the regular expression for a single percent-encoded character.
/// </summary>
private static readonly Regex _percentEncodedPattern = new Regex(@"%([0-9A-Fa-f][0-9A-Fa-f])", RegexOptions.Compiled);
static UriUtility()
{
_unreservedCharacters = new BitArray(256);
for (char i = 'a'; i <= 'z'; i++)
_unreservedCharacters.Set(i, true);
for (char i = 'A'; i <= 'Z'; i++)
_unreservedCharacters.Set(i, true);
for (char i = '0'; i <= '9'; i++)
_unreservedCharacters.Set(i, true);
_unreservedCharacters.Set('-', true);
_unreservedCharacters.Set('.', true);
_unreservedCharacters.Set('_', true);
_unreservedCharacters.Set('~', true);
_generalDelimiters = new BitArray(256);
_generalDelimiters.Set(':', true);
_generalDelimiters.Set('/', true);
_generalDelimiters.Set('?', true);
_generalDelimiters.Set('#', true);
_generalDelimiters.Set('[', true);
_generalDelimiters.Set(']', true);
_generalDelimiters.Set('@', true);
_subDelimiters = new BitArray(256);
_subDelimiters.Set('!', true);
_subDelimiters.Set('$', true);
_subDelimiters.Set('&', true);
_subDelimiters.Set('(', true);
_subDelimiters.Set(')', true);
_subDelimiters.Set('*', true);
_subDelimiters.Set('+', true);
_subDelimiters.Set(',', true);
_subDelimiters.Set(';', true);
_subDelimiters.Set('=', true);
_subDelimiters.Set('\'', true);
_reservedCharacters = new BitArray(256).Or(_generalDelimiters).Or(_subDelimiters);
_allowedHostCharacters = new BitArray(256).Or(_unreservedCharacters).Or(_subDelimiters);
_allowedPathCharacters = new BitArray(256).Or(_unreservedCharacters).Or(_subDelimiters);
_allowedPathCharacters.Set(':', true);
_allowedPathCharacters.Set('@', true);
_allowedQueryCharacters = new BitArray(256).Or(_allowedPathCharacters);
_allowedQueryCharacters.Set('/', true);
_allowedQueryCharacters.Set('?', true);
_allowedFragmentCharacters = new BitArray(256).Or(_allowedPathCharacters);
_allowedFragmentCharacters.Set('/', true);
_allowedFragmentCharacters.Set('?', true);
}
/// <summary>
/// Decodes the text of a URI by unescaping any percent-encoded character sequences and
/// then evaluating the result using the default <see cref="Encoding.UTF8"/> encoding.
/// </summary>
/// <remarks>
/// This method calls <see cref="UriDecode(string, Encoding)"/> using the default
/// <see cref="Encoding.UTF8"/> encoding.
/// </remarks>
/// <param name="text">The encoded URI.</param>
/// <returns>The decoded URI text.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
public static string UriDecode(string text)
{
return UriDecode(text, Encoding.UTF8);
}
/// <summary>
/// Decodes the text of a URI by unescaping any percent-encoded character sequences and
/// then evaluating the result using the specified encoding.
/// </summary>
/// <param name="text">The encoded URI.</param>
/// <param name="encoding">The encoding to use for Unicode characters in the URI. If this value is <see langword="null"/>, the <see cref="Encoding.UTF8"/> encoding will be used.</param>
/// <returns>The decoded URI text.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
public static string UriDecode(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
encoding = encoding ?? Encoding.UTF8;
MatchEvaluator matchEvaluator =
match =>
{
string hexValue = match.Groups[1].Value;
return ((char)byte.Parse(hexValue, NumberStyles.HexNumber)).ToString();
};
string decodedText = _percentEncodedPattern.Replace(text, matchEvaluator);
byte[] data = Array.ConvertAll(decodedText.ToCharArray(), c => (byte)c);
return encoding.GetString(data);
}
/// <summary>
/// Encodes text for inclusion in a URI using the <see cref="Encoding.UTF8"/> encoding.
/// </summary>
/// <remarks>
/// This method calls <see cref="UriEncode(string, UriPart, Encoding)"/> using the default
/// <see cref="Encoding.UTF8"/> encoding.
/// </remarks>
/// <param name="text">The text to encode for inclusion in a URI.</param>
/// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param>
/// <returns>The URI-encoded text, suitable for the specified URI part.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception>
public static string UriEncode(string text, UriPart uriPart)
{
return UriEncode(text, uriPart, Encoding.UTF8);
}
/// <summary>
/// Encodes text for inclusion in a URI.
/// </summary>
/// <param name="text">The text to encode for inclusion in a URI.</param>
/// <param name="uriPart">A <see cref="UriPart"/> value indicating where in the URI the specified text will be included.</param>
/// <param name="encoding">The encoding to use for Unicode characters in the URI. If this value is <see langword="null"/>, the <see cref="Encoding.UTF8"/> encoding will be used.</param>
/// <returns>The URI-encoded text, suitable for the specified URI part.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="uriPart"/> is not a valid <see cref="UriPart"/>.</exception>
public static string UriEncode(string text, UriPart uriPart, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
encoding = encoding ?? Encoding.UTF8;
switch (uriPart)
{
case UriPart.Any:
return UriEncodeAny(text, encoding);
case UriPart.AnyUrl:
return UriEncodeAnyUrl(text, encoding);
case UriPart.Host:
return UriEncodeHost(text, encoding);
case UriPart.Path:
return UriEncodePath(text, encoding);
case UriPart.PathSegment:
return UriEncodePathSegment(text, encoding);
case UriPart.Query:
return UriEncodeQuery(text, encoding);
case UriPart.QueryValue:
return UriEncodeQueryValue(text, encoding);
case UriPart.Fragment:
return UriEncodeFragment(text, encoding);
default:
throw new ArgumentException("The specified uriPart is not valid.", "uriPart");
}
}
private static string UriEncodeAny(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_unreservedCharacters[data[i]])
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodeAnyUrl(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_unreservedCharacters[data[i]])
{
builder.Append((char)data[i]);
}
else
{
switch ((char)data[i])
{
case '(':
case ')':
case '*':
case '!':
builder.Append((char)data[i]);
break;
default:
builder.Append('%').Append(data[i].ToString("x2"));
break;
}
}
}
return builder.ToString();
}
private static string UriEncodeHost(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedHostCharacters[data[i]])
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodePath(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedPathCharacters[data[i]] || data[i] == '/')
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodePathSegment(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedPathCharacters[data[i]])
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodeQuery(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedQueryCharacters[data[i]])
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodeQueryValue(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedQueryCharacters[data[i]] && data[i] != '&')
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
private static string UriEncodeFragment(string text, Encoding encoding)
{
if (text == null)
throw new ArgumentNullException("text");
if (encoding == null)
throw new ArgumentNullException("encoding");
if (text.Length == 0)
return text;
StringBuilder builder = new StringBuilder();
byte[] data = encoding.GetBytes(text);
for (int i = 0; i < data.Length; i++)
{
if (_allowedFragmentCharacters[data[i]])
builder.Append((char)data[i]);
else
builder.Append('%').Append(data[i].ToString("x2"));
}
return builder.ToString();
}
}
}