~ubuntu-branches/ubuntu/saucy/f-spot/saucy

« back to all changes in this revision

Viewing changes to lib/Hyena/src/Hyena/System.Web/HttpUtility.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-05-24 10:35:57 UTC
  • mfrom: (2.4.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20100524103557-1j0i8f66caybci2n
Tags: 0.7.0-1
* New upstream release 0.7.0
 + First release of the unstable 0.7 development series. Massive changes.
 + Reparenting and detaching support (Anton Keks) (Closes: #559745)
 + A new Mallard-based documentation (Harold Schreckengost)
 + No longer embeds flickrnet, uses distribution copy (Iain Lane)
 + Adoption of a large amount of Hyena functionality (Paul Lange, Peter
   Goetz)
 + No longer embeds gnome-keyring-sharp
 + Completely rewritten import, much faster and less memory hungry (Ruben
   Vermeersch) (Closes: #559080, #492658, #341790, #357811, #426017) (LP:
   #412091)
 + No longer use gphoto2-sharp, now uses gvfs which is less crash-pron
   (Ruben Vermeersch)
 + Fix Facebook support (Ruben Vermeersch)
 + Modernized unit tests
 + Revamped build (Mike Gemünde)
 + Much improved duplicate detection (much faster too) (Ruben Vermeersch)
 + Mouse selection in Iconview (Vincent Pomey)
 + Image panning support using middle mouse button (Wojciech Dzierżanowski)
 + Timeline slider now restricted to the size of the window (Iain Churcher)
 + Over 100 bugs closed (http://bit.ly/cyVjnD)
   - No more warnings about schema defaults (Closes: #584215) (LP: #586132)
* debian/control: Clean up build deps to match configure checks
* debian/rules: Don't run dh_makeshilbs as we don't ship any shared
  libraries. There are some private ones though, which get picked up and
  result in a useless postinst/postrm call to ldconfig. Thanks, lintian.
* debian/patches/debian_fix-distclean.patch,
  debian/patches/debian_fix_f-spot.exe.config.patch,
  debian/patches/debian_link-system-flickrnet.patch,
  debian/patches/debian_link-system-gnome-keyring.patch,
  debian/patches/debian_disable-unit-tests,
  debian/patches/git_transition_duration.patch,
  debian/patches/ubuntu_fix_folder_export_hang.patch:
  Clean up obsolete patches which are no longer necessary 
* debian/patches/*: Temporarily disable patches which originated from Ubuntu
  and no longer apply cleanly. We will get these back in a future upstream
  development release.
* debian/patches/*: Refresh to apply cleanly 
* debian/rules: Add new include dir to autoreconf call to pick up f-spot
  macros 

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