HandlerFactoryConfiguration.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // System.Web.Configuration.HandlerFactoryConfiguration.cs
  3. //
  4. //
  5. // Authors:
  6. // Miguel de Icaza ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (C) 2005 Novell, Inc (http://www.novell.com)
  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.Web.Util;
  34. using System.Text.RegularExpressions;
  35. namespace System.Web.Configuration {
  36. class HttpHandler {
  37. // If `null', we are the "*" match
  38. public string OriginalVerb;
  39. public string OriginalPath;
  40. public string [] Verbs;
  41. public FileMatchingInfo [] files;
  42. // To support lazy loading we keep the name around.
  43. public string TypeName;
  44. Type type;
  45. object instance;
  46. public HttpHandler (string verb, string path, string typename, Type t)
  47. {
  48. OriginalVerb = verb;
  49. OriginalPath = path;
  50. if (verb != "*")
  51. Verbs = verb.Split (',');
  52. string [] paths = path.Split (',');
  53. files = new FileMatchingInfo [paths.Length];
  54. int i = 0;
  55. foreach (string s in paths)
  56. files [i++] = new FileMatchingInfo (s);
  57. this.TypeName = typename;
  58. type = t;
  59. }
  60. //
  61. // Loads the a type by name and verifies that it implements
  62. // IHttpHandler or IHttpHandlerFactory
  63. //
  64. public static Type LoadType (string type_name)
  65. {
  66. Type t;
  67. try {
  68. t = HttpApplication.LoadType (type_name, true);
  69. } catch {
  70. throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name));
  71. }
  72. if (typeof (IHttpHandler).IsAssignableFrom (t) ||
  73. typeof (IHttpHandlerFactory).IsAssignableFrom (t))
  74. return t;
  75. throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
  76. }
  77. public bool PathMatches (string p)
  78. {
  79. int slash = p.LastIndexOf ('/');
  80. string orig = p;
  81. if (slash != -1)
  82. p = p.Substring (slash);
  83. for (int j = files.Length; j > 0; ){
  84. j--;
  85. FileMatchingInfo fm = files [j];
  86. if (fm.MatchExact != null)
  87. return fm.MatchExact.Length == orig.Length && StrUtils.EndsWith (orig, fm.MatchExact);
  88. if (fm.EndsWith != null)
  89. return StrUtils.EndsWith (p, fm.EndsWith);
  90. if (fm.MatchExpr == "*")
  91. return true;
  92. /* convert to regexp */
  93. return fm.RegExp.IsMatch (orig);
  94. }
  95. return false;
  96. }
  97. // Loads the handler, possibly delay-loaded.
  98. public object GetHandlerInstance ()
  99. {
  100. IHttpHandler ihh = instance as IHttpHandler;
  101. if (instance == null || (ihh != null && !ihh.IsReusable)){
  102. if (type == null)
  103. type = LoadType (TypeName);
  104. instance = Activator.CreateInstance (type);
  105. }
  106. return instance;
  107. }
  108. }
  109. class HandlerFactoryConfiguration {
  110. ArrayList handlers;
  111. //HandlerFactoryConfiguration parent;
  112. int parent_items;
  113. public HandlerFactoryConfiguration (HandlerFactoryConfiguration parent)
  114. {
  115. //this.parent = parent;
  116. if (parent != null) {
  117. handlers = new ArrayList (parent.handlers);
  118. parent_items = handlers.Count;
  119. } else {
  120. handlers = new ArrayList ();
  121. }
  122. }
  123. public void Clear ()
  124. {
  125. handlers.Clear ();
  126. }
  127. public void Add (string verb, string path, string type_name, bool validate)
  128. {
  129. Type type;
  130. if (validate){
  131. type = HttpHandler.LoadType (type_name);
  132. if (type == null)
  133. throw new HttpException (String.Format ("Can not load {0}", type_name));
  134. } else
  135. type = null;
  136. handlers.Add (new HttpHandler (verb, path, type_name, type));
  137. }
  138. public bool Remove (string verb, string path)
  139. {
  140. for (int i = handlers.Count - 1; i >= 0; i--) {
  141. HttpHandler handler = (HttpHandler) handlers [i];
  142. if (verb == handler.OriginalVerb && path == handler.OriginalPath){
  143. handlers.RemoveAt (i);
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. public object LocateHandler (string verb, string filepath)
  150. {
  151. int start, end;
  152. int count = handlers.Count;
  153. for (int k = 0; k < 2; k++) {
  154. // First iteration searches for the mapping in the items added to this
  155. // instance. The second one searches through the parent items if any.
  156. start = (k == 0) ? parent_items : 0;
  157. end = (k == 0) ? count : parent_items;
  158. for (int i = start; i < end; i++) {
  159. HttpHandler handler = (HttpHandler) handlers [i];
  160. if (handler.Verbs == null){
  161. if (handler.PathMatches (filepath))
  162. return handler.GetHandlerInstance ();
  163. continue;
  164. }
  165. string [] verbs = handler.Verbs;
  166. for (int j = verbs.Length; j > 0; ){
  167. j--;
  168. if (verbs [j] != verb)
  169. continue;
  170. if (handler.PathMatches (filepath))
  171. return handler.GetHandlerInstance ();
  172. }
  173. }
  174. }
  175. return null;
  176. }
  177. }
  178. }