do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.Web / System.Web / HttpUtility.cs
1 // 
2 // System.Web.HttpUtility
3 //
4 // Authors:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Wictor WilĂ©n (decode/encode functions) (wictor@ibizkit.se)
7 //   Tim Coleman (tim@timcoleman.com)
8 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 //
10 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.Specialized;
35 using System.Globalization;
36 using System.IO;
37 using System.Security.Permissions;
38 using System.Text;
39 using System.Web.Util;
40
41 namespace System.Web {
42
43 #if !MOBILE
44         // CAS - no InheritanceDemand here as the class is sealed
45         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 #endif
47         public sealed class HttpUtility
48         {
49                 sealed class HttpQSCollection : NameValueCollection
50                 {
51                         public override string ToString ()
52                         {
53                                 int count = Count;
54                                 if (count == 0)
55                                         return "";
56                                 StringBuilder sb = new StringBuilder ();
57                                 string [] keys = AllKeys;
58                                 for (int i = 0; i < count; i++) {
59                                         sb.AppendFormat ("{0}={1}&", keys [i], this [keys [i]]);
60                                 }
61                                 if (sb.Length > 0)
62                                         sb.Length--;
63                                 return sb.ToString ();
64                         }
65                 }
66                 
67                 #region Constructors
68
69                 public HttpUtility () 
70                 {
71                 }
72         
73                 #endregion // Constructors
74         
75                 #region Methods
76         
77                 public static void HtmlAttributeEncode (string s, TextWriter output) 
78                 {
79                         if (output == null) {
80 #if NET_4_0
81                                 throw new ArgumentNullException ("output");
82 #else
83                                 throw new NullReferenceException (".NET emulation");
84 #endif
85                         }
86 #if NET_4_0
87                         HttpEncoder.Current.HtmlAttributeEncode (s, output);
88 #else
89                         output.Write (HttpEncoder.HtmlAttributeEncode (s));
90 #endif
91                 }
92         
93                 public static string HtmlAttributeEncode (string s) 
94                 {
95 #if NET_4_0
96                         if (s == null)
97                                 return null;
98                         
99                         using (var sw = new StringWriter ()) {
100                                 HttpEncoder.Current.HtmlAttributeEncode (s, sw);
101                                 return sw.ToString ();
102                         }
103 #else
104                         return HttpEncoder.HtmlAttributeEncode (s);
105 #endif
106                 }
107         
108                 public static string UrlDecode (string str) 
109                 {
110                         return UrlDecode(str, Encoding.UTF8);
111                 }
112         
113                 static char [] GetChars (MemoryStream b, Encoding e)
114                 {
115                         return e.GetChars (b.GetBuffer (), 0, (int) b.Length);
116                 }
117
118                 static void WriteCharBytes (IList buf, char ch, Encoding e)
119                 {
120                         if (ch > 255) {
121                                 foreach (byte b in e.GetBytes (new char[] { ch }))
122                                         buf.Add (b);
123                         } else
124                                 buf.Add ((byte)ch);
125                 }
126                 
127                 public static string UrlDecode (string s, Encoding e)
128                 {
129                         if (null == s) 
130                                 return null;
131
132                         if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
133                                 return s;
134                         
135                         if (e == null)
136                                 e = Encoding.UTF8;
137
138                         long len = s.Length;
139                         var bytes = new List <byte> ();
140                         int xchar;
141                         char ch;
142                         
143                         for (int i = 0; i < len; i++) {
144                                 ch = s [i];
145                                 if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
146                                         if (s [i + 1] == 'u' && i + 5 < len) {
147                                                 // unicode hex sequence
148                                                 xchar = GetChar (s, i + 2, 4);
149                                                 if (xchar != -1) {
150                                                         WriteCharBytes (bytes, (char)xchar, e);
151                                                         i += 5;
152                                                 } else
153                                                         WriteCharBytes (bytes, '%', e);
154                                         } else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
155                                                 WriteCharBytes (bytes, (char)xchar, e);
156                                                 i += 2;
157                                         } else {
158                                                 WriteCharBytes (bytes, '%', e);
159                                         }
160                                         continue;
161                                 }
162
163                                 if (ch == '+')
164                                         WriteCharBytes (bytes, ' ', e);
165                                 else
166                                         WriteCharBytes (bytes, ch, e);
167                         }
168                         
169                         byte[] buf = bytes.ToArray ();
170                         bytes = null;
171                         return e.GetString (buf);
172                         
173                 }
174         
175                 public static string UrlDecode (byte [] bytes, Encoding e)
176                 {
177                         if (bytes == null)
178                                 return null;
179
180                         return UrlDecode (bytes, 0, bytes.Length, e);
181                 }
182
183                 static int GetInt (byte b)
184                 {
185                         char c = (char) b;
186                         if (c >= '0' && c <= '9')
187                                 return c - '0';
188
189                         if (c >= 'a' && c <= 'f')
190                                 return c - 'a' + 10;
191
192                         if (c >= 'A' && c <= 'F')
193                                 return c - 'A' + 10;
194
195                         return -1;
196                 }
197
198                 static int GetChar (byte [] bytes, int offset, int length)
199                 {
200                         int value = 0;
201                         int end = length + offset;
202                         for (int i = offset; i < end; i++) {
203                                 int current = GetInt (bytes [i]);
204                                 if (current == -1)
205                                         return -1;
206                                 value = (value << 4) + current;
207                         }
208
209                         return value;
210                 }
211
212                 static int GetChar (string str, int offset, int length)
213                 {
214                         int val = 0;
215                         int end = length + offset;
216                         for (int i = offset; i < end; i++) {
217                                 char c = str [i];
218                                 if (c > 127)
219                                         return -1;
220
221                                 int current = GetInt ((byte) c);
222                                 if (current == -1)
223                                         return -1;
224                                 val = (val << 4) + current;
225                         }
226
227                         return val;
228                 }
229                 
230                 public static string UrlDecode (byte [] bytes, int offset, int count, Encoding e)
231                 {
232                         if (bytes == null)
233                                 return null;
234                         if (count == 0)
235                                 return String.Empty;
236
237                         if (bytes == null)
238                                 throw new ArgumentNullException ("bytes");
239
240                         if (offset < 0 || offset > bytes.Length)
241                                 throw new ArgumentOutOfRangeException ("offset");
242
243                         if (count < 0 || offset + count > bytes.Length)
244                                 throw new ArgumentOutOfRangeException ("count");
245
246                         StringBuilder output = new StringBuilder ();
247                         MemoryStream acc = new MemoryStream ();
248
249                         int end = count + offset;
250                         int xchar;
251                         for (int i = offset; i < end; i++) {
252                                 if (bytes [i] == '%' && i + 2 < count && bytes [i + 1] != '%') {
253                                         if (bytes [i + 1] == (byte) 'u' && i + 5 < end) {
254                                                 if (acc.Length > 0) {
255                                                         output.Append (GetChars (acc, e));
256                                                         acc.SetLength (0);
257                                                 }
258                                                 xchar = GetChar (bytes, i + 2, 4);
259                                                 if (xchar != -1) {
260                                                         output.Append ((char) xchar);
261                                                         i += 5;
262                                                         continue;
263                                                 }
264                                         } else if ((xchar = GetChar (bytes, i + 1, 2)) != -1) {
265                                                 acc.WriteByte ((byte) xchar);
266                                                 i += 2;
267                                                 continue;
268                                         }
269                                 }
270
271                                 if (acc.Length > 0) {
272                                         output.Append (GetChars (acc, e));
273                                         acc.SetLength (0);
274                                 }
275
276                                 if (bytes [i] == '+') {
277                                         output.Append (' ');
278                                 } else {
279                                         output.Append ((char) bytes [i]);
280                                 }
281                         }
282
283                         if (acc.Length > 0) {
284                                 output.Append (GetChars (acc, e));
285                         }
286                         
287                         acc = null;
288                         return output.ToString ();
289                 }
290         
291                 public static byte [] UrlDecodeToBytes (byte [] bytes)
292                 {
293                         if (bytes == null)
294                                 return null;
295
296                         return UrlDecodeToBytes (bytes, 0, bytes.Length);
297                 }
298
299                 public static byte [] UrlDecodeToBytes (string str)
300                 {
301                         return UrlDecodeToBytes (str, Encoding.UTF8);
302                 }
303
304                 public static byte [] UrlDecodeToBytes (string str, Encoding e)
305                 {
306                         if (str == null)
307                                 return null;
308
309                         if (e == null)
310                                 throw new ArgumentNullException ("e");
311
312                         return UrlDecodeToBytes (e.GetBytes (str));
313                 }
314
315                 public static byte [] UrlDecodeToBytes (byte [] bytes, int offset, int count)
316                 {
317                         if (bytes == null)
318                                 return null;
319                         if (count == 0)
320                                 return new byte [0];
321
322                         int len = bytes.Length;
323                         if (offset < 0 || offset >= len)
324                                 throw new ArgumentOutOfRangeException("offset");
325
326                         if (count < 0 || offset > len - count)
327                                 throw new ArgumentOutOfRangeException("count");
328
329                         MemoryStream result = new MemoryStream ();
330                         int end = offset + count;
331                         for (int i = offset; i < end; i++){
332                                 char c = (char) bytes [i];
333                                 if (c == '+') {
334                                         c = ' ';
335                                 } else if (c == '%' && i < end - 2) {
336                                         int xchar = GetChar (bytes, i + 1, 2);
337                                         if (xchar != -1) {
338                                                 c = (char) xchar;
339                                                 i += 2;
340                                         }
341                                 }
342                                 result.WriteByte ((byte) c);
343                         }
344
345                         return result.ToArray ();
346                 }
347
348                 public static string UrlEncode(string str) 
349                 {
350                         return UrlEncode(str, Encoding.UTF8);
351                 }
352         
353                 public static string UrlEncode (string s, Encoding Enc) 
354                 {
355                         if (s == null)
356                                 return null;
357
358                         if (s == String.Empty)
359                                 return String.Empty;
360
361                         bool needEncode = false;
362                         int len = s.Length;
363                         for (int i = 0; i < len; i++) {
364                                 char c = s [i];
365                                 if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z')) {
366                                         if (HttpEncoder.NotEncoded (c))
367                                                 continue;
368
369                                         needEncode = true;
370                                         break;
371                                 }
372                         }
373
374                         if (!needEncode)
375                                 return s;
376
377                         // avoided GetByteCount call
378                         byte [] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
379                         int realLen = Enc.GetBytes (s, 0, s.Length, bytes, 0);
380                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, realLen));
381                 }
382           
383                 public static string UrlEncode (byte [] bytes)
384                 {
385                         if (bytes == null)
386                                 return null;
387
388                         if (bytes.Length == 0)
389                                 return String.Empty;
390
391                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, 0, bytes.Length));
392                 }
393
394                 public static string UrlEncode (byte [] bytes, int offset, int count)
395                 {
396                         if (bytes == null)
397                                 return null;
398
399                         if (bytes.Length == 0)
400                                 return String.Empty;
401
402                         return Encoding.ASCII.GetString (UrlEncodeToBytes (bytes, offset, count));
403                 }
404
405                 public static byte [] UrlEncodeToBytes (string str)
406                 {
407                         return UrlEncodeToBytes (str, Encoding.UTF8);
408                 }
409
410                 public static byte [] UrlEncodeToBytes (string str, Encoding e)
411                 {
412                         if (str == null)
413                                 return null;
414
415                         if (str.Length == 0)
416                                 return new byte [0];
417
418                         byte [] bytes = e.GetBytes (str);
419                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
420                 }
421
422                 public static byte [] UrlEncodeToBytes (byte [] bytes)
423                 {
424                         if (bytes == null)
425                                 return null;
426
427                         if (bytes.Length == 0)
428                                 return new byte [0];
429
430                         return UrlEncodeToBytes (bytes, 0, bytes.Length);
431                 }
432
433                 public static byte [] UrlEncodeToBytes (byte [] bytes, int offset, int count)
434                 {
435                         if (bytes == null)
436                                 return null;
437 #if NET_4_0
438                         return HttpEncoder.Current.UrlEncode (bytes, offset, count);
439 #else
440                         return HttpEncoder.UrlEncodeToBytes (bytes, offset, count);
441 #endif
442                 }
443
444                 public static string UrlEncodeUnicode (string str)
445                 {
446                         if (str == null)
447                                 return null;
448
449                         return Encoding.ASCII.GetString (UrlEncodeUnicodeToBytes (str));
450                 }
451
452                 public static byte [] UrlEncodeUnicodeToBytes (string str)
453                 {
454                         if (str == null)
455                                 return null;
456
457                         if (str.Length == 0)
458                                 return new byte [0];
459
460                         MemoryStream result = new MemoryStream (str.Length);
461                         foreach (char c in str){
462                                 HttpEncoder.UrlEncodeChar (c, result, true);
463                         }
464                         return result.ToArray ();
465                 }
466
467                 /// <summary>
468                 /// Decodes an HTML-encoded string and returns the decoded string.
469                 /// </summary>
470                 /// <param name="s">The HTML string to decode. </param>
471                 /// <returns>The decoded text.</returns>
472                 public static string HtmlDecode (string s) 
473                 {
474 #if NET_4_0
475                         if (s == null)
476                                 return null;
477                         
478                         using (var sw = new StringWriter ()) {
479                                 HttpEncoder.Current.HtmlDecode (s, sw);
480                                 return sw.ToString ();
481                         }
482 #else
483                         return HttpEncoder.HtmlDecode (s);
484 #endif
485                 }
486         
487                 /// <summary>
488                 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
489                 /// </summary>
490                 /// <param name="s">The HTML string to decode</param>
491                 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
492                 public static void HtmlDecode(string s, TextWriter output) 
493                 {
494                         if (output == null) {
495 #if NET_4_0
496                                 throw new ArgumentNullException ("output");
497 #else
498                                 throw new NullReferenceException (".NET emulation");
499 #endif
500                         }
501                                 
502                         if (!String.IsNullOrEmpty (s)) {
503 #if NET_4_0
504                                 HttpEncoder.Current.HtmlDecode (s, output);
505 #else
506                                 output.Write (HttpEncoder.HtmlDecode (s));
507 #endif
508                         }
509                 }
510
511                 public static string HtmlEncode (string s)
512                 {
513 #if NET_4_0
514                         if (s == null)
515                                 return null;
516                         
517                         using (var sw = new StringWriter ()) {
518                                 HttpEncoder.Current.HtmlEncode (s, sw);
519                                 return sw.ToString ();
520                         }
521 #else
522                         return HttpEncoder.HtmlEncode (s);
523 #endif
524                 }
525                 
526                 /// <summary>
527                 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
528                 /// </summary>
529                 /// <param name="s">The string to encode. </param>
530                 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
531                 public static void HtmlEncode(string s, TextWriter output) 
532                 {
533                         if (output == null) {
534 #if NET_4_0
535                                 throw new ArgumentNullException ("output");
536 #else
537                                 throw new NullReferenceException (".NET emulation");
538 #endif
539                         }
540                                 
541                         if (!String.IsNullOrEmpty (s)) {
542 #if NET_4_0
543                                 HttpEncoder.Current.HtmlEncode (s, output);
544 #else
545                                 output.Write (HttpEncoder.HtmlEncode (s));
546 #endif
547                         }
548                 }
549 #if NET_4_0
550                 public static string HtmlEncode (object value)
551                 {
552                         if (value == null)
553                                 return null;
554
555                         IHtmlString htmlString = value as IHtmlString;
556                         if (htmlString != null)
557                                 return htmlString.ToHtmlString ();
558                         
559                         return HtmlEncode (value.ToString ());
560                 }
561
562                 public static string JavaScriptStringEncode (string value)
563                 {
564                         return JavaScriptStringEncode (value, false);
565                 }
566
567                 public static string JavaScriptStringEncode (string value, bool addDoubleQuotes)
568                 {
569                         if (String.IsNullOrEmpty (value))
570                                 return addDoubleQuotes ? "\"\"" : String.Empty;
571
572                         int len = value.Length;
573                         bool needEncode = false;
574                         char c;
575                         for (int i = 0; i < len; i++) {
576                                 c = value [i];
577
578                                 if (c >= 0 && c <= 31 || c == 34 || c == 39 || c == 60 || c == 62 || c == 92) {
579                                         needEncode = true;
580                                         break;
581                                 }
582                         }
583
584                         if (!needEncode)
585                                 return addDoubleQuotes ? "\"" + value + "\"" : value;
586
587                         var sb = new StringBuilder ();
588                         if (addDoubleQuotes)
589                                 sb.Append ('"');
590
591                         for (int i = 0; i < len; i++) {
592                                 c = value [i];
593                                 if (c >= 0 && c <= 7 || c == 11 || c >= 14 && c <= 31 || c == 39 || c == 60 || c == 62)
594                                         sb.AppendFormat ("\\u{0:x4}", (int)c);
595                                 else switch ((int)c) {
596                                                 case 8:
597                                                         sb.Append ("\\b");
598                                                         break;
599
600                                                 case 9:
601                                                         sb.Append ("\\t");
602                                                         break;
603
604                                                 case 10:
605                                                         sb.Append ("\\n");
606                                                         break;
607
608                                                 case 12:
609                                                         sb.Append ("\\f");
610                                                         break;
611
612                                                 case 13:
613                                                         sb.Append ("\\r");
614                                                         break;
615
616                                                 case 34:
617                                                         sb.Append ("\\\"");
618                                                         break;
619
620                                                 case 92:
621                                                         sb.Append ("\\\\");
622                                                         break;
623
624                                                 default:
625                                                         sb.Append (c);
626                                                         break;
627                                         }
628                         }
629
630                         if (addDoubleQuotes)
631                                 sb.Append ('"');
632
633                         return sb.ToString ();
634                 }
635 #endif
636                 public static string UrlPathEncode (string s)
637                 {
638 #if NET_4_0
639                         return HttpEncoder.Current.UrlPathEncode (s);
640 #else
641                         return HttpEncoder.UrlPathEncode (s);
642 #endif
643                 }
644
645                 public static NameValueCollection ParseQueryString (string query)
646                 {
647                         return ParseQueryString (query, Encoding.UTF8);
648                 }
649
650                 public static NameValueCollection ParseQueryString (string query, Encoding encoding)
651                 {
652                         if (query == null)
653                                 throw new ArgumentNullException ("query");
654                         if (encoding == null)
655                                 throw new ArgumentNullException ("encoding");
656                         if (query.Length == 0 || (query.Length == 1 && query[0] == '?'))
657                                 return new HttpQSCollection ();
658                         if (query[0] == '?')
659                                 query = query.Substring (1);
660                                 
661                         NameValueCollection result = new HttpQSCollection ();
662                         ParseQueryString (query, encoding, result);
663                         return result;
664                 }
665
666                 internal static void ParseQueryString (string query, Encoding encoding, NameValueCollection result)
667                 {
668                         if (query.Length == 0)
669                                 return;
670
671                         string decoded = HtmlDecode (query);
672                         int decodedLength = decoded.Length;
673                         int namePos = 0;
674                         bool first = true;
675                         while (namePos <= decodedLength) {
676                                 int valuePos = -1, valueEnd = -1;
677                                 for (int q = namePos; q < decodedLength; q++) {
678                                         if (valuePos == -1 && decoded [q] == '=') {
679                                                 valuePos = q + 1;
680                                         } else if (decoded [q] == '&') {
681                                                 valueEnd = q;
682                                                 break;
683                                         }
684                                 }
685
686                                 if (first) {
687                                         first = false;
688                                         if (decoded [namePos] == '?')
689                                                 namePos++;
690                                 }
691                                 
692                                 string name, value;
693                                 if (valuePos == -1) {
694                                         name = null;
695                                         valuePos = namePos;
696                                 } else {
697                                         name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
698                                 }
699                                 if (valueEnd < 0) {
700                                         namePos = -1;
701                                         valueEnd = decoded.Length;
702                                 } else {
703                                         namePos = valueEnd + 1;
704                                 }
705                                 value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
706
707                                 result.Add (name, value);
708                                 if (namePos == -1)
709                                         break;
710                         }
711                 }
712                 #endregion // Methods
713         }
714 }
715