2
// System.Web.HttpUtility
5
// Patrik Torstensson (Patrik.Torstensson@labs2.com)
6
// Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)
7
// Tim Coleman (tim@timcoleman.com)
8
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
10
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12
// Permission is hereby granted, free of charge, to any person obtaining
13
// a copy of this software and associated documentation files (the
14
// "Software"), to deal in the Software without restriction, including
15
// without limitation the rights to use, copy, modify, merge, publish,
16
// distribute, sublicense, and/or sell copies of the Software, and to
17
// permit persons to whom the Software is furnished to do so, subject to
18
// the following conditions:
20
// The above copyright notice and this permission notice shall be
21
// included in all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
using System.Collections;
33
using System.Collections.Specialized;
34
using System.Globalization;
36
using System.Security.Permissions;
38
using System.Web.Util;
41
using System.Collections.Generic;
44
namespace System.Web {
47
// CAS - no InheritanceDemand here as the class is sealed
48
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
51
public sealed class HttpUtility {
54
static Hashtable entities;
55
static object lock_ = new object ();
59
static Hashtable Entities {
72
static void InitEntities ()
74
// Build the hash table of HTML entity references. This list comes
75
// from the HTML 4.01 W3C recommendation.
76
entities = new Hashtable ();
77
entities.Add ("nbsp", '\u00A0');
78
entities.Add ("iexcl", '\u00A1');
79
entities.Add ("cent", '\u00A2');
80
entities.Add ("pound", '\u00A3');
81
entities.Add ("curren", '\u00A4');
82
entities.Add ("yen", '\u00A5');
83
entities.Add ("brvbar", '\u00A6');
84
entities.Add ("sect", '\u00A7');
85
entities.Add ("uml", '\u00A8');
86
entities.Add ("copy", '\u00A9');
87
entities.Add ("ordf", '\u00AA');
88
entities.Add ("laquo", '\u00AB');
89
entities.Add ("not", '\u00AC');
90
entities.Add ("shy", '\u00AD');
91
entities.Add ("reg", '\u00AE');
92
entities.Add ("macr", '\u00AF');
93
entities.Add ("deg", '\u00B0');
94
entities.Add ("plusmn", '\u00B1');
95
entities.Add ("sup2", '\u00B2');
96
entities.Add ("sup3", '\u00B3');
97
entities.Add ("acute", '\u00B4');
98
entities.Add ("micro", '\u00B5');
99
entities.Add ("para", '\u00B6');
100
entities.Add ("middot", '\u00B7');
101
entities.Add ("cedil", '\u00B8');
102
entities.Add ("sup1", '\u00B9');
103
entities.Add ("ordm", '\u00BA');
104
entities.Add ("raquo", '\u00BB');
105
entities.Add ("frac14", '\u00BC');
106
entities.Add ("frac12", '\u00BD');
107
entities.Add ("frac34", '\u00BE');
108
entities.Add ("iquest", '\u00BF');
109
entities.Add ("Agrave", '\u00C0');
110
entities.Add ("Aacute", '\u00C1');
111
entities.Add ("Acirc", '\u00C2');
112
entities.Add ("Atilde", '\u00C3');
113
entities.Add ("Auml", '\u00C4');
114
entities.Add ("Aring", '\u00C5');
115
entities.Add ("AElig", '\u00C6');
116
entities.Add ("Ccedil", '\u00C7');
117
entities.Add ("Egrave", '\u00C8');
118
entities.Add ("Eacute", '\u00C9');
119
entities.Add ("Ecirc", '\u00CA');
120
entities.Add ("Euml", '\u00CB');
121
entities.Add ("Igrave", '\u00CC');
122
entities.Add ("Iacute", '\u00CD');
123
entities.Add ("Icirc", '\u00CE');
124
entities.Add ("Iuml", '\u00CF');
125
entities.Add ("ETH", '\u00D0');
126
entities.Add ("Ntilde", '\u00D1');
127
entities.Add ("Ograve", '\u00D2');
128
entities.Add ("Oacute", '\u00D3');
129
entities.Add ("Ocirc", '\u00D4');
130
entities.Add ("Otilde", '\u00D5');
131
entities.Add ("Ouml", '\u00D6');
132
entities.Add ("times", '\u00D7');
133
entities.Add ("Oslash", '\u00D8');
134
entities.Add ("Ugrave", '\u00D9');
135
entities.Add ("Uacute", '\u00DA');
136
entities.Add ("Ucirc", '\u00DB');
137
entities.Add ("Uuml", '\u00DC');
138
entities.Add ("Yacute", '\u00DD');
139
entities.Add ("THORN", '\u00DE');
140
entities.Add ("szlig", '\u00DF');
141
entities.Add ("agrave", '\u00E0');
142
entities.Add ("aacute", '\u00E1');
143
entities.Add ("acirc", '\u00E2');
144
entities.Add ("atilde", '\u00E3');
145
entities.Add ("auml", '\u00E4');
146
entities.Add ("aring", '\u00E5');
147
entities.Add ("aelig", '\u00E6');
148
entities.Add ("ccedil", '\u00E7');
149
entities.Add ("egrave", '\u00E8');
150
entities.Add ("eacute", '\u00E9');
151
entities.Add ("ecirc", '\u00EA');
152
entities.Add ("euml", '\u00EB');
153
entities.Add ("igrave", '\u00EC');
154
entities.Add ("iacute", '\u00ED');
155
entities.Add ("icirc", '\u00EE');
156
entities.Add ("iuml", '\u00EF');
157
entities.Add ("eth", '\u00F0');
158
entities.Add ("ntilde", '\u00F1');
159
entities.Add ("ograve", '\u00F2');
160
entities.Add ("oacute", '\u00F3');
161
entities.Add ("ocirc", '\u00F4');
162
entities.Add ("otilde", '\u00F5');
163
entities.Add ("ouml", '\u00F6');
164
entities.Add ("divide", '\u00F7');
165
entities.Add ("oslash", '\u00F8');
166
entities.Add ("ugrave", '\u00F9');
167
entities.Add ("uacute", '\u00FA');
168
entities.Add ("ucirc", '\u00FB');
169
entities.Add ("uuml", '\u00FC');
170
entities.Add ("yacute", '\u00FD');
171
entities.Add ("thorn", '\u00FE');
172
entities.Add ("yuml", '\u00FF');
173
entities.Add ("fnof", '\u0192');
174
entities.Add ("Alpha", '\u0391');
175
entities.Add ("Beta", '\u0392');
176
entities.Add ("Gamma", '\u0393');
177
entities.Add ("Delta", '\u0394');
178
entities.Add ("Epsilon", '\u0395');
179
entities.Add ("Zeta", '\u0396');
180
entities.Add ("Eta", '\u0397');
181
entities.Add ("Theta", '\u0398');
182
entities.Add ("Iota", '\u0399');
183
entities.Add ("Kappa", '\u039A');
184
entities.Add ("Lambda", '\u039B');
185
entities.Add ("Mu", '\u039C');
186
entities.Add ("Nu", '\u039D');
187
entities.Add ("Xi", '\u039E');
188
entities.Add ("Omicron", '\u039F');
189
entities.Add ("Pi", '\u03A0');
190
entities.Add ("Rho", '\u03A1');
191
entities.Add ("Sigma", '\u03A3');
192
entities.Add ("Tau", '\u03A4');
193
entities.Add ("Upsilon", '\u03A5');
194
entities.Add ("Phi", '\u03A6');
195
entities.Add ("Chi", '\u03A7');
196
entities.Add ("Psi", '\u03A8');
197
entities.Add ("Omega", '\u03A9');
198
entities.Add ("alpha", '\u03B1');
199
entities.Add ("beta", '\u03B2');
200
entities.Add ("gamma", '\u03B3');
201
entities.Add ("delta", '\u03B4');
202
entities.Add ("epsilon", '\u03B5');
203
entities.Add ("zeta", '\u03B6');
204
entities.Add ("eta", '\u03B7');
205
entities.Add ("theta", '\u03B8');
206
entities.Add ("iota", '\u03B9');
207
entities.Add ("kappa", '\u03BA');
208
entities.Add ("lambda", '\u03BB');
209
entities.Add ("mu", '\u03BC');
210
entities.Add ("nu", '\u03BD');
211
entities.Add ("xi", '\u03BE');
212
entities.Add ("omicron", '\u03BF');
213
entities.Add ("pi", '\u03C0');
214
entities.Add ("rho", '\u03C1');
215
entities.Add ("sigmaf", '\u03C2');
216
entities.Add ("sigma", '\u03C3');
217
entities.Add ("tau", '\u03C4');
218
entities.Add ("upsilon", '\u03C5');
219
entities.Add ("phi", '\u03C6');
220
entities.Add ("chi", '\u03C7');
221
entities.Add ("psi", '\u03C8');
222
entities.Add ("omega", '\u03C9');
223
entities.Add ("thetasym", '\u03D1');
224
entities.Add ("upsih", '\u03D2');
225
entities.Add ("piv", '\u03D6');
226
entities.Add ("bull", '\u2022');
227
entities.Add ("hellip", '\u2026');
228
entities.Add ("prime", '\u2032');
229
entities.Add ("Prime", '\u2033');
230
entities.Add ("oline", '\u203E');
231
entities.Add ("frasl", '\u2044');
232
entities.Add ("weierp", '\u2118');
233
entities.Add ("image", '\u2111');
234
entities.Add ("real", '\u211C');
235
entities.Add ("trade", '\u2122');
236
entities.Add ("alefsym", '\u2135');
237
entities.Add ("larr", '\u2190');
238
entities.Add ("uarr", '\u2191');
239
entities.Add ("rarr", '\u2192');
240
entities.Add ("darr", '\u2193');
241
entities.Add ("harr", '\u2194');
242
entities.Add ("crarr", '\u21B5');
243
entities.Add ("lArr", '\u21D0');
244
entities.Add ("uArr", '\u21D1');
245
entities.Add ("rArr", '\u21D2');
246
entities.Add ("dArr", '\u21D3');
247
entities.Add ("hArr", '\u21D4');
248
entities.Add ("forall", '\u2200');
249
entities.Add ("part", '\u2202');
250
entities.Add ("exist", '\u2203');
251
entities.Add ("empty", '\u2205');
252
entities.Add ("nabla", '\u2207');
253
entities.Add ("isin", '\u2208');
254
entities.Add ("notin", '\u2209');
255
entities.Add ("ni", '\u220B');
256
entities.Add ("prod", '\u220F');
257
entities.Add ("sum", '\u2211');
258
entities.Add ("minus", '\u2212');
259
entities.Add ("lowast", '\u2217');
260
entities.Add ("radic", '\u221A');
261
entities.Add ("prop", '\u221D');
262
entities.Add ("infin", '\u221E');
263
entities.Add ("ang", '\u2220');
264
entities.Add ("and", '\u2227');
265
entities.Add ("or", '\u2228');
266
entities.Add ("cap", '\u2229');
267
entities.Add ("cup", '\u222A');
268
entities.Add ("int", '\u222B');
269
entities.Add ("there4", '\u2234');
270
entities.Add ("sim", '\u223C');
271
entities.Add ("cong", '\u2245');
272
entities.Add ("asymp", '\u2248');
273
entities.Add ("ne", '\u2260');
274
entities.Add ("equiv", '\u2261');
275
entities.Add ("le", '\u2264');
276
entities.Add ("ge", '\u2265');
277
entities.Add ("sub", '\u2282');
278
entities.Add ("sup", '\u2283');
279
entities.Add ("nsub", '\u2284');
280
entities.Add ("sube", '\u2286');
281
entities.Add ("supe", '\u2287');
282
entities.Add ("oplus", '\u2295');
283
entities.Add ("otimes", '\u2297');
284
entities.Add ("perp", '\u22A5');
285
entities.Add ("sdot", '\u22C5');
286
entities.Add ("lceil", '\u2308');
287
entities.Add ("rceil", '\u2309');
288
entities.Add ("lfloor", '\u230A');
289
entities.Add ("rfloor", '\u230B');
290
entities.Add ("lang", '\u2329');
291
entities.Add ("rang", '\u232A');
292
entities.Add ("loz", '\u25CA');
293
entities.Add ("spades", '\u2660');
294
entities.Add ("clubs", '\u2663');
295
entities.Add ("hearts", '\u2665');
296
entities.Add ("diams", '\u2666');
297
entities.Add ("quot", '\u0022');
298
entities.Add ("amp", '\u0026');
299
entities.Add ("lt", '\u003C');
300
entities.Add ("gt", '\u003E');
301
entities.Add ("OElig", '\u0152');
302
entities.Add ("oelig", '\u0153');
303
entities.Add ("Scaron", '\u0160');
304
entities.Add ("scaron", '\u0161');
305
entities.Add ("Yuml", '\u0178');
306
entities.Add ("circ", '\u02C6');
307
entities.Add ("tilde", '\u02DC');
308
entities.Add ("ensp", '\u2002');
309
entities.Add ("emsp", '\u2003');
310
entities.Add ("thinsp", '\u2009');
311
entities.Add ("zwnj", '\u200C');
312
entities.Add ("zwj", '\u200D');
313
entities.Add ("lrm", '\u200E');
314
entities.Add ("rlm", '\u200F');
315
entities.Add ("ndash", '\u2013');
316
entities.Add ("mdash", '\u2014');
317
entities.Add ("lsquo", '\u2018');
318
entities.Add ("rsquo", '\u2019');
319
entities.Add ("sbquo", '\u201A');
320
entities.Add ("ldquo", '\u201C');
321
entities.Add ("rdquo", '\u201D');
322
entities.Add ("bdquo", '\u201E');
323
entities.Add ("dagger", '\u2020');
324
entities.Add ("Dagger", '\u2021');
325
entities.Add ("permil", '\u2030');
326
entities.Add ("lsaquo", '\u2039');
327
entities.Add ("rsaquo", '\u203A');
328
entities.Add ("euro", '\u20AC');
331
public HttpUtility ()
335
#endregion // Constructors
339
public static void HtmlAttributeEncode (string s, TextWriter output)
341
output.Write(HtmlAttributeEncode(s));
344
public static string HtmlAttributeEncode (string s)
349
bool needEncode = false;
350
for (int i = 0; i < s.Length; i++) {
351
if (s [i] == '&' || s [i] == '"' || s [i] == '<') {
360
StringBuilder output = new StringBuilder ();
362
for (int i = 0; i < len; i++)
365
output.Append ("&");
368
output.Append (""");
371
output.Append ("<");
374
output.Append (s [i]);
378
return output.ToString();
381
public static string UrlDecode (string str)
383
return UrlDecode(str, Encoding.UTF8);
386
static char [] GetChars (MemoryStream b, Encoding e)
388
return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
391
static void WriteCharBytes (IList buf, char ch, Encoding e)
394
foreach (byte b in e.GetBytes (new char[] { ch }))
400
public static string UrlDecode (string s, Encoding e)
405
if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
413
var bytes = new List <byte> ();
415
ArrayList bytes = new ArrayList ();
420
for (int i = 0; i < len; i++) {
422
if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
423
if (s [i + 1] == 'u' && i + 5 < len) {
424
// unicode hex sequence
425
xchar = GetChar (s, i + 2, 4);
427
WriteCharBytes (bytes, (char)xchar, e);
430
WriteCharBytes (bytes, '%', e);
431
} else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
432
WriteCharBytes (bytes, (char)xchar, e);
435
WriteCharBytes (bytes, '%', e);
441
WriteCharBytes (bytes, ' ', e);
443
WriteCharBytes (bytes, ch, e);
447
byte[] buf = bytes.ToArray ();
449
byte[] buf = (byte[])bytes.ToArray (typeof (byte));
452
return e.GetString (buf);
456
public static string UrlDecode (byte [] bytes, Encoding e)
461
return UrlDecode (bytes, 0, bytes.Length, e);
464
static int GetInt (byte b)
467
if (c >= '0' && c <= '9')
470
if (c >= 'a' && c <= 'f')
473
if (c >= 'A' && c <= 'F')
479
static int GetChar (byte [] bytes, int offset, int length)
482
int end = length + offset;
483
for (int i = offset; i < end; i++) {
484
int current = GetInt (bytes [i]);
487
value = (value << 4) + current;
493
static int GetChar (string str, int offset, int length)
496
int end = length + offset;
497
for (int i = offset; i < end; i++) {
502
int current = GetInt ((byte) c);
505
val = (val << 4) + current;
511
public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
519
throw new ArgumentNullException ("bytes");
521
if (offset < 0 || offset > bytes.Length)
522
throw new ArgumentOutOfRangeException ("offset");
524
if (count < 0 || offset + count > bytes.Length)
525
throw new ArgumentOutOfRangeException ("count");
527
StringBuilder output = new StringBuilder ();
528
MemoryStream acc = new MemoryStream ();
530
int end = count + offset;
532
for (int i = offset; i < end; i++) {
533
if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
534
if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
535
if (acc.Length > 0) {
536
output.Append (GetChars (acc, e));
539
xchar = GetChar (bytes, i + 2, 4);
541
output.Append ((char) xchar);
545
} else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
546
acc.WriteByte ((byte) xchar);
552
if (acc.Length > 0) {
553
output.Append (GetChars (acc, e));
557
if (bytes [i] == '+') {
560
output.Append ((char) bytes [i]);
564
if (acc.Length > 0) {
565
output.Append (GetChars (acc, e));
569
return output.ToString ();
572
public static byte [] UrlDecodeToBytes (byte [] bytes)
577
return UrlDecodeToBytes (bytes, 0, bytes.Length);
580
public static byte [] UrlDecodeToBytes (string str)
582
return UrlDecodeToBytes (str, Encoding.UTF8);
585
public static byte [] UrlDecodeToBytes (string str, Encoding e)
591
throw new ArgumentNullException ("e");
593
return UrlDecodeToBytes (e.GetBytes (str));
596
public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
603
int len = bytes.Length;
604
if (offset < 0 || offset >= len)
605
throw new ArgumentOutOfRangeException("offset");
607
if (count < 0 || offset > len - count)
608
throw new ArgumentOutOfRangeException("count");
610
MemoryStream result = new MemoryStream ();
611
int end = offset + count;
612
for (int i = offset; i < end; i++){
613
char c = (char) bytes [i];
616
} else if (c == '%' && i < end - 2) {
617
int xchar = GetChar (bytes, i + 1, 2);
623
result.WriteByte ((byte) c);
626
return result.ToArray ();
629
public static string UrlEncode(string str)
631
return UrlEncode(str, Encoding.UTF8);
634
public static string UrlEncode (string s, Encoding Enc)
642
bool needEncode = false;
644
for (int i = 0; i < len; i++) {
646
if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
658
// avoided GetByteCount call
659
byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
660
int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
661
return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
664
public static string UrlEncode (byte [] bytes)
669
if (bytes.Length == 0)
672
return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
675
public static string UrlEncode (byte [] bytes, int offset, int count)
680
if (bytes.Length == 0)
683
return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
686
public static byte [] UrlEncodeToBytes (string str)
688
return UrlEncodeToBytes (str, Encoding.UTF8);
691
public static byte [] UrlEncodeToBytes (string str, Encoding e)
699
byte [] bytes = e.GetBytes (str);
700
return UrlEncodeToBytes (bytes, 0, bytes.Length);
703
public static byte [] UrlEncodeToBytes (byte [] bytes)
708
if (bytes.Length == 0)
711
return UrlEncodeToBytes (bytes, 0, bytes.Length);
714
static char [] hexChars = "0123456789abcdef".ToCharArray ();
716
static bool NotEncoded (char c)
718
return (c == '!' || c == '\'' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_');
721
static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
723
//FIXME: what happens when there is an internal error?
725
// throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
729
result.WriteByte ((byte)'%');
730
result.WriteByte ((byte)'u');
732
result.WriteByte ((byte)hexChars [idx]);
733
idx = (i >> 8) & 0x0F;
734
result.WriteByte ((byte)hexChars [idx]);
735
idx = (i >> 4) & 0x0F;
736
result.WriteByte ((byte)hexChars [idx]);
738
result.WriteByte ((byte)hexChars [idx]);
742
if (c > ' ' && NotEncoded (c)) {
743
result.WriteByte ((byte)c);
747
result.WriteByte ((byte)'+');
751
(c < 'A' && c > '9') ||
752
(c > 'Z' && c < 'a') ||
754
if (isUnicode && c > 127) {
755
result.WriteByte ((byte)'%');
756
result.WriteByte ((byte)'u');
757
result.WriteByte ((byte)'0');
758
result.WriteByte ((byte)'0');
761
result.WriteByte ((byte)'%');
763
int idx = ((int) c) >> 4;
764
result.WriteByte ((byte)hexChars [idx]);
765
idx = ((int) c) & 0x0F;
766
result.WriteByte ((byte)hexChars [idx]);
769
result.WriteByte ((byte)c);
772
public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
777
int len = bytes.Length;
781
if (offset < 0 || offset >= len)
782
throw new ArgumentOutOfRangeException("offset");
784
if (count < 0 || count > len - offset)
785
throw new ArgumentOutOfRangeException("count");
787
MemoryStream result = new MemoryStream (count);
788
int end = offset + count;
789
for (int i = offset; i < end; i++)
790
UrlEncodeChar ((char)bytes [i], result, false);
792
return result.ToArray();
795
public static string UrlEncodeUnicode (string str)
800
return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
803
public static byte [] UrlEncodeUnicodeToBytes (string str)
811
MemoryStream result = new MemoryStream (str.Length);
812
foreach (char c in str){
813
UrlEncodeChar (c, result, true);
815
return result.ToArray ();
819
/// Decodes an HTML-encoded string and returns the decoded string.
821
/// <param name="s">The HTML string to decode. </param>
822
/// <returns>The decoded text.</returns>
823
public static string HtmlDecode (string s)
826
throw new ArgumentNullException ("s");
828
if (s.IndexOf ('&') == -1)
831
StringBuilder entity = new StringBuilder ();
832
StringBuilder output = new StringBuilder ();
835
// 1 -> right after '&'
836
// 2 -> between '&' and ';' but no '#'
837
// 3 -> '#' found after '&' and getting numbers
840
bool have_trailing_digits = false;
842
for (int i = 0; i < len; i++) {
856
if (have_trailing_digits) {
857
entity.Append (number.ToString (Helpers.InvariantCulture));
858
have_trailing_digits = false;
861
output.Append (entity.ToString ());
870
output.Append (entity.ToString ());
882
} else if (state == 2) {
885
string key = entity.ToString ();
886
if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
887
key = Entities [key.Substring (1, key.Length - 2)].ToString ();
893
} else if (state == 3) {
895
if (number > 65535) {
896
output.Append ("&#");
897
output.Append (number.ToString (Helpers.InvariantCulture));
900
output.Append ((char) number);
904
have_trailing_digits = false;
905
} else if (Char.IsDigit (c)) {
906
number = number * 10 + ((int) c - '0');
907
have_trailing_digits = true;
910
if (have_trailing_digits) {
911
entity.Append (number.ToString (Helpers.InvariantCulture));
912
have_trailing_digits = false;
919
if (entity.Length > 0) {
920
output.Append (entity.ToString ());
921
} else if (have_trailing_digits) {
922
output.Append (number.ToString (Helpers.InvariantCulture));
924
return output.ToString ();
928
/// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
930
/// <param name="s">The HTML string to decode</param>
931
/// <param name="output">The TextWriter output stream containing the decoded string. </param>
932
public static void HtmlDecode(string s, TextWriter output)
935
output.Write (HtmlDecode (s));
939
/// HTML-encodes a string and returns the encoded string.
941
/// <param name="s">The text string to encode. </param>
942
/// <returns>The HTML-encoded text.</returns>
943
public static string HtmlEncode (string s)
948
bool needEncode = false;
949
for (int i = 0; i < s.Length; i++) {
951
if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
960
StringBuilder output = new StringBuilder ();
963
for (int i = 0; i < len; i++)
966
output.Append ("&");
969
output.Append (">");
972
output.Append ("<");
975
output.Append (""");
978
// MS starts encoding with &# from 160 and stops at 255.
979
// We don't do that. One reason is the 65308/65310 unicode
980
// characters that look like '<' and '>'.
982
if (s [i] > 159 && s [i] < 256) {
986
output.Append ("&#");
987
output.Append (((int) s [i]).ToString (Helpers.InvariantCulture));
990
output.Append (s [i]);
994
return output.ToString ();
998
/// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
1000
/// <param name="s">The string to encode. </param>
1001
/// <param name="output">The TextWriter output stream containing the encoded string. </param>
1002
public static void HtmlEncode(string s, TextWriter output)
1005
output.Write (HtmlEncode (s));
1009
public static string UrlPathEncode (string s)
1011
if (s == null || s.Length == 0)
1014
MemoryStream result = new MemoryStream ();
1015
int length = s.Length;
1016
for (int i = 0; i < length; i++) {
1017
UrlPathEncodeChar (s [i], result);
1019
return Encoding.ASCII.GetString (result.ToArray ());
1022
static void UrlPathEncodeChar (char c, Stream result) {
1024
if (c < 33 || c > 126) {
1028
byte [] bIn = Encoding.UTF8.GetBytes (c.ToString ());
1029
for (int i = 0; i < bIn.Length; i++) {
1030
result.WriteByte ((byte) '%');
1031
int idx = ((int) bIn [i]) >> 4;
1032
result.WriteByte ((byte) hexChars [idx]);
1033
idx = ((int) bIn [i]) & 0x0F;
1034
result.WriteByte ((byte) hexChars [idx]);
1037
else if (c == ' ') {
1038
result.WriteByte ((byte) '%');
1039
result.WriteByte ((byte) '2');
1040
result.WriteByte ((byte) '0');
1043
result.WriteByte ((byte) c);
1048
class HttpQSCollection : NameValueCollection {
1049
public override string ToString ()
1054
StringBuilder sb = new StringBuilder ();
1055
string [] keys = AllKeys;
1056
for (int i = 0; i < count; i++) {
1057
sb.AppendFormat ("{0}={1}&", keys [i], this [keys [i]]);
1061
return sb.ToString ();
1065
public static NameValueCollection ParseQueryString (string query)
1067
return ParseQueryString (query, Encoding.UTF8);
1070
public static NameValueCollection ParseQueryString (string query, Encoding encoding)
1073
throw new ArgumentNullException ("query");
1074
if (encoding == null)
1075
throw new ArgumentNullException ("encoding");
1076
if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
1077
return new NameValueCollection ();
1078
if (query[0] == '?')
1079
query = query.Substring (1);
1081
NameValueCollection result = new HttpQSCollection ();
1082
ParseQueryString (query, encoding, result);
1087
internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
1089
if (query.Length == 0)
1092
string decoded = HtmlDecode (query);
1093
int decodedLength = decoded.Length;
1096
while (namePos <= decodedLength) {
1097
int valuePos = -1, valueEnd = -1;
1098
for (int q = namePos; q < decodedLength; q++) {
1099
if (valuePos == -1 && decoded [q] == '=') {
1101
} else if (decoded [q] == '&') {
1109
if (decoded [namePos] == '?')
1114
if (valuePos == -1) {
1118
name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
1122
valueEnd = decoded.Length;
1124
namePos = valueEnd + 1;
1126
value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
1128
result.Add (name, value);
1133
#endregion // Methods