2004-03-01 Larry Ewing <lewing@ximian.com>
[mono-project.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
blobb5ee3b654499a93d23c272304f16221b239081a4
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 //
10 using System;
11 using System.Collections;
12 using System.Globalization;
13 using System.IO;
14 using System.Text;
15 using System.Web.Util;
17 namespace System.Web {
18 public sealed class HttpUtility {
20 #region Fields
22 const string _hex = "0123456789ABCDEF";
23 const string _chars = "<>;:.?=&@*+%/\\";
24 static Hashtable entities;
26 #endregion // Fields
28 #region Constructors
30 static HttpUtility ()
32 // Build the hash table of HTML entity references. This list comes
33 // from the HTML 4.01 W3C recommendation.
34 entities = new Hashtable ();
35 entities.Add ("nbsp", '\u00A0');
36 entities.Add ("iexcl", '\u00A1');
37 entities.Add ("cent", '\u00A2');
38 entities.Add ("pound", '\u00A3');
39 entities.Add ("curren", '\u00A4');
40 entities.Add ("yen", '\u00A5');
41 entities.Add ("brvbar", '\u00A6');
42 entities.Add ("sect", '\u00A7');
43 entities.Add ("uml", '\u00A8');
44 entities.Add ("copy", '\u00A9');
45 entities.Add ("ordf", '\u00AA');
46 entities.Add ("laquo", '\u00AB');
47 entities.Add ("not", '\u00AC');
48 entities.Add ("shy", '\u00AD');
49 entities.Add ("reg", '\u00AE');
50 entities.Add ("macr", '\u00AF');
51 entities.Add ("deg", '\u00B0');
52 entities.Add ("plusmn", '\u00B1');
53 entities.Add ("sup2", '\u00B2');
54 entities.Add ("sup3", '\u00B3');
55 entities.Add ("acute", '\u00B4');
56 entities.Add ("micro", '\u00B5');
57 entities.Add ("para", '\u00B6');
58 entities.Add ("middot", '\u00B7');
59 entities.Add ("cedil", '\u00B8');
60 entities.Add ("sup1", '\u00B9');
61 entities.Add ("ordm", '\u00BA');
62 entities.Add ("raquo", '\u00BB');
63 entities.Add ("frac14", '\u00BC');
64 entities.Add ("frac12", '\u00BD');
65 entities.Add ("frac34", '\u00BE');
66 entities.Add ("iquest", '\u00BF');
67 entities.Add ("Agrave", '\u00C0');
68 entities.Add ("Aacute", '\u00C1');
69 entities.Add ("Acirc", '\u00C2');
70 entities.Add ("Atilde", '\u00C3');
71 entities.Add ("Auml", '\u00C4');
72 entities.Add ("Aring", '\u00C5');
73 entities.Add ("AElig", '\u00C6');
74 entities.Add ("Ccedil", '\u00C7');
75 entities.Add ("Egrave", '\u00C8');
76 entities.Add ("Eacute", '\u00C9');
77 entities.Add ("Ecirc", '\u00CA');
78 entities.Add ("Euml", '\u00CB');
79 entities.Add ("Igrave", '\u00CC');
80 entities.Add ("Iacute", '\u00CD');
81 entities.Add ("Icirc", '\u00CE');
82 entities.Add ("Iuml", '\u00CF');
83 entities.Add ("ETH", '\u00D0');
84 entities.Add ("Ntilde", '\u00D1');
85 entities.Add ("Ograve", '\u00D2');
86 entities.Add ("Oacute", '\u00D3');
87 entities.Add ("Ocirc", '\u00D4');
88 entities.Add ("Otilde", '\u00D5');
89 entities.Add ("Ouml", '\u00D6');
90 entities.Add ("times", '\u00D7');
91 entities.Add ("Oslash", '\u00D8');
92 entities.Add ("Ugrave", '\u00D9');
93 entities.Add ("Uacute", '\u00DA');
94 entities.Add ("Ucirc", '\u00DB');
95 entities.Add ("Uuml", '\u00DC');
96 entities.Add ("Yacute", '\u00DD');
97 entities.Add ("THORN", '\u00DE');
98 entities.Add ("szlig", '\u00DF');
99 entities.Add ("agrave", '\u00E0');
100 entities.Add ("aacute", '\u00E1');
101 entities.Add ("acirc", '\u00E2');
102 entities.Add ("atilde", '\u00E3');
103 entities.Add ("auml", '\u00E4');
104 entities.Add ("aring", '\u00E5');
105 entities.Add ("aelig", '\u00E6');
106 entities.Add ("ccedil", '\u00E7');
107 entities.Add ("egrave", '\u00E8');
108 entities.Add ("eacute", '\u00E9');
109 entities.Add ("ecirc", '\u00EA');
110 entities.Add ("euml", '\u00EB');
111 entities.Add ("igrave", '\u00EC');
112 entities.Add ("iacute", '\u00ED');
113 entities.Add ("icirc", '\u00EE');
114 entities.Add ("iuml", '\u00EF');
115 entities.Add ("eth", '\u00F0');
116 entities.Add ("ntilde", '\u00F1');
117 entities.Add ("ograve", '\u00F2');
118 entities.Add ("oacute", '\u00F3');
119 entities.Add ("ocirc", '\u00F4');
120 entities.Add ("otilde", '\u00F5');
121 entities.Add ("ouml", '\u00F6');
122 entities.Add ("divide", '\u00F7');
123 entities.Add ("oslash", '\u00F8');
124 entities.Add ("ugrave", '\u00F9');
125 entities.Add ("uacute", '\u00FA');
126 entities.Add ("ucirc", '\u00FB');
127 entities.Add ("uuml", '\u00FC');
128 entities.Add ("yacute", '\u00FD');
129 entities.Add ("thorn", '\u00FE');
130 entities.Add ("yuml", '\u00FF');
131 entities.Add ("fnof", '\u0192');
132 entities.Add ("Alpha", '\u0391');
133 entities.Add ("Beta", '\u0392');
134 entities.Add ("Gamma", '\u0393');
135 entities.Add ("Delta", '\u0394');
136 entities.Add ("Epsilon", '\u0395');
137 entities.Add ("Zeta", '\u0396');
138 entities.Add ("Eta", '\u0397');
139 entities.Add ("Theta", '\u0398');
140 entities.Add ("Iota", '\u0399');
141 entities.Add ("Kappa", '\u039A');
142 entities.Add ("Lambda", '\u039B');
143 entities.Add ("Mu", '\u039C');
144 entities.Add ("Nu", '\u039D');
145 entities.Add ("Xi", '\u039E');
146 entities.Add ("Omicron", '\u039F');
147 entities.Add ("Pi", '\u03A0');
148 entities.Add ("Rho", '\u03A1');
149 entities.Add ("Sigma", '\u03A3');
150 entities.Add ("Tau", '\u03A4');
151 entities.Add ("Upsilon", '\u03A5');
152 entities.Add ("Phi", '\u03A6');
153 entities.Add ("Chi", '\u03A7');
154 entities.Add ("Psi", '\u03A8');
155 entities.Add ("Omega", '\u03A9');
156 entities.Add ("alpha", '\u03B1');
157 entities.Add ("beta", '\u03B2');
158 entities.Add ("gamma", '\u03B3');
159 entities.Add ("delta", '\u03B4');
160 entities.Add ("epsilon", '\u03B5');
161 entities.Add ("zeta", '\u03B6');
162 entities.Add ("eta", '\u03B7');
163 entities.Add ("theta", '\u03B8');
164 entities.Add ("iota", '\u03B9');
165 entities.Add ("kappa", '\u03BA');
166 entities.Add ("lambda", '\u03BB');
167 entities.Add ("mu", '\u03BC');
168 entities.Add ("nu", '\u03BD');
169 entities.Add ("xi", '\u03BE');
170 entities.Add ("omicron", '\u03BF');
171 entities.Add ("pi", '\u03C0');
172 entities.Add ("rho", '\u03C1');
173 entities.Add ("sigmaf", '\u03C2');
174 entities.Add ("sigma", '\u03C3');
175 entities.Add ("tau", '\u03C4');
176 entities.Add ("upsilon", '\u03C5');
177 entities.Add ("phi", '\u03C6');
178 entities.Add ("chi", '\u03C7');
179 entities.Add ("psi", '\u03C8');
180 entities.Add ("omega", '\u03C9');
181 entities.Add ("thetasym", '\u03D1');
182 entities.Add ("upsih", '\u03D2');
183 entities.Add ("piv", '\u03D6');
184 entities.Add ("bull", '\u2022');
185 entities.Add ("hellip", '\u2026');
186 entities.Add ("prime", '\u2032');
187 entities.Add ("Prime", '\u2033');
188 entities.Add ("oline", '\u203E');
189 entities.Add ("frasl", '\u2044');
190 entities.Add ("weierp", '\u2118');
191 entities.Add ("image", '\u2111');
192 entities.Add ("real", '\u211C');
193 entities.Add ("trade", '\u2122');
194 entities.Add ("alefsym", '\u2135');
195 entities.Add ("larr", '\u2190');
196 entities.Add ("uarr", '\u2191');
197 entities.Add ("rarr", '\u2192');
198 entities.Add ("darr", '\u2193');
199 entities.Add ("harr", '\u2194');
200 entities.Add ("crarr", '\u21B5');
201 entities.Add ("lArr", '\u21D0');
202 entities.Add ("uArr", '\u21D1');
203 entities.Add ("rArr", '\u21D2');
204 entities.Add ("dArr", '\u21D3');
205 entities.Add ("hArr", '\u21D4');
206 entities.Add ("forall", '\u2200');
207 entities.Add ("part", '\u2202');
208 entities.Add ("exist", '\u2203');
209 entities.Add ("empty", '\u2205');
210 entities.Add ("nabla", '\u2207');
211 entities.Add ("isin", '\u2208');
212 entities.Add ("notin", '\u2209');
213 entities.Add ("ni", '\u220B');
214 entities.Add ("prod", '\u220F');
215 entities.Add ("sum", '\u2211');
216 entities.Add ("minus", '\u2212');
217 entities.Add ("lowast", '\u2217');
218 entities.Add ("radic", '\u221A');
219 entities.Add ("prop", '\u221D');
220 entities.Add ("infin", '\u221E');
221 entities.Add ("ang", '\u2220');
222 entities.Add ("and", '\u2227');
223 entities.Add ("or", '\u2228');
224 entities.Add ("cap", '\u2229');
225 entities.Add ("cup", '\u222A');
226 entities.Add ("int", '\u222B');
227 entities.Add ("there4", '\u2234');
228 entities.Add ("sim", '\u223C');
229 entities.Add ("cong", '\u2245');
230 entities.Add ("asymp", '\u2248');
231 entities.Add ("ne", '\u2260');
232 entities.Add ("equiv", '\u2261');
233 entities.Add ("le", '\u2264');
234 entities.Add ("ge", '\u2265');
235 entities.Add ("sub", '\u2282');
236 entities.Add ("sup", '\u2283');
237 entities.Add ("nsub", '\u2284');
238 entities.Add ("sube", '\u2286');
239 entities.Add ("supe", '\u2287');
240 entities.Add ("oplus", '\u2295');
241 entities.Add ("otimes", '\u2297');
242 entities.Add ("perp", '\u22A5');
243 entities.Add ("sdot", '\u22C5');
244 entities.Add ("lceil", '\u2308');
245 entities.Add ("rceil", '\u2309');
246 entities.Add ("lfloor", '\u230A');
247 entities.Add ("rfloor", '\u230B');
248 entities.Add ("lang", '\u2329');
249 entities.Add ("rang", '\u232A');
250 entities.Add ("loz", '\u25CA');
251 entities.Add ("spades", '\u2660');
252 entities.Add ("clubs", '\u2663');
253 entities.Add ("hearts", '\u2665');
254 entities.Add ("diams", '\u2666');
255 entities.Add ("quot", '\u0022');
256 entities.Add ("amp", '\u0026');
257 entities.Add ("lt", '\u003C');
258 entities.Add ("gt", '\u003E');
259 entities.Add ("OElig", '\u0152');
260 entities.Add ("oelig", '\u0153');
261 entities.Add ("Scaron", '\u0160');
262 entities.Add ("scaron", '\u0161');
263 entities.Add ("Yuml", '\u0178');
264 entities.Add ("circ", '\u02C6');
265 entities.Add ("tilde", '\u02DC');
266 entities.Add ("ensp", '\u2002');
267 entities.Add ("emsp", '\u2003');
268 entities.Add ("thinsp", '\u2009');
269 entities.Add ("zwnj", '\u200C');
270 entities.Add ("zwj", '\u200D');
271 entities.Add ("lrm", '\u200E');
272 entities.Add ("rlm", '\u200F');
273 entities.Add ("ndash", '\u2013');
274 entities.Add ("mdash", '\u2014');
275 entities.Add ("lsquo", '\u2018');
276 entities.Add ("rsquo", '\u2019');
277 entities.Add ("sbquo", '\u201A');
278 entities.Add ("ldquo", '\u201C');
279 entities.Add ("rdquo", '\u201D');
280 entities.Add ("bdquo", '\u201E');
281 entities.Add ("dagger", '\u2020');
282 entities.Add ("Dagger", '\u2021');
283 entities.Add ("permil", '\u2030');
284 entities.Add ("lsaquo", '\u2039');
285 entities.Add ("rsaquo", '\u203A');
286 entities.Add ("euro", '\u20AC');
289 public HttpUtility ()
293 #endregion // Constructors
295 #region Methods
297 public static void HtmlAttributeEncode (string s, TextWriter output)
299 output.Write(HtmlAttributeEncode(s));
302 public static string HtmlAttributeEncode (string s)
304 if (null == s)
305 return null;
307 StringBuilder output = new StringBuilder ();
309 foreach (char c in s)
310 switch (c) {
311 case '&' :
312 output.Append ("&amp;");
313 break;
314 case '"' :
315 output.Append ("&quot;");
316 break;
317 default:
318 output.Append (c);
319 break;
322 return output.ToString();
325 public static string UrlDecode (string str)
327 return UrlDecode(str, Encoding.UTF8);
330 private static char [] GetChars (MemoryStream b, Encoding e)
332 return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
335 public static string UrlDecode (string s, Encoding e)
337 if (null == s)
338 return null;
340 if (e == null)
341 e = Encoding.UTF8;
343 StringBuilder output = new StringBuilder ();
344 long len = s.Length;
345 NumberStyles hexa = NumberStyles.HexNumber;
346 MemoryStream bytes = new MemoryStream ();
348 for (int i = 0; i < len; i++) {
349 if (s [i] == '%' && i + 2 < len) {
350 if (s [i + 1] == 'u' && i + 5 < len) {
351 if (bytes.Length > 0) {
352 output.Append (GetChars (bytes, e));
353 bytes.SetLength (0);
355 output.Append ((char) Int32.Parse (s.Substring (i + 2, 4), hexa));
356 i += 5;
357 } else {
358 bytes.WriteByte ((byte) Int32.Parse (s.Substring (i + 1, 2), hexa));
359 i += 2;
361 continue;
364 if (bytes.Length > 0) {
365 output.Append (GetChars (bytes, e));
366 bytes.SetLength (0);
369 if (s [i] == '+') {
370 output.Append (' ');
371 } else {
372 output.Append (s [i]);
376 if (bytes.Length > 0) {
377 output.Append (GetChars (bytes, e));
380 bytes = null;
381 return output.ToString ();
384 public static string UrlDecode (byte [] bytes, Encoding e)
386 if (bytes == null)
387 return null;
389 return UrlDecode (bytes, 0, bytes.Length, e);
392 private static int GetInt (byte b)
394 char c = Char.ToUpper ((char) b);
395 if (c >= '0' && c <= '9')
396 return c - '0';
398 if (c < 'A' || c > 'F')
399 return 0;
401 return (c - 'A' + 10);
404 private static char GetChar (byte [] bytes, int offset, int length)
406 int value = 0;
407 int end = length + offset;
408 for (int i = offset; i < end; i++)
409 value = (value << 4) + GetInt (bytes [offset]);
411 return (char) value;
414 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
416 if (bytes == null || count == 0)
417 return null;
419 if (bytes == null)
420 throw new ArgumentNullException ("bytes");
422 if (offset < 0 || offset > bytes.Length)
423 throw new ArgumentOutOfRangeException ("offset");
425 if (count < 0 || offset + count > bytes.Length)
426 throw new ArgumentOutOfRangeException ("count");
428 StringBuilder output = new StringBuilder ();
429 MemoryStream acc = new MemoryStream ();
431 int end = count + offset;
432 for (int i = offset; i < end; i++) {
433 if (bytes [i] == '%' && i + 2 < count) {
434 if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
435 if (acc.Length > 0) {
436 output.Append (GetChars (acc, e));
437 acc.SetLength (0);
439 output.Append (GetChar (bytes, offset + 2, 4));
440 i += 5;
441 } else {
442 acc.WriteByte ((byte) GetChar (bytes, offset + 1, 2));
443 i += 2;
445 continue;
448 if (acc.Length > 0) {
449 output.Append (GetChars (acc, e));
450 acc.SetLength (0);
453 if (bytes [i] == '+') {
454 output.Append (' ');
455 } else {
456 output.Append ((char) bytes [i]);
460 if (acc.Length > 0) {
461 output.Append (GetChars (acc, e));
464 acc = null;
465 return output.ToString ();
468 public static byte [] UrlDecodeToBytes (byte [] bytes)
470 if (bytes == null)
471 return null;
473 return UrlDecodeToBytes (bytes, 0, bytes.Length);
476 public static byte [] UrlDecodeToBytes (string str)
478 return UrlDecodeToBytes (str, Encoding.UTF8);
481 public static byte [] UrlDecodeToBytes (string str, Encoding e)
483 if (str == null)
484 return null;
486 if (e == null)
487 throw new ArgumentNullException ("e");
489 return UrlDecodeToBytes (e.GetBytes (str));
492 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
494 if (bytes == null)
495 return null;
497 int len = bytes.Length;
498 if (offset < 0 || offset >= len)
499 throw new ArgumentOutOfRangeException("offset");
501 if (count < 0 || offset > len - count)
502 throw new ArgumentOutOfRangeException("count");
504 MemoryStream result = new MemoryStream ();
505 int end = offset + count;
506 for (int i = offset; i < end; i++){
507 char c = (char) bytes [i];
508 if (c == '+')
509 c = ' ';
510 else if (c == '%' && i < end - 2) {
511 c = GetChar (bytes, i, 2);
512 i += 2;
514 result.WriteByte ((byte) c);
517 return result.ToArray ();
520 public static string UrlEncode(string str)
522 return UrlEncode(str, Encoding.UTF8);
525 public static string UrlEncode (string s, Encoding Enc)
527 if (s == null)
528 return null;
530 if (s == "")
531 return "";
533 byte [] bytes = Enc.GetBytes (s);
534 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
537 public static string UrlEncode (byte [] bytes)
539 if (bytes == null)
540 return null;
542 if (bytes.Length == 0)
543 return "";
545 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
548 public static string UrlEncode (byte [] bytes, int offset, int count)
550 if (bytes == null)
551 return null;
553 if (bytes.Length == 0)
554 return "";
556 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
559 public static byte [] UrlEncodeToBytes (string str)
561 return UrlEncodeToBytes (str, Encoding.UTF8);
564 public static byte [] UrlEncodeToBytes (string str, Encoding e)
566 if (str == null)
567 return null;
569 if (str == "")
570 return new byte [0];
572 byte [] bytes = e.GetBytes (str);
573 return UrlEncodeToBytes (bytes, 0, bytes.Length);
576 public static byte [] UrlEncodeToBytes (byte [] bytes)
578 if (bytes == null)
579 return null;
581 if (bytes.Length == 0)
582 return new byte [0];
584 return UrlEncodeToBytes (bytes, 0, bytes.Length);
587 static char [] hexChars = "0123456789ABCDEF".ToCharArray ();
589 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
591 if (bytes == null)
592 return null;
594 int len = bytes.Length;
595 if (len == 0)
596 return new byte [0];
598 if (offset < 0 || offset >= len)
599 throw new ArgumentOutOfRangeException("offset");
601 if (count < 0 || count > len - offset)
602 throw new ArgumentOutOfRangeException("count");
604 MemoryStream result = new MemoryStream ();
605 int end = offset + count;
606 for (int i = offset; i < end; i++) {
607 char c = (char) bytes [i];
608 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
609 (c < 'A' && c > '9') ||
610 (c > 'Z' && c < 'a' && c != '_') ||
611 (c > 'z')) {
612 result.WriteByte ((byte) '%');
613 int idx = ((int) c) >> 4;
614 result.WriteByte ((byte) hexChars [idx]);
615 idx = ((int) c) & 0x0F;
616 result.WriteByte ((byte) hexChars [idx]);
617 } else {
618 result.WriteByte ((byte) c);
622 return result.ToArray ();
625 public static string UrlEncodeUnicode (string str)
627 if (str == null)
628 return null;
630 StringBuilder result = new StringBuilder ();
631 int end = str.Length;
632 for (int i = 0; i < end; i++) {
633 int idx;
634 char c = str [i];
635 if (c > 255) {
636 result.Append ("%u");
637 idx = ((int) c) >> 24;
638 result.Append (hexChars [idx]);
639 idx = (((int) c) >> 16) & 0x0F;
640 result.Append (hexChars [idx]);
641 idx = (((int) c) >> 8) & 0x0F;
642 result.Append (hexChars [idx]);
643 idx = ((int) c) & 0x0F;
644 result.Append (hexChars [idx]);
645 continue;
648 if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||
649 (c < 'A' && c > '9') ||
650 (c > 'Z' && c < 'a' && c != '_') ||
651 (c > 'z')) {
652 result.Append ('%');
653 idx = ((int) c) >> 4;
654 result.Append (hexChars [idx]);
655 idx = ((int) c) & 0x0F;
656 result.Append (hexChars [idx]);
657 continue;
660 result.Append (c);
663 return result.ToString ();
666 public static byte [] UrlEncodeUnicodeToBytes (string str)
668 if (str == null)
669 return null;
671 if (str == "")
672 return new byte [0];
674 return Encoding.ASCII.GetBytes (UrlEncodeUnicode (str));
677 /// <summary>
678 /// Decodes an HTML-encoded string and returns the decoded string.
679 /// </summary>
680 /// <param name="s">The HTML string to decode. </param>
681 /// <returns>The decoded text.</returns>
682 public static string HtmlDecode (string s)
684 if (s == null)
685 throw new ArgumentNullException ("s");
687 bool insideEntity = false; // used to indicate that we are in a potential entity
688 string entity = String.Empty;
689 StringBuilder output = new StringBuilder ();
691 foreach (char c in s) {
692 switch (c) {
693 case '&' :
694 output.Append (entity);
695 entity = "&";
696 insideEntity = true;
697 break;
698 case ';' :
699 if (!insideEntity) {
700 output.Append (c);
701 break;
704 entity += c;
705 int length = entity.Length;
706 if (length >= 2 && entity[1] == '#' && entity[2] != ';')
707 entity = ((char) Int32.Parse (entity.Substring (2, entity.Length - 3))).ToString();
708 else if (length > 1 && entities.ContainsKey (entity.Substring (1, entity.Length - 2)))
709 entity = entities [entity.Substring (1, entity.Length - 2)].ToString ();
711 output.Append (entity);
712 entity = String.Empty;
713 insideEntity = false;
714 break;
715 default :
716 if (insideEntity)
717 entity += c;
718 else
719 output.Append (c);
720 break;
723 output.Append (entity);
724 return output.ToString ();
727 /// <summary>
728 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
729 /// </summary>
730 /// <param name="s">The HTML string to decode</param>
731 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
732 public static void HtmlDecode(string s, TextWriter output)
734 if (s != null)
735 output.Write (HtmlDecode (s));
738 /// <summary>
739 /// HTML-encodes a string and returns the encoded string.
740 /// </summary>
741 /// <param name="s">The text string to encode. </param>
742 /// <returns>The HTML-encoded text.</returns>
743 public static string HtmlEncode (string s)
745 if (s == null)
746 throw new ArgumentNullException ("s");
748 StringBuilder output = new StringBuilder ();
750 foreach (char c in s)
751 switch (c) {
752 case '&' :
753 output.Append ("&amp;");
754 break;
755 case '>' :
756 output.Append ("&gt;");
757 break;
758 case '<' :
759 output.Append ("&lt;");
760 break;
761 case '"' :
762 output.Append ("&quot;");
763 break;
764 default:
765 if ((int) c > 128) {
766 output.Append ("&#");
767 output.Append (((int) c).ToString ());
768 output.Append (";");
770 else
771 output.Append (c);
772 break;
774 return output.ToString ();
777 /// <summary>
778 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
779 /// </summary>
780 /// <param name="s">The string to encode. </param>
781 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
782 public static void HtmlEncode(string s, TextWriter output)
784 if (s != null)
785 output.Write (HtmlEncode (s));
788 #if NET_1_1
789 public string UrlPathEncode (string s)
791 if (s == null)
792 return null;
794 int idx = s.IndexOf ("?");
795 string s2 = null;
796 if (idx != -1) {
797 s2 = s.Substring (0, idx-1);
798 s2 = UrlEncode (s2) + s.Substring (idx);
799 } else {
800 s2 = UrlEncode (s);
803 return s2;
805 #endif
806 #endregion // Methods