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-2009 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.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
37 using System.Security.Permissions;
39 using System.Web.Util;
41 namespace System.Web {
44 // CAS - no InheritanceDemand here as the class is sealed
45 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 public sealed class HttpUtility
49 sealed class HttpQSCollection : NameValueCollection
51 public override string ToString ()
56 StringBuilder sb = new StringBuilder ();
57 string [] keys = AllKeys;
58 for (int i = 0; i < count; i++) {
59 sb.AppendFormat ("{0}={1}&", keys [i], this [keys [i]]);
63 return sb.ToString ();
68 static Hashtable entities;
69 static object lock_ = new object ();
73 static Hashtable Entities {
86 static void InitEntities ()
88 // Build the hash table of HTML entity references. This list comes
89 // from the HTML 4.01 W3C recommendation.
90 entities = new Hashtable ();
91 entities.Add ("nbsp", '\u00A0');
92 entities.Add ("iexcl", '\u00A1');
93 entities.Add ("cent", '\u00A2');
94 entities.Add ("pound", '\u00A3');
95 entities.Add ("curren", '\u00A4');
96 entities.Add ("yen", '\u00A5');
97 entities.Add ("brvbar", '\u00A6');
98 entities.Add ("sect", '\u00A7');
99 entities.Add ("uml", '\u00A8');
100 entities.Add ("copy", '\u00A9');
101 entities.Add ("ordf", '\u00AA');
102 entities.Add ("laquo", '\u00AB');
103 entities.Add ("not", '\u00AC');
104 entities.Add ("shy", '\u00AD');
105 entities.Add ("reg", '\u00AE');
106 entities.Add ("macr", '\u00AF');
107 entities.Add ("deg", '\u00B0');
108 entities.Add ("plusmn", '\u00B1');
109 entities.Add ("sup2", '\u00B2');
110 entities.Add ("sup3", '\u00B3');
111 entities.Add ("acute", '\u00B4');
112 entities.Add ("micro", '\u00B5');
113 entities.Add ("para", '\u00B6');
114 entities.Add ("middot", '\u00B7');
115 entities.Add ("cedil", '\u00B8');
116 entities.Add ("sup1", '\u00B9');
117 entities.Add ("ordm", '\u00BA');
118 entities.Add ("raquo", '\u00BB');
119 entities.Add ("frac14", '\u00BC');
120 entities.Add ("frac12", '\u00BD');
121 entities.Add ("frac34", '\u00BE');
122 entities.Add ("iquest", '\u00BF');
123 entities.Add ("Agrave", '\u00C0');
124 entities.Add ("Aacute", '\u00C1');
125 entities.Add ("Acirc", '\u00C2');
126 entities.Add ("Atilde", '\u00C3');
127 entities.Add ("Auml", '\u00C4');
128 entities.Add ("Aring", '\u00C5');
129 entities.Add ("AElig", '\u00C6');
130 entities.Add ("Ccedil", '\u00C7');
131 entities.Add ("Egrave", '\u00C8');
132 entities.Add ("Eacute", '\u00C9');
133 entities.Add ("Ecirc", '\u00CA');
134 entities.Add ("Euml", '\u00CB');
135 entities.Add ("Igrave", '\u00CC');
136 entities.Add ("Iacute", '\u00CD');
137 entities.Add ("Icirc", '\u00CE');
138 entities.Add ("Iuml", '\u00CF');
139 entities.Add ("ETH", '\u00D0');
140 entities.Add ("Ntilde", '\u00D1');
141 entities.Add ("Ograve", '\u00D2');
142 entities.Add ("Oacute", '\u00D3');
143 entities.Add ("Ocirc", '\u00D4');
144 entities.Add ("Otilde", '\u00D5');
145 entities.Add ("Ouml", '\u00D6');
146 entities.Add ("times", '\u00D7');
147 entities.Add ("Oslash", '\u00D8');
148 entities.Add ("Ugrave", '\u00D9');
149 entities.Add ("Uacute", '\u00DA');
150 entities.Add ("Ucirc", '\u00DB');
151 entities.Add ("Uuml", '\u00DC');
152 entities.Add ("Yacute", '\u00DD');
153 entities.Add ("THORN", '\u00DE');
154 entities.Add ("szlig", '\u00DF');
155 entities.Add ("agrave", '\u00E0');
156 entities.Add ("aacute", '\u00E1');
157 entities.Add ("acirc", '\u00E2');
158 entities.Add ("atilde", '\u00E3');
159 entities.Add ("auml", '\u00E4');
160 entities.Add ("aring", '\u00E5');
161 entities.Add ("aelig", '\u00E6');
162 entities.Add ("ccedil", '\u00E7');
163 entities.Add ("egrave", '\u00E8');
164 entities.Add ("eacute", '\u00E9');
165 entities.Add ("ecirc", '\u00EA');
166 entities.Add ("euml", '\u00EB');
167 entities.Add ("igrave", '\u00EC');
168 entities.Add ("iacute", '\u00ED');
169 entities.Add ("icirc", '\u00EE');
170 entities.Add ("iuml", '\u00EF');
171 entities.Add ("eth", '\u00F0');
172 entities.Add ("ntilde", '\u00F1');
173 entities.Add ("ograve", '\u00F2');
174 entities.Add ("oacute", '\u00F3');
175 entities.Add ("ocirc", '\u00F4');
176 entities.Add ("otilde", '\u00F5');
177 entities.Add ("ouml", '\u00F6');
178 entities.Add ("divide", '\u00F7');
179 entities.Add ("oslash", '\u00F8');
180 entities.Add ("ugrave", '\u00F9');
181 entities.Add ("uacute", '\u00FA');
182 entities.Add ("ucirc", '\u00FB');
183 entities.Add ("uuml", '\u00FC');
184 entities.Add ("yacute", '\u00FD');
185 entities.Add ("thorn", '\u00FE');
186 entities.Add ("yuml", '\u00FF');
187 entities.Add ("fnof", '\u0192');
188 entities.Add ("Alpha", '\u0391');
189 entities.Add ("Beta", '\u0392');
190 entities.Add ("Gamma", '\u0393');
191 entities.Add ("Delta", '\u0394');
192 entities.Add ("Epsilon", '\u0395');
193 entities.Add ("Zeta", '\u0396');
194 entities.Add ("Eta", '\u0397');
195 entities.Add ("Theta", '\u0398');
196 entities.Add ("Iota", '\u0399');
197 entities.Add ("Kappa", '\u039A');
198 entities.Add ("Lambda", '\u039B');
199 entities.Add ("Mu", '\u039C');
200 entities.Add ("Nu", '\u039D');
201 entities.Add ("Xi", '\u039E');
202 entities.Add ("Omicron", '\u039F');
203 entities.Add ("Pi", '\u03A0');
204 entities.Add ("Rho", '\u03A1');
205 entities.Add ("Sigma", '\u03A3');
206 entities.Add ("Tau", '\u03A4');
207 entities.Add ("Upsilon", '\u03A5');
208 entities.Add ("Phi", '\u03A6');
209 entities.Add ("Chi", '\u03A7');
210 entities.Add ("Psi", '\u03A8');
211 entities.Add ("Omega", '\u03A9');
212 entities.Add ("alpha", '\u03B1');
213 entities.Add ("beta", '\u03B2');
214 entities.Add ("gamma", '\u03B3');
215 entities.Add ("delta", '\u03B4');
216 entities.Add ("epsilon", '\u03B5');
217 entities.Add ("zeta", '\u03B6');
218 entities.Add ("eta", '\u03B7');
219 entities.Add ("theta", '\u03B8');
220 entities.Add ("iota", '\u03B9');
221 entities.Add ("kappa", '\u03BA');
222 entities.Add ("lambda", '\u03BB');
223 entities.Add ("mu", '\u03BC');
224 entities.Add ("nu", '\u03BD');
225 entities.Add ("xi", '\u03BE');
226 entities.Add ("omicron", '\u03BF');
227 entities.Add ("pi", '\u03C0');
228 entities.Add ("rho", '\u03C1');
229 entities.Add ("sigmaf", '\u03C2');
230 entities.Add ("sigma", '\u03C3');
231 entities.Add ("tau", '\u03C4');
232 entities.Add ("upsilon", '\u03C5');
233 entities.Add ("phi", '\u03C6');
234 entities.Add ("chi", '\u03C7');
235 entities.Add ("psi", '\u03C8');
236 entities.Add ("omega", '\u03C9');
237 entities.Add ("thetasym", '\u03D1');
238 entities.Add ("upsih", '\u03D2');
239 entities.Add ("piv", '\u03D6');
240 entities.Add ("bull", '\u2022');
241 entities.Add ("hellip", '\u2026');
242 entities.Add ("prime", '\u2032');
243 entities.Add ("Prime", '\u2033');
244 entities.Add ("oline", '\u203E');
245 entities.Add ("frasl", '\u2044');
246 entities.Add ("weierp", '\u2118');
247 entities.Add ("image", '\u2111');
248 entities.Add ("real", '\u211C');
249 entities.Add ("trade", '\u2122');
250 entities.Add ("alefsym", '\u2135');
251 entities.Add ("larr", '\u2190');
252 entities.Add ("uarr", '\u2191');
253 entities.Add ("rarr", '\u2192');
254 entities.Add ("darr", '\u2193');
255 entities.Add ("harr", '\u2194');
256 entities.Add ("crarr", '\u21B5');
257 entities.Add ("lArr", '\u21D0');
258 entities.Add ("uArr", '\u21D1');
259 entities.Add ("rArr", '\u21D2');
260 entities.Add ("dArr", '\u21D3');
261 entities.Add ("hArr", '\u21D4');
262 entities.Add ("forall", '\u2200');
263 entities.Add ("part", '\u2202');
264 entities.Add ("exist", '\u2203');
265 entities.Add ("empty", '\u2205');
266 entities.Add ("nabla", '\u2207');
267 entities.Add ("isin", '\u2208');
268 entities.Add ("notin", '\u2209');
269 entities.Add ("ni", '\u220B');
270 entities.Add ("prod", '\u220F');
271 entities.Add ("sum", '\u2211');
272 entities.Add ("minus", '\u2212');
273 entities.Add ("lowast", '\u2217');
274 entities.Add ("radic", '\u221A');
275 entities.Add ("prop", '\u221D');
276 entities.Add ("infin", '\u221E');
277 entities.Add ("ang", '\u2220');
278 entities.Add ("and", '\u2227');
279 entities.Add ("or", '\u2228');
280 entities.Add ("cap", '\u2229');
281 entities.Add ("cup", '\u222A');
282 entities.Add ("int", '\u222B');
283 entities.Add ("there4", '\u2234');
284 entities.Add ("sim", '\u223C');
285 entities.Add ("cong", '\u2245');
286 entities.Add ("asymp", '\u2248');
287 entities.Add ("ne", '\u2260');
288 entities.Add ("equiv", '\u2261');
289 entities.Add ("le", '\u2264');
290 entities.Add ("ge", '\u2265');
291 entities.Add ("sub", '\u2282');
292 entities.Add ("sup", '\u2283');
293 entities.Add ("nsub", '\u2284');
294 entities.Add ("sube", '\u2286');
295 entities.Add ("supe", '\u2287');
296 entities.Add ("oplus", '\u2295');
297 entities.Add ("otimes", '\u2297');
298 entities.Add ("perp", '\u22A5');
299 entities.Add ("sdot", '\u22C5');
300 entities.Add ("lceil", '\u2308');
301 entities.Add ("rceil", '\u2309');
302 entities.Add ("lfloor", '\u230A');
303 entities.Add ("rfloor", '\u230B');
304 entities.Add ("lang", '\u2329');
305 entities.Add ("rang", '\u232A');
306 entities.Add ("loz", '\u25CA');
307 entities.Add ("spades", '\u2660');
308 entities.Add ("clubs", '\u2663');
309 entities.Add ("hearts", '\u2665');
310 entities.Add ("diams", '\u2666');
311 entities.Add ("quot", '\u0022');
312 entities.Add ("amp", '\u0026');
313 entities.Add ("lt", '\u003C');
314 entities.Add ("gt", '\u003E');
315 entities.Add ("OElig", '\u0152');
316 entities.Add ("oelig", '\u0153');
317 entities.Add ("Scaron", '\u0160');
318 entities.Add ("scaron", '\u0161');
319 entities.Add ("Yuml", '\u0178');
320 entities.Add ("circ", '\u02C6');
321 entities.Add ("tilde", '\u02DC');
322 entities.Add ("ensp", '\u2002');
323 entities.Add ("emsp", '\u2003');
324 entities.Add ("thinsp", '\u2009');
325 entities.Add ("zwnj", '\u200C');
326 entities.Add ("zwj", '\u200D');
327 entities.Add ("lrm", '\u200E');
328 entities.Add ("rlm", '\u200F');
329 entities.Add ("ndash", '\u2013');
330 entities.Add ("mdash", '\u2014');
331 entities.Add ("lsquo", '\u2018');
332 entities.Add ("rsquo", '\u2019');
333 entities.Add ("sbquo", '\u201A');
334 entities.Add ("ldquo", '\u201C');
335 entities.Add ("rdquo", '\u201D');
336 entities.Add ("bdquo", '\u201E');
337 entities.Add ("dagger", '\u2020');
338 entities.Add ("Dagger", '\u2021');
339 entities.Add ("permil", '\u2030');
340 entities.Add ("lsaquo", '\u2039');
341 entities.Add ("rsaquo", '\u203A');
342 entities.Add ("euro", '\u20AC');
345 public HttpUtility ()
349 #endregion // Constructors
353 public static void HtmlAttributeEncode (string s, TextWriter output)
355 output.Write(HtmlAttributeEncode(s));
358 public static string HtmlAttributeEncode (string s)
363 bool needEncode = false;
364 for (int i = 0; i < s.Length; i++) {
365 if (s [i] == '&' || s [i] == '"' || s [i] == '<') {
374 StringBuilder output = new StringBuilder ();
376 for (int i = 0; i < len; i++)
379 output.Append ("&");
382 output.Append (""");
385 output.Append ("<");
388 output.Append (s [i]);
392 return output.ToString();
395 public static string UrlDecode (string str)
397 return UrlDecode(str, Encoding.UTF8);
400 static char [] GetChars (MemoryStream b, Encoding e)
402 return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
405 static void WriteCharBytes (IList buf, char ch, Encoding e)
408 foreach (byte b in e.GetBytes (new char[] { ch }))
414 public static string UrlDecode (string s, Encoding e)
419 if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
426 var bytes = new List <byte> ();
430 for (int i = 0; i < len; i++) {
432 if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
433 if (s [i + 1] == 'u' && i + 5 < len) {
434 // unicode hex sequence
435 xchar = GetChar (s, i + 2, 4);
437 WriteCharBytes (bytes, (char)xchar, e);
440 WriteCharBytes (bytes, '%', e);
441 } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
442 WriteCharBytes (bytes, (char)xchar, e);
445 WriteCharBytes (bytes, '%', e);
451 WriteCharBytes (bytes, ' ', e);
453 WriteCharBytes (bytes, ch, e);
456 byte[] buf = bytes.ToArray ();
458 return e.GetString (buf);
462 public static string UrlDecode (byte [] bytes, Encoding e)
467 return UrlDecode (bytes, 0, bytes.Length, e);
470 static int GetInt (byte b)
473 if (c >= '0' && c <= '9')
476 if (c >= 'a' && c <= 'f')
479 if (c >= 'A' && c <= 'F')
485 static int GetChar (byte [] bytes, int offset, int length)
488 int end = length + offset;
489 for (int i = offset; i < end; i++) {
490 int current = GetInt (bytes [i]);
493 value = (value << 4) + current;
499 static int GetChar (string str, int offset, int length)
502 int end = length + offset;
503 for (int i = offset; i < end; i++) {
508 int current = GetInt ((byte) c);
511 val = (val << 4) + current;
517 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
525 throw new ArgumentNullException ("bytes");
527 if (offset < 0 || offset > bytes.Length)
528 throw new ArgumentOutOfRangeException ("offset");
530 if (count < 0 || offset + count > bytes.Length)
531 throw new ArgumentOutOfRangeException ("count");
533 StringBuilder output = new StringBuilder ();
534 MemoryStream acc = new MemoryStream ();
536 int end = count + offset;
538 for (int i = offset; i < end; i++) {
539 if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
540 if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
541 if (acc.Length > 0) {
542 output.Append (GetChars (acc, e));
545 xchar = GetChar (bytes, i + 2, 4);
547 output.Append ((char) xchar);
551 } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
552 acc.WriteByte ((byte) xchar);
558 if (acc.Length > 0) {
559 output.Append (GetChars (acc, e));
563 if (bytes [i] == '+') {
566 output.Append ((char) bytes [i]);
570 if (acc.Length > 0) {
571 output.Append (GetChars (acc, e));
575 return output.ToString ();
578 public static byte [] UrlDecodeToBytes (byte [] bytes)
583 return UrlDecodeToBytes (bytes, 0, bytes.Length);
586 public static byte [] UrlDecodeToBytes (string str)
588 return UrlDecodeToBytes (str, Encoding.UTF8);
591 public static byte [] UrlDecodeToBytes (string str, Encoding e)
597 throw new ArgumentNullException ("e");
599 return UrlDecodeToBytes (e.GetBytes (str));
602 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
609 int len = bytes.Length;
610 if (offset < 0 || offset >= len)
611 throw new ArgumentOutOfRangeException("offset");
613 if (count < 0 || offset > len - count)
614 throw new ArgumentOutOfRangeException("count");
616 MemoryStream result = new MemoryStream ();
617 int end = offset + count;
618 for (int i = offset; i < end; i++){
619 char c = (char) bytes [i];
622 } else if (c == '%' && i < end - 2) {
623 int xchar = GetChar (bytes, i + 1, 2);
629 result.WriteByte ((byte) c);
632 return result.ToArray ();
635 public static string UrlEncode(string str)
637 return UrlEncode(str, Encoding.UTF8);
640 public static string UrlEncode (string s, Encoding Enc)
648 bool needEncode = false;
650 for (int i = 0; i < len; i++) {
652 if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
664 // avoided GetByteCount call
665 byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
666 int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
667 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
670 public static string UrlEncode (byte [] bytes)
675 if (bytes.Length == 0)
678 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
681 public static string UrlEncode (byte [] bytes, int offset, int count)
686 if (bytes.Length == 0)
689 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
692 public static byte [] UrlEncodeToBytes (string str)
694 return UrlEncodeToBytes (str, Encoding.UTF8);
697 public static byte [] UrlEncodeToBytes (string str, Encoding e)
705 byte [] bytes = e.GetBytes (str);
706 return UrlEncodeToBytes (bytes, 0, bytes.Length);
709 public static byte [] UrlEncodeToBytes (byte [] bytes)
714 if (bytes.Length == 0)
717 return UrlEncodeToBytes (bytes, 0, bytes.Length);
720 static char [] hexChars = "0123456789abcdef".ToCharArray ();
722 static bool NotEncoded (char c)
724 return (c == '!' || c == '\'' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_');
727 static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
729 //FIXME: what happens when there is an internal error?
731 // throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
735 result.WriteByte ((byte)'%');
736 result.WriteByte ((byte)'u');
738 result.WriteByte ((byte)hexChars [idx]);
739 idx = (i >> 8) & 0x0F;
740 result.WriteByte ((byte)hexChars [idx]);
741 idx = (i >> 4) & 0x0F;
742 result.WriteByte ((byte)hexChars [idx]);
744 result.WriteByte ((byte)hexChars [idx]);
748 if (c > ' ' && NotEncoded (c)) {
749 result.WriteByte ((byte)c);
753 result.WriteByte ((byte)'+');
757 (c < 'A' && c > '9') ||
758 (c > 'Z' && c < 'a') ||
760 if (isUnicode && c > 127) {
761 result.WriteByte ((byte)'%');
762 result.WriteByte ((byte)'u');
763 result.WriteByte ((byte)'0');
764 result.WriteByte ((byte)'0');
767 result.WriteByte ((byte)'%');
769 int idx = ((int) c) >> 4;
770 result.WriteByte ((byte)hexChars [idx]);
771 idx = ((int) c) & 0x0F;
772 result.WriteByte ((byte)hexChars [idx]);
775 result.WriteByte ((byte)c);
778 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
783 int len = bytes.Length;
787 if (offset < 0 || offset >= len)
788 throw new ArgumentOutOfRangeException("offset");
790 if (count < 0 || count > len - offset)
791 throw new ArgumentOutOfRangeException("count");
793 MemoryStream result = new MemoryStream (count);
794 int end = offset + count;
795 for (int i = offset; i < end; i++)
796 UrlEncodeChar ((char)bytes [i], result, false);
798 return result.ToArray();
801 public static string UrlEncodeUnicode (string str)
806 return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
809 public static byte [] UrlEncodeUnicodeToBytes (string str)
817 MemoryStream result = new MemoryStream (str.Length);
818 foreach (char c in str){
819 UrlEncodeChar (c, result, true);
821 return result.ToArray ();
825 /// Decodes an HTML-encoded string and returns the decoded string.
827 /// <param name="s">The HTML string to decode. </param>
828 /// <returns>The decoded text.</returns>
829 public static string HtmlDecode (string s)
832 throw new ArgumentNullException ("s");
834 if (s.IndexOf ('&') == -1)
837 StringBuilder entity = new StringBuilder ();
838 StringBuilder output = new StringBuilder ();
841 // 1 -> right after '&'
842 // 2 -> between '&' and ';' but no '#'
843 // 3 -> '#' found after '&' and getting numbers
846 bool have_trailing_digits = false;
848 for (int i = 0; i < len; i++) {
862 if (have_trailing_digits) {
863 entity.Append (number.ToString (Helpers.InvariantCulture));
864 have_trailing_digits = false;
867 output.Append (entity.ToString ());
876 output.Append (entity.ToString ());
888 } else if (state == 2) {
891 string key = entity.ToString ();
892 if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
893 key = Entities [key.Substring (1, key.Length - 2)].ToString ();
899 } else if (state == 3) {
901 if (number > 65535) {
902 output.Append ("&#");
903 output.Append (number.ToString (Helpers.InvariantCulture));
906 output.Append ((char) number);
910 have_trailing_digits = false;
911 } else if (Char.IsDigit (c)) {
912 number = number * 10 + ((int) c - '0');
913 have_trailing_digits = true;
916 if (have_trailing_digits) {
917 entity.Append (number.ToString (Helpers.InvariantCulture));
918 have_trailing_digits = false;
925 if (entity.Length > 0) {
926 output.Append (entity.ToString ());
927 } else if (have_trailing_digits) {
928 output.Append (number.ToString (Helpers.InvariantCulture));
930 return output.ToString ();
934 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
936 /// <param name="s">The HTML string to decode</param>
937 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
938 public static void HtmlDecode(string s, TextWriter output)
941 output.Write (HtmlDecode (s));
945 /// HTML-encodes a string and returns the encoded string.
947 /// <param name="s">The text string to encode. </param>
948 /// <returns>The HTML-encoded text.</returns>
949 public static string HtmlEncode (string s)
954 bool needEncode = false;
955 for (int i = 0; i < s.Length; i++) {
957 if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
966 StringBuilder output = new StringBuilder ();
969 for (int i = 0; i < len; i++)
972 output.Append ("&");
975 output.Append (">");
978 output.Append ("<");
981 output.Append (""");
984 // MS starts encoding with &# from 160 and stops at 255.
985 // We don't do that. One reason is the 65308/65310 unicode
986 // characters that look like '<' and '>'.
988 if (s [i] > 159 && s [i] < 256) {
992 output.Append ("&#");
993 output.Append (((int) s [i]).ToString (Helpers.InvariantCulture));
996 output.Append (s [i]);
1000 return output.ToString ();
1004 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
1006 /// <param name="s">The string to encode. </param>
1007 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
1008 public static void HtmlEncode(string s, TextWriter output)
1011 output.Write (HtmlEncode (s));
1014 public static string UrlPathEncode (string s)
1016 if (s == null || s.Length == 0)
1019 MemoryStream result = new MemoryStream ();
1020 int length = s.Length;
1021 for (int i = 0; i < length; i++) {
1022 UrlPathEncodeChar (s [i], result);
1024 return Encoding.ASCII.GetString (result.ToArray ());
1027 static void UrlPathEncodeChar (char c, Stream result)
1029 if (c < 33 || c > 126) {
1030 byte [] bIn = Encoding.UTF8.GetBytes (c.ToString ());
1031 for (int i = 0; i < bIn.Length; i++) {
1032 result.WriteByte ((byte) '%');
1033 int idx = ((int) bIn [i]) >> 4;
1034 result.WriteByte ((byte) hexChars [idx]);
1035 idx = ((int) bIn [i]) & 0x0F;
1036 result.WriteByte ((byte) hexChars [idx]);
1039 else if (c == ' ') {
1040 result.WriteByte ((byte) '%');
1041 result.WriteByte ((byte) '2');
1042 result.WriteByte ((byte) '0');
1045 result.WriteByte ((byte) c);
1048 public static NameValueCollection ParseQueryString (string query)
1050 return ParseQueryString (query, Encoding.UTF8);
1053 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
1056 throw new ArgumentNullException ("query");
1057 if (encoding == null)
1058 throw new ArgumentNullException ("encoding");
1059 if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
1060 return new NameValueCollection ();
1061 if (query[0] == '?')
1062 query = query.Substring (1);
1064 NameValueCollection result = new HttpQSCollection ();
1065 ParseQueryString (query, encoding, result);
1069 internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
1071 if (query.Length == 0)
1074 string decoded = HtmlDecode (query);
1075 int decodedLength = decoded.Length;
1078 while (namePos <= decodedLength) {
1079 int valuePos = -1, valueEnd = -1;
1080 for (int q = namePos; q < decodedLength; q++) {
1081 if (valuePos == -1 && decoded [q] == '=') {
1083 } else if (decoded [q] == '&') {
1091 if (decoded [namePos] == '?')
1096 if (valuePos == -1) {
1100 name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
1104 valueEnd = decoded.Length;
1106 namePos = valueEnd + 1;
1108 value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
1110 result.Add (name, value);
1115 #endregion // Methods