HandlerFactoryConfiguration.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // System.Web.Configuration.HandlerFactoryConfiguration.cs
  3. //
  4. //
  5. // Authors:
  6. // Miguel de Icaza ([email protected]
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Web.Util;
  33. using System.Text.RegularExpressions;
  34. namespace System.Web.Configuration {
  35. class FileMatchingInfo {
  36. public string MatchExact;
  37. public string MatchExpr;
  38. // If set, we can fast-path the patch with string.EndsWith (FMI.EndsWith)
  39. public string EndsWith;
  40. public Regex RegExp;
  41. public FileMatchingInfo (string s)
  42. {
  43. MatchExpr = s;
  44. if (s[0] == '*' && (s.IndexOf ('*', 1) == -1))
  45. EndsWith = s.Substring (1);
  46. if (s.IndexOf ('*') == -1)
  47. MatchExact = "/" + s;
  48. if (MatchExpr != "*")
  49. RegExp = new Regex (MatchExpr.Replace(".", "\\.").Replace("?", "\\?").Replace("*", ".*"));
  50. }
  51. }
  52. class HttpHandler {
  53. // If `null', we are the "*" match
  54. public string OriginalVerb;
  55. public string OriginalPath;
  56. public string [] Verbs;
  57. public FileMatchingInfo [] files;
  58. // To support lazy loading we keep the name around.
  59. public string TypeName;
  60. Type type;
  61. object instance;
  62. public HttpHandler (string verb, string path, string typename, Type t)
  63. {
  64. OriginalVerb = verb;
  65. OriginalPath = path;
  66. if (verb != "*")
  67. Verbs = verb.Split (',');
  68. string [] paths = path.Split (',');
  69. files = new FileMatchingInfo [paths.Length];
  70. int i = 0;
  71. foreach (string s in paths)
  72. files [i++] = new FileMatchingInfo (s);
  73. this.TypeName = typename;
  74. type = t;
  75. }
  76. //
  77. // Loads the a type by name and verifies that it implements
  78. // IHttpHandler or IHttpHandlerFactory
  79. //
  80. public static Type LoadType (string type_name)
  81. {
  82. Type t;
  83. try {
  84. t = Type.GetType (type_name, true);
  85. } catch (Exception e) {
  86. throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name));
  87. }
  88. if (typeof (IHttpHandler).IsAssignableFrom (t) ||
  89. typeof (IHttpHandlerFactory).IsAssignableFrom (t))
  90. return t;
  91. throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
  92. }
  93. public bool PathMatches (string p)
  94. {
  95. int slash = p.LastIndexOf ('/');
  96. if (slash != -1)
  97. p = p.Substring (slash);
  98. for (int j = files.Length; j > 0; ){
  99. j--;
  100. FileMatchingInfo fm = files [j];
  101. if (fm.MatchExact != null)
  102. return fm.MatchExact.Length == p.Length && StrUtils.EndsWith (p, fm.MatchExact);
  103. if (fm.EndsWith != null)
  104. return StrUtils.EndsWith (p, fm.EndsWith);
  105. if (fm.MatchExpr == "*")
  106. return true;
  107. /* convert to regexp */
  108. return fm.RegExp.IsMatch (p);
  109. }
  110. return false;
  111. }
  112. // Loads the handler, possibly delay-loaded.
  113. public object GetHandlerInstance ()
  114. {
  115. IHttpHandler ihh = instance as IHttpHandler;
  116. if (instance == null || (ihh != null && !ihh.IsReusable)){
  117. if (type == null)
  118. type = LoadType (TypeName);
  119. instance = Activator.CreateInstance (type);
  120. }
  121. return instance;
  122. }
  123. }
  124. class HandlerFactoryConfiguration {
  125. ArrayList handlers;
  126. HandlerFactoryConfiguration parent;
  127. public HandlerFactoryConfiguration (HandlerFactoryConfiguration parent)
  128. {
  129. this.parent = parent;
  130. handlers = new ArrayList ();
  131. }
  132. public void Clear ()
  133. {
  134. handlers.Clear ();
  135. }
  136. public void Add (string verb, string path, string type_name, bool validate)
  137. {
  138. Type type;
  139. if (validate){
  140. type = HttpHandler.LoadType (type_name);
  141. if (type == null)
  142. throw new HttpException (String.Format ("Can not load {0}", type_name));
  143. } else
  144. type = null;
  145. handlers.Add (new HttpHandler (verb, path, type_name, type));
  146. }
  147. public HttpHandler Remove (string verb, string path)
  148. {
  149. int top = handlers.Count;
  150. for (int i = 0; i < top; i++){
  151. HttpHandler handler = (HttpHandler) handlers [i];
  152. if (verb == handler.OriginalVerb && path == handler.OriginalPath){
  153. handlers.Remove (i);
  154. return handler;
  155. }
  156. }
  157. return null;
  158. }
  159. public object LocateHandler (string verb, string filepath)
  160. {
  161. int top = handlers.Count;
  162. for (int i = 0; i < top; i++){
  163. HttpHandler handler = (HttpHandler) handlers [i];
  164. if (handler.Verbs == null){
  165. if (handler.PathMatches (filepath))
  166. return handler.GetHandlerInstance ();
  167. continue;
  168. }
  169. string [] verbs = handler.Verbs;
  170. for (int j = verbs.Length; j > 0; ){
  171. j--;
  172. if (verbs [j] != verb)
  173. continue;
  174. if (handler.PathMatches (filepath))
  175. return handler.GetHandlerInstance ();
  176. }
  177. }
  178. if (parent != null)
  179. return parent.LocateHandler (verb, filepath);
  180. else
  181. return null;
  182. }
  183. }
  184. }