~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to class/System.Windows.Browser/System.Windows.Browser/HttpUtility.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// System.Web.HttpUtility
 
3
//
 
4
// Authors:
 
5
//   Patrik Torstensson (Patrik.Torstensson@labs2.com)
 
6
//   Wictor WilĆ©n (decode/encode functions) (wictor@ibizkit.se)
 
7
//   Tim Coleman (tim@timcoleman.com)
 
8
//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
 
9
//
 
10
// Copyright (C) 2005, 2007 Novell, Inc.
 
11
//
 
12
// Permission is hereby granted, free of charge, to any person obtaining
 
13
// a copy of this software and associated documentation files (the
 
14
// "Software"), to deal in the Software without restriction, including
 
15
// without limitation the rights to use, copy, modify, merge, publish,
 
16
// distribute, sublicense, and/or sell copies of the Software, and to
 
17
// permit persons to whom the Software is furnished to do so, subject to
 
18
// the following conditions:
 
19
// 
 
20
// The above copyright notice and this permission notice shall be
 
21
// included in all copies or substantial portions of the Software.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
using System;
 
33
using System.Globalization;
 
34
using System.Collections.Generic;
 
35
using System.IO;
 
36
using System.Text;
 
37
 
 
38
namespace System.Windows.Browser
 
39
{
 
40
        public sealed class HttpUtility
 
41
        {
 
42
                public HttpUtility () {}
 
43
                
 
44
                static Dictionary<string,char> entities;
 
45
 
 
46
                static HttpUtility ()
 
47
                {
 
48
                        InitEntities ();
 
49
                }
 
50
                
 
51
                static void InitEntities ()
 
52
                {
 
53
                        // Build the hash table of HTML entity references.  This list comes
 
54
                        // from the HTML 4.01 W3C recommendation.
 
55
                        entities = new Dictionary<string, char> ();
 
56
                        entities.Add ("nbsp", '\u00A0');
 
57
                        entities.Add ("iexcl", '\u00A1');
 
58
                        entities.Add ("cent", '\u00A2');
 
59
                        entities.Add ("pound", '\u00A3');
 
60
                        entities.Add ("curren", '\u00A4');
 
61
                        entities.Add ("yen", '\u00A5');
 
62
                        entities.Add ("brvbar", '\u00A6');
 
63
                        entities.Add ("sect", '\u00A7');
 
64
                        entities.Add ("uml", '\u00A8');
 
65
                        entities.Add ("copy", '\u00A9');
 
66
                        entities.Add ("ordf", '\u00AA');
 
67
                        entities.Add ("laquo", '\u00AB');
 
68
                        entities.Add ("not", '\u00AC');
 
69
                        entities.Add ("shy", '\u00AD');
 
70
                        entities.Add ("reg", '\u00AE');
 
71
                        entities.Add ("macr", '\u00AF');
 
72
                        entities.Add ("deg", '\u00B0');
 
73
                        entities.Add ("plusmn", '\u00B1');
 
74
                        entities.Add ("sup2", '\u00B2');
 
75
                        entities.Add ("sup3", '\u00B3');
 
76
                        entities.Add ("acute", '\u00B4');
 
77
                        entities.Add ("micro", '\u00B5');
 
78
                        entities.Add ("para", '\u00B6');
 
79
                        entities.Add ("middot", '\u00B7');
 
80
                        entities.Add ("cedil", '\u00B8');
 
81
                        entities.Add ("sup1", '\u00B9');
 
82
                        entities.Add ("ordm", '\u00BA');
 
83
                        entities.Add ("raquo", '\u00BB');
 
84
                        entities.Add ("frac14", '\u00BC');
 
85
                        entities.Add ("frac12", '\u00BD');
 
86
                        entities.Add ("frac34", '\u00BE');
 
87
                        entities.Add ("iquest", '\u00BF');
 
88
                        entities.Add ("Agrave", '\u00C0');
 
89
                        entities.Add ("Aacute", '\u00C1');
 
90
                        entities.Add ("Acirc", '\u00C2');
 
91
                        entities.Add ("Atilde", '\u00C3');
 
92
                        entities.Add ("Auml", '\u00C4');
 
93
                        entities.Add ("Aring", '\u00C5');
 
94
                        entities.Add ("AElig", '\u00C6');
 
95
                        entities.Add ("Ccedil", '\u00C7');
 
96
                        entities.Add ("Egrave", '\u00C8');
 
97
                        entities.Add ("Eacute", '\u00C9');
 
98
                        entities.Add ("Ecirc", '\u00CA');
 
99
                        entities.Add ("Euml", '\u00CB');
 
100
                        entities.Add ("Igrave", '\u00CC');
 
101
                        entities.Add ("Iacute", '\u00CD');
 
102
                        entities.Add ("Icirc", '\u00CE');
 
103
                        entities.Add ("Iuml", '\u00CF');
 
104
                        entities.Add ("ETH", '\u00D0');
 
105
                        entities.Add ("Ntilde", '\u00D1');
 
106
                        entities.Add ("Ograve", '\u00D2');
 
107
                        entities.Add ("Oacute", '\u00D3');
 
108
                        entities.Add ("Ocirc", '\u00D4');
 
109
                        entities.Add ("Otilde", '\u00D5');
 
110
                        entities.Add ("Ouml", '\u00D6');
 
111
                        entities.Add ("times", '\u00D7');
 
112
                        entities.Add ("Oslash", '\u00D8');
 
113
                        entities.Add ("Ugrave", '\u00D9');
 
114
                        entities.Add ("Uacute", '\u00DA');
 
115
                        entities.Add ("Ucirc", '\u00DB');
 
116
                        entities.Add ("Uuml", '\u00DC');
 
117
                        entities.Add ("Yacute", '\u00DD');
 
118
                        entities.Add ("THORN", '\u00DE');
 
119
                        entities.Add ("szlig", '\u00DF');
 
120
                        entities.Add ("agrave", '\u00E0');
 
121
                        entities.Add ("aacute", '\u00E1');
 
122
                        entities.Add ("acirc", '\u00E2');
 
123
                        entities.Add ("atilde", '\u00E3');
 
124
                        entities.Add ("auml", '\u00E4');
 
125
                        entities.Add ("aring", '\u00E5');
 
126
                        entities.Add ("aelig", '\u00E6');
 
127
                        entities.Add ("ccedil", '\u00E7');
 
128
                        entities.Add ("egrave", '\u00E8');
 
129
                        entities.Add ("eacute", '\u00E9');
 
130
                        entities.Add ("ecirc", '\u00EA');
 
131
                        entities.Add ("euml", '\u00EB');
 
132
                        entities.Add ("igrave", '\u00EC');
 
133
                        entities.Add ("iacute", '\u00ED');
 
134
                        entities.Add ("icirc", '\u00EE');
 
135
                        entities.Add ("iuml", '\u00EF');
 
136
                        entities.Add ("eth", '\u00F0');
 
137
                        entities.Add ("ntilde", '\u00F1');
 
138
                        entities.Add ("ograve", '\u00F2');
 
139
                        entities.Add ("oacute", '\u00F3');
 
140
                        entities.Add ("ocirc", '\u00F4');
 
141
                        entities.Add ("otilde", '\u00F5');
 
142
                        entities.Add ("ouml", '\u00F6');
 
143
                        entities.Add ("divide", '\u00F7');
 
144
                        entities.Add ("oslash", '\u00F8');
 
145
                        entities.Add ("ugrave", '\u00F9');
 
146
                        entities.Add ("uacute", '\u00FA');
 
147
                        entities.Add ("ucirc", '\u00FB');
 
148
                        entities.Add ("uuml", '\u00FC');
 
149
                        entities.Add ("yacute", '\u00FD');
 
150
                        entities.Add ("thorn", '\u00FE');
 
151
                        entities.Add ("yuml", '\u00FF');
 
152
                        entities.Add ("fnof", '\u0192');
 
153
                        entities.Add ("Alpha", '\u0391');
 
154
                        entities.Add ("Beta", '\u0392');
 
155
                        entities.Add ("Gamma", '\u0393');
 
156
                        entities.Add ("Delta", '\u0394');
 
157
                        entities.Add ("Epsilon", '\u0395');
 
158
                        entities.Add ("Zeta", '\u0396');
 
159
                        entities.Add ("Eta", '\u0397');
 
160
                        entities.Add ("Theta", '\u0398');
 
161
                        entities.Add ("Iota", '\u0399');
 
162
                        entities.Add ("Kappa", '\u039A');
 
163
                        entities.Add ("Lambda", '\u039B');
 
164
                        entities.Add ("Mu", '\u039C');
 
165
                        entities.Add ("Nu", '\u039D');
 
166
                        entities.Add ("Xi", '\u039E');
 
167
                        entities.Add ("Omicron", '\u039F');
 
168
                        entities.Add ("Pi", '\u03A0');
 
169
                        entities.Add ("Rho", '\u03A1');
 
170
                        entities.Add ("Sigma", '\u03A3');
 
171
                        entities.Add ("Tau", '\u03A4');
 
172
                        entities.Add ("Upsilon", '\u03A5');
 
173
                        entities.Add ("Phi", '\u03A6');
 
174
                        entities.Add ("Chi", '\u03A7');
 
175
                        entities.Add ("Psi", '\u03A8');
 
176
                        entities.Add ("Omega", '\u03A9');
 
177
                        entities.Add ("alpha", '\u03B1');
 
178
                        entities.Add ("beta", '\u03B2');
 
179
                        entities.Add ("gamma", '\u03B3');
 
180
                        entities.Add ("delta", '\u03B4');
 
181
                        entities.Add ("epsilon", '\u03B5');
 
182
                        entities.Add ("zeta", '\u03B6');
 
183
                        entities.Add ("eta", '\u03B7');
 
184
                        entities.Add ("theta", '\u03B8');
 
185
                        entities.Add ("iota", '\u03B9');
 
186
                        entities.Add ("kappa", '\u03BA');
 
187
                        entities.Add ("lambda", '\u03BB');
 
188
                        entities.Add ("mu", '\u03BC');
 
189
                        entities.Add ("nu", '\u03BD');
 
190
                        entities.Add ("xi", '\u03BE');
 
191
                        entities.Add ("omicron", '\u03BF');
 
192
                        entities.Add ("pi", '\u03C0');
 
193
                        entities.Add ("rho", '\u03C1');
 
194
                        entities.Add ("sigmaf", '\u03C2');
 
195
                        entities.Add ("sigma", '\u03C3');
 
196
                        entities.Add ("tau", '\u03C4');
 
197
                        entities.Add ("upsilon", '\u03C5');
 
198
                        entities.Add ("phi", '\u03C6');
 
199
                        entities.Add ("chi", '\u03C7');
 
200
                        entities.Add ("psi", '\u03C8');
 
201
                        entities.Add ("omega", '\u03C9');
 
202
                        entities.Add ("thetasym", '\u03D1');
 
203
                        entities.Add ("upsih", '\u03D2');
 
204
                        entities.Add ("piv", '\u03D6');
 
205
                        entities.Add ("bull", '\u2022');
 
206
                        entities.Add ("hellip", '\u2026');
 
207
                        entities.Add ("prime", '\u2032');
 
208
                        entities.Add ("Prime", '\u2033');
 
209
                        entities.Add ("oline", '\u203E');
 
210
                        entities.Add ("frasl", '\u2044');
 
211
                        entities.Add ("weierp", '\u2118');
 
212
                        entities.Add ("image", '\u2111');
 
213
                        entities.Add ("real", '\u211C');
 
214
                        entities.Add ("trade", '\u2122');
 
215
                        entities.Add ("alefsym", '\u2135');
 
216
                        entities.Add ("larr", '\u2190');
 
217
                        entities.Add ("uarr", '\u2191');
 
218
                        entities.Add ("rarr", '\u2192');
 
219
                        entities.Add ("darr", '\u2193');
 
220
                        entities.Add ("harr", '\u2194');
 
221
                        entities.Add ("crarr", '\u21B5');
 
222
                        entities.Add ("lArr", '\u21D0');
 
223
                        entities.Add ("uArr", '\u21D1');
 
224
                        entities.Add ("rArr", '\u21D2');
 
225
                        entities.Add ("dArr", '\u21D3');
 
226
                        entities.Add ("hArr", '\u21D4');
 
227
                        entities.Add ("forall", '\u2200');
 
228
                        entities.Add ("part", '\u2202');
 
229
                        entities.Add ("exist", '\u2203');
 
230
                        entities.Add ("empty", '\u2205');
 
231
                        entities.Add ("nabla", '\u2207');
 
232
                        entities.Add ("isin", '\u2208');
 
233
                        entities.Add ("notin", '\u2209');
 
234
                        entities.Add ("ni", '\u220B');
 
235
                        entities.Add ("prod", '\u220F');
 
236
                        entities.Add ("sum", '\u2211');
 
237
                        entities.Add ("minus", '\u2212');
 
238
                        entities.Add ("lowast", '\u2217');
 
239
                        entities.Add ("radic", '\u221A');
 
240
                        entities.Add ("prop", '\u221D');
 
241
                        entities.Add ("infin", '\u221E');
 
242
                        entities.Add ("ang", '\u2220');
 
243
                        entities.Add ("and", '\u2227');
 
244
                        entities.Add ("or", '\u2228');
 
245
                        entities.Add ("cap", '\u2229');
 
246
                        entities.Add ("cup", '\u222A');
 
247
                        entities.Add ("int", '\u222B');
 
248
                        entities.Add ("there4", '\u2234');
 
249
                        entities.Add ("sim", '\u223C');
 
250
                        entities.Add ("cong", '\u2245');
 
251
                        entities.Add ("asymp", '\u2248');
 
252
                        entities.Add ("ne", '\u2260');
 
253
                        entities.Add ("equiv", '\u2261');
 
254
                        entities.Add ("le", '\u2264');
 
255
                        entities.Add ("ge", '\u2265');
 
256
                        entities.Add ("sub", '\u2282');
 
257
                        entities.Add ("sup", '\u2283');
 
258
                        entities.Add ("nsub", '\u2284');
 
259
                        entities.Add ("sube", '\u2286');
 
260
                        entities.Add ("supe", '\u2287');
 
261
                        entities.Add ("oplus", '\u2295');
 
262
                        entities.Add ("otimes", '\u2297');
 
263
                        entities.Add ("perp", '\u22A5');
 
264
                        entities.Add ("sdot", '\u22C5');
 
265
                        entities.Add ("lceil", '\u2308');
 
266
                        entities.Add ("rceil", '\u2309');
 
267
                        entities.Add ("lfloor", '\u230A');
 
268
                        entities.Add ("rfloor", '\u230B');
 
269
                        entities.Add ("lang", '\u2329');
 
270
                        entities.Add ("rang", '\u232A');
 
271
                        entities.Add ("loz", '\u25CA');
 
272
                        entities.Add ("spades", '\u2660');
 
273
                        entities.Add ("clubs", '\u2663');
 
274
                        entities.Add ("hearts", '\u2665');
 
275
                        entities.Add ("diams", '\u2666');
 
276
                        entities.Add ("quot", '\u0022');
 
277
                        entities.Add ("amp", '\u0026');
 
278
                        entities.Add ("lt", '\u003C');
 
279
                        entities.Add ("gt", '\u003E');
 
280
                        entities.Add ("OElig", '\u0152');
 
281
                        entities.Add ("oelig", '\u0153');
 
282
                        entities.Add ("Scaron", '\u0160');
 
283
                        entities.Add ("scaron", '\u0161');
 
284
                        entities.Add ("Yuml", '\u0178');
 
285
                        entities.Add ("circ", '\u02C6');
 
286
                        entities.Add ("tilde", '\u02DC');
 
287
                        entities.Add ("ensp", '\u2002');
 
288
                        entities.Add ("emsp", '\u2003');
 
289
                        entities.Add ("thinsp", '\u2009');
 
290
                        entities.Add ("zwnj", '\u200C');
 
291
                        entities.Add ("zwj", '\u200D');
 
292
                        entities.Add ("lrm", '\u200E');
 
293
                        entities.Add ("rlm", '\u200F');
 
294
                        entities.Add ("ndash", '\u2013');
 
295
                        entities.Add ("mdash", '\u2014');
 
296
                        entities.Add ("lsquo", '\u2018');
 
297
                        entities.Add ("rsquo", '\u2019');
 
298
                        entities.Add ("sbquo", '\u201A');
 
299
                        entities.Add ("ldquo", '\u201C');
 
300
                        entities.Add ("rdquo", '\u201D');
 
301
                        entities.Add ("bdquo", '\u201E');
 
302
                        entities.Add ("dagger", '\u2020');
 
303
                        entities.Add ("Dagger", '\u2021');
 
304
                        entities.Add ("permil", '\u2030');
 
305
                        entities.Add ("lsaquo", '\u2039');
 
306
                        entities.Add ("rsaquo", '\u203A');
 
307
                        entities.Add ("euro", '\u20AC');
 
308
                }
 
309
 
 
310
 
 
311
                /// <summary>
 
312
                /// Decodes an HTML-encoded string and returns the decoded string.
 
313
                /// </summary>
 
314
                /// <param name="s">The HTML string to decode. </param>
 
315
                /// <returns>The decoded text.</returns>
 
316
                public static string HtmlDecode (string s) 
 
317
                {
 
318
                        if (s == null)
 
319
                                throw new ArgumentNullException ("s");
 
320
 
 
321
                        if (s.IndexOf ('&') == -1)
 
322
                                return s;
 
323
 
 
324
                        StringBuilder entity = new StringBuilder ();
 
325
                        StringBuilder output = new StringBuilder ();
 
326
                        int len = s.Length;
 
327
                        // 0 -> nothing,
 
328
                        // 1 -> right after '&'
 
329
                        // 2 -> between '&' and ';' but no '#'
 
330
                        // 3 -> '#' found after '&' and getting numbers
 
331
                        int state = 0;
 
332
                        int number = 0;
 
333
                        bool have_trailing_digits = false;
 
334
        
 
335
                        for (int i = 0; i < len; i++) {
 
336
                                char c = s [i];
 
337
                                if (state == 0) {
 
338
                                        if (c == '&') {
 
339
                                                entity.Append (c);
 
340
                                                state = 1;
 
341
                                        } else {
 
342
                                                output.Append (c);
 
343
                                        }
 
344
                                        continue;
 
345
                                }
 
346
 
 
347
                                if (c == '&') {
 
348
                                        state = 1;
 
349
                                        if (have_trailing_digits) {
 
350
                                                entity.Append (number.ToString (CultureInfo.InvariantCulture));
 
351
                                                have_trailing_digits = false;
 
352
                                        }
 
353
 
 
354
                                        output.Append (entity.ToString ());
 
355
                                        entity.Length = 0;
 
356
                                        entity.Append ('&');
 
357
                                        continue;
 
358
                                }
 
359
 
 
360
                                if (state == 1) {
 
361
                                        if (c == ';') {
 
362
                                                state = 0;
 
363
                                                output.Append (entity.ToString ());
 
364
                                                output.Append (c);
 
365
                                                entity.Length = 0;
 
366
                                        } else {
 
367
                                                number = 0;
 
368
                                                if (c != '#') {
 
369
                                                        state = 2;
 
370
                                                } else {
 
371
                                                        state = 3;
 
372
                                                }
 
373
                                                entity.Append (c);
 
374
                                        }
 
375
                                } else if (state == 2) {
 
376
                                        entity.Append (c);
 
377
                                        if (c == ';') {
 
378
                                                string key = entity.ToString ();
 
379
                                                if (key.Length > 1 && entities.ContainsKey (key.Substring (1, key.Length - 2)))
 
380
                                                        key = entities [key.Substring (1, key.Length - 2)].ToString ();
 
381
 
 
382
                                                output.Append (key);
 
383
                                                state = 0;
 
384
                                                entity.Length = 0;
 
385
                                        }
 
386
                                } else if (state == 3) {
 
387
                                        if (c == ';') {
 
388
                                                if (number > 65535) {
 
389
                                                        output.Append ("&#");
 
390
                                                        output.Append (number.ToString (CultureInfo.InvariantCulture));
 
391
                                                        output.Append (";");
 
392
                                                } else {
 
393
                                                        output.Append ((char) number);
 
394
                                                }
 
395
                                                state = 0;
 
396
                                                entity.Length = 0;
 
397
                                                have_trailing_digits = false;
 
398
                                        } else if (Char.IsDigit (c)) {
 
399
                                                number = number * 10 + ((int) c - '0');
 
400
                                                have_trailing_digits = true;
 
401
                                        } else {
 
402
                                                state = 2;
 
403
                                                if (have_trailing_digits) {
 
404
                                                        entity.Append (number.ToString (CultureInfo.InvariantCulture));
 
405
                                                        have_trailing_digits = false;
 
406
                                                }
 
407
                                                entity.Append (c);
 
408
                                        }
 
409
                                }
 
410
                        }
 
411
 
 
412
                        if (entity.Length > 0) {
 
413
                                output.Append (entity.ToString ());
 
414
                        } else if (have_trailing_digits) {
 
415
                                output.Append (number.ToString (CultureInfo.InvariantCulture));
 
416
                        }
 
417
                        return output.ToString ();
 
418
                }
 
419
        
 
420
                /// <summary>
 
421
                /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
 
422
                /// </summary>
 
423
                /// <param name="s">The HTML string to decode</param>
 
424
                /// <param name="output">The TextWriter output stream containing the decoded string. </param>
 
425
                public static void HtmlDecode(string s, TextWriter output) 
 
426
                {
 
427
                        if (s != null)
 
428
                                output.Write (HtmlDecode (s));
 
429
                }
 
430
        
 
431
                /// <summary>
 
432
                /// HTML-encodes a string and returns the encoded string.
 
433
                /// </summary>
 
434
                /// <param name="s">The text string to encode. </param>
 
435
                /// <returns>The HTML-encoded text.</returns>
 
436
                public static string HtmlEncode (string s) 
 
437
                {
 
438
                        if (s == null)
 
439
                                return null;
 
440
 
 
441
                        bool needEncode = false;
 
442
                        for (int i = 0; i < s.Length; i++) {
 
443
                                char c = s [i];
 
444
                                if (c == '&' || c == '"' || c == '<' || c == '>' || c > 159) {
 
445
                                        needEncode = true;
 
446
                                        break;
 
447
                                }
 
448
                        }
 
449
 
 
450
                        if (!needEncode)
 
451
                                return s;
 
452
 
 
453
                        StringBuilder output = new StringBuilder ();
 
454
                        
 
455
                        int len = s.Length;
 
456
                        for (int i = 0; i < len; i++) 
 
457
                                switch (s [i]) {
 
458
                                case '&' :
 
459
                                        output.Append ("&amp;");
 
460
                                        break;
 
461
                                case '>' : 
 
462
                                        output.Append ("&gt;");
 
463
                                        break;
 
464
                                case '<' :
 
465
                                        output.Append ("&lt;");
 
466
                                        break;
 
467
                                case '"' :
 
468
                                        output.Append ("&quot;");
 
469
                                        break;
 
470
                                default:
 
471
                                        // MS starts encoding with &# from 160 and stops at 255.
 
472
                                        // We don't do that. One reason is the 65308/65310 unicode
 
473
                                        // characters that look like '<' and '>'.
 
474
                                        if (s [i] > 159) {
 
475
                                                output.Append ("&#");
 
476
                                                output.Append (((int) s [i]).ToString (CultureInfo.InvariantCulture));
 
477
                                                output.Append (";");
 
478
                                        } else {
 
479
                                                output.Append (s [i]);
 
480
                                        }
 
481
                                        break;
 
482
                                }
 
483
                        return output.ToString ();
 
484
                }
 
485
        
 
486
                /// <summary>
 
487
                /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
 
488
                /// </summary>
 
489
                /// <param name="s">The string to encode. </param>
 
490
                /// <param name="output">The TextWriter output stream containing the encoded string. </param>
 
491
                public static void HtmlEncode(string s, TextWriter output) 
 
492
                {
 
493
                        if (s != null)
 
494
                                output.Write (HtmlEncode (s));
 
495
                }
 
496
 
 
497
                private static int GetInt (byte b)
 
498
                {
 
499
                        char c = (char) b;
 
500
                        if (c >= '0' && c <= '9')
 
501
                                return c - '0';
 
502
 
 
503
                        if (c >= 'a' && c <= 'f')
 
504
                                return c - 'a' + 10;
 
505
 
 
506
                        if (c >= 'A' && c <= 'F')
 
507
                                return c - 'A' + 10;
 
508
 
 
509
                        return -1;
 
510
                }
 
511
 
 
512
                private static char [] GetChars (MemoryStream b, Encoding e)
 
513
                {
 
514
                        return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
 
515
                }
 
516
 
 
517
                private static int GetChar (byte [] bytes, int offset, int length)
 
518
                {
 
519
                        int value = 0;
 
520
                        int end = length + offset;
 
521
                        for (int i = offset; i < end; i++) {
 
522
                                int current = GetInt (bytes [i]);
 
523
                                if (current == -1)
 
524
                                        return -1;
 
525
                                value = (value << 4) + current;
 
526
                        }
 
527
 
 
528
                        return value;
 
529
                }
 
530
 
 
531
                private static int GetChar (string str, int offset, int length)
 
532
                {
 
533
                        int val = 0;
 
534
                        int end = length + offset;
 
535
                        for (int i = offset; i < end; i++) {
 
536
                                char c = str [i];
 
537
                                if (c > 127)
 
538
                                        return -1;
 
539
 
 
540
                                int current = GetInt ((byte) c);
 
541
                                if (current == -1)
 
542
                                        return -1;
 
543
                                val = (val << 4) + current;
 
544
                        }
 
545
 
 
546
                        return val;
 
547
                }
 
548
                
 
549
                public static string UrlDecode (string str) 
 
550
                {
 
551
                        return UrlDecode(str, Encoding.UTF8);
 
552
                }
 
553
        
 
554
                public static string UrlDecode (string str, Encoding e)
 
555
                {
 
556
                        if (null == str) 
 
557
                                return null;
 
558
 
 
559
                        if (str.IndexOf ('%') == -1 && str.IndexOf ('+') == -1)
 
560
                                return str;
 
561
 
 
562
                        if (e == null)
 
563
                                e = Encoding.UTF8;
 
564
        
 
565
                        StringBuilder output = new StringBuilder ();
 
566
                        long len = str.Length;
 
567
                        MemoryStream bytes = new MemoryStream ();
 
568
                        int xchar;
 
569
        
 
570
                        for (int i = 0; i < len; i++) {
 
571
                                if (str [i] == '%' && i + 2 < len && str [i + 1] != '%') {
 
572
                                        if (str [i + 1] == 'u' && i + 5 < len) {
 
573
                                                if (bytes.Length > 0) {
 
574
                                                        output.Append (GetChars (bytes, e));
 
575
                                                        bytes.SetLength (0);
 
576
                                                }
 
577
 
 
578
                                                xchar = GetChar (str, i + 2, 4);
 
579
                                                if (xchar != -1) {
 
580
                                                        output.Append ((char) xchar);
 
581
                                                        i += 5;
 
582
                                                } else {
 
583
                                                        output.Append ('%');
 
584
                                                }
 
585
                                        } else if ((xchar = GetChar (str, i + 1, 2)) != -1) {
 
586
                                                bytes.WriteByte ((byte) xchar);
 
587
                                                i += 2;
 
588
                                        } else {
 
589
                                                output.Append ('%');
 
590
                                        }
 
591
                                        continue;
 
592
                                }
 
593
 
 
594
                                if (bytes.Length > 0) {
 
595
                                        output.Append (GetChars (bytes, e));
 
596
                                        bytes.SetLength (0);
 
597
                                }
 
598
 
 
599
                                if (str [i] == '+') {
 
600
                                        output.Append (' ');
 
601
                                } else {
 
602
                                        output.Append (str [i]);
 
603
                                }
 
604
                        }
 
605
        
 
606
                        if (bytes.Length > 0) {
 
607
                                output.Append (GetChars (bytes, e));
 
608
                        }
 
609
 
 
610
                        bytes = null;
 
611
                        return output.ToString ();
 
612
                }
 
613
        
 
614
                public static string UrlDecode (byte [] bytes, Encoding e)
 
615
                {
 
616
                        if (bytes == null)
 
617
                                return null;
 
618
 
 
619
                        return UrlDecode (bytes, 0, bytes.Length, e);
 
620
                }
 
621
 
 
622
                public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
 
623
                {
 
624
                        if (bytes == null)
 
625
                                return null;
 
626
                        if (count == 0)
 
627
                                return String.Empty;
 
628
 
 
629
                        if (bytes == null)
 
630
                                throw new ArgumentNullException ("bytes");
 
631
 
 
632
                        if (offset < 0 || offset > bytes.Length)
 
633
                                throw new ArgumentOutOfRangeException ("offset");
 
634
 
 
635
                        if (count < 0 || offset + count > bytes.Length)
 
636
                                throw new ArgumentOutOfRangeException ("count");
 
637
 
 
638
                        StringBuilder output = new StringBuilder ();
 
639
                        MemoryStream acc = new MemoryStream ();
 
640
 
 
641
                        int end = count + offset;
 
642
                        int xchar;
 
643
                        for (int i = offset; i < end; i++) {
 
644
                                if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
 
645
                                        if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
 
646
                                                if (acc.Length > 0) {
 
647
                                                        output.Append (GetChars (acc, e));
 
648
                                                        acc.SetLength (0);
 
649
                                                }
 
650
                                                xchar = GetChar (bytes, i + 2, 4);
 
651
                                                if (xchar != -1) {
 
652
                                                        output.Append ((char) xchar);
 
653
                                                        i += 5;
 
654
                                                        continue;
 
655
                                                }
 
656
                                        } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
 
657
                                                acc.WriteByte ((byte) xchar);
 
658
                                                i += 2;
 
659
                                                continue;
 
660
                                        }
 
661
                                }
 
662
 
 
663
                                if (acc.Length > 0) {
 
664
                                        output.Append (GetChars (acc, e));
 
665
                                        acc.SetLength (0);
 
666
                                }
 
667
 
 
668
                                if (bytes [i] == '+') {
 
669
                                        output.Append (' ');
 
670
                                } else {
 
671
                                        output.Append ((char) bytes [i]);
 
672
                                }
 
673
                        }
 
674
 
 
675
                        if (acc.Length > 0) {
 
676
                                output.Append (GetChars (acc, e));
 
677
                        }
 
678
                        
 
679
                        acc = null;
 
680
                        return output.ToString ();
 
681
                }
 
682
        
 
683
                public static byte [] UrlDecodeToBytes (byte [] bytes)
 
684
                {
 
685
                        if (bytes == null)
 
686
                                return null;
 
687
 
 
688
                        return UrlDecodeToBytes (bytes, 0, bytes.Length);
 
689
                }
 
690
 
 
691
                public static byte [] UrlDecodeToBytes (string str)
 
692
                {
 
693
                        return UrlDecodeToBytes (str, Encoding.UTF8);
 
694
                }
 
695
 
 
696
                public static byte [] UrlDecodeToBytes (string str, Encoding e)
 
697
                {
 
698
                        if (str == null)
 
699
                                return null;
 
700
 
 
701
                        if (e == null)
 
702
                                throw new ArgumentNullException ("e");
 
703
 
 
704
                        return UrlDecodeToBytes (e.GetBytes (str));
 
705
                }
 
706
 
 
707
                public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
 
708
                {
 
709
                        if (bytes == null)
 
710
                                return null;
 
711
                        if (count == 0)
 
712
                                return new byte [0];
 
713
 
 
714
                        int len = bytes.Length;
 
715
                        if (offset < 0 || offset >= len)
 
716
                                throw new ArgumentOutOfRangeException("offset");
 
717
 
 
718
                        if (count < 0 || offset > len - count)
 
719
                                throw new ArgumentOutOfRangeException("count");
 
720
 
 
721
                        MemoryStream result = new MemoryStream ();
 
722
                        int end = offset + count;
 
723
                        for (int i = offset; i < end; i++){
 
724
                                char c = (char) bytes [i];
 
725
                                if (c == '+') {
 
726
                                        c = ' ';
 
727
                                } else if (c == '%' && i < end - 2) {
 
728
                                        int xchar = GetChar (bytes, i + 1, 2);
 
729
                                        if (xchar != -1) {
 
730
                                                c = (char) xchar;
 
731
                                                i += 2;
 
732
                                        }
 
733
                                }
 
734
                                result.WriteByte ((byte) c);
 
735
                        }
 
736
 
 
737
                        return result.ToArray ();
 
738
                }
 
739
 
 
740
                public static string UrlEncode (string str) 
 
741
                {
 
742
                        if (str == null)
 
743
                                return null;
 
744
 
 
745
                        if (str == "")
 
746
                                return "";
 
747
 
 
748
                        byte [] bytes = Encoding.UTF8.GetBytes (str);
 
749
                        bytes = UrlEncodeToBytes (bytes, 0, bytes.Length);
 
750
                        char [] copy = new char [bytes.Length];
 
751
                        for (int i = 0; i < bytes.Length; i++)
 
752
                                copy [i] = (char) bytes [i];
 
753
                        
 
754
                        return new string (copy);
 
755
                }
 
756
          
 
757
                public static byte [] UrlEncodeToBytes (string str, Encoding e)
 
758
                {
 
759
                        if (str == null)
 
760
                                return null;
 
761
 
 
762
                        if (str == "")
 
763
                                return new byte [0];
 
764
 
 
765
                        byte [] bytes = e.GetBytes (str);
 
766
                        return UrlEncodeToBytes (bytes, 0, bytes.Length);
 
767
                }
 
768
 
 
769
                public static byte [] UrlEncodeToBytes (byte [] bytes)
 
770
                {
 
771
                        if (bytes == null)
 
772
                                return null;
 
773
 
 
774
                        if (bytes.Length == 0)
 
775
                                return new byte [0];
 
776
 
 
777
                        return UrlEncodeToBytes (bytes, 0, bytes.Length);
 
778
                }
 
779
 
 
780
                static char [] hexChars = "0123456789abcdef".ToCharArray ();
 
781
                const string notEncoded = "!'()*-._";
 
782
 
 
783
                static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
 
784
                        if (c > 255) {
 
785
                                //FIXME: what happens when there is an internal error?
 
786
                                //if (!isUnicode)
 
787
                                //      throw new ArgumentOutOfRangeException ("c", c, "c must be less than 256");
 
788
                                int idx;
 
789
                                int i = (int) c;
 
790
 
 
791
                                result.WriteByte ((byte)'%');
 
792
                                result.WriteByte ((byte)'u');
 
793
                                idx = i >> 12;
 
794
                                result.WriteByte ((byte)hexChars [idx]);
 
795
                                idx = (i >> 8) & 0x0F;
 
796
                                result.WriteByte ((byte)hexChars [idx]);
 
797
                                idx = (i >> 4) & 0x0F;
 
798
                                result.WriteByte ((byte)hexChars [idx]);
 
799
                                idx = i & 0x0F;
 
800
                                result.WriteByte ((byte)hexChars [idx]);
 
801
                                return;
 
802
                        }
 
803
                        
 
804
                        if (c>' ' && notEncoded.IndexOf (c)!=-1) {
 
805
                                result.WriteByte ((byte)c);
 
806
                                return;
 
807
                        }
 
808
                        if (c==' ') {
 
809
                                result.WriteByte ((byte)'+');
 
810
                                return;
 
811
                        }
 
812
                        if (    (c < '0') ||
 
813
                                (c < 'A' && c > '9') ||
 
814
                                (c > 'Z' && c < 'a') ||
 
815
                                (c > 'z')) {
 
816
                                if (isUnicode && c > 127) {
 
817
                                        result.WriteByte ((byte)'%');
 
818
                                        result.WriteByte ((byte)'u');
 
819
                                        result.WriteByte ((byte)'0');
 
820
                                        result.WriteByte ((byte)'0');
 
821
                                }
 
822
                                else
 
823
                                        result.WriteByte ((byte)'%');
 
824
                                
 
825
                                int idx = ((int) c) >> 4;
 
826
                                result.WriteByte ((byte)hexChars [idx]);
 
827
                                idx = ((int) c) & 0x0F;
 
828
                                result.WriteByte ((byte)hexChars [idx]);
 
829
                        }
 
830
                        else
 
831
                                result.WriteByte ((byte)c);
 
832
                }
 
833
 
 
834
                public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
 
835
                {
 
836
                        if (bytes == null)
 
837
                                return null;
 
838
 
 
839
                        int len = bytes.Length;
 
840
                        if (len == 0)
 
841
                                return new byte [0];
 
842
 
 
843
                        if (offset < 0 || offset >= len)
 
844
                                throw new ArgumentOutOfRangeException("offset");
 
845
 
 
846
                        if (count < 0 || count > len - offset)
 
847
                                throw new ArgumentOutOfRangeException("count");
 
848
 
 
849
                        MemoryStream result = new MemoryStream (count);
 
850
                        int end = offset + count;
 
851
                        for (int i = offset; i < end; i++)
 
852
                                UrlEncodeChar ((char)bytes [i], result, false);
 
853
 
 
854
                        return result.ToArray();
 
855
                }
 
856
 
 
857
        }
 
858
}
 
859