(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
blobc7f3fecf63b2df718fdb98f494fdc16dd430980a
1 //
2 // System.Web.HttpUtility
3 //
4 // Authors:
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)
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.IO;
35 using System.Text;
36 using System.Web.Util;
38 namespace System.Web {
39 public sealed class HttpUtility {
41 #region Fields
43 const string _hex = "0123456789ABCDEF";
44 const string _chars = "<>;:.?=&@*+%/\\";
45 static Hashtable entities;
46 static object lock_ = new object ();
48 #endregion // Fields
50 static Hashtable Entities {
51 get {
52 lock (lock_) {
53 if (entities == null)
54 InitEntities ();
56 return entities;
61 #region Constructors
63 static void InitEntities ()
65 // Build the hash table of HTML entity references. This list comes
66 // from the HTML 4.01 W3C recommendation.
67 entities = new Hashtable ();
68 entities.Add ("nbsp", '\u00A0');
69 entities.Add ("iexcl", '\u00A1');
70 entities.Add ("cent", '\u00A2');
71 entities.Add ("pound", '\u00A3');
72 entities.Add ("curren", '\u00A4');
73 entities.Add ("yen", '\u00A5');
74 entities.Add ("brvbar", '\u00A6');
75 entities.Add ("sect", '\u00A7');
76 entities.Add ("uml", '\u00A8');
77 entities.Add ("copy", '\u00A9');
78 entities.Add ("ordf", '\u00AA');
79 entities.Add ("laquo", '\u00AB');
80 entities.Add ("not", '\u00AC');
81 entities.Add ("shy", '\u00AD');
82 entities.Add ("reg", '\u00AE');
83 entities.Add ("macr", '\u00AF');
84 entities.Add ("deg", '\u00B0');
85 entities.Add ("plusmn", '\u00B1');
86 entities.Add ("sup2", '\u00B2');
87 entities.Add ("sup3", '\u00B3');
88 entities.Add ("acute", '\u00B4');
89 entities.Add ("micro", '\u00B5');
90 entities.Add ("para", '\u00B6');
91 entities.Add ("middot", '\u00B7');
92 entities.Add ("cedil", '\u00B8');
93 entities.Add ("sup1", '\u00B9');
94 entities.Add ("ordm", '\u00BA');
95 entities.Add ("raquo", '\u00BB');
96 entities.Add ("frac14", '\u00BC');
97 entities.Add ("frac12", '\u00BD');
98 entities.Add ("frac34", '\u00BE');
99 entities.Add ("iquest", '\u00BF');
100 entities.Add ("Agrave", '\u00C0');
101 entities.Add ("Aacute", '\u00C1');
102 entities.Add ("Acirc", '\u00C2');
103 entities.Add ("Atilde", '\u00C3');
104 entities.Add ("Auml", '\u00C4');
105 entities.Add ("Aring", '\u00C5');
106 entities.Add ("AElig", '\u00C6');
107 entities.Add ("Ccedil", '\u00C7');
108 entities.Add ("Egrave", '\u00C8');
109 entities.Add ("Eacute", '\u00C9');
110 entities.Add ("Ecirc", '\u00CA');
111 entities.Add ("Euml", '\u00CB');
112 entities.Add ("Igrave", '\u00CC');
113 entities.Add ("Iacute", '\u00CD');
114 entities.Add ("Icirc", '\u00CE');
115 entities.Add ("Iuml", '\u00CF');
116 entities.Add ("ETH", '\u00D0');
117 entities.Add ("Ntilde", '\u00D1');
118 entities.Add ("Ograve", '\u00D2');
119 entities.Add ("Oacute", '\u00D3');
120 entities.Add ("Ocirc", '\u00D4');
121 entities.Add ("Otilde", '\u00D5');
122 entities.Add ("Ouml", '\u00D6');
123 entities.Add ("times", '\u00D7');
124 entities.Add ("Oslash", '\u00D8');
125 entities.Add ("Ugrave", '\u00D9');
126 entities.Add ("Uacute", '\u00DA');
127 entities.Add ("Ucirc", '\u00DB');
128 entities.Add ("Uuml", '\u00DC');
129 entities.Add ("Yacute", '\u00DD');
130 entities.Add ("THORN", '\u00DE');
131 entities.Add ("szlig", '\u00DF');
132 entities.Add ("agrave", '\u00E0');
133 entities.Add ("aacute", '\u00E1');
134 entities.Add ("acirc", '\u00E2');
135 entities.Add ("atilde", '\u00E3');
136 entities.Add ("auml", '\u00E4');
137 entities.Add ("aring", '\u00E5');
138 entities.Add ("aelig", '\u00E6');
139 entities.Add ("ccedil", '\u00E7');
140 entities.Add ("egrave", '\u00E8');
141 entities.Add ("eacute", '\u00E9');
142 entities.Add ("ecirc", '\u00EA');
143 entities.Add ("euml", '\u00EB');
144 entities.Add ("igrave", '\u00EC');
145 entities.Add ("iacute", '\u00ED');
146 entities.Add ("icirc", '\u00EE');
147 entities.Add ("iuml", '\u00EF');
148 entities.Add ("eth", '\u00F0');
149 entities.Add ("ntilde", '\u00F1');
150 entities.Add ("ograve", '\u00F2');
151 entities.Add ("oacute", '\u00F3');
152 entities.Add ("ocirc", '\u00F4');
153 entities.Add ("otilde", '\u00F5');
154 entities.Add ("ouml", '\u00F6');
155 entities.Add ("divide", '\u00F7');
156 entities.Add ("oslash", '\u00F8');
157 entities.Add ("ugrave", '\u00F9');
158 entities.Add ("uacute", '\u00FA');
159 entities.Add ("ucirc", '\u00FB');
160 entities.Add ("uuml", '\u00FC');
161 entities.Add ("yacute", '\u00FD');
162 entities.Add ("thorn", '\u00FE');
163 entities.Add ("yuml", '\u00FF');
164 entities.Add ("fnof", '\u0192');
165 entities.Add ("Alpha", '\u0391');
166 entities.Add ("Beta", '\u0392');
167 entities.Add ("Gamma", '\u0393');
168 entities.Add ("Delta", '\u0394');
169 entities.Add ("Epsilon", '\u0395');
170 entities.Add ("Zeta", '\u0396');
171 entities.Add ("Eta", '\u0397');
172 entities.Add ("Theta", '\u0398');
173 entities.Add ("Iota", '\u0399');
174 entities.Add ("Kappa", '\u039A');
175 entities.Add ("Lambda", '\u039B');
176 entities.Add ("Mu", '\u039C');
177 entities.Add ("Nu", '\u039D');
178 entities.Add ("Xi", '\u039E');
179 entities.Add ("Omicron", '\u039F');
180 entities.Add ("Pi", '\u03A0');
181 entities.Add ("Rho", '\u03A1');
182 entities.Add ("Sigma", '\u03A3');
183 entities.Add ("Tau", '\u03A4');
184 entities.Add ("Upsilon", '\u03A5');
185 entities.Add ("Phi", '\u03A6');
186 entities.Add ("Chi", '\u03A7');
187 entities.Add ("Psi", '\u03A8');
188 entities.Add ("Omega", '\u03A9');
189 entities.Add ("alpha", '\u03B1');
190 entities.Add ("beta", '\u03B2');
191 entities.Add ("gamma", '\u03B3');
192 entities.Add ("delta", '\u03B4');
193 entities.Add ("epsilon", '\u03B5');
194 entities.Add ("zeta", '\u03B6');
195 entities.Add ("eta", '\u03B7');
196 entities.Add ("theta", '\u03B8');
197 entities.Add ("iota", '\u03B9');
198 entities.Add ("kappa", '\u03BA');
199 entities.Add ("lambda", '\u03BB');
200 entities.Add ("mu", '\u03BC');
201 entities.Add ("nu", '\u03BD');
202 entities.Add ("xi", '\u03BE');
203 entities.Add ("omicron", '\u03BF');
204 entities.Add ("pi", '\u03C0');
205 entities.Add ("rho", '\u03C1');
206 entities.Add ("sigmaf", '\u03C2');
207 entities.Add ("sigma", '\u03C3');
208 entities.Add ("tau", '\u03C4');
209 entities.Add ("upsilon", '\u03C5');
210 entities.Add ("phi", '\u03C6');
211 entities.Add ("chi", '\u03C7');
212 entities.Add ("psi", '\u03C8');
213 entities.Add ("omega", '\u03C9');
214 entities.Add ("thetasym", '\u03D1');
215 entities.Add ("upsih", '\u03D2');
216 entities.Add ("piv", '\u03D6');
217 entities.Add ("bull", '\u2022');
218 entities.Add ("hellip", '\u2026');
219 entities.Add ("prime", '\u2032');
220 entities.Add ("Prime", '\u2033');
221 entities.Add ("oline", '\u203E');
222 entities.Add ("frasl", '\u2044');
223 entities.Add ("weierp", '\u2118');
224 entities.Add ("image", '\u2111');
225 entities.Add ("real", '\u211C');
226 entities.Add ("trade", '\u2122');
227 entities.Add ("alefsym", '\u2135');
228 entities.Add ("larr", '\u2190');
229 entities.Add ("uarr", '\u2191');
230 entities.Add ("rarr", '\u2192');
231 entities.Add ("darr", '\u2193');
232 entities.Add ("harr", '\u2194');
233 entities.Add ("crarr", '\u21B5');
234 entities.Add ("lArr", '\u21D0');
235 entities.Add ("uArr", '\u21D1');
236 entities.Add ("rArr", '\u21D2');
237 entities.Add ("dArr", '\u21D3');
238 entities.Add ("hArr", '\u21D4');
239 entities.Add ("forall", '\u2200');
240 entities.Add ("part", '\u2202');
241 entities.Add ("exist", '\u2203');
242 entities.Add ("empty", '\u2205');
243 entities.Add ("nabla", '\u2207');
244 entities.Add ("isin", '\u2208');
245 entities.Add ("notin", '\u2209');
246 entities.Add ("ni", '\u220B');
247 entities.Add ("prod", '\u220F');
248 entities.Add ("sum", '\u2211');
249 entities.Add ("minus", '\u2212');
250 entities.Add ("lowast", '\u2217');
251 entities.Add ("radic", '\u221A');
252 entities.Add ("prop", '\u221D');
253 entities.Add ("infin", '\u221E');
254 entities.Add ("ang", '\u2220');
255 entities.Add ("and", '\u2227');
256 entities.Add ("or", '\u2228');
257 entities.Add ("cap", '\u2229');
258 entities.Add ("cup", '\u222A');
259 entities.Add ("int", '\u222B');
260 entities.Add ("there4", '\u2234');
261 entities.Add ("sim", '\u223C');
262 entities.Add ("cong", '\u2245');
263 entities.Add ("asymp", '\u2248');
264 entities.Add ("ne", '\u2260');
265 entities.Add ("equiv", '\u2261');
266 entities.Add ("le", '\u2264');
267 entities.Add ("ge", '\u2265');
268 entities.Add ("sub", '\u2282');
269 entities.Add ("sup", '\u2283');
270 entities.Add ("nsub", '\u2284');
271 entities.Add ("sube", '\u2286');
272 entities.Add ("supe", '\u2287');
273 entities.Add ("oplus", '\u2295');
274 entities.Add ("otimes", '\u2297');
275 entities.Add ("perp", '\u22A5');
276 entities.Add ("sdot", '\u22C5');
277 entities.Add ("lceil", '\u2308');
278 entities.Add ("rceil", '\u2309');
279 entities.Add ("lfloor", '\u230A');
280 entities.Add ("rfloor", '\u230B');
281 entities.Add ("lang", '\u2329');
282 entities.Add ("rang", '\u232A');
283 entities.Add ("loz", '\u25CA');
284 entities.Add ("spades", '\u2660');
285 entities.Add ("clubs", '\u2663');
286 entities.Add ("hearts", '\u2665');
287 entities.Add ("diams", '\u2666');
288 entities.Add ("quot", '\u0022');
289 entities.Add ("amp", '\u0026');
290 entities.Add ("lt", '\u003C');
291 entities.Add ("gt", '\u003E');
292 entities.Add ("OElig", '\u0152');
293 entities.Add ("oelig", '\u0153');
294 entities.Add ("Scaron", '\u0160');
295 entities.Add ("scaron", '\u0161');
296 entities.Add ("Yuml", '\u0178');
297 entities.Add ("circ", '\u02C6');
298 entities.Add ("tilde", '\u02DC');
299 entities.Add ("ensp", '\u2002');
300 entities.Add ("emsp", '\u2003');
301 entities.Add ("thinsp", '\u2009');
302 entities.Add ("zwnj", '\u200C');
303 entities.Add ("zwj", '\u200D');
304 entities.Add ("lrm", '\u200E');
305 entities.Add ("rlm", '\u200F');
306 entities.Add ("ndash", '\u2013');
307 entities.Add ("mdash", '\u2014');
308 entities.Add ("lsquo", '\u2018');
309 entities.Add ("rsquo", '\u2019');
310 entities.Add ("sbquo", '\u201A');
311 entities.Add ("ldquo", '\u201C');
312 entities.Add ("rdquo", '\u201D');
313 entities.Add ("bdquo", '\u201E');
314 entities.Add ("dagger", '\u2020');
315 entities.Add ("Dagger", '\u2021');
316 entities.Add ("permil", '\u2030');
317 entities.Add ("lsaquo", '\u2039');
318 entities.Add ("rsaquo", '\u203A');
319 entities.Add ("euro", '\u20AC');
322 public HttpUtility ()
326 #endregion // Constructors
328 #region Methods
330 public static void HtmlAttributeEncode (string s, TextWriter output)
332 output.Write(HtmlAttributeEncode(s));
335 public static string HtmlAttributeEncode (string s)
337 if (null == s)
338 return null;
340 if (s.IndexOf ('&') == -1 && s.IndexOf ('"') == -1)
341 return s;
343 StringBuilder output = new StringBuilder ();
344 foreach (char c in s)
345 switch (c) {
346 case '&' :
347 output.Append ("&amp;");
348 break;
349 case '"' :
350 output.Append ("&quot;");
351 break;
352 default:
353 output.Append (c);
354 break;
357 return output.ToString();
360 public static string UrlDecode (string str)
362 return UrlDecode(str, Encoding.UTF8);
365 private static char [] GetChars (MemoryStream b, Encoding e)
367 return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
370 public static string UrlDecode (string s, Encoding e)
372 if (null == s)
373 return null;
375 if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
376 return s;
378 if (e == null)
379 e = Encoding.UTF8;
381 StringBuilder output = new StringBuilder ();
382 long len = s.Length;
383 NumberStyles hexa = NumberStyles.HexNumber;
384 MemoryStream bytes = new MemoryStream ();
386 for (int i = 0; i < len; i++) {
387 if (s [i] == '%' && i + 2 < len) {
388 if (s [i + 1] == 'u' && i + 5 < len) {
389 if (bytes.Length > 0) {
390 output.Append (GetChars (bytes, e));
391 bytes.SetLength (0);
393 output.Append ((char) Int32.Parse (s.Substring (i + 2, 4), hexa));
394 i += 5;
395 } else {
396 bytes.WriteByte ((byte) Int32.Parse (s.Substring (i + 1, 2), hexa));
397 i += 2;
399 continue;
402 if (bytes.Length > 0) {
403 output.Append (GetChars (bytes, e));
404 bytes.SetLength (0);
407 if (s [i] == '+') {
408 output.Append (' ');
409 } else {
410 output.Append (s [i]);
414 if (bytes.Length > 0) {
415 output.Append (GetChars (bytes, e));
418 bytes = null;
419 return output.ToString ();
422 public static string UrlDecode (byte [] bytes, Encoding e)
424 if (bytes == null)
425 return null;
427 return UrlDecode (bytes, 0, bytes.Length, e);
430 private static int GetInt (byte b)
432 char c = Char.ToUpper ((char) b);
433 if (c >= '0' && c <= '9')
434 return c - '0';
436 if (c < 'A' || c > 'F')
437 return 0;
439 return (c - 'A' + 10);
442 private static char GetChar (byte [] bytes, int offset, int length)
444 int value = 0;
445 int end = length + offset;
446 for (int i = offset; i < end; i++)
447 value = (value << 4) + GetInt (bytes [i]);
449 return (char) value;
452 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
454 if (bytes == null || count == 0)
455 return null;
457 if (bytes == null)
458 throw new ArgumentNullException ("bytes");
460 if (offset < 0 || offset > bytes.Length)
461 throw new ArgumentOutOfRangeException ("offset");
463 if (count < 0 || offset + count > bytes.Length)
464 throw new ArgumentOutOfRangeException ("count");
466 StringBuilder output = new StringBuilder ();
467 MemoryStream acc = new MemoryStream ();
469 int end = count + offset;
470 for (int i = offset; i < end; i++) {
471 if (bytes [i] == '%' && i + 2 < count) {
472 if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
473 if (acc.Length > 0) {
474 output.Append (GetChars (acc, e));
475 acc.SetLength (0);
477 output.Append (GetChar (bytes, i + 2, 4));
478 i += 5;
479 } else {
480 acc.WriteByte ((byte) GetChar (bytes, i + 1, 2));
481 i += 2;
483 continue;
486 if (acc.Length > 0) {
487 output.Append (GetChars (acc, e));
488 acc.SetLength (0);
491 if (bytes [i] == '+') {
492 output.Append (' ');
493 } else {
494 output.Append ((char) bytes [i]);
498 if (acc.Length > 0) {
499 output.Append (GetChars (acc, e));
502 acc = null;
503 return output.ToString ();
506 public static byte [] UrlDecodeToBytes (byte [] bytes)
508 if (bytes == null)
509 return null;
511 return UrlDecodeToBytes (bytes, 0, bytes.Length);
514 public static byte [] UrlDecodeToBytes (string str)
516 return UrlDecodeToBytes (str, Encoding.UTF8);
519 public static byte [] UrlDecodeToBytes (string str, Encoding e)
521 if (str == null)
522 return null;
524 if (e == null)
525 throw new ArgumentNullException ("e");
527 return UrlDecodeToBytes (e.GetBytes (str));
530 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
532 if (bytes == null)
533 return null;
535 int len = bytes.Length;
536 if (offset < 0 || offset >= len)
537 throw new ArgumentOutOfRangeException("offset");
539 if (count < 0 || offset > len - count)
540 throw new ArgumentOutOfRangeException("count");
542 MemoryStream result = new MemoryStream ();
543 int end = offset + count;
544 for (int i = offset; i < end; i++){
545 char c = (char) bytes [i];
546 if (c == '+')
547 c = ' ';
548 else if (c == '%' && i < end - 2) {
549 c = GetChar (bytes, i, 2);
550 i += 2;
552 result.WriteByte ((byte) c);
555 return result.ToArray ();
558 public static string UrlEncode(string str)
560 return UrlEncode(str, Encoding.UTF8);
563 public static string UrlEncode (string s, Encoding Enc)
565 if (s == null)
566 return null;
568 if (s == "")
569 return "";
571 byte [] bytes = Enc.GetBytes (s);
572 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
575 public static string UrlEncode (byte [] bytes)
577 if (bytes == null)
578 return null;
580 if (bytes.Length == 0)
581 return "";
583 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
586 public static string UrlEncode (byte [] bytes, int offset, int count)
588 if (bytes == null)
589 return null;
591 if (bytes.Length == 0)
592 return "";
594 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
597 public static byte [] UrlEncodeToBytes (string str)
599 return UrlEncodeToBytes (str, Encoding.UTF8);
602 public static byte [] UrlEncodeToBytes (string str, Encoding e)
604 if (str == null)
605 return null;
607 if (str == "")
608 return new byte [0];
610 byte [] bytes = e.GetBytes (str);
611 return UrlEncodeToBytes (bytes, 0, bytes.Length);
614 public static byte [] UrlEncodeToBytes (byte [] bytes)
616 if (bytes == null)
617 return null;
619 if (bytes.Length == 0)
620 return new byte [0];
622 return UrlEncodeToBytes (bytes, 0, bytes.Length);
625 static char [] hexChars = "0123456789abcdef".ToCharArray ();
627 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
629 if (bytes == null)
630 return null;
632 int len = bytes.Length;
633 if (len == 0)
634 return new byte [0];
636 if (offset < 0 || offset >= len)
637 throw new ArgumentOutOfRangeException("offset");
639 if (count < 0 || count > len - offset)
640 throw new ArgumentOutOfRangeException("count");
642 MemoryStream result = new MemoryStream ();
643 int end = offset + count;
644 for (int i = offset; i < end; i++) {
645 char c = (char) bytes [i];
646 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
647 (c < 'A' && c > '9') ||
648 (c > 'Z' && c < 'a' && c != '_') ||
649 (c > 'z')) {
650 result.WriteByte ((byte) '%');
651 int idx = ((int) c) >> 4;
652 result.WriteByte ((byte) hexChars [idx]);
653 idx = ((int) c) & 0x0F;
654 result.WriteByte ((byte) hexChars [idx]);
655 } else {
656 result.WriteByte ((byte) c);
660 return result.ToArray ();
663 public static string UrlEncodeUnicode (string str)
665 if (str == null)
666 return null;
668 StringBuilder result = new StringBuilder ();
669 int end = str.Length;
670 for (int i = 0; i < end; i++) {
671 int idx;
672 char c = str [i];
673 if (c > 255) {
674 result.Append ("%u");
675 idx = ((int) c) >> 24;
676 result.Append (hexChars [idx]);
677 idx = (((int) c) >> 16) & 0x0F;
678 result.Append (hexChars [idx]);
679 idx = (((int) c) >> 8) & 0x0F;
680 result.Append (hexChars [idx]);
681 idx = ((int) c) & 0x0F;
682 result.Append (hexChars [idx]);
683 continue;
686 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
687 (c < 'A' && c > '9') ||
688 (c > 'Z' && c < 'a' && c != '_') ||
689 (c > 'z')) {
690 result.Append ('%');
691 idx = ((int) c) >> 4;
692 result.Append (hexChars [idx]);
693 idx = ((int) c) & 0x0F;
694 result.Append (hexChars [idx]);
695 continue;
698 result.Append (c);
701 return result.ToString ();
704 public static byte [] UrlEncodeUnicodeToBytes (string str)
706 if (str == null)
707 return null;
709 if (str == "")
710 return new byte [0];
712 return Encoding.ASCII.GetBytes (UrlEncodeUnicode (str));
715 /// <summary>
716 /// Decodes an HTML-encoded string and returns the decoded string.
717 /// </summary>
718 /// <param name="s">The HTML string to decode. </param>
719 /// <returns>The decoded text.</returns>
720 public static string HtmlDecode (string s)
722 if (s == null)
723 throw new ArgumentNullException ("s");
725 if (s.IndexOf ('&') == -1)
726 return s;
728 bool insideEntity = false; // used to indicate that we are in a potential entity
729 string entity = String.Empty;
730 StringBuilder output = new StringBuilder ();
731 int len = s.Length;
733 for (int i = 0; i < len; i++) {
734 char c = s [i];
735 switch (c) {
736 case '&' :
737 output.Append (entity);
738 entity = "&";
739 insideEntity = true;
740 break;
741 case ';' :
742 if (!insideEntity) {
743 output.Append (c);
744 break;
747 entity += c;
748 int length = entity.Length;
749 if (length >= 2 && entity[1] == '#' && entity[2] != ';')
750 entity = ((char) Int32.Parse (entity.Substring (2, entity.Length - 3))).ToString();
751 else if (length > 1 && Entities.ContainsKey (entity.Substring (1, entity.Length - 2)))
752 entity = Entities [entity.Substring (1, entity.Length - 2)].ToString ();
754 output.Append (entity);
755 entity = String.Empty;
756 insideEntity = false;
757 break;
758 default :
759 if (insideEntity)
760 entity += c;
761 else
762 output.Append (c);
763 break;
766 output.Append (entity);
767 return output.ToString ();
770 /// <summary>
771 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
772 /// </summary>
773 /// <param name="s">The HTML string to decode</param>
774 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
775 public static void HtmlDecode(string s, TextWriter output)
777 if (s != null)
778 output.Write (HtmlDecode (s));
781 /// <summary>
782 /// HTML-encodes a string and returns the encoded string.
783 /// </summary>
784 /// <param name="s">The text string to encode. </param>
785 /// <returns>The HTML-encoded text.</returns>
786 public static string HtmlEncode (string s)
788 if (s == null)
789 return null;
791 StringBuilder output = new StringBuilder ();
793 foreach (char c in s)
794 switch (c) {
795 case '&' :
796 output.Append ("&amp;");
797 break;
798 case '>' :
799 output.Append ("&gt;");
800 break;
801 case '<' :
802 output.Append ("&lt;");
803 break;
804 case '"' :
805 output.Append ("&quot;");
806 break;
807 default:
808 if ((int) c > 128) {
809 output.Append ("&#");
810 output.Append (((int) c).ToString ());
811 output.Append (";");
813 else
814 output.Append (c);
815 break;
817 return output.ToString ();
820 /// <summary>
821 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
822 /// </summary>
823 /// <param name="s">The string to encode. </param>
824 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
825 public static void HtmlEncode(string s, TextWriter output)
827 if (s != null)
828 output.Write (HtmlEncode (s));
831 #if NET_1_1
832 public static string UrlPathEncode (string s)
834 if (s == null)
835 return null;
837 int idx = s.IndexOf ("?");
838 string s2 = null;
839 if (idx != -1) {
840 s2 = s.Substring (0, idx-1);
841 s2 = UrlEncode (s2) + s.Substring (idx);
842 } else {
843 s2 = UrlEncode (s);
846 return s2;
848 #endif
849 #endregion // Methods