Initial compilation in TARGET_J2EE config
[mono.git] / mcs / class / System.Web / System.Web.Configuration / HandlerItem.cs
1 // 
2 // System.Web.Configuration.HandlerItem
3 //
4 // Authors:
5 //      Patrik Torstensson (ptorsten@hotmail.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 //   (c) 2002 Ximian, Inc. (http://www.ximian.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 using System;
32 using System.Collections;
33 using System.Text;
34 using System.Text.RegularExpressions;
35 using System.Threading;
36
37 namespace System.Web.Configuration
38 {
39         class HandlerItem
40         {
41                 private Type _type;
42                 private string _typeName;
43                 private string _path;
44                 private string _requestType;
45                 private Regex requestRegex;
46                 private Regex pathRegex;
47                 static Hashtable regexCache;
48                 object instance;
49                 bool validated;
50
51                 public HandlerItem (string requestType, string path, string type, bool validate)
52                 {
53                         _typeName = type;
54                         _path = path;
55                         _requestType = requestType.Replace (" ", "");
56                         requestRegex = GetRegex (_requestType, true);
57                         pathRegex = GetRegex (_path, false);
58                         if (validate)
59                                 DoValidation ();
60                 }
61
62                 public object GetInstance ()
63                 {
64                         object obj = Interlocked.CompareExchange (ref instance, null, null);
65                         if (obj != null)
66                                 return obj;
67
68                         DoValidation ();
69                         obj = Activator.CreateInstance (_type, true);
70                         IHttpHandler hnd = obj as IHttpHandler;
71                         if (hnd != null && hnd.IsReusable)
72                                 Interlocked.CompareExchange (ref instance, hnd, null);
73
74                         return obj;
75                 }
76
77                 public Type Type
78                 {
79                         get {
80                                 DoValidation ();
81                                 return _type;
82                         }
83                 }
84
85                 public bool IsMatch (string type, string path)
86                 {
87                         return (MatchVerb (type) && MatchPath (path));
88                 }
89
90                 bool MatchVerb (string verb)
91                 {
92                         return requestRegex.IsMatch (verb);
93                 }
94
95                 bool MatchPath (string path)
96                 {
97                         if (pathRegex.IsMatch (path))
98                                 return true;
99
100                         int slash = path.LastIndexOf ('/');
101                         if (slash != -1 && path.Length > slash + 1)
102                                 return pathRegex.IsMatch (path.Substring (slash + 1));
103
104                         return false;
105                 }
106
107                 void DoValidation ()
108                 {
109                         if (validated)
110                                 return;
111
112                         lock (this) {
113                                 Type t = Type.GetType (_typeName, true);
114                                 _type = t;
115                                 validated = true;
116                         }
117
118                         if (typeof (IHttpHandler).IsAssignableFrom (_type))
119                                 return;
120                         
121                         if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
122                                 return;
123
124                         throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
125                 }
126
127                 static string ToRegexPattern (string dosPattern)
128                 {
129                         string result = dosPattern.Replace (".", "\\.");
130                         result = result.Replace ("*", ".*");
131                         result = result.Replace ('?', '.');
132                         return result;
133                 }
134                         
135                 static Regex GetRegex (string verb, bool match_all)
136                 {
137                         EnsureCache ();
138                         if (regexCache.ContainsKey (verb))
139                                 return (Regex) regexCache [verb];
140
141                         StringBuilder result = new StringBuilder ((match_all) ? "\\A" : "");
142                         string [] expressions = verb.Split (',');
143                         int end = expressions.Length;
144                         for (int i = 0; i < end; i++) {
145                                 string regex = ToRegexPattern (expressions [i]);
146                                 if (i + 1 < end) {
147                                         result.AppendFormat ("{0}\\z|\\A", regex);
148                                 } else {
149                                         result.AppendFormat ("({0})\\z", regex);
150                                 }
151                         }
152
153                         Regex r = new Regex (result.ToString ());
154                         regexCache [verb] = r;
155                         return r;
156                 }
157
158                 static void EnsureCache ()
159                 {
160                         if (regexCache == null)
161                                 regexCache = new Hashtable ();
162                 }
163         }
164 }
165