HandlerFactoryConfiguration.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 FileMatchingInfo (string s)
  41. {
  42. MatchExpr = s;
  43. if (s[0] == '*' && (s.IndexOf ('*', 1) == -1))
  44. EndsWith = s.Substring (1);
  45. if (s.IndexOf ('*') == -1)
  46. MatchExact = "/" + s;
  47. }
  48. }
  49. class HttpHandler {
  50. // If `null', we are the "*" match
  51. public string OriginalVerb;
  52. public string OriginalPath;
  53. public string [] Verbs;
  54. public FileMatchingInfo [] files;
  55. // To support lazy loading we keep the name around.
  56. public string TypeName;
  57. Type type;
  58. object instance;
  59. public HttpHandler (string verb, string path, string typename, Type t)
  60. {
  61. OriginalVerb = verb;
  62. OriginalPath = path;
  63. if (verb != "*")
  64. Verbs = verb.Split (',');
  65. string [] paths = path.Split (',');
  66. files = new FileMatchingInfo [paths.Length];
  67. int i = 0;
  68. foreach (string s in paths)
  69. files [i++] = new FileMatchingInfo (s);
  70. this.TypeName = typename;
  71. type = t;
  72. }
  73. //
  74. // Loads the a type by name and verifies that it implements
  75. // IHttpHandler or IHttpHandlerFactory
  76. //
  77. public static Type LoadType (string type_name)
  78. {
  79. Type t;
  80. try {
  81. t = Type.GetType (type_name, true);
  82. } catch (Exception e) {
  83. throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name));
  84. }
  85. if (typeof (IHttpHandler).IsAssignableFrom (t) ||
  86. typeof (IHttpHandlerFactory).IsAssignableFrom (t))
  87. return t;
  88. throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
  89. }
  90. public bool PathMatches (string p)
  91. {
  92. for (int j = files.Length; j > 0; ){
  93. j--;
  94. FileMatchingInfo fm = files [j];
  95. if (fm.MatchExact != null)
  96. return fm.MatchExact.Length == p.Length && StrUtils.EndsWith (p, fm.MatchExact);
  97. if (fm.EndsWith != null)
  98. return StrUtils.EndsWith (p, fm.EndsWith);
  99. if (fm.MatchExpr == "*")
  100. return true;
  101. /* convert to regexp */
  102. string match_regexp = fm.MatchExpr.Replace(".", "\\.").Replace("?", "\\?").Replace("*", ".*");
  103. return Regex.IsMatch (p, match_regexp);
  104. }
  105. return false;
  106. }
  107. // Loads the handler, possibly delay-loaded.
  108. public object GetHandlerInstance ()
  109. {
  110. IHttpHandler ihh = instance as IHttpHandler;
  111. if (instance == null || (ihh != null && !ihh.IsReusable)){
  112. if (type == null)
  113. type = LoadType (TypeName);
  114. instance = Activator.CreateInstance (type);
  115. }
  116. return instance;
  117. }
  118. }
  119. class HandlerFactoryConfiguration {
  120. ArrayList handlers;
  121. HandlerFactoryConfiguration parent;
  122. public HandlerFactoryConfiguration (HandlerFactoryConfiguration parent)
  123. {
  124. this.parent = parent;
  125. handlers = new ArrayList ();
  126. }
  127. public void Clear ()
  128. {
  129. handlers.Clear ();
  130. }
  131. public void Add (string verb, string path, string type_name, bool validate)
  132. {
  133. Type type;
  134. if (validate){
  135. type = HttpHandler.LoadType (type_name);
  136. if (type == null)
  137. throw new HttpException (String.Format ("Can not load {0}", type_name));
  138. } else
  139. type = null;
  140. handlers.Add (new HttpHandler (verb, path, type_name, type));
  141. }
  142. public HttpHandler Remove (string verb, string path)
  143. {
  144. int top = handlers.Count;
  145. for (int i = 0; i < top; i++){
  146. HttpHandler handler = (HttpHandler) handlers [i];
  147. if (verb == handler.OriginalVerb && path == handler.OriginalPath){
  148. handlers.Remove (i);
  149. return handler;
  150. }
  151. }
  152. return null;
  153. }
  154. public object LocateHandler (string verb, string filepath)
  155. {
  156. int top = handlers.Count;
  157. for (int i = 0; i < top; i++){
  158. HttpHandler handler = (HttpHandler) handlers [i];
  159. if (handler.Verbs == null){
  160. if (handler.PathMatches (filepath))
  161. return handler.GetHandlerInstance ();
  162. continue;
  163. }
  164. string [] verbs = handler.Verbs;
  165. for (int j = verbs.Length; j > 0; ){
  166. j--;
  167. if (verbs [j] != verb)
  168. continue;
  169. if (handler.PathMatches (filepath))
  170. return handler.GetHandlerInstance ();
  171. }
  172. }
  173. if (parent != null)
  174. return parent.LocateHandler (verb, filepath);
  175. else
  176. return null;
  177. }
  178. }
  179. }