initial implementation of 'unify request'
[mono.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
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 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Globalization;
35 using System.IO;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Web.Util;
39
40 namespace System.Web {
41
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class HttpUtility {
45                 #region Fields
46         
47                 static Hashtable entities;
48                 static object lock_ = new object ();
49         
50                 #endregion // Fields
51         
52                 static Hashtable Entities {
53                         get {
54                                 lock (lock_) {
55                                         if (entities == null)
56                                                 InitEntities ();
57
58                                         return entities;
59                                 }
60                         }
61                 }
62                 
63                 #region Constructors
64
65                 static void InitEntities ()
66                 {
67                         // Build the hash table of HTML entity references.  This list comes
68                         // from the HTML 4.01 W3C recommendation.
69                         entities = new Hashtable ();
70                         entities.Add ("nbsp", '\u00A0');
71                         entities.Add ("iexcl", '\u00A1');
72                         entities.Add ("cent", '\u00A2');
73                         entities.Add ("pound", '\u00A3');
74                         entities.Add ("curren", '\u00A4');
75                         entities.Add ("yen", '\u00A5');
76                         entities.Add ("brvbar", '\u00A6');
77                         entities.Add ("sect", '\u00A7');
78                         entities.Add ("uml", '\u00A8');
79                         entities.Add ("copy", '\u00A9');
80                         entities.Add ("ordf", '\u00AA');
81                         entities.Add ("laquo", '\u00AB');
82                         entities.Add ("not", '\u00AC');
83                         entities.Add ("shy", '\u00AD');
84                         entities.Add ("reg", '\u00AE');
85                         entities.Add ("macr", '\u00AF');
86                         entities.Add ("deg", '\u00B0');
87                         entities.Add ("plusmn", '\u00B1');
88                         entities.Add ("sup2", '\u00B2');
89                         entities.Add ("sup3", '\u00B3');
90                         entities.Add ("acute", '\u00B4');
91                         entities.Add ("micro", '\u00B5');
92                         entities.Add ("para", '\u00B6');
93                         entities.Add ("middot", '\u00B7');
94                         entities.Add ("cedil", '\u00B8');
95                         entities.Add ("sup1", '\u00B9');
96                         entities.Add ("ordm", '\u00BA');
97                         entities.Add ("raquo", '\u00BB');
98                         entities.Add ("frac14", '\u00BC');
99                         entities.Add ("frac12", '\u00BD');
100                         entities.Add ("frac34", '\u00BE');
101                         entities.Add ("iquest", '\u00BF');
102                         entities.Add ("Agrave", '\u00C0');
103                         entities.Add ("Aacute", '\u00C1');
104                         entities.Add ("Acirc", '\u00C2');
105                         entities.Add ("Atilde", '\u00C3');
106                         entities.Add ("Auml", '\u00C4');
107                         entities.Add ("Aring", '\u00C5');
108                         entities.Add ("AElig", '\u00C6');
109                         entities.Add ("Ccedil", '\u00C7');
110                         entities.Add ("Egrave", '\u00C8');
111                         entities.Add ("Eacute", '\u00C9');
112                         entities.Add ("Ecirc", '\u00CA');
113                         entities.Add ("Euml", '\u00CB');
114                         entities.Add ("Igrave", '\u00CC');
115                         entities.Add ("Iacute", '\u00CD');
116                         entities.Add ("Icirc", '\u00CE');
117                         entities.Add ("Iuml", '\u00CF');
118                         entities.Add ("ETH", '\u00D0');
119                         entities.Add ("Ntilde", '\u00D1');
120                         entities.Add ("Ograve", '\u00D2');
121                         entities.Add ("Oacute", '\u00D3');
122                         entities.Add ("Ocirc", '\u00D4');
123                         entities.Add ("Otilde", '\u00D5');
124                         entities.Add ("Ouml", '\u00D6');
125                         entities.Add ("times", '\u00D7');
126                         entities.Add ("Oslash", '\u00D8');
127                         entities.Add ("Ugrave", '\u00D9');
128                         entities.Add ("Uacute", '\u00DA');
129                         entities.Add ("Ucirc", '\u00DB');
130                         entities.Add ("Uuml", '\u00DC');
131                         entities.Add ("Yacute", '\u00DD');
132                         entities.Add ("THORN", '\u00DE');
133                         entities.Add ("szlig", '\u00DF');
134                         entities.Add ("agrave", '\u00E0');
135                         entities.Add ("aacute", '\u00E1');
136                         entities.Add ("acirc", '\u00E2');
137                         entities.Add ("atilde", '\u00E3');
138                         entities.Add ("auml", '\u00E4');
139                         entities.Add ("aring", '\u00E5');
140                         entities.Add ("aelig", '\u00E6');
141                         entities.Add ("ccedil", '\u00E7');
142                         entities.Add ("egrave", '\u00E8');
143                         entities.Add ("eacute", '\u00E9');
144                         entities.Add ("ecirc", '\u00EA');
145                         entities.Add ("euml", '\u00EB');
146                         entities.Add ("igrave", '\u00EC');
147                         entities.Add ("iacute", '\u00ED');
148                         entities.Add ("icirc", '\u00EE');
149                         entities.Add ("iuml", '\u00EF');
150                         entities.Add ("eth", '\u00F0');
151                         entities.Add ("ntilde", '\u00F1');
152                         entities.Add ("ograve", '\u00F2');
153                         entities.Add ("oacute", '\u00F3');
154                         entities.Add ("ocirc", '\u00F4');
155                         entities.Add ("otilde", '\u00F5');
156                         entities.Add ("ouml", '\u00F6');
157                         entities.Add ("divide", '\u00F7');
158                         entities.Add ("oslash", '\u00F8');
159                         entities.Add ("ugrave", '\u00F9');
160                         entities.Add ("uacute", '\u00FA');
161                         entities.Add ("ucirc", '\u00FB');
162                         entities.Add ("uuml", '\u00FC');
163                         entities.Add ("yacute", '\u00FD');
164                         entities.Add ("thorn", '\u00FE');
165                         entities.Add ("yuml", '\u00FF');
166                         entities.Add ("fnof", '\u0192');
167                         entities.Add ("Alpha", '\u0391');
168                         entities.Add ("Beta", '\u0392');
169                         entities.Add ("Gamma", '\u0393');
170                         entities.Add ("Delta", '\u0394');
171                         entities.Add ("Epsilon", '\u0395');
172                         entities.Add ("Zeta", '\u0396');
173                         entities.Add ("Eta", '\u0397');
174                         entities.Add ("Theta", '\u0398');
175                         entities.Add ("Iota", '\u0399');
176                         entities.Add ("Kappa", '\u039A');
177                         entities.Add ("Lambda", '\u039B');
178                         entities.Add ("Mu", '\u039C');
179                         entities.Add ("Nu", '\u039D');
180                         entities.Add ("Xi", '\u039E');
181                         entities.Add ("Omicron", '\u039F');
182                         entities.Add ("Pi", '\u03A0');
183                         entities.Add ("Rho", '\u03A1');
184                         entities.Add ("Sigma", '\u03A3');
185                         entities.Add ("Tau", '\u03A4');
186                         entities.Add ("Upsilon", '\u03A5');
187                         entities.Add ("Phi", '\u03A6');
188                         entities.Add ("Chi", '\u03A7');
189                         entities.Add ("Psi", '\u03A8');
190                         entities.Add ("Omega", '\u03A9');
191                         entities.Add ("alpha", '\u03B1');
192                         entities.Add ("beta", '\u03B2');
193                         entities.Add ("gamma", '\u03B3');
194                         entities.Add ("delta", '\u03B4');
195                         entities.Add ("epsilon", '\u03B5');
196                         entities.Add ("zeta", '\u03B6');
197                         entities.Add ("eta", '\u03B7');
198                         entities.Add ("theta", '\u03B8');
199                         entities.Add ("iota", '\u03B9');
200                         entities.Add ("kappa", '\u03BA');
201                         entities.Add ("lambda", '\u03BB');
202                         entities.Add ("mu", '\u03BC');
203                         entities.Add ("nu", '\u03BD');
204                         entities.Add ("xi", '\u03BE');
205                         entities.Add ("omicron", '\u03BF');
206                         entities.Add ("pi", '\u03C0');
207                         entities.Add ("rho", '\u03C1');
208                         entities.Add ("sigmaf", '\u03C2');
209                         entities.Add ("sigma", '\u03C3');
210                         entities.Add ("tau", '\u03C4');
211                         entities.Add ("upsilon", '\u03C5');
212                         entities.Add ("phi", '\u03C6');
213                         entities.Add ("chi", '\u03C7');
214                         entities.Add ("psi", '\u03C8');
215                         entities.Add ("omega", '\u03C9');
216                         entities.Add ("thetasym", '\u03D1');
217                         entities.Add ("upsih", '\u03D2');
218                         entities.Add ("piv", '\u03D6');
219                         entities.Add ("bull", '\u2022');
220                         entities.Add ("hellip", '\u2026');
221                         entities.Add ("prime", '\u2032');
222                         entities.Add ("Prime", '\u2033');
223                         entities.Add ("oline", '\u203E');
224                         entities.Add ("frasl", '\u2044');
225                         entities.Add ("weierp", '\u2118');
226                         entities.Add ("image", '\u2111');
227                         entities.Add ("real", '\u211C');
228                         entities.Add ("trade", '\u2122');
229                         entities.Add ("alefsym", '\u2135');
230                         entities.Add ("larr", '\u2190');
231                         entities.Add ("uarr", '\u2191');
232                         entities.Add ("rarr", '\u2192');
233                         entities.Add ("darr", '\u2193');
234                         entities.Add ("harr", '\u2194');
235                         entities.Add ("crarr", '\u21B5');
236                         entities.Add ("lArr", '\u21D0');
237                         entities.Add ("uArr", '\u21D1');
238                         entities.Add ("rArr", '\u21D2');
239                         entities.Add ("dArr", '\u21D3');
240                         entities.Add ("hArr", '\u21D4');
241                         entities.Add ("forall", '\u2200');
242                         entities.Add ("part", '\u2202');
243                         entities.Add ("exist", '\u2203');
244                         entities.Add ("empty", '\u2205');
245                         entities.Add ("nabla", '\u2207');
246                         entities.Add ("isin", '\u2208');
247                         entities.Add ("notin", '\u2209');
248                         entities.Add ("ni", '\u220B');
249                         entities.Add ("prod", '\u220F');
250                         entities.Add ("sum", '\u2211');
251                         entities.Add ("minus", '\u2212');
252                         entities.Add ("lowast", '\u2217');
253                         entities.Add ("radic", '\u221A');
254                         entities.Add ("prop", '\u221D');
255                         entities.Add ("infin", '\u221E');
256                         entities.Add ("ang", '\u2220');
257                         entities.Add ("and", '\u2227');
258                         entities.Add ("or", '\u2228');
259                         entities.Add ("cap", '\u2229');
260                         entities.Add ("cup", '\u222A');
261                         entities.Add ("int", '\u222B');
262                         entities.Add ("there4", '\u2234');
263                         entities.Add ("sim", '\u223C');
264                         entities.Add ("cong", '\u2245');
265                         entities.Add ("asymp", '\u2248');
266                         entities.Add ("ne", '\u2260');
267                         entities.Add ("equiv", '\u2261');
268                         entities.Add ("le", '\u2264');
269                         entities.Add ("ge", '\u2265');
270                         entities.Add ("sub", '\u2282');
271                         entities.Add ("sup", '\u2283');
272                         entities.Add ("nsub", '\u2284');
273                         entities.Add ("sube", '\u2286');
274                         entities.Add ("supe", '\u2287');
275                         entities.Add ("oplus", '\u2295');
276                         entities.Add ("otimes", '\u2297');
277                         entities.Add ("perp", '\u22A5');
278                         entities.Add ("sdot", '\u22C5');
279                         entities.Add ("lceil", '\u2308');
280                         entities.Add ("rceil", '\u2309');
281                         entities.Add ("lfloor", '\u230A');
282                         entities.Add ("rfloor", '\u230B');
283                         entities.Add ("lang", '\u2329');
284                         entities.Add ("rang", '\u232A');
285                         entities.Add ("loz", '\u25CA');
286                         entities.Add ("spades", '\u2660');
287                         entities.Add ("clubs", '\u2663');
288                         entities.Add ("hearts", '\u2665');
289                         entities.Add ("diams", '\u2666');
290                         entities.Add ("quot", '\u0022');
291                         entities.Add ("amp", '\u0026');
292                         entities.Add ("lt", '\u003C');
293                         entities.Add ("gt", '\u003E');
294                         entities.Add ("OElig", '\u0152');
295                         entities.Add ("oelig", '\u0153');
296                         entities.Add ("Scaron", '\u0160');
297                         entities.Add ("scaron", '\u0161');
298                         entities.Add ("Yuml", '\u0178');
299                         entities.Add ("circ", '\u02C6');
300                         entities.Add ("tilde", '\u02DC');
301                         entities.Add ("ensp", '\u2002');
302                         entities.Add ("emsp", '\u2003');
303                         entities.Add ("thinsp", '\u2009');
304                         entities.Add ("zwnj", '\u200C');
305                         entities.Add ("zwj", '\u200D');
306                         entities.Add ("lrm", '\u200E');
307                         entities.Add ("rlm", '\u200F');
308                         entities.Add ("ndash", '\u2013');
309                         entities.Add ("mdash", '\u2014');
310                         entities.Add ("lsquo", '\u2018');
311                         entities.Add ("rsquo", '\u2019');
312                         entities.Add ("sbquo", '\u201A');
313                         entities.Add ("ldquo", '\u201C');
314                         entities.Add ("rdquo", '\u201D');
315                         entities.Add ("bdquo", '\u201E');
316                         entities.Add ("dagger", '\u2020');
317                         entities.Add ("Dagger", '\u2021');
318                         entities.Add ("permil", '\u2030');
319                         entities.Add ("lsaquo", '\u2039');
320                         entities.Add ("rsaquo", '\u203A');
321                         entities.Add ("euro", '\u20AC');
322                 }
323
324                 public HttpUtility () 
325                 {
326                 }
327         
328                 #endregion // Constructors
329         
330                 #region Methods
331         
332                 public static void HtmlAttributeEncode (string s, TextWriter output) 
333                 {
334                         output.Write(HtmlAttributeEncode(s));
335                 }
336         
337                 public static string HtmlAttributeEncode (string s) 
338                 {
339                         if (null == s) 
340                                 return null;
341         
342                         bool needEncode = false;
343                         for (int i = 0; i < s.Length; i++) {
344                                 if (s [i] == '&' || s [i] == '"' || s [i] == '<') {
345                                         needEncode = true;
346                                         break;
347                                 }
348                         }
349
350                         if (!needEncode)
351                                 return s;
352
353                         StringBuilder output = new StringBuilder ();
354                         int len = s.Length;
355                         for (int i = 0; i < len; i++)
356                                 switch (s [i]) {
357                                 case '&' : 
358                                         output.Append ("&amp;");
359                                         break;
360                                 case '"' :
361                                         output.Append ("&quot;");
362                                         break;
363                                 case '<':
364                                         output.Append ("&lt;");
365                                         break;
366                                 default:
367                                         output.Append (s [i]);
368                                         break;
369                                 }
370         
371                         return output.ToString();
372                 }
373         
374                 public static string UrlDecode (string str) 
375                 {
376                         return UrlDecode(str, Encoding.UTF8);
377                 }
378         
379                 private static char [] GetChars (MemoryStream b, Encoding e)
380                 {
381                         return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
382                 }
383
384                 public static string UrlDecode (string s, Encoding e)
385                 {
386                         if (null == s) 
387                                 return null;
388
389                         if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
390                                 return s;
391
392                         if (e == null)
393                                 e = Encoding.UTF8;
394         
395                         StringBuilder output = new StringBuilder ();
396                         long len = s.Length;
397                         MemoryStream bytes = new MemoryStream ();
398                         int xchar;
399         
400                         for (int i = 0; i < len; i++) {
401                                 if (s [i] == '%' && i + 2 < len && s [i + 1] != '%') {
402                                         if (s [i + 1] == 'u' && i + 5 < len) {
403                                                 if (bytes.Length > 0) {
404                                                         output.Append (GetChars (bytes, e));
405                                                         bytes.SetLength (0);
406                                                 }
407
408                                                 xchar = GetChar (s, i + 2, 4);
409                                                 if (xchar != -1) {
410                                                         output.Append ((char) xchar);
411                                                         i += 5;
412                                                 } else {
413                                                         output.Append ('%');
414                                                 }
415                                         } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
416                                                 bytes.WriteByte ((byte) xchar);
417                                                 i += 2;
418                                         } else {
419                                                 output.Append ('%');
420                                         }
421                                         continue;
422                                 }
423
424                                 if (bytes.Length > 0) {
425                                         output.Append (GetChars (bytes, e));
426                                         bytes.SetLength (0);
427                                 }
428
429                                 if (s [i] == '+') {
430                                         output.Append (' ');
431                                 } else {
432                                         output.Append (s [i]);
433                                 }
434                         }
435         
436                         if (bytes.Length > 0) {
437                                 output.Append (GetChars (bytes, e));
438                         }
439
440                         bytes = null;
441                         return output.ToString ();
442                 }
443         
444                 public static string UrlDecode (byte [] bytes, Encoding e)
445                 {
446                         if (bytes == null)
447                                 return null;
448
449                         return UrlDecode (bytes, 0, bytes.Length, e);
450                 }
451
452                 private static int GetInt (byte b)
453                 {
454                         char c = (char) b;
455                         if (c >= '0' && c <= '9')
456                                 return c - '0';
457
458                         if (c >= 'a' && c <= 'f')
459                                 return c - 'a' + 10;
460
461                         if (c >= 'A' && c <= 'F')
462                                 return c - 'A' + 10;
463
464                         return -1;
465                 }
466
467                 private static int GetChar (byte [] bytes, int offset, int length)
468                 {
469                         int value = 0;
470                         int end = length + offset;
471                         for (int i = offset; i < end; i++) {
472                                 int current = GetInt (bytes [i]);
473                                 if (current == -1)
474                                         return -1;
475                                 value = (value << 4) + current;
476                         }
477
478                         return value;
479                 }
480
481                 private static int GetChar (string str, int offset, int length)
482                 {
483                         int val = 0;
484                         int end = length + offset;
485                         for (int i = offset; i < end; i++) {
486                                 char c = str [i];
487                                 if (c > 127)
488                                         return -1;
489
490                                 int current = GetInt ((byte) c);
491                                 if (current == -1)
492                                         return -1;
493                                 val = (val << 4) + current;
494                         }
495
496                         return val;
497                 }
498                 
499                 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
500                 {
501                         if (bytes == null)
502                                 return null;
503                         if (count == 0)
504                                 return String.Empty;
505
506                         if (bytes == null)
507                                 throw new ArgumentNullException ("bytes");
508
509                         if (offset < 0 || offset > bytes.Length)
510                                 throw new ArgumentOutOfRangeException ("offset");
511
512                         if (count < 0 || offset + count > bytes.Length)
513                                 throw new ArgumentOutOfRangeException ("count");
514
515                         StringBuilder output = new StringBuilder ();
516                         MemoryStream acc = new MemoryStream ();
517
518                         int end = count + offset;
519                         int xchar;
520                         for (int i = offset; i < end; i++) {
521                                 if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
522                                         if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
523                                                 if (acc.Length > 0) {
524                                                         output.Append (GetChars (acc, e));
525                                                         acc.SetLength (0);
526                                                 }
527                                                 xchar = GetChar (bytes, i + 2, 4);
528                                                 if (xchar != -1) {
529                                                         output.Append ((char) xchar);
530                                                         i += 5;
531                                                         continue;
532                                                 }
533                                         } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
534                                                 acc.WriteByte ((byte) xchar);
535                                                 i += 2;
536                                                 continue;
537                                         }
538                                 }
539
540                                 if (acc.Length > 0) {
541                                         output.Append (GetChars (acc, e));
542                                         acc.SetLength (0);
543                                 }
544
545                                 if (bytes [i] == '+') {
546                                         output.Append (' ');
547                                 } else {
548                                         output.Append ((char) bytes [i]);
549                                 }
550                         }
551
552                         if (acc.Length > 0) {
553                                 output.Append (GetChars (acc, e));
554                         }
555                         
556                         acc = null;
557                         return output.ToString ();
558                 }
559         
560                 public static byte [] UrlDecodeToBytes (byte [] bytes)
561                 {
562                         if (bytes == null)
563                                 return null;
564
565                         return UrlDecodeToBytes (bytes, 0, bytes.Length);
566                 }
567
568                 public static byte [] UrlDecodeToBytes (string str)
569                 {
570                         return UrlDecodeToBytes (str, Encoding.UTF8);
571                 }
572
573                 public static byte [] UrlDecodeToBytes (string str, Encoding e)
574                 {
575                         if (str == null)
576                                 return null;
577
578                         if (e == null)
579                                 throw new ArgumentNullException ("e");
580
581                         return UrlDecodeToBytes (e.GetBytes (str));
582                 }
583
584                 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
585                 {
586                         if (bytes == null)
587                                 return null;
588                         if (count == 0)
589                                 return new byte [0];
590
591                         int len = bytes.Length;
592                         if (offset < 0 || offset >= len)
593                                 throw new ArgumentOutOfRangeException("offset");
594
595                         if (count < 0 || offset > len - count)
596                                 throw new ArgumentOutOfRangeException("count");
597
598                         MemoryStream result = new MemoryStream ();
599                         int end = offset + count;
600                         for (int i = offset; i < end; i++){
601                                 char c = (char) bytes [i];
602                                 if (c == '+') {
603                                         c = ' ';
604                                 } else if (c == '%' && i < end - 2) {
605                                         int xchar = GetChar (bytes, i + 1, 2);
606                                         if (xchar != -1) {
607                                                 c = (char) xchar;
608                                                 i += 2;
609                                         }
610                                 }
611                                 result.WriteByte ((byte) c);
612                         }
613
614                         return result.ToArray ();
615                 }
616
617                 public static string UrlEncode(string str) 
618                 {
619                         return UrlEncode(str, Encoding.UTF8);
620                 }
621         
622                 public static string UrlEncode (string s, Encoding Enc) 
623                 {
624                         if (s == null)
625                                 return null;
626
627                         if (s == "")
628                                 return "";
629
630                         bool needEncode = false;
631                         int len = s.Length;
632                         for (int i = 0; i < len; i++) {
633                                 char c = s [i];
634                                 if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
635                                         if (NotEncoded (c))
636                                                 continue;
637
638                                         needEncode = true;
639                                         break;
640                                 }
641                         }
642
643                         if (!needEncode)
644                                 return s;
645
646                         // avoided GetByteCount call
647                         byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
648                         int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
649                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
650                 }
651           
652                 public static string UrlEncode (byte [] bytes)
653                 {
654                         if (bytes == null)
655                                 return null;
656
657                         if (bytes.Length == 0)
658                                 return "";
659
660                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
661                 }
662
663                 public static string UrlEncode (byte [] bytes, int offset, int count)
664                 {
665                         if (bytes == null)
666                                 return null;
667
668                         if (bytes.Length == 0)
669                                 return "";
670
671                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
672                 }
673
674                 public static byte [] UrlEncodeToBytes (string str)
675                 {
676                         return UrlEncodeToBytes (str, Encoding.UTF8);
677                 }
678
679                 public static byte [] UrlEncodeToBytes (string str, Encoding e)
680                 {
681                         if (str == null)
682                                 return null;
683
684                         if (str == "")
685                                 return new byte [0];
686
687                         byte [] bytes = e.GetBytes (str);
688                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
689                 }
690
691                 public static byte [] UrlEncodeToBytes (byte [] bytes)
692                 {
693                         if (bytes == null)
694                                 return null;
695
696                         if (bytes.Length == 0)
697                                 return new byte [0];
698
699                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
700                 }
701
702                 static char [] hexChars = "0123456789abcdef".ToCharArray ();
703
704                 static bool NotEncoded (char c)
705                 {
706                         return (c == '!' || c == '\'' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_');
707                 }
708
709                 static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
710                         if (c > 255) {
711                                 //FIXME: what happens when there is an internal error?
712                                 //if (!isUnicode)
713                                 //      throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
714                                 int idx;
715                                 int i = (int) c;
716
717                                 result.WriteByte ((byte)'%');
718                                 result.WriteByte ((byte)'u');
719                                 idx = i >> 12;
720                                 result.WriteByte ((byte)hexChars [idx]);
721                                 idx = (i >> 8) & 0x0F;
722                                 result.WriteByte ((byte)hexChars [idx]);
723                                 idx = (i >> 4) & 0x0F;
724                                 result.WriteByte ((byte)hexChars [idx]);
725                                 idx = i & 0x0F;
726                                 result.WriteByte ((byte)hexChars [idx]);
727                                 return;
728                         }
729                         
730                         if (c > ' ' && NotEncoded (c)) {
731                                 result.WriteByte ((byte)c);
732                                 return;
733                         }
734                         if (c==' ') {
735                                 result.WriteByte ((byte)'+');
736                                 return;
737                         }
738                         if (    (c < '0') ||
739                                 (c < 'A' && c > '9') ||
740                                 (c > 'Z' && c < 'a') ||
741                                 (c > 'z')) {
742                                 if (isUnicode && c > 127) {
743                                         result.WriteByte ((byte)'%');
744                                         result.WriteByte ((byte)'u');
745                                         result.WriteByte ((byte)'0');
746                                         result.WriteByte ((byte)'0');
747                                 }
748                                 else
749                                         result.WriteByte ((byte)'%');
750                                 
751                                 int idx = ((int) c) >> 4;
752                                 result.WriteByte ((byte)hexChars [idx]);
753                                 idx = ((int) c) & 0x0F;
754                                 result.WriteByte ((byte)hexChars [idx]);
755                         }
756                         else
757                                 result.WriteByte ((byte)c);
758                 }
759
760                 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
761                 {
762                         if (bytes == null)
763                                 return null;
764
765                         int len = bytes.Length;
766                         if (len == 0)
767                                 return new byte [0];
768
769                         if (offset < 0 || offset >= len)
770                                 throw new ArgumentOutOfRangeException("offset");
771
772                         if (count < 0 || count > len - offset)
773                                 throw new ArgumentOutOfRangeException("count");
774
775                         MemoryStream result = new MemoryStream (count);
776                         int end = offset + count;
777                         for (int i = offset; i < end; i++)
778                                 UrlEncodeChar ((char)bytes [i], result, false);
779
780                         return result.ToArray();
781                 }
782
783                 public static string UrlEncodeUnicode (string str)
784                 {
785                         if (str == null)
786                                 return null;
787
788                         return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
789                 }
790
791                 public static byte [] UrlEncodeUnicodeToBytes (string str)
792                 {
793                         if (str == null)
794                                 return null;
795
796                         if (str == "")
797                                 return new byte [0];
798
799                         MemoryStream result = new MemoryStream (str.Length);
800                         foreach (char c in str){
801                                 UrlEncodeChar (c, result, true);
802                         }
803                         return result.ToArray ();
804                 }
805
806                 /// <summary>
807                 /// Decodes an HTML-encoded string and returns the decoded string.
808                 /// </summary>
809                 /// <param name="s">The HTML string to decode. </param>
810                 /// <returns>The decoded text.</returns>
811                 public static string HtmlDecode (string s) 
812                 {
813                         if (s == null)
814                                 throw new ArgumentNullException ("s");
815
816                         if (s.IndexOf ('&') == -1)
817                                 return s;
818
819                         StringBuilder entity = new StringBuilder ();
820                         StringBuilder output = new StringBuilder ();
821                         int len = s.Length;
822                         // 0 -> nothing,
823                         // 1 -> right after '&'
824                         // 2 -> between '&' and ';' but no '#'
825                         // 3 -> '#' found after '&' and getting numbers
826                         int state = 0;
827                         int number = 0;
828                         bool have_trailing_digits = false;
829         
830                         for (int i = 0; i < len; i++) {
831                                 char c = s [i];
832                                 if (state == 0) {
833                                         if (c == '&') {
834                                                 entity.Append (c);
835                                                 state = 1;
836                                         } else {
837                                                 output.Append (c);
838                                         }
839                                         continue;
840                                 }
841
842                                 if (c == '&') {
843                                         state = 1;
844                                         if (have_trailing_digits) {
845                                                 entity.Append (number.ToString (CultureInfo.InvariantCulture));
846                                                 have_trailing_digits = false;
847                                         }
848
849                                         output.Append (entity.ToString ());
850                                         entity.Length = 0;
851                                         entity.Append ('&');
852                                         continue;
853                                 }
854
855                                 if (state == 1) {
856                                         if (c == ';') {
857                                                 state = 0;
858                                                 output.Append (entity.ToString ());
859                                                 output.Append (c);
860                                                 entity.Length = 0;
861                                         } else {
862                                                 number = 0;
863                                                 if (c != '#') {
864                                                         state = 2;
865                                                 } else {
866                                                         state = 3;
867                                                 }
868                                                 entity.Append (c);
869                                         }
870                                 } else if (state == 2) {
871                                         entity.Append (c);
872                                         if (c == ';') {
873                                                 string key = entity.ToString ();
874                                                 if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
875                                                         key = Entities [key.Substring (1, key.Length - 2)].ToString ();
876
877                                                 output.Append (key);
878                                                 state = 0;
879                                                 entity.Length = 0;
880                                         }
881                                 } else if (state == 3) {
882                                         if (c == ';') {
883                                                 if (number > 65535) {
884                                                         output.Append ("&#");
885                                                         output.Append (number.ToString (CultureInfo.InvariantCulture));
886                                                         output.Append (";");
887                                                 } else {
888                                                         output.Append ((char) number);
889                                                 }
890                                                 state = 0;
891                                                 entity.Length = 0;
892                                                 have_trailing_digits = false;
893                                         } else if (Char.IsDigit (c)) {
894                                                 number = number * 10 + ((int) c - '0');
895                                                 have_trailing_digits = true;
896                                         } else {
897                                                 state = 2;
898                                                 if (have_trailing_digits) {
899                                                         entity.Append (number.ToString (CultureInfo.InvariantCulture));
900                                                         have_trailing_digits = false;
901                                                 }
902                                                 entity.Append (c);
903                                         }
904                                 }
905                         }
906
907                         if (entity.Length > 0) {
908                                 output.Append (entity.ToString ());
909                         } else if (have_trailing_digits) {
910                                 output.Append (number.ToString (CultureInfo.InvariantCulture));
911                         }
912                         return output.ToString ();
913                 }
914         
915                 /// <summary>
916                 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
917                 /// </summary>
918                 /// <param name="s">The HTML string to decode</param>
919                 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
920                 public static void HtmlDecode(string s, TextWriter output) 
921                 {
922                         if (s != null)
923                                 output.Write (HtmlDecode (s));
924                 }
925         
926                 /// <summary>
927                 /// HTML-encodes a string and returns the encoded string.
928                 /// </summary>
929                 /// <param name="s">The text string to encode. </param>
930                 /// <returns>The HTML-encoded text.</returns>
931                 public static string HtmlEncode (string s) 
932                 {
933                         if (s == null)
934                                 return null;
935
936                         bool needEncode = false;
937                         for (int i = 0; i < s.Length; i++) {
938                                 char c = s [i];
939                                 if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
940                                         needEncode = true;
941                                         break;
942                                 }
943                         }
944
945                         if (!needEncode)
946                                 return s;
947
948                         StringBuilder output = new StringBuilder ();
949                         
950                         int len = s.Length;
951                         for (int i = 0; i < len; i++) 
952                                 switch (s [i]) {
953                                 case '&' :
954                                         output.Append ("&amp;");
955                                         break;
956                                 case '>' : 
957                                         output.Append ("&gt;");
958                                         break;
959                                 case '<' :
960                                         output.Append ("&lt;");
961                                         break;
962                                 case '"' :
963                                         output.Append ("&quot;");
964                                         break;
965                                 default:
966                                         // MS starts encoding with &# from 160 and stops at 255.
967                                         // We don't do that. One reason is the 65308/65310 unicode
968                                         // characters that look like '<' and '>'.
969 #if TARGET_JVM
970                                         if (s [i] > 159 && s [i] < 256) {
971 #else
972                                         if (s [i] > 159) {
973 #endif
974                                                 output.Append ("&#");
975                                                 output.Append (((int) s [i]).ToString (CultureInfo.InvariantCulture));
976                                                 output.Append (";");
977                                         } else {
978                                                 output.Append (s [i]);
979                                         }
980                                         break;
981                                 }
982                         return output.ToString ();
983                 }
984         
985                 /// <summary>
986                 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
987                 /// </summary>
988                 /// <param name="s">The string to encode. </param>
989                 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
990                 public static void HtmlEncode(string s, TextWriter output) 
991                 {
992                         if (s != null)
993                                 output.Write (HtmlEncode (s));
994                 }
995
996 #if NET_1_1
997                 public static string UrlPathEncode (string s)
998                 {
999                         if (s == null || s.Length == 0)
1000                                 return s;
1001
1002                         MemoryStream result = new MemoryStream ();
1003                         int length = s.Length;
1004             for (int i = 0; i < length; i++) {
1005                                 UrlPathEncodeChar (s [i], result);
1006                         }
1007                         return Encoding.ASCII.GetString (result.ToArray ());
1008                 }
1009                 
1010                 static void UrlPathEncodeChar (char c, Stream result) {
1011 #if NET_2_0
1012                         if (c < 33 || c > 126) {
1013 #else
1014                         if (c > 127) {
1015 #endif
1016                                 byte [] bIn = Encoding.UTF8.GetBytes (c.ToString ());
1017                                 for (int i = 0; i < bIn.Length; i++) {
1018                                         result.WriteByte ((byte) '%');
1019                                         int idx = ((int) bIn [i]) >> 4;
1020                                         result.WriteByte ((byte) hexChars [idx]);
1021                                         idx = ((int) bIn [i]) & 0x0F;
1022                                         result.WriteByte ((byte) hexChars [idx]);
1023                                 }
1024                         }
1025                         else if (c == ' ') {
1026                                 result.WriteByte ((byte) '%');
1027                                 result.WriteByte ((byte) '2');
1028                                 result.WriteByte ((byte) '0');
1029                         }
1030                         else
1031                                 result.WriteByte ((byte) c);
1032                 }
1033 #endif
1034
1035 #if NET_2_0
1036                 public static NameValueCollection ParseQueryString (string query)
1037                 {
1038                         return ParseQueryString (query, Encoding.UTF8);
1039                 }
1040
1041                 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
1042                 {
1043                         if (query == null)
1044                                 throw new ArgumentNullException ("query");
1045                         if (encoding == null)
1046                                 throw new ArgumentNullException ("encoding");
1047                         if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
1048                                 return new NameValueCollection ();
1049                         if (query[0] == '?')
1050                                 query = query.Substring (1);
1051                                 
1052                         NameValueCollection result = new NameValueCollection ();
1053                         ParseQueryString (query, encoding, result);
1054                         return result;
1055                 }                               
1056 #endif
1057
1058                 internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
1059                 {
1060                         if (query.Length == 0)
1061                                 return;
1062
1063                         int namePos = 0;
1064                         bool first = true;
1065                         while (namePos <= query.Length) {
1066                                 int valuePos = -1, valueEnd = -1;
1067                                 for (int q = namePos; q < query.Length; q++) {
1068                                         if (valuePos == -1 && query[q] == '=') {
1069                                                 valuePos = q + 1;
1070                                         } else if (query[q] == '&') {
1071                                                 valueEnd = q;
1072                                                 break;
1073                                         }
1074                                 }
1075
1076                                 if (first) {
1077                                         first = false;
1078                                         if (query [namePos] == '?')
1079                                                 namePos++;
1080                                 }
1081                                 
1082                                 string name, value;
1083                                 if (valuePos == -1) {
1084                                         name = null;
1085                                         valuePos = namePos;
1086                                 } else {
1087                                         name = UrlDecode (query.Substring (namePos, valuePos - namePos - 1), encoding);
1088                                 }
1089                                 if (valueEnd < 0) {
1090                                         namePos = -1;
1091                                         valueEnd = query.Length;
1092                                 } else {
1093                                         namePos = valueEnd + 1;
1094                                 }
1095                                 value = UrlDecode (query.Substring (valuePos, valueEnd - valuePos), encoding);
1096
1097                                 result.Add (name, value);
1098                                 if (namePos == -1)
1099                                         break;
1100                         }
1101                 }
1102                 #endregion // Methods
1103         }
1104 }
1105