Fix for running against RabbitMQ 2.2
[mono.git] / mcs / class / System.Web / System.Web / BrowserCapabilities.cs
1 // 
2 // System.Web.HttpBrowserCapabilities
3 //
4 // Authors:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2003-2009 Novell, Inc. (http://www.novell.com)
9 //
10
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.Globalization;
34 using System.Web.Configuration;
35 using System.Web.UI;
36 using System.Security.Permissions;
37 using System.Web.Util;
38
39 namespace System.Web.Configuration
40 {
41         public partial class HttpCapabilitiesBase
42         {
43                 const int HaveActiveXControls = 1; // 1;
44                 const int HaveAdapters = 2;
45                 const int HaveAOL = 3; // 2;
46                 const int HaveBackGroundSounds = 4; // 3;
47                 const int HaveBeta = 5; // 4;
48                 const int HaveBrowser = 6; // 5;
49                 const int HaveBrowsers = 7;
50                 const int HaveCanCombineFormsInDeck = 8;
51                 const int HaveCanInitiateVoiceCall = 9;
52                 const int HaveCanRenderAfterInputOrSelectElement = 10;
53                 const int HaveCanRenderEmptySelects = 11;
54                 const int HaveCanRenderInputAndSelectElementsTogether = 12;
55                 const int HaveCanRenderMixedSelects = 13;
56                 const int HaveCanRenderOneventAndPrevElementsTogether = 14;
57                 const int HaveCanRenderPostBackCards = 15;
58                 const int HaveCanRenderSetvarZeroWithMultiSelectionList = 16;
59                 const int HaveCanSendMail = 17;
60                 const int HaveCDF = 18; // 6;
61                 //const int HaveClrVersion = 19; // 7;
62                 const int HaveCookies = 20; // 8;
63                 const int HaveCrawler = 21; // 9;
64                 const int HaveDefaultSubmitButtonLimit = 22;
65                 const int HaveEcmaScriptVersion = 23;
66                 const int HaveFrames = 24; // 11;
67                 const int HaveGatewayMajorVersion = 25;
68                 const int HaveGatewayMinorVersion = 26;
69                 const int HaveGatewayVersion = 27;
70                 const int HaveHasBackButton = 28;
71                 const int HaveHidesRightAlignedMultiselectScrollbars = 29;
72                 const int HaveHtmlTextWriter = 30;
73                 const int HaveId = 31;
74                 const int HaveInputType = 32;
75                 const int HaveIsColor = 33;
76                 const int HaveIsMobileDevice = 34;
77                 const int HaveJavaApplets = 35; // 12;
78                 const int HaveJavaScript = 36; // 13;
79                 const int HaveJScriptVersion = 37;
80                 const int HaveMajorVersion = 38; // 14;
81                 const int HaveMaximumHrefLength = 39;
82                 const int HaveMaximumRenderedPageSize = 40;
83                 const int HaveMaximumSoftkeyLabelLength = 41;
84                 const int HaveMinorVersion = 42; // 15;
85                 const int HaveMinorVersionString = 43;
86                 const int HaveMobileDeviceManufacturer = 44;
87                 const int HaveMobileDeviceModel = 45;
88                 const int HaveMSDomVersion = 46; // 16;
89                 const int HaveNumberOfSoftkeys = 47;
90                 const int HavePlatform = 48; // 17;
91                 const int HavePreferredImageMime = 49;
92                 const int HavePreferredRenderingMime = 50;
93                 const int HavePreferredRenderingType = 51;
94                 const int HavePreferredRequestEncoding = 52;
95                 const int HavePreferredResponseEncoding = 53;
96                 const int HaveRendersBreakBeforeWmlSelectAndInput = 54;
97                 const int HaveRendersBreaksAfterHtmlLists = 55;
98                 const int HaveRendersBreaksAfterWmlAnchor = 56;
99                 const int HaveRendersBreaksAfterWmlInput = 57;
100                 const int HaveRendersWmlDoAcceptsInline = 58;
101                 const int HaveRendersWmlSelectsAsMenuCards = 59;
102                 const int HaveRequiredMetaTagNameValue = 60;
103                 const int HaveRequiresAttributeColonSubstitution = 61;
104                 const int HaveRequiresContentTypeMetaTag = 62;
105                 const int HaveRequiresControlStateInSession = 63;
106                 const int HaveRequiresDBCSCharacter = 64;
107                 const int HaveRequiresHtmlAdaptiveErrorReporting = 65;
108                 const int HaveRequiresLeadingPageBreak = 66;
109                 const int HaveRequiresNoBreakInFormatting = 67;
110                 const int HaveRequiresOutputOptimization = 68;
111                 const int HaveRequiresPhoneNumbersAsPlainText = 69;
112                 const int HaveRequiresSpecialViewStateEncoding = 70;
113                 const int HaveRequiresUniqueFilePathSuffix = 71;
114                 const int HaveRequiresUniqueHtmlCheckboxNames = 72;
115                 const int HaveRequiresUniqueHtmlInputNames = 73;
116                 const int HaveRequiresUrlEncodedPostfieldValues = 74;
117                 const int HaveScreenBitDepth = 75;
118                 const int HaveScreenCharactersHeight = 76;
119                 const int HaveScreenCharactersWidth = 77;
120                 const int HaveScreenPixelsHeight = 78;
121                 const int HaveScreenPixelsWidth = 79;
122                 const int HaveSupportsAccesskeyAttribute = 80;
123                 const int HaveSupportsBodyColor = 81;
124                 const int HaveSupportsBold = 82;
125                 const int HaveSupportsCacheControlMetaTag = 83;
126                 const int HaveSupportsCallback = 84;
127                 const int HaveSupportsCss = 85;
128                 const int HaveSupportsDivAlign = 86;
129                 const int HaveSupportsDivNoWrap = 87;
130                 const int HaveSupportsEmptyStringInCookieValue = 88;
131                 const int HaveSupportsFontColor = 89;
132                 const int HaveSupportsFontName = 90;
133                 const int HaveSupportsFontSize = 91;
134                 const int HaveSupportsImageSubmit = 92;
135                 const int HaveSupportsIModeSymbols = 93;
136                 const int HaveSupportsInputIStyle = 94;
137                 const int HaveSupportsInputMode = 95;
138                 const int HaveSupportsItalic = 96;
139                 const int HaveSupportsJPhoneMultiMediaAttributes = 97;
140                 const int HaveSupportsJPhoneSymbols = 98;
141                 const int HaveSupportsQueryStringInFormAction = 99;
142                 const int HaveSupportsRedirectWithCookie = 100;
143                 const int HaveSupportsSelectMultiple = 101;
144                 const int HaveSupportsUncheck = 102;
145                 const int HaveSupportsXmlHttp = 103;
146                 const int HaveTables = 104; // 18;
147                 const int HaveTagWriter = 105; // 19;
148                 const int HaveType = 106;
149                 const int HaveUseOptimizedCacheKey = 107;
150                 const int HaveVBScript = 108; // 20;
151                 const int HaveVersion = 109; // 21;
152                 const int HaveW3CDomVersion = 110; // 22;
153                 const int HaveWin16 = 111; // 23;
154                 const int HaveWin32 = 112; // 24;
155                 const int LastHaveFlag = 113;
156
157                 BitArray flags = new BitArray (LastHaveFlag);
158                 bool activeXControls;
159                 bool aol;
160                 bool backgroundSounds;
161                 bool beta;
162                 string browser;
163                 bool cdf;
164                 Version clrVersion;
165                 bool cookies;
166                 bool crawler;
167                 Version ecmaScriptVersion;
168                 bool frames;
169                 bool javaApplets;
170                 bool javaScript;
171                 int majorVersion;
172                 double minorVersion;
173                 Version msDomVersion;
174                 string platform;
175                 bool tables;
176                 Type tagWriter;
177                 bool vbscript;
178                 string version;
179                 Version w3CDomVersion;
180                 bool win16;
181                 bool win32;
182                 Version [] clrVersions;
183                 internal string useragent;
184
185                 public bool ActiveXControls {
186                         get {
187                                 if (!Get (HaveActiveXControls)) {
188                                         activeXControls = ReadBoolean ("activexcontrols");
189                                         Set (HaveActiveXControls);
190                                 }
191
192                                 return activeXControls;
193                         }
194                 }
195
196                 public bool AOL {
197                         get {
198                                 if (!Get (HaveAOL)) {
199                                         aol = ReadBoolean ("aol");
200                                         Set (HaveAOL);
201                                 }
202
203                                 return aol;
204                         }
205                 }
206
207                 public bool BackgroundSounds {
208                         get {
209                                 if (!Get (HaveBackGroundSounds)) {
210                                         backgroundSounds = ReadBoolean ("backgroundsounds");
211                                         Set (HaveBackGroundSounds);
212                                 }
213
214                                 return backgroundSounds;
215                         }
216                 }
217
218                 public bool Beta {
219                         get {
220                                 if (!Get (HaveBeta)) {
221                                         beta = ReadBoolean ("beta");
222                                         Set (HaveBeta);
223                                 }
224
225                                 return beta;
226                         }
227                 }
228
229                 public string Browser {
230                         get {
231                                 if (!Get (HaveBrowser)) {
232                                         browser = this ["browser"];
233                                         if (browser == null || browser.Length == 0)
234                                                 browser = "Unknown";
235                                         Set (HaveBrowser);
236                                 }
237
238                                 return browser;
239                         }
240                 }
241
242                 ArrayList browsers = null;
243                 public ArrayList Browsers {
244                         get {
245                                 if (!Get (HaveBrowsers)) {
246                                         browsers = ReadArrayList ("browsers");
247                                         Set (HaveBrowsers);
248                                 }
249
250                                 return browsers;
251                         }
252                 }
253
254                 public bool IsBrowser (string browserName) 
255                 {
256                         foreach (string browser in Browsers) {
257                                 if (0 == String.Compare (browser, "Unknown", StringComparison.OrdinalIgnoreCase)) {
258                                         continue;
259                                 }
260                                 if (0 == String.Compare (browserName, browser, StringComparison.OrdinalIgnoreCase)) {
261                                         return true;
262                                 }
263                         }
264                         return false;
265                 }
266
267                 public bool CDF {
268                         get {
269                                 if (!Get (HaveCDF)) {
270                                         cdf = ReadBoolean ("cdf");
271                                         Set (HaveCDF);
272                                 }
273
274                                 return cdf;
275                         }
276                 }
277
278                 public Version ClrVersion {
279                         get {
280                                 if (clrVersion == null)
281                                         InternalGetClrVersions ();
282
283                                 return clrVersion;
284                         }
285                 }
286
287                 public bool Cookies {
288                         get {
289                                 if (!Get (HaveCookies)) {
290                                         cookies = ReadBoolean ("cookies");
291                                         Set (HaveCookies);
292                                 }
293
294                                 return cookies;
295                         }
296                 }
297
298                 public bool Crawler {
299                         get {
300                                 if (!Get (HaveCrawler)) {
301                                         crawler = ReadBoolean ("crawler");
302                                         Set (HaveCrawler);
303                                 }
304
305                                 return crawler;
306                         }
307                 }
308
309                 public Version EcmaScriptVersion {
310                         get {
311                                 if (!Get (HaveEcmaScriptVersion)) {
312                                         ecmaScriptVersion = ReadVersion ("ecmascriptversion");
313                                         Set (HaveEcmaScriptVersion);
314                                 }
315
316                                 return ecmaScriptVersion;
317                         }
318                 }
319
320                 public bool Frames {
321                         get {
322                                 if (!Get (HaveFrames)) {
323                                         frames = ReadBoolean ("frames");
324                                         Set (HaveFrames);
325                                 }
326
327                                 return frames;
328                         }
329                 }
330
331                 public bool JavaApplets {
332                         get {
333                                 if (!Get (HaveJavaApplets)) {
334                                         javaApplets = ReadBoolean ("javaapplets");
335                                         Set (HaveJavaApplets);
336                                 }
337
338                                 return javaApplets;
339                         }
340                 }
341
342
343                 [Obsolete ("The recommended alternative is the EcmaScriptVersion property. A Major version value greater than or equal to 1 implies JavaScript support. http://go.microsoft.com/fwlink/?linkid=14202")]
344                 public bool JavaScript {
345                         get {
346                                 if (!Get (HaveJavaScript)) {
347                                         javaScript = ReadBoolean ("javascript");
348                                         Set (HaveJavaScript);
349                                 }
350
351                                 return javaScript;
352                         }
353                 }
354
355                 public int MajorVersion {
356                         get {
357                                 if (!Get (HaveMajorVersion)) {
358                                         majorVersion = ReadInt32 ("majorver");
359                                         Set (HaveMajorVersion);
360                                 }
361
362                                 return majorVersion;
363                         }
364                 }
365
366                 public double MinorVersion {
367                         get {
368                                 if (!Get (HaveMinorVersion)) {
369                                         minorVersion = ReadDouble ("minorver");
370                                         Set (HaveMinorVersion);
371                                 }
372
373                                 return minorVersion;
374                         }
375                 }
376
377                 public Version MSDomVersion {
378                         get {
379                                 if (!Get (HaveMSDomVersion)) {
380                                         msDomVersion = ReadVersion ("msdomversion");
381                                         Set (HaveMSDomVersion);
382                                 }
383
384                                 return msDomVersion;
385                         }
386                 }
387
388                 public string Platform {
389                         get {
390                                 if (!Get (HavePlatform)) {
391                                         platform = ReadString ("platform");
392                                         Set (HavePlatform);
393                                 }
394
395                                 return platform;
396                         }
397                 }
398
399                 public bool Tables {
400                         get {
401                                 if (!Get (HaveTables)) {
402                                         tables = ReadBoolean ("tables");
403                                         Set (HaveTables);
404                                 }
405
406                                 return tables;
407                         }
408                 }
409
410                 public Type TagWriter {
411                         get {
412                                 if (!Get (HaveTagWriter)) {
413                                         tagWriter = GetTagWriter ();
414                                         Set (HaveTagWriter);
415                                 }
416                                 return tagWriter;
417                         }
418                 }
419                 
420                 internal virtual Type GetTagWriter ()
421                 {
422                                 return typeof (HtmlTextWriter);                 
423                 }
424
425                 public string Type {
426                         get {
427                                 return Browser + MajorVersion;
428                         }
429                 }
430
431                 public bool VBScript {
432                         get {
433                                 if (!Get (HaveVBScript)) {
434                                         vbscript = ReadBoolean ("vbscript");
435                                         Set (HaveVBScript);
436                                 }
437
438                                 return vbscript;
439                         }
440                 }
441
442                 public string Version {
443                         get {
444                                 if (!Get (HaveVersion)) {
445                                         version = ReadString ("version");
446                                         Set (HaveVersion);
447                                 }
448
449                                 return version;
450                         }
451                 }
452
453                 public Version W3CDomVersion {
454                         get {
455                                 if (!Get (HaveW3CDomVersion)) {
456                                         w3CDomVersion = ReadVersion ("w3cdomversion");
457                                         Set (HaveW3CDomVersion);
458                                 }
459
460                                 return w3CDomVersion;
461                         }
462                 }
463
464                 public bool Win16 {
465                         get {
466                                 if (!Get (HaveWin16)) {
467                                         win16 = ReadBoolean ("win16");
468                                         Set (HaveWin16);
469                                 }
470
471                                 return win16;
472                         }
473                 }
474
475                 public bool Win32 {
476                         get {
477                                 // This is the list of different windows platforms that browscap.ini has.
478                                 // Win16 Win2000 Win2003 Win32 Win95 Win98 WinME WinNT WinVI WinXP
479                                 if (!Get (HaveWin32)) {
480                                         string platform = Platform;
481                                         win32 = (platform != "Win16" && platform.StartsWith ("Win"));
482                                         Set (HaveWin32);
483                                 }
484                                 return win32;
485                         }
486                 }
487
488                 public Version [] GetClrVersions ()
489                 {
490                         if (clrVersions == null)
491                                 InternalGetClrVersions ();
492
493                         return clrVersions;
494                 }
495
496                 void InternalGetClrVersions ()
497                 {
498                         char [] anychars = new char [] { ';', ')' };
499                         string s = useragent;
500                         ArrayList list = null;
501                         int idx;
502                         while ((s != null) && (idx = s.IndexOf (".NET CLR ")) != -1) {
503                                 int end = s.IndexOfAny (anychars, idx + 9);
504                                 if (end == -1)
505                                         break;
506
507                                 string ver = s.Substring (idx + 9, end - idx - 9);
508                                 Version v = null;
509                                 try {
510                                         v = new Version (ver);
511                                         if (clrVersion == null || v > clrVersion)
512                                                 clrVersion = v;
513
514                                         if (list == null)
515                                                 list = new ArrayList (4);
516
517                                         list.Add (v);
518                                 } catch { }
519                                 s = s.Substring (idx + 9);
520                         }
521                         
522                         if (list == null || list.Count == 0) {
523                                 clrVersion = new Version ();
524                                 clrVersions = null;
525                         } else {
526                                 list.Sort ();
527                                 clrVersions = (Version []) list.ToArray (typeof (Version));
528                         }
529                 }
530
531                 bool ReadBoolean (string key)
532                 {
533                         string v = this [key];
534                         if (v == null) {
535                                 throw CreateCapabilityNotFoundException (key);
536                         }
537
538                         return (String.Compare (v, "True", true, Helpers.InvariantCulture) == 0);
539                 }
540
541                 int ReadInt32 (string key)
542                 {
543                         string v = this [key];
544                         if (v == null) {
545                                 throw CreateCapabilityNotFoundException (key);
546                         }
547
548                         try {
549                                 return Int32.Parse (v);
550                         } catch {
551                                 throw CreateCapabilityNotFoundException (key);
552                         }
553                 }
554
555                 double ReadDouble (string key)
556                 {
557                         string v = this [key];
558                         if (v == null) {
559                                 throw CreateCapabilityNotFoundException (key);
560                         }
561
562                         try {
563                                 return Double.Parse (v);
564                         } catch {
565                                 throw CreateCapabilityNotFoundException (key);
566                         }
567                 }
568
569                 string ReadString (string key) 
570                 {
571                         string v = this [key];
572                         if (v == null) {
573                                 throw CreateCapabilityNotFoundException (key);
574                         }
575
576                         return v;
577                 }
578
579                 Version ReadVersion (string key) 
580                 {
581                         string v = this [key];
582                         if (v == null) {
583                                 throw CreateCapabilityNotFoundException (key);
584                         }
585
586                         try {
587                                 return new Version (v);
588                         }
589                         catch {
590                                 throw CreateCapabilityNotFoundException (key);
591                         }
592                 }
593
594                 ArrayList ReadArrayList (string key) 
595                 {
596                         ArrayList v = (ArrayList)this.capabilities [key];
597                         if (v == null) {
598                                 throw CreateCapabilityNotFoundException (key);
599                         }
600
601                         return v;
602                 }
603
604                 Exception CreateCapabilityNotFoundException (string key) {
605                         return new ArgumentNullException (String.Format ("browscaps.ini does not contain a definition for capability {0} for userAgent {1}", key, Browser));
606                 }
607
608                 bool Get (int idx)
609                 {
610                         return flags.Get (idx);
611                 }
612
613                 void Set (int idx)
614                 {
615                         flags.Set (idx, true);
616                 }
617         }
618 }