New test.
[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 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
43 namespace System.Web
44 {
45         class BrowserData
46         {
47                 static char [] wildchars = new char [] {'*', '?'};
48                 BrowserData parent;
49                 string text;
50                 string pattern;
51                 Regex regex;
52                 ListDictionary data;
53
54                 public BrowserData (string pattern)
55                 {
56                         int norx = pattern.IndexOfAny (wildchars);
57                         if (norx == -1) {
58                                 text = pattern;
59                         } else {
60                                 this.pattern = pattern.Substring (norx);
61                                 text = pattern.Substring (0, norx);
62                                 if (text.Length == 0)
63                                         text = null;
64
65                                 this.pattern = this.pattern.Replace (".", "\\.");
66                                 this.pattern = this.pattern.Replace ("(", "\\(");
67                                 this.pattern = this.pattern.Replace (")", "\\)");
68                                 this.pattern = this.pattern.Replace ("[", "\\[");
69                                 this.pattern = this.pattern.Replace ("]", "\\]");
70                                 this.pattern = this.pattern.Replace ("?", ".");
71                                 this.pattern = this.pattern.Replace ("*", ".*");
72                         }
73                 }
74
75                 public BrowserData Parent {
76                         get { return parent; }
77                         set { parent = value; }
78                 }
79
80                 public void Add (string key, string value)
81                 {
82                         if (data == null)
83                                 data = new ListDictionary ();
84
85                         data.Add (key, value);
86                 }
87
88                 public Hashtable GetProperties (Hashtable tbl)
89                 {
90                         if (parent != null)
91                                 parent.GetProperties (tbl);
92
93                         foreach (string key in data.Keys)
94                                 tbl [key] = data [key];
95
96                         return tbl;
97                 }
98                 
99                 public string GetParentName ()
100                 {
101                         return (string) data ["parent"];
102                 }
103                 
104                 public string GetAlternateBrowser ()
105                 {
106                         return (pattern == null) ? text : null;
107                 }
108
109                 public string GetBrowser ()
110                 {
111                         if (pattern == null)
112                                 return text;
113
114                         return (string) data ["browser"];
115                 }
116                 
117                 public bool IsMatch (string expression)
118                 {
119                         if (expression == null || expression.Length == 0)
120                                 return false;
121
122                         if (text != null) {
123                                 if (text [0] != expression [0] ||
124                                     String.Compare (text, 1, expression, 1,
125                                                     text.Length - 1, false,
126                                                     CultureInfo.InvariantCulture) != 0) {
127                                         return false;
128                                 }
129                                 expression = expression.Substring (text.Length);
130                         }
131                         
132                         if (pattern == null)
133                                 return expression.Length == 0;
134
135                         lock (this) {
136                                 if (regex == null)
137                                         regex = new Regex (pattern);
138                         }
139
140                         return regex.Match (expression).Success;
141                 }
142         }
143         
144         class CapabilitiesLoader : MarshalByRefObject
145         {
146                 static volatile bool loaded;
147                 static ICollection alldata;
148                 static Hashtable defaultCaps;
149                 static readonly object lockobj = new object ();
150                 private CapabilitiesLoader () {}
151
152                 static CapabilitiesLoader ()
153                 {
154                         defaultCaps = new Hashtable ();
155                         defaultCaps.Add ("frames", "True");
156                         defaultCaps.Add ("tables", "True");
157                 }
158                         
159                 public static Hashtable GetCapabilities (string userAgent)
160                 {
161                         Init ();
162                         if (userAgent != null)
163                                 userAgent = userAgent.Trim ();
164
165                         if (alldata == null || userAgent == null || userAgent == "")
166                                 return defaultCaps;
167
168                         foreach (BrowserData bd in alldata) {
169                                 if (bd.IsMatch (userAgent)) {
170                                         Hashtable tbl;
171 #if NET_2_0
172                                         tbl = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
173 #else
174                                         tbl = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
175                                                                 CaseInsensitiveComparer.DefaultInvariant);
176 #endif
177
178                                         return bd.GetProperties (tbl);
179                                 }
180                         }
181                         
182                         return defaultCaps;
183                 }
184
185                 static void Init ()
186                 {
187                         if (loaded)
188                                 return;
189
190                         lock (lockobj) {
191                                 if (loaded)
192                                         return;
193 #if TARGET_J2EE
194                                 string filepath = "browscap.ini";
195 #else
196 #if NET_2_0
197                                 string dir = Path.GetDirectoryName (WebConfigurationManager.OpenMachineConfiguration().FilePath);
198 #else
199                                 string dir = Path.GetDirectoryName (WebConfigurationSettings.MachineConfigPath);
200 #endif
201                                 string filepath = Path.Combine (dir, "browscap.ini");
202                                 if (!File.Exists (filepath)) {
203                                         // try removing the trailing version directory
204                                         dir = Path.GetDirectoryName (dir);
205                                         filepath = Path.Combine (dir, "browscap.ini");
206                                 }
207 #endif
208                                 try {
209                                         LoadFile (filepath);
210                                 } catch (Exception) { }
211
212                                 loaded = true;
213                         }
214                 }
215
216 #if TARGET_J2EE
217                 private static TextReader GetJavaTextReader(string filename)
218                 {
219                         Stream s;
220                         try
221                         {
222                                 java.lang.ClassLoader cl = (java.lang.ClassLoader)
223                                         AppDomain.CurrentDomain.GetData("GH_ContextClassLoader");
224                                 if (cl == null)
225                                         return null;
226
227                                 string custom = String.Concat("browscap/", filename);
228                                 
229                                 java.io.InputStream inputStream = cl.getResourceAsStream(custom);
230                                 if (inputStream == null)
231                                         inputStream = cl.getResourceAsStream(filename);
232
233                                 s = (Stream)vmw.common.IOUtils.getStream(inputStream);
234                         }
235                         catch (Exception e)
236                         {
237                                 return null;
238                         }
239                         return new StreamReader (s);
240                 }
241 #endif
242
243                 static void LoadFile (string filename)
244                 {
245 #if TARGET_J2EE
246                         TextReader input = GetJavaTextReader(filename);
247                         if(input == null)
248                                 return;
249 #else
250                         if (!File.Exists (filename))
251                                 return;
252
253                         TextReader input = new StreamReader (File.OpenRead (filename));
254 #endif
255                         string str;
256                         Hashtable allhash = new Hashtable ();
257                         int aux = 0;
258                         while ((str = input.ReadLine ()) != null) {
259                                 if (str.Length == 0 || str [0] == ';')
260                                         continue;
261
262                                 string userAgent = str.Substring (1, str.Length - 2);
263                                 BrowserData data = new BrowserData (userAgent);
264                                 ReadCapabilities (input, data);
265
266                                 /* Ignore default browser and file version information */
267                                 if (userAgent == "*" || userAgent == "GJK_Browscap_Version")
268                                         continue;
269
270                                 string key = data.GetBrowser ();
271                                 if (key == null || allhash.ContainsKey (key)) {
272                                         allhash.Add (aux++, data);
273                                 } else {
274                                         allhash.Add (key, data);
275                                 }
276                         }
277
278                         alldata = allhash.Values;
279                         foreach (BrowserData data in alldata) {
280                                 if (data.Parent != null)
281                                         continue;
282
283                                 string pname = data.GetParentName ();
284                                 if (pname != null)
285                                         data.Parent = (BrowserData) allhash [pname];
286                         }
287                 }
288
289                 static char [] eq = new char []{'='};
290                 static void ReadCapabilities (TextReader input, BrowserData data)
291                 {
292                         string str;
293                         while ((str = input.ReadLine ()) != null && str.Length != 0) {
294                                 string [] keyvalue = str.Split (eq, 2);
295                                 data.Add (keyvalue [0], keyvalue [1]);
296                         }
297                 }
298         }
299 }
300