2 // System.Web.Configuration.HandlerItem
5 // Patrik Torstensson (ptorsten@hotmail.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
34 using System.Text.RegularExpressions;
35 using System.Threading;
37 namespace System.Web.Configuration
42 private string _typeName;
44 private string _requestType;
45 private Regex requestRegex;
46 private Regex pathRegex;
47 static Hashtable regexCache;
51 public HandlerItem (string requestType, string path, string type, bool validate)
55 _requestType = requestType.Replace (" ", "");
56 requestRegex = GetRegex (_requestType, true);
57 pathRegex = GetRegex (_path, false);
62 public object GetInstance ()
64 object obj = Interlocked.CompareExchange (ref instance, null, null);
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);
85 public bool IsMatch (string type, string path)
87 return (MatchVerb (type) && MatchPath (path));
90 bool MatchVerb (string verb)
92 return requestRegex.IsMatch (verb);
95 bool MatchPath (string path)
97 if (pathRegex.IsMatch (path))
100 int slash = path.LastIndexOf ('/');
101 if (slash != -1 && path.Length > slash + 1)
102 return pathRegex.IsMatch (path.Substring (slash + 1));
113 Type t = Type.GetType (_typeName, true);
118 if (typeof (IHttpHandler).IsAssignableFrom (_type))
121 if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
124 throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
127 static string ToRegexPattern (string dosPattern)
129 string result = dosPattern.Replace (".", "\\.");
130 result = result.Replace ("*", ".*");
131 result = result.Replace ('?', '.');
135 static Regex GetRegex (string verb, bool match_all)
138 if (regexCache.ContainsKey (verb))
139 return (Regex) regexCache [verb];
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]);
147 result.AppendFormat ("{0}\\z|\\A", regex);
149 result.AppendFormat ("({0})\\z", regex);
153 Regex r = new Regex (result.ToString ());
154 regexCache [verb] = r;
158 static void EnsureCache ()
160 if (regexCache == null)
161 regexCache = new Hashtable ();