2002-04-11 Patrik Torstensson <patrik.torstensson@labs2.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
1 // \r
2 // System.Web.HttpUtility\r
3 //\r
4 // Author:\r
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)\r
6 //   Wictor WilĂ©n (decode/encode functions) (wictor@ibizkit.se)\r
7 //\r
8 using System;\r
9 using System.Text;\r
10 using System.IO;\r
11 \r
12 namespace System.Web {\r
13    public sealed class HttpUtility {\r
14       // private stuff\r
15       private const string _hex = "0123456789ABCDEF";\r
16       private const string _chars = "<>;:.?=&@*+%/\\";\r
17 \r
18       public HttpUtility() {\r
19       }\r
20 \r
21       public static void HtmlAttributeEncode(string s, TextWriter output) {\r
22          output.Write(HtmlAttributeEncode(s));\r
23       }\r
24 \r
25       public static string HtmlAttributeEncode(string s) {\r
26          if (null == s) {\r
27             return s;\r
28          }\r
29 \r
30          StringBuilder oStr = new StringBuilder(128);\r
31 \r
32          for (int i = 0; i != s.Length; i++) {\r
33             if (s[i] == '&') {\r
34                oStr.Append("&amp;");\r
35             } else \r
36             if (s[i] == '"') {\r
37                oStr.Append("&quot;");\r
38             } else {\r
39                oStr.Append(s[i]);\r
40             }\r
41          }\r
42 \r
43          return oStr.ToString();\r
44       }\r
45 \r
46       public static string UrlDecode(string str) {\r
47          return UrlDecode(str, Encoding.UTF8);\r
48       }\r
49 \r
50       [MonoTODO("Use Encoding")]\r
51       public static string UrlDecode(string s, Encoding Enc) {\r
52          if (null == s) {\r
53             return null;\r
54          }\r
55 \r
56          string dest = "";\r
57          long len = s.Length;\r
58          string tmp = "";\r
59 \r
60 \r
61          for(int i = 0; i < len; i++) {\r
62             if(s[i] == '%') {\r
63                tmp = s[i+1].ToString ();\r
64                tmp += s[i+2];\r
65                dest += char.Parse(tmp);\r
66             } \r
67             else if( s[i] == '+') {\r
68                dest += " ";\r
69             }\r
70             else\r
71                dest += s[i];\r
72 \r
73          }\r
74          return dest;\r
75       }\r
76 \r
77 \r
78 \r
79       public static string UrlEncode(string str) {\r
80          return UrlDecode(str, Encoding.UTF8);\r
81       }\r
82 \r
83       [MonoTODO("Use encoding")]\r
84       public static string UrlEncode(string s, Encoding Enc) {\r
85          if (null == s) {\r
86             return null;\r
87          }\r
88 \r
89          string dest = "";\r
90          long len = s.Length;\r
91          int h1, h2;\r
92 \r
93          for(int i = 0; i < len; i++) {\r
94             if(s[i] == ' ') // space character is replaced with '+'\r
95                dest += "+";\r
96             else if ( _chars.IndexOf(s[i]) >= 0 ) {\r
97                h1 = (int)s[i] % 16;\r
98                h2 = (int)s[i] / 16;\r
99                dest += "%";\r
100                dest += _hex[h1].ToString();\r
101                dest += _hex[h2].ToString();\r
102             }\r
103             else\r
104                dest += s[i].ToString();\r
105 \r
106          }\r
107          return dest;\r
108       }\r
109    \r
110       /// <summary>\r
111       /// Decodes an HTML-encoded string and returns the decoded string.\r
112       /// </summary>\r
113       /// <param name="s">The HTML string to decode. </param>\r
114       /// <returns>The decoded text.</returns>\r
115       [MonoTODO()]\r
116       public static string HtmlDecode(string s) {\r
117          throw new System.NotImplementedException();\r
118       }\r
119 \r
120       /// <summary>\r
121       /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.\r
122       /// </summary>\r
123       /// <param name="s">The HTML string to decode</param>\r
124       /// <param name="output">The TextWriter output stream containing the decoded string. </param>\r
125       [MonoTODO()]\r
126       public static void HtmlDecode(string s, TextWriter output) {\r
127          throw new System.NotImplementedException();\r
128       }\r
129 \r
130       /// <summary>\r
131       /// HTML-encodes a string and returns the encoded string.\r
132       /// </summary>\r
133       /// <param name="s">The text string to encode. </param>\r
134       /// <returns>The HTML-encoded text.</returns>\r
135       public static string HtmlEncode(string s) {\r
136          string dest = "";\r
137          long len = s.Length;\r
138          int v;\r
139 \r
140 \r
141 \r
142          for(int i = 0; i < len; i++) {\r
143             switch(s[i]) {\r
144                case '>':\r
145                   dest += "&gt;";\r
146                   break;\r
147                case '<':\r
148                   dest += "&lt;";\r
149                   break;\r
150                case '"':\r
151                   dest += "&quot;";\r
152                   break;\r
153                case '&':\r
154                   dest += "&amp;";\r
155                   break;\r
156                default:\r
157                   if(s[i] >= 128) {\r
158                      dest += "&H";\r
159                      v = (int) s[i];\r
160                      dest += v.ToString() ;\r
161                                                         \r
162                   }\r
163                   else\r
164                      dest += s[i];\r
165                   break;\r
166             }\r
167          }\r
168          return dest;\r
169       }\r
170 \r
171       /// <summary>\r
172       /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.\r
173       /// </summary>\r
174       /// <param name="s">The string to encode. </param>\r
175       /// <param name="output">The TextWriter output stream containing the encoded string. </param>\r
176       public static void HtmlEncode(    string s, TextWriter output) {\r
177          output.Write(HtmlEncode(s));\r
178       }\r
179    }\r
180 }\r