2009-07-28 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
index 84727e75c9bdf286b5dc30a598646b6f3b68321a..fe54758eac0e2d5631dca95d962b402b2083d411 100644 (file)
@@ -1,13 +1,13 @@
-// \r
-// System.Web.HttpUtility\r
-//\r
-// Authors:\r
-//   Patrik Torstensson (Patrik.Torstensson@labs2.com)\r
-//   Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)\r
-//   Tim Coleman (tim@timcoleman.com)\r
-//   Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
-//\r
-
+// 
+// System.Web.HttpUtility
+//
+// Authors:
+//   Patrik Torstensson (Patrik.Torstensson@labs2.com)
+//   Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)
+//   Tim Coleman (tim@timcoleman.com)
+//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-using System;\r
-using System.Collections;\r
-using System.Globalization;\r
-using System.IO;\r
-using System.Text;\r
-using System.Web.Util;\r
-\r
-namespace System.Web {\r
-       public sealed class HttpUtility {\r
-\r
-               #region Fields\r
-       \r
-               static Hashtable entities;\r
-               static object lock_ = new object ();\r
-       \r
-               #endregion // Fields\r
-       \r
-               static Hashtable Entities {\r
-                       get {\r
-                               lock (lock_) {\r
-                                       if (entities == null)\r
-                                               InitEntities ();\r
-\r
-                                       return entities;\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               #region Constructors\r
-\r
-               static void InitEntities ()\r
-               {\r
-                       // Build the hash table of HTML entity references.  This list comes\r
-                       // from the HTML 4.01 W3C recommendation.\r
-                       entities = new Hashtable ();\r
-                       entities.Add ("nbsp", '\u00A0');\r
-                       entities.Add ("iexcl", '\u00A1');\r
-                       entities.Add ("cent", '\u00A2');\r
-                       entities.Add ("pound", '\u00A3');\r
-                       entities.Add ("curren", '\u00A4');\r
-                       entities.Add ("yen", '\u00A5');\r
-                       entities.Add ("brvbar", '\u00A6');\r
-                       entities.Add ("sect", '\u00A7');\r
-                       entities.Add ("uml", '\u00A8');\r
-                       entities.Add ("copy", '\u00A9');\r
-                       entities.Add ("ordf", '\u00AA');\r
-                       entities.Add ("laquo", '\u00AB');\r
-                       entities.Add ("not", '\u00AC');\r
-                       entities.Add ("shy", '\u00AD');\r
-                       entities.Add ("reg", '\u00AE');\r
-                       entities.Add ("macr", '\u00AF');\r
-                       entities.Add ("deg", '\u00B0');\r
-                       entities.Add ("plusmn", '\u00B1');\r
-                       entities.Add ("sup2", '\u00B2');\r
-                       entities.Add ("sup3", '\u00B3');\r
-                       entities.Add ("acute", '\u00B4');\r
-                       entities.Add ("micro", '\u00B5');\r
-                       entities.Add ("para", '\u00B6');\r
-                       entities.Add ("middot", '\u00B7');\r
-                       entities.Add ("cedil", '\u00B8');\r
-                       entities.Add ("sup1", '\u00B9');\r
-                       entities.Add ("ordm", '\u00BA');\r
-                       entities.Add ("raquo", '\u00BB');\r
-                       entities.Add ("frac14", '\u00BC');\r
-                       entities.Add ("frac12", '\u00BD');\r
-                       entities.Add ("frac34", '\u00BE');\r
-                       entities.Add ("iquest", '\u00BF');\r
-                       entities.Add ("Agrave", '\u00C0');\r
-                       entities.Add ("Aacute", '\u00C1');\r
-                       entities.Add ("Acirc", '\u00C2');\r
-                       entities.Add ("Atilde", '\u00C3');\r
-                       entities.Add ("Auml", '\u00C4');\r
-                       entities.Add ("Aring", '\u00C5');\r
-                       entities.Add ("AElig", '\u00C6');\r
-                       entities.Add ("Ccedil", '\u00C7');\r
-                       entities.Add ("Egrave", '\u00C8');\r
-                       entities.Add ("Eacute", '\u00C9');\r
-                       entities.Add ("Ecirc", '\u00CA');\r
-                       entities.Add ("Euml", '\u00CB');\r
-                       entities.Add ("Igrave", '\u00CC');\r
-                       entities.Add ("Iacute", '\u00CD');\r
-                       entities.Add ("Icirc", '\u00CE');\r
-                       entities.Add ("Iuml", '\u00CF');\r
-                       entities.Add ("ETH", '\u00D0');\r
-                       entities.Add ("Ntilde", '\u00D1');\r
-                       entities.Add ("Ograve", '\u00D2');\r
-                       entities.Add ("Oacute", '\u00D3');\r
-                       entities.Add ("Ocirc", '\u00D4');\r
-                       entities.Add ("Otilde", '\u00D5');\r
-                       entities.Add ("Ouml", '\u00D6');\r
-                       entities.Add ("times", '\u00D7');\r
-                       entities.Add ("Oslash", '\u00D8');\r
-                       entities.Add ("Ugrave", '\u00D9');\r
-                       entities.Add ("Uacute", '\u00DA');\r
-                       entities.Add ("Ucirc", '\u00DB');\r
-                       entities.Add ("Uuml", '\u00DC');\r
-                       entities.Add ("Yacute", '\u00DD');\r
-                       entities.Add ("THORN", '\u00DE');\r
-                       entities.Add ("szlig", '\u00DF');\r
-                       entities.Add ("agrave", '\u00E0');\r
-                       entities.Add ("aacute", '\u00E1');\r
-                       entities.Add ("acirc", '\u00E2');\r
-                       entities.Add ("atilde", '\u00E3');\r
-                       entities.Add ("auml", '\u00E4');\r
-                       entities.Add ("aring", '\u00E5');\r
-                       entities.Add ("aelig", '\u00E6');\r
-                       entities.Add ("ccedil", '\u00E7');\r
-                       entities.Add ("egrave", '\u00E8');\r
-                       entities.Add ("eacute", '\u00E9');\r
-                       entities.Add ("ecirc", '\u00EA');\r
-                       entities.Add ("euml", '\u00EB');\r
-                       entities.Add ("igrave", '\u00EC');\r
-                       entities.Add ("iacute", '\u00ED');\r
-                       entities.Add ("icirc", '\u00EE');\r
-                       entities.Add ("iuml", '\u00EF');\r
-                       entities.Add ("eth", '\u00F0');\r
-                       entities.Add ("ntilde", '\u00F1');\r
-                       entities.Add ("ograve", '\u00F2');\r
-                       entities.Add ("oacute", '\u00F3');\r
-                       entities.Add ("ocirc", '\u00F4');\r
-                       entities.Add ("otilde", '\u00F5');\r
-                       entities.Add ("ouml", '\u00F6');\r
-                       entities.Add ("divide", '\u00F7');\r
-                       entities.Add ("oslash", '\u00F8');\r
-                       entities.Add ("ugrave", '\u00F9');\r
-                       entities.Add ("uacute", '\u00FA');\r
-                       entities.Add ("ucirc", '\u00FB');\r
-                       entities.Add ("uuml", '\u00FC');\r
-                       entities.Add ("yacute", '\u00FD');\r
-                       entities.Add ("thorn", '\u00FE');\r
-                       entities.Add ("yuml", '\u00FF');\r
-                       entities.Add ("fnof", '\u0192');\r
-                       entities.Add ("Alpha", '\u0391');\r
-                       entities.Add ("Beta", '\u0392');\r
-                       entities.Add ("Gamma", '\u0393');\r
-                       entities.Add ("Delta", '\u0394');\r
-                       entities.Add ("Epsilon", '\u0395');\r
-                       entities.Add ("Zeta", '\u0396');\r
-                       entities.Add ("Eta", '\u0397');\r
-                       entities.Add ("Theta", '\u0398');\r
-                       entities.Add ("Iota", '\u0399');\r
-                       entities.Add ("Kappa", '\u039A');\r
-                       entities.Add ("Lambda", '\u039B');\r
-                       entities.Add ("Mu", '\u039C');\r
-                       entities.Add ("Nu", '\u039D');\r
-                       entities.Add ("Xi", '\u039E');\r
-                       entities.Add ("Omicron", '\u039F');\r
-                       entities.Add ("Pi", '\u03A0');\r
-                       entities.Add ("Rho", '\u03A1');\r
-                       entities.Add ("Sigma", '\u03A3');\r
-                       entities.Add ("Tau", '\u03A4');\r
-                       entities.Add ("Upsilon", '\u03A5');\r
-                       entities.Add ("Phi", '\u03A6');\r
-                       entities.Add ("Chi", '\u03A7');\r
-                       entities.Add ("Psi", '\u03A8');\r
-                       entities.Add ("Omega", '\u03A9');\r
-                       entities.Add ("alpha", '\u03B1');\r
-                       entities.Add ("beta", '\u03B2');\r
-                       entities.Add ("gamma", '\u03B3');\r
-                       entities.Add ("delta", '\u03B4');\r
-                       entities.Add ("epsilon", '\u03B5');\r
-                       entities.Add ("zeta", '\u03B6');\r
-                       entities.Add ("eta", '\u03B7');\r
-                       entities.Add ("theta", '\u03B8');\r
-                       entities.Add ("iota", '\u03B9');\r
-                       entities.Add ("kappa", '\u03BA');\r
-                       entities.Add ("lambda", '\u03BB');\r
-                       entities.Add ("mu", '\u03BC');\r
-                       entities.Add ("nu", '\u03BD');\r
-                       entities.Add ("xi", '\u03BE');\r
-                       entities.Add ("omicron", '\u03BF');\r
-                       entities.Add ("pi", '\u03C0');\r
-                       entities.Add ("rho", '\u03C1');\r
-                       entities.Add ("sigmaf", '\u03C2');\r
-                       entities.Add ("sigma", '\u03C3');\r
-                       entities.Add ("tau", '\u03C4');\r
-                       entities.Add ("upsilon", '\u03C5');\r
-                       entities.Add ("phi", '\u03C6');\r
-                       entities.Add ("chi", '\u03C7');\r
-                       entities.Add ("psi", '\u03C8');\r
-                       entities.Add ("omega", '\u03C9');\r
-                       entities.Add ("thetasym", '\u03D1');\r
-                       entities.Add ("upsih", '\u03D2');\r
-                       entities.Add ("piv", '\u03D6');\r
-                       entities.Add ("bull", '\u2022');\r
-                       entities.Add ("hellip", '\u2026');\r
-                       entities.Add ("prime", '\u2032');\r
-                       entities.Add ("Prime", '\u2033');\r
-                       entities.Add ("oline", '\u203E');\r
-                       entities.Add ("frasl", '\u2044');\r
-                       entities.Add ("weierp", '\u2118');\r
-                       entities.Add ("image", '\u2111');\r
-                       entities.Add ("real", '\u211C');\r
-                       entities.Add ("trade", '\u2122');\r
-                       entities.Add ("alefsym", '\u2135');\r
-                       entities.Add ("larr", '\u2190');\r
-                       entities.Add ("uarr", '\u2191');\r
-                       entities.Add ("rarr", '\u2192');\r
-                       entities.Add ("darr", '\u2193');\r
-                       entities.Add ("harr", '\u2194');\r
-                       entities.Add ("crarr", '\u21B5');\r
-                       entities.Add ("lArr", '\u21D0');\r
-                       entities.Add ("uArr", '\u21D1');\r
-                       entities.Add ("rArr", '\u21D2');\r
-                       entities.Add ("dArr", '\u21D3');\r
-                       entities.Add ("hArr", '\u21D4');\r
-                       entities.Add ("forall", '\u2200');\r
-                       entities.Add ("part", '\u2202');\r
-                       entities.Add ("exist", '\u2203');\r
-                       entities.Add ("empty", '\u2205');\r
-                       entities.Add ("nabla", '\u2207');\r
-                       entities.Add ("isin", '\u2208');\r
-                       entities.Add ("notin", '\u2209');\r
-                       entities.Add ("ni", '\u220B');\r
-                       entities.Add ("prod", '\u220F');\r
-                       entities.Add ("sum", '\u2211');\r
-                       entities.Add ("minus", '\u2212');\r
-                       entities.Add ("lowast", '\u2217');\r
-                       entities.Add ("radic", '\u221A');\r
-                       entities.Add ("prop", '\u221D');\r
-                       entities.Add ("infin", '\u221E');\r
-                       entities.Add ("ang", '\u2220');\r
-                       entities.Add ("and", '\u2227');\r
-                       entities.Add ("or", '\u2228');\r
-                       entities.Add ("cap", '\u2229');\r
-                       entities.Add ("cup", '\u222A');\r
-                       entities.Add ("int", '\u222B');\r
-                       entities.Add ("there4", '\u2234');\r
-                       entities.Add ("sim", '\u223C');\r
-                       entities.Add ("cong", '\u2245');\r
-                       entities.Add ("asymp", '\u2248');\r
-                       entities.Add ("ne", '\u2260');\r
-                       entities.Add ("equiv", '\u2261');\r
-                       entities.Add ("le", '\u2264');\r
-                       entities.Add ("ge", '\u2265');\r
-                       entities.Add ("sub", '\u2282');\r
-                       entities.Add ("sup", '\u2283');\r
-                       entities.Add ("nsub", '\u2284');\r
-                       entities.Add ("sube", '\u2286');\r
-                       entities.Add ("supe", '\u2287');\r
-                       entities.Add ("oplus", '\u2295');\r
-                       entities.Add ("otimes", '\u2297');\r
-                       entities.Add ("perp", '\u22A5');\r
-                       entities.Add ("sdot", '\u22C5');\r
-                       entities.Add ("lceil", '\u2308');\r
-                       entities.Add ("rceil", '\u2309');\r
-                       entities.Add ("lfloor", '\u230A');\r
-                       entities.Add ("rfloor", '\u230B');\r
-                       entities.Add ("lang", '\u2329');\r
-                       entities.Add ("rang", '\u232A');\r
-                       entities.Add ("loz", '\u25CA');\r
-                       entities.Add ("spades", '\u2660');\r
-                       entities.Add ("clubs", '\u2663');\r
-                       entities.Add ("hearts", '\u2665');\r
-                       entities.Add ("diams", '\u2666');\r
-                       entities.Add ("quot", '\u0022');\r
-                       entities.Add ("amp", '\u0026');\r
-                       entities.Add ("lt", '\u003C');\r
-                       entities.Add ("gt", '\u003E');\r
-                       entities.Add ("OElig", '\u0152');\r
-                       entities.Add ("oelig", '\u0153');\r
-                       entities.Add ("Scaron", '\u0160');\r
-                       entities.Add ("scaron", '\u0161');\r
-                       entities.Add ("Yuml", '\u0178');\r
-                       entities.Add ("circ", '\u02C6');\r
-                       entities.Add ("tilde", '\u02DC');\r
-                       entities.Add ("ensp", '\u2002');\r
-                       entities.Add ("emsp", '\u2003');\r
-                       entities.Add ("thinsp", '\u2009');\r
-                       entities.Add ("zwnj", '\u200C');\r
-                       entities.Add ("zwj", '\u200D');\r
-                       entities.Add ("lrm", '\u200E');\r
-                       entities.Add ("rlm", '\u200F');\r
-                       entities.Add ("ndash", '\u2013');\r
-                       entities.Add ("mdash", '\u2014');\r
-                       entities.Add ("lsquo", '\u2018');\r
-                       entities.Add ("rsquo", '\u2019');\r
-                       entities.Add ("sbquo", '\u201A');\r
-                       entities.Add ("ldquo", '\u201C');\r
-                       entities.Add ("rdquo", '\u201D');\r
-                       entities.Add ("bdquo", '\u201E');\r
-                       entities.Add ("dagger", '\u2020');\r
-                       entities.Add ("Dagger", '\u2021');\r
-                       entities.Add ("permil", '\u2030');\r
-                       entities.Add ("lsaquo", '\u2039');\r
-                       entities.Add ("rsaquo", '\u203A');\r
-                       entities.Add ("euro", '\u20AC');\r
-               }\r
-       \r
-               public HttpUtility () \r
-               {\r
-               }\r
-       \r
-               #endregion // Constructors\r
-       \r
-               #region Methods\r
-       \r
-               public static void HtmlAttributeEncode (string s, TextWriter output) \r
-               {\r
-                       output.Write(HtmlAttributeEncode(s));\r
-               }\r
-       \r
-               public static string HtmlAttributeEncode (string s) \r
-               {\r
-                       if (null == s) \r
-                               return null;\r
-       \r
-                       if (s.IndexOf ('&') == -1 && s.IndexOf ('"') == -1)\r
-                               return s;\r
-\r
-                       StringBuilder output = new StringBuilder ();\r
-                       foreach (char c in s) \r
-                               switch (c) {\r
-                               case '&' : \r
-                                       output.Append ("&amp;");\r
-                                       break;\r
-                               case '"' :\r
-                                       output.Append ("&quot;");\r
-                                       break;\r
-                               default:\r
-                                       output.Append (c);\r
-                                       break;\r
-                               }\r
-       \r
-                       return output.ToString();\r
-               }\r
-       \r
-               public static string UrlDecode (string str) \r
-               {\r
-                       return UrlDecode(str, Encoding.UTF8);\r
-               }\r
-       \r
-               private static char [] GetChars (MemoryStream b, Encoding e)\r
-               {\r
-                       return e.GetChars (b.GetBuffer (), 0, (int) b.Length);\r
-               }\r
-               \r
-               public static string UrlDecode (string s, Encoding e)\r
-               {\r
-                       if (null == s) \r
-                               return null;\r
-\r
-                       if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)\r
-                               return s;\r
-\r
-                       if (e == null)\r
-                               e = Encoding.UTF8;\r
-       \r
-                       StringBuilder output = new StringBuilder ();\r
-                       long len = s.Length;\r
-                       NumberStyles hexa = NumberStyles.HexNumber;\r
-                       MemoryStream bytes = new MemoryStream ();\r
-       \r
-                       for (int i = 0; i < len; i++) {\r
-                               if (s [i] == '%' && i + 2 < len) {\r
-                                       if (s [i + 1] == 'u' && i + 5 < len) {\r
-                                               if (bytes.Length > 0) {\r
-                                                       output.Append (GetChars (bytes, e));\r
-                                                       bytes.SetLength (0);\r
-                                               }\r
-                                               output.Append ((char) Int32.Parse (s.Substring (i + 2, 4), hexa));\r
-                                               i += 5;\r
-                                       } else {\r
-                                               bytes.WriteByte ((byte) Int32.Parse (s.Substring (i + 1, 2), hexa));\r
-                                               i += 2;\r
-                                       }\r
-                                       continue;\r
-                               }\r
-\r
-                               if (bytes.Length > 0) {\r
-                                       output.Append (GetChars (bytes, e));\r
-                                       bytes.SetLength (0);\r
-                               }\r
-\r
-                               if (s [i] == '+') {\r
-                                       output.Append (' ');\r
-                               } else {\r
-                                       output.Append (s [i]);\r
-                               }\r
-                       }\r
-       \r
-                       if (bytes.Length > 0) {\r
-                               output.Append (GetChars (bytes, e));\r
-                       }\r
-\r
-                       bytes = null;\r
-                       return output.ToString ();\r
-               }\r
-       \r
-               public static string UrlDecode (byte [] bytes, Encoding e)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       return UrlDecode (bytes, 0, bytes.Length, e);\r
-               }\r
-\r
-               private static int GetInt (byte b)\r
-               {\r
-                       char c = Char.ToUpper ((char) b);\r
-                       if (c >= '0' && c <= '9')\r
-                               return c - '0';\r
-\r
-                       if (c < 'A' || c > 'F')\r
-                               return 0;\r
-\r
-                       return (c - 'A' + 10);\r
-               }\r
-\r
-               private static char GetChar (byte [] bytes, int offset, int length)\r
-               {\r
-                       int value = 0;\r
-                       int end = length + offset;\r
-                       for (int i = offset; i < end; i++)\r
-                               value = (value << 4) + GetInt (bytes [i]);\r
-\r
-                       return (char) value;\r
-               }\r
-               \r
-               public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)\r
-               {\r
-                       if (bytes == null || count == 0)\r
-                               return null;\r
-\r
-                       if (bytes == null)\r
-                               throw new ArgumentNullException ("bytes");\r
-\r
-                       if (offset < 0 || offset > bytes.Length)\r
-                               throw new ArgumentOutOfRangeException ("offset");\r
-\r
-                       if (count < 0 || offset + count > bytes.Length)\r
-                               throw new ArgumentOutOfRangeException ("count");\r
-\r
-                       StringBuilder output = new StringBuilder ();\r
-                       MemoryStream acc = new MemoryStream ();\r
-\r
-                       int end = count + offset;\r
-                       for (int i = offset; i < end; i++) {\r
-                               if (bytes [i] == '%' && i + 2 < count) {\r
-                                       if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {\r
-                                               if (acc.Length > 0) {\r
-                                                       output.Append (GetChars (acc, e));\r
-                                                       acc.SetLength (0);\r
-                                               }\r
-                                               output.Append (GetChar (bytes, i + 2, 4));\r
-                                               i += 5;\r
-                                       } else {\r
-                                               acc.WriteByte ((byte) GetChar (bytes, i + 1, 2));\r
-                                               i += 2;\r
-                                       }\r
-                                       continue;\r
-                               }\r
-\r
-                               if (acc.Length > 0) {\r
-                                       output.Append (GetChars (acc, e));\r
-                                       acc.SetLength (0);\r
-                               }\r
-\r
-                               if (bytes [i] == '+') {\r
-                                       output.Append (' ');\r
-                               } else {\r
-                                       output.Append ((char) bytes [i]);\r
-                               }\r
-                       }\r
-\r
-                       if (acc.Length > 0) {\r
-                               output.Append (GetChars (acc, e));\r
-                       }\r
-                       \r
-                       acc = null;\r
-                       return output.ToString ();\r
-               }\r
-       \r
-               public static byte [] UrlDecodeToBytes (byte [] bytes)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       return UrlDecodeToBytes (bytes, 0, bytes.Length);\r
-               }\r
-\r
-               public static byte [] UrlDecodeToBytes (string str)\r
-               {\r
-                       return UrlDecodeToBytes (str, Encoding.UTF8);\r
-               }\r
-\r
-               public static byte [] UrlDecodeToBytes (string str, Encoding e)\r
-               {\r
-                       if (str == null)\r
-                               return null;\r
-\r
-                       if (e == null)\r
-                               throw new ArgumentNullException ("e");\r
-\r
-                       return UrlDecodeToBytes (e.GetBytes (str));\r
-               }\r
-\r
-               public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       int len = bytes.Length;\r
-                       if (offset < 0 || offset >= len)\r
-                               throw new ArgumentOutOfRangeException("offset");\r
-\r
-                       if (count < 0 || offset > len - count)\r
-                               throw new ArgumentOutOfRangeException("count");\r
-\r
-                       MemoryStream result = new MemoryStream ();\r
-                       int end = offset + count;\r
-                       for (int i = offset; i < end; i++){\r
-                               char c = (char) bytes [i];\r
-                               if (c == '+')\r
-                                       c = ' ';\r
-                               else if (c == '%' && i < end - 2) {\r
-                                       c = GetChar (bytes, i, 2);\r
-                                       i += 2;\r
-                               }\r
-                               result.WriteByte ((byte) c);\r
-                       }\r
-\r
-                       return result.ToArray ();\r
-               }\r
-\r
-               public static string UrlEncode(string str) \r
-               {\r
-                       return UrlEncode(str, Encoding.UTF8);\r
-               }\r
-       \r
-               public static string UrlEncode (string s, Encoding Enc) \r
-               {\r
-                       if (s == null)\r
-                               return null;\r
-\r
-                       if (s == "")\r
-                               return "";\r
-\r
-                       byte [] bytes = Enc.GetBytes (s);\r
-                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));\r
-               }\r
-         \r
-               public static string UrlEncode (byte [] bytes)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       if (bytes.Length == 0)\r
-                               return "";\r
-\r
-                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));\r
-               }\r
-\r
-               public static string UrlEncode (byte [] bytes, int offset, int count)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       if (bytes.Length == 0)\r
-                               return "";\r
-\r
-                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));\r
-               }\r
-\r
-               public static byte [] UrlEncodeToBytes (string str)\r
-               {\r
-                       return UrlEncodeToBytes (str, Encoding.UTF8);\r
-               }\r
-\r
-               public static byte [] UrlEncodeToBytes (string str, Encoding e)\r
-               {\r
-                       if (str == null)\r
-                               return null;\r
-\r
-                       if (str == "")\r
-                               return new byte [0];\r
-\r
-                       byte [] bytes = e.GetBytes (str);\r
-                       return UrlEncodeToBytes (bytes, 0, bytes.Length);\r
-               }\r
-\r
-               public static byte [] UrlEncodeToBytes (byte [] bytes)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       if (bytes.Length == 0)\r
-                               return new byte [0];\r
-\r
-                       return UrlEncodeToBytes (bytes, 0, bytes.Length);\r
-               }\r
-\r
-               static char [] hexChars = "0123456789abcdef".ToCharArray ();\r
-\r
-               public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)\r
-               {\r
-                       if (bytes == null)\r
-                               return null;\r
-\r
-                       int len = bytes.Length;\r
-                       if (len == 0)\r
-                               return new byte [0];\r
-\r
-                       if (offset < 0 || offset >= len)\r
-                               throw new ArgumentOutOfRangeException("offset");\r
-\r
-                       if (count < 0 || count > len - offset)\r
-                               throw new ArgumentOutOfRangeException("count");\r
-\r
-                       MemoryStream result = new MemoryStream ();\r
-                       int end = offset + count;\r
-                       for (int i = offset; i < end; i++) {\r
-                               char c = (char) bytes [i];\r
-                               if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||\r
-                                   (c < 'A' && c > '9') ||\r
-                                   (c > 'Z' && c < 'a' && c != '_') ||\r
-                                   (c > 'z')) {\r
-                                       result.WriteByte ((byte) '%');\r
-                                       int idx = ((int) c) >> 4;\r
-                                       result.WriteByte ((byte) hexChars [idx]);\r
-                                       idx = ((int) c) & 0x0F;\r
-                                       result.WriteByte ((byte) hexChars [idx]);\r
-                               } else {\r
-                                       result.WriteByte ((byte) c);\r
-                               }\r
-                       }\r
-\r
-                       return result.ToArray ();\r
-               }\r
-\r
-               public static string UrlEncodeUnicode (string str)\r
-               {\r
-                       if (str == null)\r
-                               return null;\r
-\r
-                       StringBuilder result = new StringBuilder ();\r
-                       int end = str.Length;\r
-                       for (int i = 0; i < end; i++) {\r
-                               int idx;\r
-                               char c = str [i];\r
-                               if (c > 255) {\r
-                                       result.Append ("%u");\r
-                                       idx = ((int) c) >> 24;\r
-                                       result.Append (hexChars [idx]);\r
-                                       idx = (((int) c) >> 16) & 0x0F;\r
-                                       result.Append (hexChars [idx]);\r
-                                       idx = (((int) c) >> 8) & 0x0F;\r
-                                       result.Append (hexChars [idx]);\r
-                                       idx = ((int) c) & 0x0F;\r
-                                       result.Append (hexChars [idx]);\r
-                                       continue;\r
-                               }\r
-                               \r
-                               if ((c == ' ') || (c < '0' && c != '-' && c != '.') ||\r
-                                   (c < 'A' && c > '9') ||\r
-                                   (c > 'Z' && c < 'a' && c != '_') ||\r
-                                   (c > 'z')) {\r
-                                       result.Append ('%');\r
-                                       idx = ((int) c) >> 4;\r
-                                       result.Append (hexChars [idx]);\r
-                                       idx = ((int) c) & 0x0F;\r
-                                       result.Append (hexChars [idx]);\r
-                                       continue;\r
-                               }\r
-\r
-                               result.Append (c);\r
-                       }\r
-\r
-                       return result.ToString ();\r
-               }\r
-\r
-               public static byte [] UrlEncodeUnicodeToBytes (string str)\r
-               {\r
-                       if (str == null)\r
-                               return null;\r
-\r
-                       if (str == "")\r
-                               return new byte [0];\r
-\r
-                       return Encoding.ASCII.GetBytes (UrlEncodeUnicode (str));\r
-               }\r
-\r
-               /// <summary>\r
-               /// Decodes an HTML-encoded string and returns the decoded string.\r
-               /// </summary>\r
-               /// <param name="s">The HTML string to decode. </param>\r
-               /// <returns>The decoded text.</returns>\r
-               public static string HtmlDecode (string s) \r
-               {\r
-                       if (s == null)\r
-                               throw new ArgumentNullException ("s");\r
-\r
-                       if (s.IndexOf ('&') == -1)\r
-                               return s;\r
-\r
-                       bool insideEntity = false; // used to indicate that we are in a potential entity\r
-                       string entity = String.Empty;\r
-                       StringBuilder output = new StringBuilder ();\r
-                       int len = s.Length;\r
-       \r
-                       for (int i = 0; i < len; i++) {\r
-                               char c = s [i];\r
-                               switch (c) {\r
-                               case '&' :\r
-                                       output.Append (entity);\r
-                                       entity = "&";\r
-                                       insideEntity = true;\r
-                                       break;\r
-                               case ';' :\r
-                                       if (!insideEntity) {\r
-                                               output.Append (c);\r
-                                               break;\r
-                                       }\r
-\r
-                                       entity += c;\r
-                                       int length = entity.Length;\r
-                                       if (length >= 2 && entity[1] == '#' && entity[2] != ';')\r
-                                               entity = ((char) Int32.Parse (entity.Substring (2, entity.Length - 3))).ToString();\r
-                                       else if (length > 1 && Entities.ContainsKey (entity.Substring (1, entity.Length - 2)))\r
-                                               entity = Entities [entity.Substring (1, entity.Length - 2)].ToString ();\r
-                                       \r
-                                       output.Append (entity);\r
-                                       entity = String.Empty;\r
-                                       insideEntity = false;\r
-                                       break;\r
-                               default :\r
-                                       if (insideEntity)\r
-                                               entity += c;\r
-                                       else\r
-                                               output.Append (c);\r
-                                       break;\r
-                               }\r
-                       }\r
-                       output.Append (entity);\r
-                       return output.ToString ();\r
-               }\r
-       \r
-               /// <summary>\r
-               /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.\r
-               /// </summary>\r
-               /// <param name="s">The HTML string to decode</param>\r
-               /// <param name="output">The TextWriter output stream containing the decoded string. </param>\r
-               public static void HtmlDecode(string s, TextWriter output) \r
-               {\r
-                       if (s != null)\r
-                               output.Write (HtmlDecode (s));\r
-               }\r
-       \r
-               /// <summary>\r
-               /// HTML-encodes a string and returns the encoded string.\r
-               /// </summary>\r
-               /// <param name="s">The text string to encode. </param>\r
-               /// <returns>The HTML-encoded text.</returns>\r
-               public static string HtmlEncode (string s) \r
-               {\r
-                       if (s == null)\r
-                               return null;\r
-\r
-                       StringBuilder output = new StringBuilder ();\r
-                       \r
-                       foreach (char c in s) \r
-                               switch (c) {\r
-                               case '&' :\r
-                                       output.Append ("&amp;");\r
-                                       break;\r
-                               case '>' : \r
-                                       output.Append ("&gt;");\r
-                                       break;\r
-                               case '<' :\r
-                                       output.Append ("&lt;");\r
-                                       break;\r
-                               case '"' :\r
-                                       output.Append ("&quot;");\r
-                                       break;\r
-                               default:\r
-                                       if ((int) c > 128) {\r
-                                               output.Append ("&#");\r
-                                               output.Append (((int) c).ToString ());\r
-                                               output.Append (";");\r
-                                       }\r
-                                       else\r
-                                               output.Append (c);\r
-                                       break;\r
-                               }\r
-                       return output.ToString ();\r
-               }\r
-       \r
-               /// <summary>\r
-               /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.\r
-               /// </summary>\r
-               /// <param name="s">The string to encode. </param>\r
-               /// <param name="output">The TextWriter output stream containing the encoded string. </param>\r
-               public static void HtmlEncode(string s, TextWriter output) \r
-               {\r
-                       if (s != null)\r
-                               output.Write (HtmlEncode (s));\r
-               }\r
-\r
-#if NET_1_1\r
-               public static string UrlPathEncode (string s)\r
-               {\r
-                       if (s == null)\r
-                               return null;\r
-\r
-                       int idx = s.IndexOf ("?");\r
-                       string s2 = null;\r
-                       if (idx != -1) {\r
-                               s2 = s.Substring (0, idx-1);\r
-                               s2 = UrlEncode (s2) + s.Substring (idx);\r
-                       } else {\r
-                               s2 = UrlEncode (s);\r
-                       }\r
-\r
-                       return s2;\r
-               }\r
-#endif\r
-               #endregion // Methods\r
-       }\r
-}\r
+
+using System.Collections;
+using System.Collections.Specialized;
+using System.Globalization;
+using System.IO;
+using System.Security.Permissions;
+using System.Text;
+using System.Web.Util;
+
+#if NET_2_0
+using System.Collections.Generic;
+#endif
+
+namespace System.Web {
+
+       // CAS - no InheritanceDemand here as the class is sealed
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public sealed class HttpUtility {
+               #region Fields
+       
+               static Hashtable entities;
+               static object lock_ = new object ();
+       
+               #endregion // Fields
+       
+               static Hashtable Entities {
+                       get {
+                               lock (lock_) {
+                                       if (entities == null)
+                                               InitEntities ();
+
+                                       return entities;
+                               }
+                       }
+               }
+               
+               #region Constructors
+
+               static void InitEntities ()
+               {
+                       // Build the hash table of HTML entity references.  This list comes
+                       // from the HTML 4.01 W3C recommendation.
+                       entities = new Hashtable ();
+                       entities.Add ("nbsp", '\u00A0');
+                       entities.Add ("iexcl", '\u00A1');
+                       entities.Add ("cent", '\u00A2');
+                       entities.Add ("pound", '\u00A3');
+                       entities.Add ("curren", '\u00A4');
+                       entities.Add ("yen", '\u00A5');
+                       entities.Add ("brvbar", '\u00A6');
+                       entities.Add ("sect", '\u00A7');
+                       entities.Add ("uml", '\u00A8');
+                       entities.Add ("copy", '\u00A9');
+                       entities.Add ("ordf", '\u00AA');
+                       entities.Add ("laquo", '\u00AB');
+                       entities.Add ("not", '\u00AC');
+                       entities.Add ("shy", '\u00AD');
+                       entities.Add ("reg", '\u00AE');
+                       entities.Add ("macr", '\u00AF');
+                       entities.Add ("deg", '\u00B0');
+                       entities.Add ("plusmn", '\u00B1');
+                       entities.Add ("sup2", '\u00B2');
+                       entities.Add ("sup3", '\u00B3');
+                       entities.Add ("acute", '\u00B4');
+                       entities.Add ("micro", '\u00B5');
+                       entities.Add ("para", '\u00B6');
+                       entities.Add ("middot", '\u00B7');
+                       entities.Add ("cedil", '\u00B8');
+                       entities.Add ("sup1", '\u00B9');
+                       entities.Add ("ordm", '\u00BA');
+                       entities.Add ("raquo", '\u00BB');
+                       entities.Add ("frac14", '\u00BC');
+                       entities.Add ("frac12", '\u00BD');
+                       entities.Add ("frac34", '\u00BE');
+                       entities.Add ("iquest", '\u00BF');
+                       entities.Add ("Agrave", '\u00C0');
+                       entities.Add ("Aacute", '\u00C1');
+                       entities.Add ("Acirc", '\u00C2');
+                       entities.Add ("Atilde", '\u00C3');
+                       entities.Add ("Auml", '\u00C4');
+                       entities.Add ("Aring", '\u00C5');
+                       entities.Add ("AElig", '\u00C6');
+                       entities.Add ("Ccedil", '\u00C7');
+                       entities.Add ("Egrave", '\u00C8');
+                       entities.Add ("Eacute", '\u00C9');
+                       entities.Add ("Ecirc", '\u00CA');
+                       entities.Add ("Euml", '\u00CB');
+                       entities.Add ("Igrave", '\u00CC');
+                       entities.Add ("Iacute", '\u00CD');
+                       entities.Add ("Icirc", '\u00CE');
+                       entities.Add ("Iuml", '\u00CF');
+                       entities.Add ("ETH", '\u00D0');
+                       entities.Add ("Ntilde", '\u00D1');
+                       entities.Add ("Ograve", '\u00D2');
+                       entities.Add ("Oacute", '\u00D3');
+                       entities.Add ("Ocirc", '\u00D4');
+                       entities.Add ("Otilde", '\u00D5');
+                       entities.Add ("Ouml", '\u00D6');
+                       entities.Add ("times", '\u00D7');
+                       entities.Add ("Oslash", '\u00D8');
+                       entities.Add ("Ugrave", '\u00D9');
+                       entities.Add ("Uacute", '\u00DA');
+                       entities.Add ("Ucirc", '\u00DB');
+                       entities.Add ("Uuml", '\u00DC');
+                       entities.Add ("Yacute", '\u00DD');
+                       entities.Add ("THORN", '\u00DE');
+                       entities.Add ("szlig", '\u00DF');
+                       entities.Add ("agrave", '\u00E0');
+                       entities.Add ("aacute", '\u00E1');
+                       entities.Add ("acirc", '\u00E2');
+                       entities.Add ("atilde", '\u00E3');
+                       entities.Add ("auml", '\u00E4');
+                       entities.Add ("aring", '\u00E5');
+                       entities.Add ("aelig", '\u00E6');
+                       entities.Add ("ccedil", '\u00E7');
+                       entities.Add ("egrave", '\u00E8');
+                       entities.Add ("eacute", '\u00E9');
+                       entities.Add ("ecirc", '\u00EA');
+                       entities.Add ("euml", '\u00EB');
+                       entities.Add ("igrave", '\u00EC');
+                       entities.Add ("iacute", '\u00ED');
+                       entities.Add ("icirc", '\u00EE');
+                       entities.Add ("iuml", '\u00EF');
+                       entities.Add ("eth", '\u00F0');
+                       entities.Add ("ntilde", '\u00F1');
+                       entities.Add ("ograve", '\u00F2');
+                       entities.Add ("oacute", '\u00F3');
+                       entities.Add ("ocirc", '\u00F4');
+                       entities.Add ("otilde", '\u00F5');
+                       entities.Add ("ouml", '\u00F6');
+                       entities.Add ("divide", '\u00F7');
+                       entities.Add ("oslash", '\u00F8');
+                       entities.Add ("ugrave", '\u00F9');
+                       entities.Add ("uacute", '\u00FA');
+                       entities.Add ("ucirc", '\u00FB');
+                       entities.Add ("uuml", '\u00FC');
+                       entities.Add ("yacute", '\u00FD');
+                       entities.Add ("thorn", '\u00FE');
+                       entities.Add ("yuml", '\u00FF');
+                       entities.Add ("fnof", '\u0192');
+                       entities.Add ("Alpha", '\u0391');
+                       entities.Add ("Beta", '\u0392');
+                       entities.Add ("Gamma", '\u0393');
+                       entities.Add ("Delta", '\u0394');
+                       entities.Add ("Epsilon", '\u0395');
+                       entities.Add ("Zeta", '\u0396');
+                       entities.Add ("Eta", '\u0397');
+                       entities.Add ("Theta", '\u0398');
+                       entities.Add ("Iota", '\u0399');
+                       entities.Add ("Kappa", '\u039A');
+                       entities.Add ("Lambda", '\u039B');
+                       entities.Add ("Mu", '\u039C');
+                       entities.Add ("Nu", '\u039D');
+                       entities.Add ("Xi", '\u039E');
+                       entities.Add ("Omicron", '\u039F');
+                       entities.Add ("Pi", '\u03A0');
+                       entities.Add ("Rho", '\u03A1');
+                       entities.Add ("Sigma", '\u03A3');
+                       entities.Add ("Tau", '\u03A4');
+                       entities.Add ("Upsilon", '\u03A5');
+                       entities.Add ("Phi", '\u03A6');
+                       entities.Add ("Chi", '\u03A7');
+                       entities.Add ("Psi", '\u03A8');
+                       entities.Add ("Omega", '\u03A9');
+                       entities.Add ("alpha", '\u03B1');
+                       entities.Add ("beta", '\u03B2');
+                       entities.Add ("gamma", '\u03B3');
+                       entities.Add ("delta", '\u03B4');
+                       entities.Add ("epsilon", '\u03B5');
+                       entities.Add ("zeta", '\u03B6');
+                       entities.Add ("eta", '\u03B7');
+                       entities.Add ("theta", '\u03B8');
+                       entities.Add ("iota", '\u03B9');
+                       entities.Add ("kappa", '\u03BA');
+                       entities.Add ("lambda", '\u03BB');
+                       entities.Add ("mu", '\u03BC');
+                       entities.Add ("nu", '\u03BD');
+                       entities.Add ("xi", '\u03BE');
+                       entities.Add ("omicron", '\u03BF');
+                       entities.Add ("pi", '\u03C0');
+                       entities.Add ("rho", '\u03C1');
+                       entities.Add ("sigmaf", '\u03C2');
+                       entities.Add ("sigma", '\u03C3');
+                       entities.Add ("tau", '\u03C4');
+                       entities.Add ("upsilon", '\u03C5');
+                       entities.Add ("phi", '\u03C6');
+                       entities.Add ("chi", '\u03C7');
+                       entities.Add ("psi", '\u03C8');
+                       entities.Add ("omega", '\u03C9');
+                       entities.Add ("thetasym", '\u03D1');
+                       entities.Add ("upsih", '\u03D2');
+                       entities.Add ("piv", '\u03D6');
+                       entities.Add ("bull", '\u2022');
+                       entities.Add ("hellip", '\u2026');
+                       entities.Add ("prime", '\u2032');
+                       entities.Add ("Prime", '\u2033');
+                       entities.Add ("oline", '\u203E');
+                       entities.Add ("frasl", '\u2044');
+                       entities.Add ("weierp", '\u2118');
+                       entities.Add ("image", '\u2111');
+                       entities.Add ("real", '\u211C');
+                       entities.Add ("trade", '\u2122');
+                       entities.Add ("alefsym", '\u2135');
+                       entities.Add ("larr", '\u2190');
+                       entities.Add ("uarr", '\u2191');
+                       entities.Add ("rarr", '\u2192');
+                       entities.Add ("darr", '\u2193');
+                       entities.Add ("harr", '\u2194');
+                       entities.Add ("crarr", '\u21B5');
+                       entities.Add ("lArr", '\u21D0');
+                       entities.Add ("uArr", '\u21D1');
+                       entities.Add ("rArr", '\u21D2');
+                       entities.Add ("dArr", '\u21D3');
+                       entities.Add ("hArr", '\u21D4');
+                       entities.Add ("forall", '\u2200');
+                       entities.Add ("part", '\u2202');
+                       entities.Add ("exist", '\u2203');
+                       entities.Add ("empty", '\u2205');
+                       entities.Add ("nabla", '\u2207');
+                       entities.Add ("isin", '\u2208');
+                       entities.Add ("notin", '\u2209');
+                       entities.Add ("ni", '\u220B');
+                       entities.Add ("prod", '\u220F');
+                       entities.Add ("sum", '\u2211');
+                       entities.Add ("minus", '\u2212');
+                       entities.Add ("lowast", '\u2217');
+                       entities.Add ("radic", '\u221A');
+                       entities.Add ("prop", '\u221D');
+                       entities.Add ("infin", '\u221E');
+                       entities.Add ("ang", '\u2220');
+                       entities.Add ("and", '\u2227');
+                       entities.Add ("or", '\u2228');
+                       entities.Add ("cap", '\u2229');
+                       entities.Add ("cup", '\u222A');
+                       entities.Add ("int", '\u222B');
+                       entities.Add ("there4", '\u2234');
+                       entities.Add ("sim", '\u223C');
+                       entities.Add ("cong", '\u2245');
+                       entities.Add ("asymp", '\u2248');
+                       entities.Add ("ne", '\u2260');
+                       entities.Add ("equiv", '\u2261');
+                       entities.Add ("le", '\u2264');
+                       entities.Add ("ge", '\u2265');
+                       entities.Add ("sub", '\u2282');
+                       entities.Add ("sup", '\u2283');
+                       entities.Add ("nsub", '\u2284');
+                       entities.Add ("sube", '\u2286');
+                       entities.Add ("supe", '\u2287');
+                       entities.Add ("oplus", '\u2295');
+                       entities.Add ("otimes", '\u2297');
+                       entities.Add ("perp", '\u22A5');
+                       entities.Add ("sdot", '\u22C5');
+                       entities.Add ("lceil", '\u2308');
+                       entities.Add ("rceil", '\u2309');
+                       entities.Add ("lfloor", '\u230A');
+                       entities.Add ("rfloor", '\u230B');
+                       entities.Add ("lang", '\u2329');
+                       entities.Add ("rang", '\u232A');
+                       entities.Add ("loz", '\u25CA');
+                       entities.Add ("spades", '\u2660');
+                       entities.Add ("clubs", '\u2663');
+                       entities.Add ("hearts", '\u2665');
+                       entities.Add ("diams", '\u2666');
+                       entities.Add ("quot", '\u0022');
+                       entities.Add ("amp", '\u0026');
+                       entities.Add ("lt", '\u003C');
+                       entities.Add ("gt", '\u003E');
+                       entities.Add ("OElig", '\u0152');
+                       entities.Add ("oelig", '\u0153');
+                       entities.Add ("Scaron", '\u0160');
+                       entities.Add ("scaron", '\u0161');
+                       entities.Add ("Yuml", '\u0178');
+                       entities.Add ("circ", '\u02C6');
+                       entities.Add ("tilde", '\u02DC');
+                       entities.Add ("ensp", '\u2002');
+                       entities.Add ("emsp", '\u2003');
+                       entities.Add ("thinsp", '\u2009');
+                       entities.Add ("zwnj", '\u200C');
+                       entities.Add ("zwj", '\u200D');
+                       entities.Add ("lrm", '\u200E');
+                       entities.Add ("rlm", '\u200F');
+                       entities.Add ("ndash", '\u2013');
+                       entities.Add ("mdash", '\u2014');
+                       entities.Add ("lsquo", '\u2018');
+                       entities.Add ("rsquo", '\u2019');
+                       entities.Add ("sbquo", '\u201A');
+                       entities.Add ("ldquo", '\u201C');
+                       entities.Add ("rdquo", '\u201D');
+                       entities.Add ("bdquo", '\u201E');
+                       entities.Add ("dagger", '\u2020');
+                       entities.Add ("Dagger", '\u2021');
+                       entities.Add ("permil", '\u2030');
+                       entities.Add ("lsaquo", '\u2039');
+                       entities.Add ("rsaquo", '\u203A');
+                       entities.Add ("euro", '\u20AC');
+               }
+
+               public HttpUtility () 
+               {
+               }
+       
+               #endregion // Constructors
+       
+               #region Methods
+       
+               public static void HtmlAttributeEncode (string s, TextWriter output) 
+               {
+                       output.Write(HtmlAttributeEncode(s));
+               }
+       
+               public static string HtmlAttributeEncode (string s) 
+               {
+                       if (null == s) 
+                               return null;
+       
+                       bool needEncode = false;
+                       for (int i = 0; i < s.Length; i++) {
+                               if (s [i] == '&' || s [i] == '"' || s [i] == '<') {
+                                       needEncode = true;
+                                       break;
+                               }
+                       }
+
+                       if (!needEncode)
+                               return s;
+
+                       StringBuilder output = new StringBuilder ();
+                       int len = s.Length;
+                       for (int i = 0; i < len; i++)
+                               switch (s [i]) {
+                               case '&' : 
+                                       output.Append ("&amp;");
+                                       break;
+                               case '"' :
+                                       output.Append ("&quot;");
+                                       break;
+                               case '<':
+                                       output.Append ("&lt;");
+                                       break;
+                               default:
+                                       output.Append (s [i]);
+                                       break;
+                               }
+       
+                       return output.ToString();
+               }
+       
+               public static string UrlDecode (string str) 
+               {
+                       return UrlDecode(str, Encoding.UTF8);
+               }
+       
+               static char [] GetChars (MemoryStream b, Encoding e)
+               {
+                       return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
+               }
+
+               static void WriteCharBytes (IList buf, char ch, Encoding e)
+               {
+                       if (ch > 255) {
+                               foreach (byte b in e.GetBytes (new char[] { ch }))
+                                       buf.Add (b);
+                       } else
+                               buf.Add ((byte)ch);
+               }
+               
+               public static string UrlDecode (string s, Encoding e)
+               {
+                       if (null == s) 
+                               return null;
+
+                       if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
+                               return s;
+                       
+                       if (e == null)
+                               e = Encoding.UTF8;
+
+                       long len = s.Length;
+#if NET_2_0
+                       var bytes = new List <byte> ();
+#else
+                       ArrayList bytes = new ArrayList ();
+#endif
+                       int xchar;
+                       char ch;
+                       
+                       for (int i = 0; i < len; i++) {
+                               ch = s [i];
+                               if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
+                                       if (s [i + 1] == 'u' && i + 5 < len) {
+                                               // unicode hex sequence
+                                               xchar = GetChar (s, i + 2, 4);
+                                               if (xchar != -1) {
+                                                       WriteCharBytes (bytes, (char)xchar, e);
+                                                       i += 5;
+                                               } else
+                                                       WriteCharBytes (bytes, '%', e);
+                                       } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
+                                               WriteCharBytes (bytes, (char)xchar, e);
+                                               i += 2;
+                                       } else {
+                                               WriteCharBytes (bytes, '%', e);
+                                       }
+                                       continue;
+                               }
+
+                               if (ch == '+')
+                                       WriteCharBytes (bytes, ' ', e);
+                               else
+                                       WriteCharBytes (bytes, ch, e);
+                       }
+                       
+#if NET_2_0
+                       byte[] buf = bytes.ToArray ();
+#else
+                       byte[] buf = (byte[])bytes.ToArray (typeof (byte));
+#endif
+                       bytes = null;
+                       return e.GetString (buf);
+                       
+               }
+       
+               public static string UrlDecode (byte [] bytes, Encoding e)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       return UrlDecode (bytes, 0, bytes.Length, e);
+               }
+
+               static int GetInt (byte b)
+               {
+                       char c = (char) b;
+                       if (c >= '0' && c <= '9')
+                               return c - '0';
+
+                       if (c >= 'a' && c <= 'f')
+                               return c - 'a' + 10;
+
+                       if (c >= 'A' && c <= 'F')
+                               return c - 'A' + 10;
+
+                       return -1;
+               }
+
+               static int GetChar (byte [] bytes, int offset, int length)
+               {
+                       int value = 0;
+                       int end = length + offset;
+                       for (int i = offset; i < end; i++) {
+                               int current = GetInt (bytes [i]);
+                               if (current == -1)
+                                       return -1;
+                               value = (value << 4) + current;
+                       }
+
+                       return value;
+               }
+
+               static int GetChar (string str, int offset, int length)
+               {
+                       int val = 0;
+                       int end = length + offset;
+                       for (int i = offset; i < end; i++) {
+                               char c = str [i];
+                               if (c > 127)
+                                       return -1;
+
+                               int current = GetInt ((byte) c);
+                               if (current == -1)
+                                       return -1;
+                               val = (val << 4) + current;
+                       }
+
+                       return val;
+               }
+               
+               public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
+               {
+                       if (bytes == null)
+                               return null;
+                       if (count == 0)
+                               return String.Empty;
+
+                       if (bytes == null)
+                               throw new ArgumentNullException ("bytes");
+
+                       if (offset < 0 || offset > bytes.Length)
+                               throw new ArgumentOutOfRangeException ("offset");
+
+                       if (count < 0 || offset + count > bytes.Length)
+                               throw new ArgumentOutOfRangeException ("count");
+
+                       StringBuilder output = new StringBuilder ();
+                       MemoryStream acc = new MemoryStream ();
+
+                       int end = count + offset;
+                       int xchar;
+                       for (int i = offset; i < end; i++) {
+                               if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
+                                       if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
+                                               if (acc.Length > 0) {
+                                                       output.Append (GetChars (acc, e));
+                                                       acc.SetLength (0);
+                                               }
+                                               xchar = GetChar (bytes, i + 2, 4);
+                                               if (xchar != -1) {
+                                                       output.Append ((char) xchar);
+                                                       i += 5;
+                                                       continue;
+                                               }
+                                       } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
+                                               acc.WriteByte ((byte) xchar);
+                                               i += 2;
+                                               continue;
+                                       }
+                               }
+
+                               if (acc.Length > 0) {
+                                       output.Append (GetChars (acc, e));
+                                       acc.SetLength (0);
+                               }
+
+                               if (bytes [i] == '+') {
+                                       output.Append (' ');
+                               } else {
+                                       output.Append ((char) bytes [i]);
+                               }
+                       }
+
+                       if (acc.Length > 0) {
+                               output.Append (GetChars (acc, e));
+                       }
+                       
+                       acc = null;
+                       return output.ToString ();
+               }
+       
+               public static byte [] UrlDecodeToBytes (byte [] bytes)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       return UrlDecodeToBytes (bytes, 0, bytes.Length);
+               }
+
+               public static byte [] UrlDecodeToBytes (string str)
+               {
+                       return UrlDecodeToBytes (str, Encoding.UTF8);
+               }
+
+               public static byte [] UrlDecodeToBytes (string str, Encoding e)
+               {
+                       if (str == null)
+                               return null;
+
+                       if (e == null)
+                               throw new ArgumentNullException ("e");
+
+                       return UrlDecodeToBytes (e.GetBytes (str));
+               }
+
+               public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
+               {
+                       if (bytes == null)
+                               return null;
+                       if (count == 0)
+                               return new byte [0];
+
+                       int len = bytes.Length;
+                       if (offset < 0 || offset >= len)
+                               throw new ArgumentOutOfRangeException("offset");
+
+                       if (count < 0 || offset > len - count)
+                               throw new ArgumentOutOfRangeException("count");
+
+                       MemoryStream result = new MemoryStream ();
+                       int end = offset + count;
+                       for (int i = offset; i < end; i++){
+                               char c = (char) bytes [i];
+                               if (c == '+') {
+                                       c = ' ';
+                               } else if (c == '%' && i < end - 2) {
+                                       int xchar = GetChar (bytes, i + 1, 2);
+                                       if (xchar != -1) {
+                                               c = (char) xchar;
+                                               i += 2;
+                                       }
+                               }
+                               result.WriteByte ((byte) c);
+                       }
+
+                       return result.ToArray ();
+               }
+
+               public static string UrlEncode(string str) 
+               {
+                       return UrlEncode(str, Encoding.UTF8);
+               }
+       
+               public static string UrlEncode (string s, Encoding Enc) 
+               {
+                       if (s == null)
+                               return null;
+
+                       if (s == "")
+                               return "";
+
+                       bool needEncode = false;
+                       int len = s.Length;
+                       for (int i = 0; i < len; i++) {
+                               char c = s [i];
+                               if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
+                                       if (NotEncoded (c))
+                                               continue;
+
+                                       needEncode = true;
+                                       break;
+                               }
+                       }
+
+                       if (!needEncode)
+                               return s;
+
+                       // avoided GetByteCount call
+                       byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
+                       int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
+                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
+               }
+         
+               public static string UrlEncode (byte [] bytes)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       if (bytes.Length == 0)
+                               return "";
+
+                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
+               }
+
+               public static string UrlEncode (byte [] bytes, int offset, int count)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       if (bytes.Length == 0)
+                               return "";
+
+                       return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
+               }
+
+               public static byte [] UrlEncodeToBytes (string str)
+               {
+                       return UrlEncodeToBytes (str, Encoding.UTF8);
+               }
+
+               public static byte [] UrlEncodeToBytes (string str, Encoding e)
+               {
+                       if (str == null)
+                               return null;
+
+                       if (str == "")
+                               return new byte [0];
+
+                       byte [] bytes = e.GetBytes (str);
+                       return UrlEncodeToBytes (bytes, 0, bytes.Length);
+               }
+
+               public static byte [] UrlEncodeToBytes (byte [] bytes)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       if (bytes.Length == 0)
+                               return new byte [0];
+
+                       return UrlEncodeToBytes (bytes, 0, bytes.Length);
+               }
+
+               static char [] hexChars = "0123456789abcdef".ToCharArray ();
+
+               static bool NotEncoded (char c)
+               {
+                       return (c == '!' || c == '\'' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_');
+               }
+
+               static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
+                       if (c > 255) {
+                               //FIXME: what happens when there is an internal error?
+                               //if (!isUnicode)
+                               //      throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
+                               int idx;
+                               int i = (int) c;
+
+                               result.WriteByte ((byte)'%');
+                               result.WriteByte ((byte)'u');
+                               idx = i >> 12;
+                               result.WriteByte ((byte)hexChars [idx]);
+                               idx = (i >> 8) & 0x0F;
+                               result.WriteByte ((byte)hexChars [idx]);
+                               idx = (i >> 4) & 0x0F;
+                               result.WriteByte ((byte)hexChars [idx]);
+                               idx = i & 0x0F;
+                               result.WriteByte ((byte)hexChars [idx]);
+                               return;
+                       }
+                       
+                       if (c > ' ' && NotEncoded (c)) {
+                               result.WriteByte ((byte)c);
+                               return;
+                       }
+                       if (c==' ') {
+                               result.WriteByte ((byte)'+');
+                               return;
+                       }
+                       if (    (c < '0') ||
+                               (c < 'A' && c > '9') ||
+                               (c > 'Z' && c < 'a') ||
+                               (c > 'z')) {
+                               if (isUnicode && c > 127) {
+                                       result.WriteByte ((byte)'%');
+                                       result.WriteByte ((byte)'u');
+                                       result.WriteByte ((byte)'0');
+                                       result.WriteByte ((byte)'0');
+                               }
+                               else
+                                       result.WriteByte ((byte)'%');
+                               
+                               int idx = ((int) c) >> 4;
+                               result.WriteByte ((byte)hexChars [idx]);
+                               idx = ((int) c) & 0x0F;
+                               result.WriteByte ((byte)hexChars [idx]);
+                       }
+                       else
+                               result.WriteByte ((byte)c);
+               }
+
+               public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
+               {
+                       if (bytes == null)
+                               return null;
+
+                       int len = bytes.Length;
+                       if (len == 0)
+                               return new byte [0];
+
+                       if (offset < 0 || offset >= len)
+                               throw new ArgumentOutOfRangeException("offset");
+
+                       if (count < 0 || count > len - offset)
+                               throw new ArgumentOutOfRangeException("count");
+
+                       MemoryStream result = new MemoryStream (count);
+                       int end = offset + count;
+                       for (int i = offset; i < end; i++)
+                               UrlEncodeChar ((char)bytes [i], result, false);
+
+                       return result.ToArray();
+               }
+
+               public static string UrlEncodeUnicode (string str)
+               {
+                       if (str == null)
+                               return null;
+
+                       return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
+               }
+
+               public static byte [] UrlEncodeUnicodeToBytes (string str)
+               {
+                       if (str == null)
+                               return null;
+
+                       if (str == "")
+                               return new byte [0];
+
+                       MemoryStream result = new MemoryStream (str.Length);
+                       foreach (char c in str){
+                               UrlEncodeChar (c, result, true);
+                       }
+                       return result.ToArray ();
+               }
+
+               /// <summary>
+               /// Decodes an HTML-encoded string and returns the decoded string.
+               /// </summary>
+               /// <param name="s">The HTML string to decode. </param>
+               /// <returns>The decoded text.</returns>
+               public static string HtmlDecode (string s) 
+               {
+                       if (s == null)
+                               throw new ArgumentNullException ("s");
+
+                       if (s.IndexOf ('&') == -1)
+                               return s;
+
+                       StringBuilder entity = new StringBuilder ();
+                       StringBuilder output = new StringBuilder ();
+                       int len = s.Length;
+                       // 0 -> nothing,
+                       // 1 -> right after '&'
+                       // 2 -> between '&' and ';' but no '#'
+                       // 3 -> '#' found after '&' and getting numbers
+                       int state = 0;
+                       int number = 0;
+                       bool have_trailing_digits = false;
+       
+                       for (int i = 0; i < len; i++) {
+                               char c = s [i];
+                               if (state == 0) {
+                                       if (c == '&') {
+                                               entity.Append (c);
+                                               state = 1;
+                                       } else {
+                                               output.Append (c);
+                                       }
+                                       continue;
+                               }
+
+                               if (c == '&') {
+                                       state = 1;
+                                       if (have_trailing_digits) {
+                                               entity.Append (number.ToString (CultureInfo.InvariantCulture));
+                                               have_trailing_digits = false;
+                                       }
+
+                                       output.Append (entity.ToString ());
+                                       entity.Length = 0;
+                                       entity.Append ('&');
+                                       continue;
+                               }
+
+                               if (state == 1) {
+                                       if (c == ';') {
+                                               state = 0;
+                                               output.Append (entity.ToString ());
+                                               output.Append (c);
+                                               entity.Length = 0;
+                                       } else {
+                                               number = 0;
+                                               if (c != '#') {
+                                                       state = 2;
+                                               } else {
+                                                       state = 3;
+                                               }
+                                               entity.Append (c);
+                                       }
+                               } else if (state == 2) {
+                                       entity.Append (c);
+                                       if (c == ';') {
+                                               string key = entity.ToString ();
+                                               if (key.Length > 1 && Entities.ContainsKey (key.Substring (1, key.Length - 2)))
+                                                       key = Entities [key.Substring (1, key.Length - 2)].ToString ();
+
+                                               output.Append (key);
+                                               state = 0;
+                                               entity.Length = 0;
+                                       }
+                               } else if (state == 3) {
+                                       if (c == ';') {
+                                               if (number > 65535) {
+                                                       output.Append ("&#");
+                                                       output.Append (number.ToString (CultureInfo.InvariantCulture));
+                                                       output.Append (";");
+                                               } else {
+                                                       output.Append ((char) number);
+                                               }
+                                               state = 0;
+                                               entity.Length = 0;
+                                               have_trailing_digits = false;
+                                       } else if (Char.IsDigit (c)) {
+                                               number = number * 10 + ((int) c - '0');
+                                               have_trailing_digits = true;
+                                       } else {
+                                               state = 2;
+                                               if (have_trailing_digits) {
+                                                       entity.Append (number.ToString (CultureInfo.InvariantCulture));
+                                                       have_trailing_digits = false;
+                                               }
+                                               entity.Append (c);
+                                       }
+                               }
+                       }
+
+                       if (entity.Length > 0) {
+                               output.Append (entity.ToString ());
+                       } else if (have_trailing_digits) {
+                               output.Append (number.ToString (CultureInfo.InvariantCulture));
+                       }
+                       return output.ToString ();
+               }
+       
+               /// <summary>
+               /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
+               /// </summary>
+               /// <param name="s">The HTML string to decode</param>
+               /// <param name="output">The TextWriter output stream containing the decoded string. </param>
+               public static void HtmlDecode(string s, TextWriter output) 
+               {
+                       if (s != null)
+                               output.Write (HtmlDecode (s));
+               }
+       
+               /// <summary>
+               /// HTML-encodes a string and returns the encoded string.
+               /// </summary>
+               /// <param name="s">The text string to encode. </param>
+               /// <returns>The HTML-encoded text.</returns>
+               public static string HtmlEncode (string s) 
+               {
+                       if (s == null)
+                               return null;
+
+                       bool needEncode = false;
+                       for (int i = 0; i < s.Length; i++) {
+                               char c = s [i];
+                               if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
+                                       needEncode = true;
+                                       break;
+                               }
+                       }
+
+                       if (!needEncode)
+                               return s;
+
+                       StringBuilder output = new StringBuilder ();
+                       
+                       int len = s.Length;
+                       for (int i = 0; i < len; i++) 
+                               switch (s [i]) {
+                               case '&' :
+                                       output.Append ("&amp;");
+                                       break;
+                               case '>' : 
+                                       output.Append ("&gt;");
+                                       break;
+                               case '<' :
+                                       output.Append ("&lt;");
+                                       break;
+                               case '"' :
+                                       output.Append ("&quot;");
+                                       break;
+                               default:
+                                       // MS starts encoding with &# from 160 and stops at 255.
+                                       // We don't do that. One reason is the 65308/65310 unicode
+                                       // characters that look like '<' and '>'.
+#if TARGET_JVM
+                                       if (s [i] > 159 && s [i] < 256) {
+#else
+                                       if (s [i] > 159) {
+#endif
+                                               output.Append ("&#");
+                                               output.Append (((int) s [i]).ToString (CultureInfo.InvariantCulture));
+                                               output.Append (";");
+                                       } else {
+                                               output.Append (s [i]);
+                                       }
+                                       break;
+                               }
+                       return output.ToString ();
+               }
+       
+               /// <summary>
+               /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
+               /// </summary>
+               /// <param name="s">The string to encode. </param>
+               /// <param name="output">The TextWriter output stream containing the encoded string. </param>
+               public static void HtmlEncode(string s, TextWriter output) 
+               {
+                       if (s != null)
+                               output.Write (HtmlEncode (s));
+               }
+
+#if NET_1_1
+               public static string UrlPathEncode (string s)
+               {
+                       if (s == null || s.Length == 0)
+                               return s;
+
+                       MemoryStream result = new MemoryStream ();
+                       int length = s.Length;
+            for (int i = 0; i < length; i++) {
+                               UrlPathEncodeChar (s [i], result);
+                       }
+                       return Encoding.ASCII.GetString (result.ToArray ());
+               }
+               
+               static void UrlPathEncodeChar (char c, Stream result) {
+#if NET_2_0
+                       if (c < 33 || c > 126) {
+#else
+                       if (c > 127) {
+#endif
+                               byte [] bIn = Encoding.UTF8.GetBytes (c.ToString ());
+                               for (int i = 0; i < bIn.Length; i++) {
+                                       result.WriteByte ((byte) '%');
+                                       int idx = ((int) bIn [i]) >> 4;
+                                       result.WriteByte ((byte) hexChars [idx]);
+                                       idx = ((int) bIn [i]) & 0x0F;
+                                       result.WriteByte ((byte) hexChars [idx]);
+                               }
+                       }
+                       else if (c == ' ') {
+                               result.WriteByte ((byte) '%');
+                               result.WriteByte ((byte) '2');
+                               result.WriteByte ((byte) '0');
+                       }
+                       else
+                               result.WriteByte ((byte) c);
+               }
+#endif
+
+#if NET_2_0
+               class HttpQSCollection : NameValueCollection {
+                       public override string ToString ()
+                       {
+                               int count = Count;
+                               if (count == 0)
+                                       return "";
+                               StringBuilder sb = new StringBuilder ();
+                               string [] keys = AllKeys;
+                               for (int i = 0; i < count; i++) {
+                                       sb.AppendFormat ("{0}={1}&", keys [i], this [keys [i]]);
+                               }
+                               if (sb.Length > 0)
+                                       sb.Length--;
+                               return sb.ToString ();
+                       }
+               }
+
+               public static NameValueCollection ParseQueryString (string query)
+               {
+                       return ParseQueryString (query, Encoding.UTF8);
+               }
+
+               public static NameValueCollection ParseQueryString (string query, Encoding encoding)
+               {
+                       if (query == null)
+                               throw new ArgumentNullException ("query");
+                       if (encoding == null)
+                               throw new ArgumentNullException ("encoding");
+                       if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
+                               return new NameValueCollection ();
+                       if (query[0] == '?')
+                               query = query.Substring (1);
+                               
+                       NameValueCollection result = new HttpQSCollection ();
+                       ParseQueryString (query, encoding, result);
+                       return result;
+               }                               
+#endif
+
+               internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
+               {
+                       if (query.Length == 0)
+                               return;
+
+                       string decoded = HtmlDecode (query);
+                       int decodedLength = decoded.Length;
+                       int namePos = 0;
+                       bool first = true;
+                       while (namePos <= decodedLength) {
+                               int valuePos = -1, valueEnd = -1;
+                               for (int q = namePos; q < decodedLength; q++) {
+                                       if (valuePos == -1 && decoded [q] == '=') {
+                                               valuePos = q + 1;
+                                       } else if (decoded [q] == '&') {
+                                               valueEnd = q;
+                                               break;
+                                       }
+                               }
+
+                               if (first) {
+                                       first = false;
+                                       if (decoded [namePos] == '?')
+                                               namePos++;
+                               }
+                               
+                               string name, value;
+                               if (valuePos == -1) {
+                                       name = null;
+                                       valuePos = namePos;
+                               } else {
+                                       name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
+                               }
+                               if (valueEnd < 0) {
+                                       namePos = -1;
+                                       valueEnd = decoded.Length;
+                               } else {
+                                       namePos = valueEnd + 1;
+                               }
+                               value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
+
+                               result.Add (name, value);
+                               if (namePos == -1)
+                                       break;
+                       }
+               }
+               #endregion // Methods
+       }
+}
+