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++) {
366 if (c == '&' || c == '"' || c == '<'
379 StringBuilder output = new StringBuilder ();
381 for (int i = 0; i < len; i++)
384 output.Append ("&");
387 output.Append (""");
390 output.Append ("<");
394 output.Append ("'");
398 output.Append (s [i]);
402 return output.ToString();
405 public static string UrlDecode (string str)
407 return UrlDecode(str, Encoding.UTF8);
410 static char [] GetChars (MemoryStream b, Encoding e)
412 return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
415 static void WriteCharBytes (IList buf, char ch, Encoding e)
418 foreach (byte b in e.GetBytes (new char[] { ch }))
424 public static string UrlDecode (string s, Encoding e)
429 if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
436 var bytes = new List <byte> ();
440 for (int i = 0; i < len; i++) {
442 if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
443 if (s [i + 1] == 'u' && i + 5 < len) {
444 // unicode hex sequence
445 xchar = GetChar (s, i + 2, 4);
447 WriteCharBytes (bytes, (char)xchar, e);
450 WriteCharBytes (bytes, '%', e);
451 } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
452 WriteCharBytes (bytes, (char)xchar, e);
455 WriteCharBytes (bytes, '%', e);
461 WriteCharBytes (bytes, ' ', e);
463 WriteCharBytes (bytes, ch, e);
466 byte[] buf = bytes.ToArray ();
468 return e.GetString (buf);
472 public static string UrlDecode (byte [] bytes, Encoding e)
477 return UrlDecode (bytes, 0, bytes.Length, e);
480 static int GetInt (byte b)
483 if (c >= '0' && c <= '9')
486 if (c >= 'a' && c <= 'f')
489 if (c >= 'A' && c <= 'F')
495 static int GetChar (byte [] bytes, int offset, int length)
498 int end = length + offset;
499 for (int i = offset; i < end; i++) {
500 int current = GetInt (bytes [i]);
503 value = (value << 4) + current;
509 static int GetChar (string str, int offset, int length)
512 int end = length + offset;
513 for (int i = offset; i < end; i++) {
518 int current = GetInt ((byte) c);
521 val = (val << 4) + current;
527 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
535 throw new ArgumentNullException ("bytes");
537 if (offset < 0 || offset > bytes.Length)
538 throw new ArgumentOutOfRangeException ("offset");
540 if (count < 0 || offset + count > bytes.Length)
541 throw new ArgumentOutOfRangeException ("count");
543 StringBuilder output = new StringBuilder ();
544 MemoryStream acc = new MemoryStream ();
546 int end = count + offset;
548 for (int i = offset; i < end; i++) {
549 if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
550 if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
551 if (acc.Length > 0) {
552 output.Append (GetChars (acc, e));
555 xchar = GetChar (bytes, i + 2, 4);
557 output.Append ((char) xchar);
561 } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
562 acc.WriteByte ((byte) xchar);
568 if (acc.Length > 0) {
569 output.Append (GetChars (acc, e));
573 if (bytes [i] == '+') {
576 output.Append ((char) bytes [i]);
580 if (acc.Length > 0) {
581 output.Append (GetChars (acc, e));
585 return output.ToString ();
588 public static byte [] UrlDecodeToBytes (byte [] bytes)
593 return UrlDecodeToBytes (bytes, 0, bytes.Length);
596 public static byte [] UrlDecodeToBytes (string str)
598 return UrlDecodeToBytes (str, Encoding.UTF8);
601 public static byte [] UrlDecodeToBytes (string str, Encoding e)
607 throw new ArgumentNullException ("e");
609 return UrlDecodeToBytes (e.GetBytes (str));
612 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
619 int len = bytes.Length;
620 if (offset < 0 || offset >= len)
621 throw new ArgumentOutOfRangeException("offset");
623 if (count < 0 || offset > len - count)
624 throw new ArgumentOutOfRangeException("count");
626 MemoryStream result = new MemoryStream ();
627 int end = offset + count;
628 for (int i = offset; i < end; i++){
629 char c = (char) bytes [i];
632 } else if (c == '%' && i < end - 2) {
633 int xchar = GetChar (bytes, i + 1, 2);
639 result.WriteByte ((byte) c);
642 return result.ToArray ();
645 public static string UrlEncode(string str)
647 return UrlEncode(str, Encoding.UTF8);
650 public static string UrlEncode (string s, Encoding Enc)
655 if (s == String.Empty)
658 bool needEncode = false;
660 for (int i = 0; i < len; i++) {
662 if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
674 // avoided GetByteCount call
675 byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
676 int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
677 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
680 public static string UrlEncode (byte [] bytes)
685 if (bytes.Length == 0)
688 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
691 public static string UrlEncode (byte [] bytes, int offset, int count)
696 if (bytes.Length == 0)
699 return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
702 public static byte [] UrlEncodeToBytes (string str)
704 return UrlEncodeToBytes (str, Encoding.UTF8);
707 public static byte [] UrlEncodeToBytes (string str, Encoding e)
715 byte [] bytes = e.GetBytes (str);
716 return UrlEncodeToBytes (bytes, 0, bytes.Length);
719 public static byte [] UrlEncodeToBytes (byte [] bytes)
724 if (bytes.Length == 0)
727 return UrlEncodeToBytes (bytes, 0, bytes.Length);
730 static char [] hexChars = "0123456789abcdef".ToCharArray ();
732 static bool NotEncoded (char c)
734 return (c == '!' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_'
741 static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
743 //FIXME: what happens when there is an internal error?
745 // throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
749 result.WriteByte ((byte)'%');
750 result.WriteByte ((byte)'u');
752 result.WriteByte ((byte)hexChars [idx]);
753 idx = (i >> 8) & 0x0F;
754 result.WriteByte ((byte)hexChars [idx]);
755 idx = (i >> 4) & 0x0F;
756 result.WriteByte ((byte)hexChars [idx]);
758 result.WriteByte ((byte)hexChars [idx]);
762 if (c > ' ' && NotEncoded (c)) {
763 result.WriteByte ((byte)c);
767 result.WriteByte ((byte)'+');
771 (c < 'A' && c > '9') ||
772 (c > 'Z' && c < 'a') ||
774 if (isUnicode && c > 127) {
775 result.WriteByte ((byte)'%');
776 result.WriteByte ((byte)'u');
777 result.WriteByte ((byte)'0');
778 result.WriteByte ((byte)'0');
781 result.WriteByte ((byte)'%');
783 int idx = ((int) c) >> 4;
784 result.WriteByte ((byte)hexChars [idx]);
785 idx = ((int) c) & 0x0F;
786 result.WriteByte ((byte)hexChars [idx]);
789 result.WriteByte ((byte)c);
792 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
797 int len = bytes.Length;
801 if (offset < 0 || offset >= len)
802 throw new ArgumentOutOfRangeException("offset");
804 if (count < 0 || count > len - offset)
805 throw new ArgumentOutOfRangeException("count");
807 MemoryStream result = new MemoryStream (count);
808 int end = offset + count;
809 for (int i = offset; i < end; i++)
810 UrlEncodeChar ((char)bytes [i], result, false);
812 return result.ToArray();
815 public static string UrlEncodeUnicode (string str)
820 return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
823 public static byte [] UrlEncodeUnicodeToBytes (string str)
831 MemoryStream result = new MemoryStream (str.Length);
832 foreach (char c in str){
833 UrlEncodeChar (c, result, true);
835 return result.ToArray ();
839 /// Decodes an HTML-encoded string and returns the decoded string.
841 /// <param name="s">The HTML string to decode. </param>
842 /// <returns>The decoded text.</returns>
843 public static string HtmlDecode (string s)
846 throw new ArgumentNullException ("s");
848 if (s.IndexOf ('&') == -1)
851 StringBuilder entity = new StringBuilder ();
852 StringBuilder output = new StringBuilder ();
855 // 1 -> right after '&'
856 // 2 -> between '&' and ';' but no '#'
857 // 3 -> '#' found after '&' and getting numbers
860 bool is_hex_value = false;
861 bool have_trailing_digits = false;
863 for (int i = 0; i < len; i++) {
877 if (have_trailing_digits) {
878 entity.Append (number.ToString (Helpers.InvariantCulture));
879 have_trailing_digits = false;
882 output.Append (entity.ToString ());
891 output.Append (entity.ToString ());
896 is_hex_value = false;
904 } else if (state == 2) {
907 string key = entity.ToString ();
908 if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
909 key = Entities [key.Substring (1, key.Length - 2)].ToString ();
915 } else if (state == 3) {
917 if (number > 65535) {
918 output.Append ("&#");
919 output.Append (number.ToString (Helpers.InvariantCulture));
922 output.Append ((char) number);
926 have_trailing_digits = false;
927 } else if (is_hex_value && Uri.IsHexDigit(c)) {
928 number = number * 16 + Uri.FromHex(c);
929 have_trailing_digits = true;
930 } else if (Char.IsDigit (c)) {
931 number = number * 10 + ((int) c - '0');
932 have_trailing_digits = true;
933 } else if (number == 0 && (c == 'x' || c == 'X')) {
937 if (have_trailing_digits) {
938 entity.Append (number.ToString (Helpers.InvariantCulture));
939 have_trailing_digits = false;
946 if (entity.Length > 0) {
947 output.Append (entity.ToString ());
948 } else if (have_trailing_digits) {
949 output.Append (number.ToString (Helpers.InvariantCulture));
951 return output.ToString ();
955 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
957 /// <param name="s">The HTML string to decode</param>
958 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
959 public static void HtmlDecode(string s, TextWriter output)
962 output.Write (HtmlDecode (s));
966 /// HTML-encodes a string and returns the encoded string.
968 /// <param name="s">The text string to encode. </param>
969 /// <returns>The HTML-encoded text.</returns>
970 public static string HtmlEncode (string s)
975 bool needEncode = false;
976 for (int i = 0; i < s.Length; i++) {
978 if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159
991 StringBuilder output = new StringBuilder ();
994 for (int i = 0; i < len; i++)
997 output.Append ("&");
1000 output.Append (">");
1003 output.Append ("<");
1006 output.Append (""");
1010 output.Append ("'");
1014 // MS starts encoding with &# from 160 and stops at 255.
1015 // We don't do that. One reason is the 65308/65310 unicode
1016 // characters that look like '<' and '>'.
1018 if (s [i] > 159 && s [i] < 256) {
1022 output.Append ("&#");
1023 output.Append (((int) s [i]).ToString (Helpers.InvariantCulture));
1024 output.Append (";");
1026 output.Append (s [i]);
1030 return output.ToString ();
1034 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
1036 /// <param name="s">The string to encode. </param>
1037 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
1038 public static void HtmlEncode(string s, TextWriter output)
1041 output.Write (HtmlEncode (s));
1044 public static string HtmlEncode (object value)
1049 return HtmlEncode (value.ToString ());
1052 public static string JavaScriptStringEncode (string value)
1054 return JavaScriptStringEncode (value, false);
1057 public static string JavaScriptStringEncode (string value, bool addDoubleQuotes)
1059 if (String.IsNullOrEmpty (value))
1060 return addDoubleQuotes ? "\"\"" : String.Empty;
1062 int len = value.Length;
1063 bool needEncode = false;
1065 for (int i = 0; i < len; i++) {
1068 if (c >= 0 && c <= 31 || c == 34 || c == 39 || c == 60 || c == 62 || c == 92) {
1075 return addDoubleQuotes ? "\"" + value + "\"" : value;
1077 var sb = new StringBuilder ();
1078 if (addDoubleQuotes)
1081 for (int i = 0; i < len; i++) {
1083 if (c >= 0 && c <= 7 || c == 11 || c >= 14 && c <= 31 || c == 39 || c == 60 || c == 62)
1084 sb.AppendFormat ("\\u{0:x4}", (int)c);
1085 else switch ((int)c) {
1120 if (addDoubleQuotes)
1123 return sb.ToString ();
1126 public static string UrlPathEncode (string s)
1128 if (s == null || s.Length == 0)
1131 MemoryStream result = new MemoryStream ();
1132 int length = s.Length;
1133 for (int i = 0; i < length; i++) {
1134 UrlPathEncodeChar (s [i], result);
1136 return Encoding.ASCII.GetString (result.ToArray ());
1139 static void UrlPathEncodeChar (char c, Stream result)
1141 if (c < 33 || c > 126) {
1142 byte [] bIn = Encoding.UTF8.GetBytes (c.ToString ());
1143 for (int i = 0; i < bIn.Length; i++) {
1144 result.WriteByte ((byte) '%');
1145 int idx = ((int) bIn [i]) >> 4;
1146 result.WriteByte ((byte) hexChars [idx]);
1147 idx = ((int) bIn [i]) & 0x0F;
1148 result.WriteByte ((byte) hexChars [idx]);
1151 else if (c == ' ') {
1152 result.WriteByte ((byte) '%');
1153 result.WriteByte ((byte) '2');
1154 result.WriteByte ((byte) '0');
1157 result.WriteByte ((byte) c);
1160 public static NameValueCollection ParseQueryString (string query)
1162 return ParseQueryString (query, Encoding.UTF8);
1165 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
1168 throw new ArgumentNullException ("query");
1169 if (encoding == null)
1170 throw new ArgumentNullException ("encoding");
1171 if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
1172 return new NameValueCollection ();
1173 if (query[0] == '?')
1174 query = query.Substring (1);
1176 NameValueCollection result = new HttpQSCollection ();
1177 ParseQueryString (query, encoding, result);
1181 internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
1183 if (query.Length == 0)
1186 string decoded = HtmlDecode (query);
1187 int decodedLength = decoded.Length;
1190 while (namePos <= decodedLength) {
1191 int valuePos = -1, valueEnd = -1;
1192 for (int q = namePos; q < decodedLength; q++) {
1193 if (valuePos == -1 && decoded [q] == '=') {
1195 } else if (decoded [q] == '&') {
1203 if (decoded [namePos] == '?')
1208 if (valuePos == -1) {
1212 name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
1216 valueEnd = decoded.Length;
1218 namePos = valueEnd + 1;
1220 value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
1222 result.Add (name, value);
1227 #endregion // Methods