merged Sys.Web.Services 2.0 support in my branch:
[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 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
38 #if NET_2_0
39 namespace System.Web.Configuration {
40         public partial class HttpCapabilitiesBase
41 #else
42
43 namespace System.Web {
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         public class HttpBrowserCapabilities : HttpCapabilitiesBase
47 #endif
48         {
49                 const int HaveActiveXControls = 1;
50                 const int HaveAOL = 2;
51                 const int HaveBackGroundSounds = 3;
52                 const int HaveBeta = 4;
53                 const int HaveBrowser = 5;
54                 const int HaveCDF = 6;
55                 //const int HaveClrVersion = 7;
56                 const int HaveCookies = 8;
57                 const int HaveCrawler = 9;
58                 const int HaveEcmaScriptVersion = 10;
59                 const int HaveFrames = 11;
60                 const int HaveJavaApplets = 12;
61                 const int HaveJavaScript = 13;
62                 const int HaveMajorVersion = 14;
63                 const int HaveMinorVersion = 15;
64                 const int HaveMSDomVersion = 16;
65                 const int HavePlatform = 17;
66                 const int HaveTables = 18;
67                 //const int HaveTagWriter = 19;
68                 const int HaveVBScript = 20;
69                 const int HaveVersion = 21;
70                 const int HaveW3CDomVersion = 22;
71                 const int HaveWin16 = 23;
72                 const int HaveWin32 = 24;
73
74                 int flags;
75                 bool activeXControls;
76                 bool aol;
77                 bool backgroundSounds;
78                 bool beta;
79                 string browser;
80                 bool cdf;
81                 Version clrVersion;
82                 bool cookies;
83                 bool crawler;
84                 Version ecmaScriptVersion;
85                 bool frames;
86                 bool javaApplets;
87                 bool javaScript;
88                 int majorVersion;
89                 double minorVersion;
90                 Version msDomVersion;
91                 string platform;
92                 bool tables;
93                 //Type tagWriter;
94                 bool vbscript;
95                 string version;
96                 Version w3CDomVersion;
97                 bool win16;
98                 bool win32;
99                 Version [] clrVersions;
100                 internal string useragent;
101
102 #if !NET_2_0
103                 public HttpBrowserCapabilities ()
104                 {
105                 }
106 #endif
107
108                 public bool ActiveXControls {
109                         get {
110                                 if (!Get (HaveActiveXControls)) {
111                                         Set (HaveActiveXControls);
112                                         activeXControls = ReadBoolean ("activexcontrols", false);
113                                 }
114
115                                 return activeXControls;
116                         }
117                 }
118
119                 public bool AOL {
120                         get {
121                                 if (!Get (HaveAOL)) {
122                                         Set (HaveAOL);
123                                         aol = ReadBoolean ("aol", false);
124                                 }
125
126                                 return aol;
127                         }
128                 }
129
130                 public bool BackgroundSounds {
131                         get {
132                                 if (!Get (HaveBackGroundSounds)) {
133                                         Set (HaveBackGroundSounds);
134                                         backgroundSounds = ReadBoolean ("backgroundsounds", false);
135                                 }
136
137                                 return backgroundSounds;
138                         }
139                 }
140
141                 public bool Beta {
142                         get {
143                                 if (!Get (HaveBeta)) {
144                                         Set (HaveBeta);
145                                         beta = ReadBoolean ("beta", false);
146                                 }
147
148                                 return beta;
149                         }
150                 }
151
152                 public string Browser {
153                         get {
154                                 if (!Get (HaveBrowser)) {
155                                         Set (HaveBrowser);
156                                         browser = this ["browser"];
157                                         if (browser == null)
158                                                 browser = "Unknown";
159                                 }
160
161                                 return browser;
162                         }
163                 }
164 #if NET_2_0
165                 [MonoTODO ("Not implemented")]
166                 public ArrayList Browsers {
167                         get { throw new NotImplementedException (); }
168                 }
169 #endif
170                 public bool CDF {
171                         get {
172                                 if (!Get (HaveCDF)) {
173                                         Set (HaveCDF);
174                                         cdf = ReadBoolean ("cdf", false);
175                                 }
176
177                                 return cdf;
178                         }
179                 }
180
181                 public Version ClrVersion {
182                         get {
183                                 if (clrVersion == null)
184                                         InternalGetClrVersions ();
185
186                                 return clrVersion;
187                         }
188                 }
189
190                 public bool Cookies {
191                         get {
192                                 if (!Get (HaveCookies)) {
193                                         Set (HaveCookies);
194                                         cookies = ReadBoolean ("cookies", false);
195                                 }
196
197                                 return cookies;
198                         }
199                 }
200
201                 public bool Crawler {
202                         get {
203                                 if (!Get (HaveCrawler)) {
204                                         Set (HaveCrawler);
205                                         crawler = ReadBoolean ("crawler", false);
206                                 }
207
208                                 return crawler;
209                         }
210                 }
211
212                 public Version EcmaScriptVersion {
213                         get {
214                                 if (!Get (HaveEcmaScriptVersion)) {
215                                         string ver_str;
216                                         Set (HaveEcmaScriptVersion);
217                                         ver_str = this ["ecmascriptversion"];
218                                         if (ver_str == null)
219                                                 ecmaScriptVersion = new Version (0, 0);
220                                         else
221                                                 ecmaScriptVersion = new Version (ver_str);
222                                 }
223
224                                 return ecmaScriptVersion;
225                         }
226                 }
227
228                 public bool Frames {
229                         get {
230                                 if (!Get (HaveFrames)) {
231                                         Set (HaveFrames);
232                                         frames = ReadBoolean ("frames", false);
233                                 }
234
235                                 return frames;
236                         }
237                 }
238
239                 public bool JavaApplets {
240                         get {
241                                 if (!Get (HaveJavaApplets)) {
242                                         Set (HaveJavaApplets);
243                                         javaApplets = ReadBoolean ("javaapplets", false);
244                                 }
245
246                                 return javaApplets;
247                         }
248                 }
249
250                 public bool JavaScript {
251                         get {
252                                 if (!Get (HaveJavaScript)) {
253                                         Set (HaveJavaScript);
254                                         javaScript = ReadBoolean ("javascript", false);
255                                 }
256
257                                 return javaScript;
258                         }
259                 }
260
261                 public int MajorVersion {
262                         get {
263                                 if (!Get (HaveMajorVersion)) {
264                                         Set (HaveMajorVersion);
265                                         majorVersion = ReadInt32 ("majorver", 0);
266                                 }
267
268                                 return majorVersion;
269                         }
270                 }
271
272                 public double MinorVersion {
273                         get {
274                                 if (!Get (HaveMinorVersion)) {
275                                         Set (HaveMinorVersion);
276                                         minorVersion = ReadDouble ("minorver", 0);
277                                 }
278
279                                 return minorVersion;
280                         }
281                 }
282
283                 public Version MSDomVersion {
284                         get {
285                                 if (!Get (HaveMSDomVersion)) {
286                                         string ver_str;
287                                         Set (HaveMSDomVersion);
288                                         ver_str = this ["msdomversion"];
289                                         if (ver_str == null)
290                                                 msDomVersion = new Version (0, 0);
291                                         else
292                                                 msDomVersion = new Version (ver_str);
293                                 }
294
295                                 return msDomVersion;
296                         }
297                 }
298
299                 public string Platform {
300                         get {
301                                 if (!Get (HavePlatform)) {
302                                         Set (HavePlatform);
303                                         platform = this ["platform"];
304                                         if (platform == null)
305                                                 platform = "";
306                                 }
307
308                                 return platform;
309                         }
310                 }
311
312                 public bool Tables {
313                         get {
314                                 if (!Get (HaveTables)) {
315                                         Set (HaveTables);
316                                         tables = ReadBoolean ("tables", false);
317                                 }
318
319                                 return tables;
320                         }
321                 }
322
323                 public Type TagWriter {
324                         get {
325                                 return typeof (HtmlTextWriter);
326                         }
327                 }
328
329                 public string Type {
330                         get {
331                                 return Browser + MajorVersion;
332                         }
333                 }
334
335                 public bool VBScript {
336                         get {
337                                 if (!Get (HaveVBScript)) {
338                                         Set (HaveVBScript);
339                                         vbscript = ReadBoolean ("vbscript", false);
340                                 }
341
342                                 return vbscript;
343                         }
344                 }
345
346                 public string Version {
347                         get {
348                                 if (!Get (HaveVersion)) {
349                                         Set (HaveVersion);
350                                         version = this ["version"];
351                                         if (version == null)
352                                                 version = "";
353                                 }
354
355                                 return version;
356                         }
357                 }
358
359                 public Version W3CDomVersion {
360                         get {
361                                 if (!Get (HaveW3CDomVersion)) {
362                                         string ver_str;
363                                         Set (HaveW3CDomVersion);
364                                         ver_str = this ["w3cdomversion"];
365                                         if (ver_str == null)
366                                                 w3CDomVersion = new Version (0, 0);
367                                         else
368                                                 w3CDomVersion = new Version (ver_str);
369                                 }
370
371                                 return w3CDomVersion;
372                         }
373                 }
374
375                 public bool Win16 {
376                         get {
377                                 if (!Get (HaveWin16)) {
378                                         Set (HaveWin16);
379                                         win16 = ReadBoolean ("win16", false);
380                                 }
381
382                                 return win16;
383                         }
384                 }
385
386                 public bool Win32 {
387                         get {
388                                 // This is the list of different windows platforms that browscap.ini has.
389                                 // Win16 Win2000 Win2003 Win32 Win95 Win98 WinME WinNT WinVI WinXP
390                                 if (!Get (HaveWin32)) {
391                                         Set (HaveWin32);
392                                         string platform = Platform;
393                                         win32 = (platform != "Win16" && platform.StartsWith ("Win"));
394                                 }
395                                 return win32;
396                         }
397                 }
398
399 #if NET_1_1
400                 public Version [] GetClrVersions ()
401                 {
402                         if ((clrVersions == null) && (clrVersion == null))
403                                 InternalGetClrVersions ();
404
405                         return clrVersions;
406                 }
407 #endif
408
409                 void InternalGetClrVersions ()
410                 {
411                         char [] anychars = new char [] { ';', ')' };
412                         string s = useragent;
413                         ArrayList list = null;
414                         int idx;
415                         while ((s != null) && (idx = s.IndexOf (".NET CLR ")) != -1) {
416                                 int end = s.IndexOfAny (anychars, idx + 9);
417                                 if (end == -1)
418                                         break;
419
420                                 string ver = s.Substring (idx + 9, end - idx - 9);
421                                 Version v = null;
422                                 try {
423                                         v = new Version (ver);
424                                         if (clrVersion == null || v > clrVersion)
425                                                 clrVersion = v;
426
427                                         if (list == null)
428                                                 list = new ArrayList (4);
429
430                                         list.Add (v);
431                                 } catch { }
432                                 s = s.Substring (idx + 9);
433                         }
434                         
435                         if (list == null || list.Count == 0) {
436                                 clrVersion = new Version ();
437 #if NET_2_0
438                                 clrVersions = null;
439 #else
440                                 clrVersions = new Version [1] { clrVersion };
441 #endif
442                         } else {
443                                 list.Sort ();
444                                 clrVersions = (Version []) list.ToArray (typeof (Version));
445                         }
446                 }
447
448                 bool ReadBoolean (string key, bool dflt)
449                 {
450                         string v = this [key];
451                         if (v == null)
452                                 return dflt;
453
454                         return (String.Compare (v, "True", true, CultureInfo.InvariantCulture) == 0);
455                 }
456
457                 int ReadInt32 (string key, int dflt)
458                 {
459                         string v = this [key];
460                         if (v == null)
461                                 return dflt;
462
463                         try {
464                                 return Int32.Parse (v);
465                         } catch {
466                                 return dflt;
467                         }
468                 }
469
470                 double ReadDouble (string key, double dflt)
471                 {
472                         string v = this [key];
473                         if (v == null)
474                                 return dflt;
475
476                         try {
477                                 return Double.Parse (v);
478                         } catch {
479                                 return dflt;
480                         }
481                 }
482
483                 bool Get (int idx)
484                 {
485                         return (flags & (1 << idx)) != 0;
486                 }
487
488                 void Set (int idx)
489                 {
490                         flags |= (1 << idx);
491                 }
492         }
493 }