HttpHandlerAction.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // System.Web.Configuration.HttpHandlerAction
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.ComponentModel;
  32. using System.Configuration;
  33. using System.Text.RegularExpressions;
  34. using System.Web.Util;
  35. namespace System.Web.Configuration
  36. {
  37. public sealed class HttpHandlerAction: ConfigurationElement
  38. {
  39. static ConfigurationPropertyCollection _properties;
  40. static ConfigurationProperty pathProp;
  41. static ConfigurationProperty typeProp;
  42. static ConfigurationProperty validateProp;
  43. static ConfigurationProperty verbProp;
  44. static HttpHandlerAction ()
  45. {
  46. pathProp = new ConfigurationProperty ("path", typeof (string), null,
  47. TypeDescriptor.GetConverter (typeof (string)),
  48. PropertyHelper.NonEmptyStringValidator,
  49. ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
  50. typeProp = new ConfigurationProperty ("type", typeof (string), null,
  51. TypeDescriptor.GetConverter (typeof (string)),
  52. PropertyHelper.NonEmptyStringValidator,
  53. ConfigurationPropertyOptions.IsRequired);
  54. validateProp = new ConfigurationProperty ("validate", typeof (bool), true);
  55. verbProp = new ConfigurationProperty ("verb", typeof (string), null,
  56. TypeDescriptor.GetConverter (typeof (string)),
  57. PropertyHelper.NonEmptyStringValidator,
  58. ConfigurationPropertyOptions.IsRequired);
  59. _properties = new ConfigurationPropertyCollection ();
  60. _properties.Add (pathProp);
  61. _properties.Add (typeProp);
  62. _properties.Add (validateProp);
  63. _properties.Add (verbProp);
  64. }
  65. internal HttpHandlerAction ()
  66. { }
  67. public HttpHandlerAction (string path, string type, string verb)
  68. : this (path, type, verb, true)
  69. { }
  70. public HttpHandlerAction (string path, string type, string verb, bool validate)
  71. {
  72. Path = path;
  73. Type = type;
  74. Verb = verb;
  75. Validate = validate;
  76. }
  77. [ConfigurationProperty ("path", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
  78. // LAMESPEC: MS lists no validator here but provides one in Properties.
  79. public string Path {
  80. get { return (string) base[pathProp]; }
  81. set { base[pathProp] = value; }
  82. }
  83. [ConfigurationProperty ("type", Options = ConfigurationPropertyOptions.IsRequired)]
  84. // LAMESPEC: MS lists no validator here but provides one in Properties.
  85. public string Type {
  86. get { return (string) base[typeProp]; }
  87. set { base[typeProp] = value; }
  88. }
  89. [ConfigurationProperty ("validate", DefaultValue = true)]
  90. public bool Validate {
  91. get { return (bool) base[validateProp]; }
  92. set { base[validateProp] = value; }
  93. }
  94. [ConfigurationProperty ("verb", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
  95. // LAMESPEC: MS lists no validator here but provides one in Properties.
  96. public string Verb {
  97. get { return (string) base[verbProp]; }
  98. set { base[verbProp] = value; }
  99. }
  100. protected override ConfigurationPropertyCollection Properties {
  101. get { return _properties; }
  102. }
  103. #region CompatabilityCode
  104. object instance;
  105. Type type;
  106. string cached_verb = null;
  107. string[] cached_verbs;
  108. string cached_path = null;
  109. FileMatchingInfo[] cached_files;
  110. FileMatchingInfo[] SplitPaths ()
  111. {
  112. string [] paths = Path.Split (',');
  113. cached_files = new FileMatchingInfo [paths.Length];
  114. int i = 0;
  115. foreach (string s in paths)
  116. cached_files [i++] = new FileMatchingInfo (s);
  117. return cached_files;
  118. }
  119. string[] SplitVerbs ()
  120. {
  121. if (Verb == "*")
  122. cached_verbs = null;
  123. else
  124. cached_verbs = Verb.Split (',');
  125. return cached_verbs;
  126. }
  127. internal string[] Verbs {
  128. get {
  129. if (cached_verb != Verb) {
  130. cached_verbs = SplitVerbs();
  131. cached_verb = Verb;
  132. }
  133. return cached_verbs;
  134. }
  135. }
  136. FileMatchingInfo[] Paths {
  137. get {
  138. if (cached_path != Path) {
  139. cached_files = SplitPaths ();
  140. cached_path = Path;
  141. }
  142. return cached_files;
  143. }
  144. }
  145. //
  146. // Loads the a type by name and verifies that it implements
  147. // IHttpHandler or IHttpHandlerFactory
  148. //
  149. internal static Type LoadType (string type_name)
  150. {
  151. Type t;
  152. try {
  153. t = System.Type.GetType (type_name, true);
  154. } catch (Exception e) {
  155. throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name), e);
  156. }
  157. if (typeof (IHttpHandler).IsAssignableFrom (t) ||
  158. typeof (IHttpHandlerFactory).IsAssignableFrom (t))
  159. return t;
  160. throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
  161. }
  162. internal bool PathMatches (string p)
  163. {
  164. int slash = p.LastIndexOf ('/');
  165. string orig = p;
  166. if (slash != -1)
  167. p = p.Substring (slash);
  168. for (int j = Paths.Length; j > 0; ){
  169. j--;
  170. FileMatchingInfo fm = Paths [j];
  171. if (fm.MatchExact != null)
  172. return fm.MatchExact.Length == p.Length && StrUtils.EndsWith (p, fm.MatchExact);
  173. if (fm.EndsWith != null)
  174. return StrUtils.EndsWith (p, fm.EndsWith);
  175. if (fm.MatchExpr == "*")
  176. return true;
  177. /* convert to regexp */
  178. return fm.RegExp.IsMatch (orig);
  179. }
  180. return false;
  181. }
  182. // Loads the handler, possibly delay-loaded.
  183. internal object GetHandlerInstance ()
  184. {
  185. IHttpHandler ihh = instance as IHttpHandler;
  186. if (instance == null || (ihh != null && !ihh.IsReusable)){
  187. if (type == null)
  188. type = LoadType (Type);
  189. instance = Activator.CreateInstance (type);
  190. }
  191. return instance;
  192. }
  193. class FileMatchingInfo {
  194. public string MatchExact;
  195. public string MatchExpr;
  196. // If set, we can fast-path the patch with string.EndsWith (FMI.EndsWith)
  197. public string EndsWith;
  198. public Regex RegExp;
  199. public FileMatchingInfo (string s)
  200. {
  201. MatchExpr = s;
  202. if (s[0] == '*' && (s.IndexOf ('*', 1) == -1))
  203. EndsWith = s.Substring (1);
  204. if (s.IndexOf ('*') == -1)
  205. MatchExact = "/" + s;
  206. if (MatchExpr != "*") {
  207. string expr = MatchExpr.Replace(".", "\\.").Replace("?", "\\?").Replace("*", ".*");
  208. if (expr.Length > 0 && expr [0] =='/')
  209. expr = expr.Substring (1);
  210. expr += "\\z";
  211. RegExp = new Regex (expr);
  212. }
  213. }
  214. }
  215. #endregion
  216. }
  217. }
  218. #endif