89e22ffb7c30248f4cdf1a5c3cf23154454137a4
[mono.git] / mcs / class / System.Web / System.Web / CapabilitiesLoader.cs
1 // 
2 // System.Web.CapabilitiesLoader
3 //
4 // Loads data from browscap.ini file provided by Gary J. Keith from
5 // http://www.GaryKeith.com/browsers. Please don't abuse the
6 // site when updating browscap.ini file. Use the update-browscap.exe tool.
7 //
8 // Authors:
9 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
10 //
11 // (c) 2003-2009 Novell, Inc. (http://www.novell.com)
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.Globalization;
39 using System.IO;
40 using System.Text.RegularExpressions;
41 using System.Web.Configuration;
42 using System.Web.Util;
43
44 namespace System.Web
45 {
46         sealed class BrowserData
47         {
48                 static char [] wildchars = new char [] {'*', '?'};
49
50                 object this_lock = new object ();
51                 BrowserData parent;
52                 string text;
53                 string pattern;
54 #if TARGET_JVM
55                 java.util.regex.Pattern regex;
56 #else
57                 Regex regex;
58 #endif
59                 ListDictionary data;
60
61                 public BrowserData (string pattern)
62                 {
63                         int norx = pattern.IndexOfAny (wildchars);
64                         if (norx == -1) {
65                                 text = pattern;
66                         } else {
67                                 this.pattern = pattern.Substring (norx);
68                                 text = pattern.Substring (0, norx);
69                                 if (text.Length == 0)
70                                         text = null;
71
72                                 this.pattern = this.pattern.Replace (".", "\\.");
73                                 this.pattern = this.pattern.Replace ("(", "\\(");
74                                 this.pattern = this.pattern.Replace (")", "\\)");
75                                 this.pattern = this.pattern.Replace ("[", "\\[");
76                                 this.pattern = this.pattern.Replace ("]", "\\]");
77                                 this.pattern = this.pattern.Replace ('?', '.');
78                                 this.pattern = this.pattern.Replace ("*", ".*");
79                         }
80                 }
81
82                 public BrowserData Parent {
83                         get { return parent; }
84                         set { parent = value; }
85                 }
86
87                 public void Add (string key, string value)
88                 {
89                         if (data == null)
90                                 data = new ListDictionary ();
91
92                         data.Add (key, value);
93                 }
94
95                 public Hashtable GetProperties (Hashtable tbl)
96                 {
97                         if (parent != null)
98                                 parent.GetProperties (tbl);
99
100                         if (data ["browser"] != null) { // Last one (most derived) will win.
101                                 tbl ["browser"] = data ["browser"];
102                         } else if (tbl ["browser"] == null) { // If none so far defined value set to *
103                                 tbl ["browser"] = "*";
104                         }
105
106                         if (!tbl.ContainsKey ("browsers")) {
107                                 tbl ["browsers"] = new ArrayList ();
108                         }
109
110                         ((ArrayList) tbl ["browsers"]).Add (tbl["browser"]);
111
112                         foreach (string key in data.Keys)
113                                 tbl [key.ToLower (Helpers.InvariantCulture).Trim ()] = data [key];
114                         
115                         return tbl;
116                 }
117                 
118                 public string GetParentName ()
119                 {
120                         return (string)(data.Contains("parent")? data ["parent"] : null);
121                 }
122                 
123                 public string GetAlternateBrowser ()
124                 {
125                         return (pattern == null) ? text : null;
126                 }
127
128                 public string GetBrowser ()
129                 {
130                         if (pattern == null)
131                                 return text;
132
133                         return (string) data ["browser"];
134                 }
135                 
136                 public bool IsMatch (string expression)
137                 {
138                         if (expression == null || expression.Length == 0)
139                                 return false;
140
141                         if (text != null) {
142                                 if (text [0] != expression [0] ||
143                                     String.Compare (text, 1, expression, 1,
144                                                     text.Length - 1, false,
145                                                     Helpers.InvariantCulture) != 0) {
146                                         return false;
147                                 }
148                                 expression = expression.Substring (text.Length);
149                         }
150                         
151                         if (pattern == null)
152                                 return expression.Length == 0;
153
154                         lock (this_lock) {
155                                 if (regex == null)
156 #if TARGET_JVM
157                                         regex = java.util.regex.Pattern.compile (pattern);
158 #else
159                                 regex = new Regex (pattern);
160 #endif
161                         }
162 #if TARGET_JVM
163                         return regex.matcher ((java.lang.CharSequence) (object) expression).matches ();
164 #else
165                         return regex.Match (expression).Success;
166 #endif
167                 }
168         }
169         
170         sealed class CapabilitiesLoader : MarshalByRefObject
171         {
172                 const int userAgentsCacheSize = 3000;
173                 static Hashtable defaultCaps;
174                 static readonly object lockobj = new object ();
175
176 #if TARGET_JVM
177                 static bool loaded {
178                         get {
179                                 return alldata != null;
180                         }
181                         set {
182                                 if (alldata == null)
183                                         alldata = new ArrayList ();
184                         }
185                 }
186
187                 const string alldataKey = "System.Web.CapabilitiesLoader.alldata";
188                 static ICollection alldata {
189                         get {
190                                 return (ICollection) AppDomain.CurrentDomain.GetData (alldataKey);
191                         }
192                         set {
193                                 AppDomain.CurrentDomain.SetData (alldataKey, value);
194                         }
195                 }
196
197                 const string userAgentsCacheKey = "System.Web.CapabilitiesLoader.userAgentsCache";
198                 static Hashtable userAgentsCache {
199                         get {
200                                 lock (typeof (CapabilitiesLoader)) {
201                                         Hashtable agentsCache = (Hashtable) AppDomain.CurrentDomain.GetData (userAgentsCacheKey);
202                                         if (agentsCache == null) {
203                                                 agentsCache = Hashtable.Synchronized (new Hashtable (userAgentsCacheSize + 10));
204                                                 AppDomain.CurrentDomain.SetData (userAgentsCacheKey, agentsCache);
205                                         }
206
207                                         return agentsCache;
208                                 }
209                         }
210                 }
211 #else
212                 static volatile bool loaded;
213                 static ICollection alldata;
214                 static Hashtable userAgentsCache = Hashtable.Synchronized(new Hashtable(userAgentsCacheSize+10));
215 #endif
216
217                 CapabilitiesLoader () {}
218
219                 static CapabilitiesLoader ()
220                 {
221                         defaultCaps = new Hashtable ();
222                         defaultCaps.Add ("activexcontrols", "False");
223                         defaultCaps.Add ("alpha", "False");
224                         defaultCaps.Add ("aol", "False");
225                         defaultCaps.Add ("aolversion", "0");
226                         defaultCaps.Add ("authenticodeupdate", "");
227                         defaultCaps.Add ("backgroundsounds", "False");
228                         defaultCaps.Add ("beta", "False");
229                         defaultCaps.Add ("browser", "*");
230                         defaultCaps.Add ("browsers", new ArrayList ());
231                         defaultCaps.Add ("cdf", "False");
232                         defaultCaps.Add ("clrversion", "0");
233                         defaultCaps.Add ("cookies", "False");
234                         defaultCaps.Add ("crawler", "False");
235                         defaultCaps.Add ("css", "0");
236                         defaultCaps.Add ("cssversion", "0");
237                         defaultCaps.Add ("ecmascriptversion", "0.0");
238                         defaultCaps.Add ("frames", "False");
239                         defaultCaps.Add ("iframes", "False");
240                         defaultCaps.Add ("isbanned", "False");
241                         defaultCaps.Add ("ismobiledevice", "False");
242                         defaultCaps.Add ("issyndicationreader", "False");
243                         defaultCaps.Add ("javaapplets", "False");
244                         defaultCaps.Add ("javascript", "False");
245                         defaultCaps.Add ("majorver", "0");
246                         defaultCaps.Add ("minorver", "0");
247                         defaultCaps.Add ("msdomversion", "0.0");
248                         defaultCaps.Add ("netclr", "False");
249                         defaultCaps.Add ("platform", "unknown");
250                         defaultCaps.Add ("stripper", "False");
251                         defaultCaps.Add ("supportscss", "False");
252                         defaultCaps.Add ("tables", "False");
253                         defaultCaps.Add ("vbscript", "False");
254                         defaultCaps.Add ("version", "0");
255                         defaultCaps.Add ("w3cdomversion", "0.0");
256                         defaultCaps.Add ("wap", "False");
257                         defaultCaps.Add ("win16", "False");
258                         defaultCaps.Add ("win32", "False");
259                         defaultCaps.Add ("win64", "False");
260                         defaultCaps.Add ("adapters", new Hashtable ());
261                         defaultCaps.Add ("cancombineformsindeck", "False");
262                         defaultCaps.Add ("caninitiatevoicecall", "False");
263                         defaultCaps.Add ("canrenderafterinputorselectelement", "False");
264                         defaultCaps.Add ("canrenderemptyselects", "False");
265                         defaultCaps.Add ("canrenderinputandselectelementstogether", "False");
266                         defaultCaps.Add ("canrendermixedselects", "False");
267                         defaultCaps.Add ("canrenderoneventandprevelementstogether", "False");
268                         defaultCaps.Add ("canrenderpostbackcards", "False");
269                         defaultCaps.Add ("canrendersetvarzerowithmultiselectionlist", "False");
270                         defaultCaps.Add ("cansendmail", "False");
271                         defaultCaps.Add ("defaultsubmitbuttonlimit", "0");
272                         defaultCaps.Add ("gatewayminorversion", "0");
273                         defaultCaps.Add ("gatewaymajorversion", "0");
274                         defaultCaps.Add ("gatewayversion", "None");
275                         defaultCaps.Add ("hasbackbutton", "True");
276                         defaultCaps.Add ("hidesrightalignedmultiselectscrollbars", "False");
277                         defaultCaps.Add ("inputtype", "telephoneKeypad");
278                         defaultCaps.Add ("iscolor", "False");
279                         defaultCaps.Add ("jscriptversion", "0.0");
280                         defaultCaps.Add ("maximumhreflength", "0");
281                         defaultCaps.Add ("maximumrenderedpagesize", "2000");
282                         defaultCaps.Add ("maximumsoftkeylabellength", "5");
283                         defaultCaps.Add ("minorversionstring", "0.0");
284                         defaultCaps.Add ("mobiledevicemanufacturer", "Unknown");
285                         defaultCaps.Add ("mobiledevicemodel", "Unknown");
286                         defaultCaps.Add ("numberofsoftkeys", "0");
287                         defaultCaps.Add ("preferredimagemime", "image/gif");
288                         defaultCaps.Add ("preferredrenderingmime", "text/html");
289                         defaultCaps.Add ("preferredrenderingtype", "html32");
290                         defaultCaps.Add ("preferredrequestencoding", "");
291                         defaultCaps.Add ("preferredresponseencoding", "");
292                         defaultCaps.Add ("rendersbreakbeforewmlselectandinput", "False");
293                         defaultCaps.Add ("rendersbreaksafterhtmllists", "True");
294                         defaultCaps.Add ("rendersbreaksafterwmlanchor", "False");
295                         defaultCaps.Add ("rendersbreaksafterwmlinput", "False");
296                         defaultCaps.Add ("renderswmldoacceptsinline", "True");
297                         defaultCaps.Add ("renderswmlselectsasmenucards", "False");
298                         defaultCaps.Add ("requiredmetatagnamevalue", "");
299                         defaultCaps.Add ("requiresattributecolonsubstitution", "False");
300                         defaultCaps.Add ("requirescontenttypemetatag", "False");
301                         defaultCaps.Add ("requirescontrolstateinsession", "False");
302                         defaultCaps.Add ("requiresdbcscharacter", "False");
303                         defaultCaps.Add ("requireshtmladaptiveerrorreporting", "False");
304                         defaultCaps.Add ("requiresleadingpagebreak", "False");
305                         defaultCaps.Add ("requiresnobreakinformatting", "False");
306                         defaultCaps.Add ("requiresoutputoptimization", "False");
307                         defaultCaps.Add ("requiresphonenumbersasplaintext", "False");
308                         defaultCaps.Add ("requiresspecialviewstateencoding", "False");
309                         defaultCaps.Add ("requiresuniquefilepathsuffix", "False");
310                         defaultCaps.Add ("requiresuniquehtmlcheckboxnames", "False");
311                         defaultCaps.Add ("requiresuniquehtmlinputnames", "False");
312                         defaultCaps.Add ("requiresurlencodedpostfieldvalues", "False");
313                         defaultCaps.Add ("screenbitdepth", "1");
314                         defaultCaps.Add ("screencharactersheight", "6");
315                         defaultCaps.Add ("screencharacterswidth", "12");
316                         defaultCaps.Add ("screenpixelsheight", "72");
317                         defaultCaps.Add ("screenpixelswidth", "96");
318                         defaultCaps.Add ("supportsaccesskeyattribute", "False");
319                         defaultCaps.Add ("supportsbodycolor", "True");
320                         defaultCaps.Add ("supportsbold", "False");
321                         defaultCaps.Add ("supportscachecontrolmetatag", "True");
322                         defaultCaps.Add ("supportscallback", "False");
323                         defaultCaps.Add ("supportsdivalign", "True");
324                         defaultCaps.Add ("supportsdivnowrap", "False");
325                         defaultCaps.Add ("supportsemptystringincookievalue", "False");
326                         defaultCaps.Add ("supportsfontcolor", "True");
327                         defaultCaps.Add ("supportsfontname", "False");
328                         defaultCaps.Add ("supportsfontsize", "False");
329                         defaultCaps.Add ("supportsimagesubmit", "False");
330                         defaultCaps.Add ("supportsimodesymbols", "False");
331                         defaultCaps.Add ("supportsinputistyle", "False");
332                         defaultCaps.Add ("supportsinputmode", "False");
333                         defaultCaps.Add ("supportsitalic", "False");
334                         defaultCaps.Add ("supportsjphonemultimediaattributes", "False");
335                         defaultCaps.Add ("supportsjphonesymbols", "False");
336                         defaultCaps.Add ("supportsquerystringinformaction", "True");
337                         defaultCaps.Add ("supportsredirectwithcookie", "True");
338                         defaultCaps.Add ("supportsselectmultiple", "True");
339                         defaultCaps.Add ("supportsuncheck", "True");
340                         defaultCaps.Add ("supportsxmlhttp", "False");
341                         defaultCaps.Add ("type", "Unknown");
342                 }
343                 
344                 public static Hashtable GetCapabilities (string userAgent)
345                 {
346                         Init ();
347                         if (userAgent != null)
348                                 userAgent = userAgent.Trim ();
349
350                         if (alldata == null || userAgent == null || userAgent.Length == 0)
351                                 return defaultCaps;
352
353                         Hashtable userBrowserCaps = (Hashtable) (userAgentsCache.Contains(userAgent)? userAgentsCache [userAgent] : null);
354                         if (userBrowserCaps == null) {
355                                 foreach (BrowserData bd in alldata) {
356                                         if (bd.IsMatch (userAgent)) {
357                                                 Hashtable tbl;
358                                                 tbl = new Hashtable (defaultCaps);
359                                                 userBrowserCaps = bd.GetProperties (tbl);
360                                                 break;
361                                         }
362                                 }
363
364                                 if (userBrowserCaps == null)
365                                         userBrowserCaps = defaultCaps;
366
367                                 lock (lockobj) {
368                                         if (userAgentsCache.Count >= userAgentsCacheSize)
369                                                 userAgentsCache.Clear ();
370                                 }
371                                 userAgentsCache [userAgent] = userBrowserCaps;
372                         }
373                         return userBrowserCaps;
374                 }
375
376                 static void Init ()
377                 {
378                         if (loaded)
379                                 return;
380
381                         lock (lockobj) {
382                                 if (loaded)
383                                         return;
384 #if TARGET_J2EE
385                                 string filepath = "browscap.ini";
386 #else
387                                 string dir = HttpRuntime.MachineConfigurationDirectory;
388                                 string filepath = Path.Combine (dir, "browscap.ini");
389                                 if (!File.Exists (filepath)) {
390                                         // try removing the trailing version directory
391                                         dir = Path.GetDirectoryName (dir);
392                                         filepath = Path.Combine (dir, "browscap.ini");
393                                 }
394 #endif
395                                 try {
396                                         LoadFile (filepath);
397                                 } catch (Exception) {}
398
399                                 loaded = true;
400                         }
401                 }
402
403 #if TARGET_J2EE
404                 static TextReader GetJavaTextReader(string filename)
405                 {
406                         try
407                         {
408                                 java.lang.ClassLoader cl = (java.lang.ClassLoader)
409                                         AppDomain.CurrentDomain.GetData("GH_ContextClassLoader");
410                                 if (cl == null)
411                                         return null;
412
413                                 string custom = String.Concat("browscap/", filename);
414                                 
415                                 java.io.InputStream inputStream = cl.getResourceAsStream(custom);
416                                 if (inputStream == null)
417                                         inputStream = cl.getResourceAsStream(filename);
418
419                                 if (inputStream == null)
420                                         return null;
421
422                                 return new StreamReader (new System.Web.J2EE.J2EEUtils.InputStreamWrapper (inputStream));
423                         }
424                         catch (Exception e)
425                         {
426                                 return null;
427                         }
428                 }
429 #endif
430
431                 static void LoadFile (string filename)
432                 {
433 #if TARGET_J2EE
434                         TextReader input = GetJavaTextReader(filename);
435                         if(input == null)
436                                 return;
437 #else
438                         if (!File.Exists (filename))
439                                 return;
440
441                         TextReader input = new StreamReader (File.OpenRead (filename));
442 #endif
443                         using (input) {
444                         string str;
445                         Hashtable allhash = new Hashtable ();
446                         int aux = 0;
447                         ArrayList browserData = new ArrayList ();
448                         while ((str = input.ReadLine ()) != null) {
449                                 if (str.Length == 0 || str [0] == ';')
450                                         continue;
451
452                                 string userAgent = str.Substring (1, str.Length - 2);
453                                 BrowserData data = new BrowserData (userAgent);
454                                 ReadCapabilities (input, data);
455
456                                 /* Ignore default browser and file version information */
457                                 if (userAgent == "*" || userAgent == "GJK_Browscap_Version")
458                                         continue;
459
460                                 string key = data.GetBrowser ();
461                                 if (key == null || allhash.ContainsKey (key)) {
462                                         allhash.Add (aux++, data);
463                                         browserData.Add (data);
464                                 } else {
465                                         allhash.Add (key, data);
466                                         browserData.Add (data);
467                                 }
468                         }                       
469
470                         alldata = browserData;
471                         foreach (BrowserData data in alldata) {
472                                 string pname = data.GetParentName ();
473                                 if (pname == null)
474                                         continue;
475
476                                 data.Parent = (BrowserData) allhash [pname];
477                         }
478                         }
479                 }
480
481                 static char [] eq = new char []{'='};
482                 static void ReadCapabilities (TextReader input, BrowserData data)
483                 {
484                         string str, key;
485                         string [] keyvalue;
486                         
487                         while ((str = input.ReadLine ()) != null && str.Length != 0) {
488                                 keyvalue = str.Split (eq, 2);
489                                 key = keyvalue [0].ToLower (Helpers.InvariantCulture).Trim ();
490                                 if (key.Length == 0)
491                                         continue;
492                                 data.Add (key, keyvalue [1]);
493                         }
494                 }
495         }
496 }