2010-06-01 Marek Safar <marek.safar@gmail.com>
[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-2009 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.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.IO;
37 using System.Security.Permissions;
38 using System.Text;
39 using System.Web.Util;
40
41 namespace System.Web {
42
43 #if !MONOTOUCH
44         // CAS - no InheritanceDemand here as the class is sealed
45         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 #endif
47         public sealed class HttpUtility
48         {
49                 sealed class HttpQSCollection : NameValueCollection
50                 {
51                         public override string ToString ()
52                         {
53                                 int count = Count;
54                                 if (count == 0)
55                                         return "";
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]]);
60                                 }
61                                 if (sb.Length > 0)
62                                         sb.Length--;
63                                 return sb.ToString ();
64                         }
65                 }
66                 #region Fields
67         
68                 static Hashtable entities;
69                 static object lock_ = new object ();
70         
71                 #endregion // Fields
72         
73                 static Hashtable Entities {
74                         get {
75                                 lock (lock_) {
76                                         if (entities == null)
77                                                 InitEntities ();
78
79                                         return entities;
80                                 }
81                         }
82                 }
83                 
84                 #region Constructors
85
86                 static void InitEntities ()
87                 {
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');
343                 }
344
345                 public HttpUtility () 
346                 {
347                 }
348         
349                 #endregion // Constructors
350         
351                 #region Methods
352         
353                 public static void HtmlAttributeEncode (string s, TextWriter output) 
354                 {
355                         output.Write(HtmlAttributeEncode(s));
356                 }
357         
358                 public static string HtmlAttributeEncode (string s) 
359                 {
360                         if (null == s) 
361                                 return null;
362         
363                         bool needEncode = false;
364                         for (int i = 0; i < s.Length; i++) {
365                                 char c = s [i];
366                                 if (c == '&' || c == '"' || c == '<'
367 #if NET_4_0
368                                     || c == '\''
369 #endif
370                                 ) {
371                                         needEncode = true;
372                                         break;
373                                 }
374                         }
375
376                         if (!needEncode)
377                                 return s;
378
379                         StringBuilder output = new StringBuilder ();
380                         int len = s.Length;
381                         for (int i = 0; i < len; i++)
382                                 switch (s [i]) {
383                                 case '&' : 
384                                         output.Append ("&amp;");
385                                         break;
386                                 case '"' :
387                                         output.Append ("&quot;");
388                                         break;
389                                 case '<':
390                                         output.Append ("&lt;");
391                                         break;
392 #if NET_4_0
393                                 case '\'':
394                                         output.Append ("&#39;");
395                                         break;
396 #endif
397                                 default:
398                                         output.Append (s [i]);
399                                         break;
400                                 }
401         
402                         return output.ToString();
403                 }
404         
405                 public static string UrlDecode (string str) 
406                 {
407                         return UrlDecode(str, Encoding.UTF8);
408                 }
409         
410                 static char [] GetChars (MemoryStream b, Encoding e)
411                 {
412                         return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
413                 }
414
415                 static void WriteCharBytes (IList buf, char ch, Encoding e)
416                 {
417                         if (ch > 255) {
418                                 foreach (byte b in e.GetBytes (new char[] { ch }))
419                                         buf.Add (b);
420                         } else
421                                 buf.Add ((byte)ch);
422                 }
423                 
424                 public static string UrlDecode (string s, Encoding e)
425                 {
426                         if (null == s) 
427                                 return null;
428
429                         if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
430                                 return s;
431                         
432                         if (e == null)
433                                 e = Encoding.UTF8;
434
435                         long len = s.Length;
436                         var bytes = new List <byte> ();
437                         int xchar;
438                         char ch;
439                         
440                         for (int i = 0; i < len; i++) {
441                                 ch = s [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);
446                                                 if (xchar != -1) {
447                                                         WriteCharBytes (bytes, (char)xchar, e);
448                                                         i += 5;
449                                                 } else
450                                                         WriteCharBytes (bytes, '%', e);
451                                         } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
452                                                 WriteCharBytes (bytes, (char)xchar, e);
453                                                 i += 2;
454                                         } else {
455                                                 WriteCharBytes (bytes, '%', e);
456                                         }
457                                         continue;
458                                 }
459
460                                 if (ch == '+')
461                                         WriteCharBytes (bytes, ' ', e);
462                                 else
463                                         WriteCharBytes (bytes, ch, e);
464                         }
465                         
466                         byte[] buf = bytes.ToArray ();
467                         bytes = null;
468                         return e.GetString (buf);
469                         
470                 }
471         
472                 public static string UrlDecode (byte [] bytes, Encoding e)
473                 {
474                         if (bytes == null)
475                                 return null;
476
477                         return UrlDecode (bytes, 0, bytes.Length, e);
478                 }
479
480                 static int GetInt (byte b)
481                 {
482                         char c = (char) b;
483                         if (c >= '0' && c <= '9')
484                                 return c - '0';
485
486                         if (c >= 'a' && c <= 'f')
487                                 return c - 'a' + 10;
488
489                         if (c >= 'A' && c <= 'F')
490                                 return c - 'A' + 10;
491
492                         return -1;
493                 }
494
495                 static int GetChar (byte [] bytes, int offset, int length)
496                 {
497                         int value = 0;
498                         int end = length + offset;
499                         for (int i = offset; i < end; i++) {
500                                 int current = GetInt (bytes [i]);
501                                 if (current == -1)
502                                         return -1;
503                                 value = (value << 4) + current;
504                         }
505
506                         return value;
507                 }
508
509                 static int GetChar (string str, int offset, int length)
510                 {
511                         int val = 0;
512                         int end = length + offset;
513                         for (int i = offset; i < end; i++) {
514                                 char c = str [i];
515                                 if (c > 127)
516                                         return -1;
517
518                                 int current = GetInt ((byte) c);
519                                 if (current == -1)
520                                         return -1;
521                                 val = (val << 4) + current;
522                         }
523
524                         return val;
525                 }
526                 
527                 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
528                 {
529                         if (bytes == null)
530                                 return null;
531                         if (count == 0)
532                                 return String.Empty;
533
534                         if (bytes == null)
535                                 throw new ArgumentNullException ("bytes");
536
537                         if (offset < 0 || offset > bytes.Length)
538                                 throw new ArgumentOutOfRangeException ("offset");
539
540                         if (count < 0 || offset + count > bytes.Length)
541                                 throw new ArgumentOutOfRangeException ("count");
542
543                         StringBuilder output = new StringBuilder ();
544                         MemoryStream acc = new MemoryStream ();
545
546                         int end = count + offset;
547                         int xchar;
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));
553                                                         acc.SetLength (0);
554                                                 }
555                                                 xchar = GetChar (bytes, i + 2, 4);
556                                                 if (xchar != -1) {
557                                                         output.Append ((char) xchar);
558                                                         i += 5;
559                                                         continue;
560                                                 }
561                                         } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
562                                                 acc.WriteByte ((byte) xchar);
563                                                 i += 2;
564                                                 continue;
565                                         }
566                                 }
567
568                                 if (acc.Length > 0) {
569                                         output.Append (GetChars (acc, e));
570                                         acc.SetLength (0);
571                                 }
572
573                                 if (bytes [i] == '+') {
574                                         output.Append (' ');
575                                 } else {
576                                         output.Append ((char) bytes [i]);
577                                 }
578                         }
579
580                         if (acc.Length > 0) {
581                                 output.Append (GetChars (acc, e));
582                         }
583                         
584                         acc = null;
585                         return output.ToString ();
586                 }
587         
588                 public static byte [] UrlDecodeToBytes (byte [] bytes)
589                 {
590                         if (bytes == null)
591                                 return null;
592
593                         return UrlDecodeToBytes (bytes, 0, bytes.Length);
594                 }
595
596                 public static byte [] UrlDecodeToBytes (string str)
597                 {
598                         return UrlDecodeToBytes (str, Encoding.UTF8);
599                 }
600
601                 public static byte [] UrlDecodeToBytes (string str, Encoding e)
602                 {
603                         if (str == null)
604                                 return null;
605
606                         if (e == null)
607                                 throw new ArgumentNullException ("e");
608
609                         return UrlDecodeToBytes (e.GetBytes (str));
610                 }
611
612                 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
613                 {
614                         if (bytes == null)
615                                 return null;
616                         if (count == 0)
617                                 return new byte [0];
618
619                         int len = bytes.Length;
620                         if (offset < 0 || offset >= len)
621                                 throw new ArgumentOutOfRangeException("offset");
622
623                         if (count < 0 || offset > len - count)
624                                 throw new ArgumentOutOfRangeException("count");
625
626                         MemoryStream result = new MemoryStream ();
627                         int end = offset + count;
628                         for (int i = offset; i < end; i++){
629                                 char c = (char) bytes [i];
630                                 if (c == '+') {
631                                         c = ' ';
632                                 } else if (c == '%' && i < end - 2) {
633                                         int xchar = GetChar (bytes, i + 1, 2);
634                                         if (xchar != -1) {
635                                                 c = (char) xchar;
636                                                 i += 2;
637                                         }
638                                 }
639                                 result.WriteByte ((byte) c);
640                         }
641
642                         return result.ToArray ();
643                 }
644
645                 public static string UrlEncode(string str) 
646                 {
647                         return UrlEncode(str, Encoding.UTF8);
648                 }
649         
650                 public static string UrlEncode (string s, Encoding Enc) 
651                 {
652                         if (s == null)
653                                 return null;
654
655                         if (s == String.Empty)
656                                 return String.Empty;
657
658                         bool needEncode = false;
659                         int len = s.Length;
660                         for (int i = 0; i < len; i++) {
661                                 char c = s [i];
662                                 if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
663                                         if (NotEncoded (c))
664                                                 continue;
665
666                                         needEncode = true;
667                                         break;
668                                 }
669                         }
670
671                         if (!needEncode)
672                                 return s;
673
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));
678                 }
679           
680                 public static string UrlEncode (byte [] bytes)
681                 {
682                         if (bytes == null)
683                                 return null;
684
685                         if (bytes.Length == 0)
686                                 return "";
687
688                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
689                 }
690
691                 public static string UrlEncode (byte [] bytes, int offset, int count)
692                 {
693                         if (bytes == null)
694                                 return null;
695
696                         if (bytes.Length == 0)
697                                 return "";
698
699                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
700                 }
701
702                 public static byte [] UrlEncodeToBytes (string str)
703                 {
704                         return UrlEncodeToBytes (str, Encoding.UTF8);
705                 }
706
707                 public static byte [] UrlEncodeToBytes (string str, Encoding e)
708                 {
709                         if (str == null)
710                                 return null;
711
712                         if (str == "")
713                                 return new byte [0];
714
715                         byte [] bytes = e.GetBytes (str);
716                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
717                 }
718
719                 public static byte [] UrlEncodeToBytes (byte [] bytes)
720                 {
721                         if (bytes == null)
722                                 return null;
723
724                         if (bytes.Length == 0)
725                                 return new byte [0];
726
727                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
728                 }
729
730                 static char [] hexChars = "0123456789abcdef".ToCharArray ();
731
732                 static bool NotEncoded (char c)
733                 {
734                         return (c == '!' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_'
735 #if !NET_4_0
736                                 || c == '\''
737 #endif
738                         );
739                 }
740
741                 static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
742                         if (c > 255) {
743                                 //FIXME: what happens when there is an internal error?
744                                 //if (!isUnicode)
745                                 //      throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
746                                 int idx;
747                                 int i = (int) c;
748
749                                 result.WriteByte ((byte)'%');
750                                 result.WriteByte ((byte)'u');
751                                 idx = i >> 12;
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]);
757                                 idx = i & 0x0F;
758                                 result.WriteByte ((byte)hexChars [idx]);
759                                 return;
760                         }
761                         
762                         if (c > ' ' && NotEncoded (c)) {
763                                 result.WriteByte ((byte)c);
764                                 return;
765                         }
766                         if (c==' ') {
767                                 result.WriteByte ((byte)'+');
768                                 return;
769                         }
770                         if (    (c < '0') ||
771                                 (c < 'A' && c > '9') ||
772                                 (c > 'Z' && c < 'a') ||
773                                 (c > 'z')) {
774                                 if (isUnicode && c > 127) {
775                                         result.WriteByte ((byte)'%');
776                                         result.WriteByte ((byte)'u');
777                                         result.WriteByte ((byte)'0');
778                                         result.WriteByte ((byte)'0');
779                                 }
780                                 else
781                                         result.WriteByte ((byte)'%');
782                                 
783                                 int idx = ((int) c) >> 4;
784                                 result.WriteByte ((byte)hexChars [idx]);
785                                 idx = ((int) c) & 0x0F;
786                                 result.WriteByte ((byte)hexChars [idx]);
787                         }
788                         else
789                                 result.WriteByte ((byte)c);
790                 }
791
792                 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
793                 {
794                         if (bytes == null)
795                                 return null;
796
797                         int len = bytes.Length;
798                         if (len == 0)
799                                 return new byte [0];
800
801                         if (offset < 0 || offset >= len)
802                                 throw new ArgumentOutOfRangeException("offset");
803
804                         if (count < 0 || count > len - offset)
805                                 throw new ArgumentOutOfRangeException("count");
806
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);
811
812                         return result.ToArray();
813                 }
814
815                 public static string UrlEncodeUnicode (string str)
816                 {
817                         if (str == null)
818                                 return null;
819
820                         return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
821                 }
822
823                 public static byte [] UrlEncodeUnicodeToBytes (string str)
824                 {
825                         if (str == null)
826                                 return null;
827
828                         if (str == "")
829                                 return new byte [0];
830
831                         MemoryStream result = new MemoryStream (str.Length);
832                         foreach (char c in str){
833                                 UrlEncodeChar (c, result, true);
834                         }
835                         return result.ToArray ();
836                 }
837
838                 /// <summary>
839                 /// Decodes an HTML-encoded string and returns the decoded string.
840                 /// </summary>
841                 /// <param name="s">The HTML string to decode. </param>
842                 /// <returns>The decoded text.</returns>
843                 public static string HtmlDecode (string s) 
844                 {
845                         if (s == null)
846                                 throw new ArgumentNullException ("s");
847
848                         if (s.IndexOf ('&') == -1)
849                                 return s;
850
851                         StringBuilder entity = new StringBuilder ();
852                         StringBuilder output = new StringBuilder ();
853                         int len = s.Length;
854                         // 0 -> nothing,
855                         // 1 -> right after '&'
856                         // 2 -> between '&' and ';' but no '#'
857                         // 3 -> '#' found after '&' and getting numbers
858                         int state = 0;
859                         int number = 0;
860                         bool is_hex_value = false;
861                         bool have_trailing_digits = false;
862         
863                         for (int i = 0; i < len; i++) {
864                                 char c = s [i];
865                                 if (state == 0) {
866                                         if (c == '&') {
867                                                 entity.Append (c);
868                                                 state = 1;
869                                         } else {
870                                                 output.Append (c);
871                                         }
872                                         continue;
873                                 }
874
875                                 if (c == '&') {
876                                         state = 1;
877                                         if (have_trailing_digits) {
878                                                 entity.Append (number.ToString (Helpers.InvariantCulture));
879                                                 have_trailing_digits = false;
880                                         }
881
882                                         output.Append (entity.ToString ());
883                                         entity.Length = 0;
884                                         entity.Append ('&');
885                                         continue;
886                                 }
887
888                                 if (state == 1) {
889                                         if (c == ';') {
890                                                 state = 0;
891                                                 output.Append (entity.ToString ());
892                                                 output.Append (c);
893                                                 entity.Length = 0;
894                                         } else {
895                                                 number = 0;
896                                                 is_hex_value = false;
897                                                 if (c != '#') {
898                                                         state = 2;
899                                                 } else {
900                                                         state = 3;
901                                                 }
902                                                 entity.Append (c);
903                                         }
904                                 } else if (state == 2) {
905                                         entity.Append (c);
906                                         if (c == ';') {
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 ();
910
911                                                 output.Append (key);
912                                                 state = 0;
913                                                 entity.Length = 0;
914                                         }
915                                 } else if (state == 3) {
916                                         if (c == ';') {
917                                                 if (number > 65535) {
918                                                         output.Append ("&#");
919                                                         output.Append (number.ToString (Helpers.InvariantCulture));
920                                                         output.Append (";");
921                                                 } else {
922                                                         output.Append ((char) number);
923                                                 }
924                                                 state = 0;
925                                                 entity.Length = 0;
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')) {
934                                                 is_hex_value = true;
935                                         } else {
936                                                 state = 2;
937                                                 if (have_trailing_digits) {
938                                                         entity.Append (number.ToString (Helpers.InvariantCulture));
939                                                         have_trailing_digits = false;
940                                                 }
941                                                 entity.Append (c);
942                                         }
943                                 }
944                         }
945
946                         if (entity.Length > 0) {
947                                 output.Append (entity.ToString ());
948                         } else if (have_trailing_digits) {
949                                 output.Append (number.ToString (Helpers.InvariantCulture));
950                         }
951                         return output.ToString ();
952                 }
953         
954                 /// <summary>
955                 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
956                 /// </summary>
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) 
960                 {
961                         if (s != null)
962                                 output.Write (HtmlDecode (s));
963                 }
964         
965                 /// <summary>
966                 /// HTML-encodes a string and returns the encoded string.
967                 /// </summary>
968                 /// <param name="s">The text string to encode. </param>
969                 /// <returns>The HTML-encoded text.</returns>
970                 public static string HtmlEncode (string s) 
971                 {
972                         if (s == null)
973                                 return null;
974
975                         bool needEncode = false;
976                         for (int i = 0; i < s.Length; i++) {
977                                 char c = s [i];
978                                 if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159
979 #if NET_4_0
980                                     || c == '\''
981 #endif
982                                 ) {
983                                         needEncode = true;
984                                         break;
985                                 }
986                         }
987
988                         if (!needEncode)
989                                 return s;
990
991                         StringBuilder output = new StringBuilder ();
992                         
993                         int len = s.Length;
994                         for (int i = 0; i < len; i++) 
995                                 switch (s [i]) {
996                                 case '&' :
997                                         output.Append ("&amp;");
998                                         break;
999                                 case '>' : 
1000                                         output.Append ("&gt;");
1001                                         break;
1002                                 case '<' :
1003                                         output.Append ("&lt;");
1004                                         break;
1005                                 case '"' :
1006                                         output.Append ("&quot;");
1007                                         break;
1008 #if NET_4_0
1009                                 case '\'':
1010                                         output.Append ("&#39;");
1011                                         break;
1012 #endif
1013                                 default:
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 '>'.
1017 #if TARGET_JVM
1018                                         if (s [i] > 159 && s [i] < 256) {
1019 #else
1020                                         if (s [i] > 159) {
1021 #endif
1022                                                 output.Append ("&#");
1023                                                 output.Append (((int) s [i]).ToString (Helpers.InvariantCulture));
1024                                                 output.Append (";");
1025                                         } else {
1026                                                 output.Append (s [i]);
1027                                         }
1028                                         break;
1029                                 }
1030                         return output.ToString ();
1031                 }
1032         
1033                 /// <summary>
1034                 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
1035                 /// </summary>
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) 
1039                 {
1040                         if (s != null)
1041                                 output.Write (HtmlEncode (s));
1042                 }
1043 #if NET_4_0
1044                 public static string HtmlEncode (object value)
1045                 {
1046                         if (value == null)
1047                                 return null;
1048
1049                         return HtmlEncode (value.ToString ());
1050                 }
1051
1052                 public static string JavaScriptStringEncode (string value)
1053                 {
1054                         return JavaScriptStringEncode (value, false);
1055                 }
1056
1057                 public static string JavaScriptStringEncode (string value, bool addDoubleQuotes)
1058                 {
1059                         if (String.IsNullOrEmpty (value))
1060                                 return addDoubleQuotes ? "\"\"" : String.Empty;
1061
1062                         int len = value.Length;
1063                         bool needEncode = false;
1064                         char c;
1065                         for (int i = 0; i < len; i++) {
1066                                 c = value [i];
1067
1068                                 if (c >= 0 && c <= 31 || c == 34 || c == 39 || c == 60 || c == 62 || c == 92) {
1069                                         needEncode = true;
1070                                         break;
1071                                 }
1072                         }
1073
1074                         if (!needEncode)
1075                                 return addDoubleQuotes ? "\"" + value + "\"" : value;
1076
1077                         var sb = new StringBuilder ();
1078                         if (addDoubleQuotes)
1079                                 sb.Append ('"');
1080
1081                         for (int i = 0; i < len; i++) {
1082                                 c = value [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) {
1086                                                 case 8:
1087                                                         sb.Append ("\\b");
1088                                                         break;
1089
1090                                                 case 9:
1091                                                         sb.Append ("\\t");
1092                                                         break;
1093
1094                                                 case 10:
1095                                                         sb.Append ("\\n");
1096                                                         break;
1097
1098                                                 case 12:
1099                                                         sb.Append ("\\f");
1100                                                         break;
1101
1102                                                 case 13:
1103                                                         sb.Append ("\\r");
1104                                                         break;
1105
1106                                                 case 34:
1107                                                         sb.Append ("\\\"");
1108                                                         break;
1109
1110                                                 case 92:
1111                                                         sb.Append ("\\\\");
1112                                                         break;
1113
1114                                                 default:
1115                                                         sb.Append (c);
1116                                                         break;
1117                                         }
1118                         }
1119
1120                         if (addDoubleQuotes)
1121                                 sb.Append ('"');
1122
1123                         return sb.ToString ();
1124                 }
1125 #endif
1126                 public static string UrlPathEncode (string s)
1127                 {
1128                         if (s == null || s.Length == 0)
1129                                 return s;
1130
1131                         MemoryStream result = new MemoryStream ();
1132                         int length = s.Length;
1133                         for (int i = 0; i < length; i++) {
1134                                 UrlPathEncodeChar (s [i], result);
1135                         }
1136                         return Encoding.ASCII.GetString (result.ToArray ());
1137                 }
1138                 
1139                 static void UrlPathEncodeChar (char c, Stream result)
1140                 {
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]);
1149                                 }
1150                         }
1151                         else if (c == ' ') {
1152                                 result.WriteByte ((byte) '%');
1153                                 result.WriteByte ((byte) '2');
1154                                 result.WriteByte ((byte) '0');
1155                         }
1156                         else
1157                                 result.WriteByte ((byte) c);
1158                 }
1159
1160                 public static NameValueCollection ParseQueryString (string query)
1161                 {
1162                         return ParseQueryString (query, Encoding.UTF8);
1163                 }
1164
1165                 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
1166                 {
1167                         if (query == null)
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);
1175                                 
1176                         NameValueCollection result = new HttpQSCollection ();
1177                         ParseQueryString (query, encoding, result);
1178                         return result;
1179                 }
1180
1181                 internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
1182                 {
1183                         if (query.Length == 0)
1184                                 return;
1185
1186                         string decoded = HtmlDecode (query);
1187                         int decodedLength = decoded.Length;
1188                         int namePos = 0;
1189                         bool first = true;
1190                         while (namePos <= decodedLength) {
1191                                 int valuePos = -1, valueEnd = -1;
1192                                 for (int q = namePos; q < decodedLength; q++) {
1193                                         if (valuePos == -1 && decoded [q] == '=') {
1194                                                 valuePos = q + 1;
1195                                         } else if (decoded [q] == '&') {
1196                                                 valueEnd = q;
1197                                                 break;
1198                                         }
1199                                 }
1200
1201                                 if (first) {
1202                                         first = false;
1203                                         if (decoded [namePos] == '?')
1204                                                 namePos++;
1205                                 }
1206                                 
1207                                 string name, value;
1208                                 if (valuePos == -1) {
1209                                         name = null;
1210                                         valuePos = namePos;
1211                                 } else {
1212                                         name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
1213                                 }
1214                                 if (valueEnd < 0) {
1215                                         namePos = -1;
1216                                         valueEnd = decoded.Length;
1217                                 } else {
1218                                         namePos = valueEnd + 1;
1219                                 }
1220                                 value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
1221
1222                                 result.Add (name, value);
1223                                 if (namePos == -1)
1224                                         break;
1225                         }
1226                 }
1227                 #endregion // Methods
1228         }
1229 }
1230