HttpHandlerAction.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.Collections;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.Reflection;
  35. using System.Text.RegularExpressions;
  36. using System.Web.Util;
  37. namespace System.Web.Configuration
  38. {
  39. public sealed class HttpHandlerAction: ConfigurationElement
  40. {
  41. static ConfigurationPropertyCollection _properties;
  42. static ConfigurationProperty pathProp;
  43. static ConfigurationProperty typeProp;
  44. static ConfigurationProperty validateProp;
  45. static ConfigurationProperty verbProp;
  46. static HttpHandlerAction ()
  47. {
  48. pathProp = new ConfigurationProperty ("path", typeof (string), null,
  49. TypeDescriptor.GetConverter (typeof (string)),
  50. PropertyHelper.NonEmptyStringValidator,
  51. ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
  52. typeProp = new ConfigurationProperty ("type", typeof (string), null,
  53. TypeDescriptor.GetConverter (typeof (string)),
  54. PropertyHelper.NonEmptyStringValidator,
  55. ConfigurationPropertyOptions.IsRequired);
  56. validateProp = new ConfigurationProperty ("validate", typeof (bool), true);
  57. verbProp = new ConfigurationProperty ("verb", typeof (string), null,
  58. TypeDescriptor.GetConverter (typeof (string)),
  59. PropertyHelper.NonEmptyStringValidator,
  60. ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
  61. _properties = new ConfigurationPropertyCollection ();
  62. _properties.Add (pathProp);
  63. _properties.Add (typeProp);
  64. _properties.Add (validateProp);
  65. _properties.Add (verbProp);
  66. }
  67. internal HttpHandlerAction ()
  68. { }
  69. public HttpHandlerAction (string path, string type, string verb)
  70. : this (path, type, verb, true)
  71. { }
  72. public HttpHandlerAction (string path, string type, string verb, bool validate)
  73. {
  74. Path = path;
  75. Type = type;
  76. Verb = verb;
  77. Validate = validate;
  78. }
  79. [ConfigurationProperty ("path", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
  80. // LAMESPEC: MS lists no validator here but provides one in Properties.
  81. public string Path {
  82. get { return (string) base[pathProp]; }
  83. set { base[pathProp] = value; }
  84. }
  85. [ConfigurationProperty ("type", Options = ConfigurationPropertyOptions.IsRequired)]
  86. // LAMESPEC: MS lists no validator here but provides one in Properties.
  87. public string Type {
  88. get { return (string) base[typeProp]; }
  89. set { base[typeProp] = value; }
  90. }
  91. [ConfigurationProperty ("validate", DefaultValue = true)]
  92. public bool Validate {
  93. get { return (bool) base[validateProp]; }
  94. set { base[validateProp] = value; }
  95. }
  96. [ConfigurationProperty ("verb", Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
  97. // LAMESPEC: MS lists no validator here but provides one in Properties.
  98. public string Verb {
  99. get { return (string) base[verbProp]; }
  100. set { base[verbProp] = value; }
  101. }
  102. protected override ConfigurationPropertyCollection Properties {
  103. get { return _properties; }
  104. }
  105. #region CompatabilityCode
  106. object instance;
  107. Type type;
  108. string cached_verb = null;
  109. string[] cached_verbs;
  110. FileMatchingInfo[] cached_files;
  111. FileMatchingInfo[] SplitPaths ()
  112. {
  113. string [] paths = Path.Split (',');
  114. FileMatchingInfo [] tmpCachedFiles = new FileMatchingInfo [paths.Length];
  115. int i = 0;
  116. foreach (string s in paths)
  117. tmpCachedFiles [i++] = new FileMatchingInfo (s);
  118. return tmpCachedFiles;
  119. }
  120. string[] SplitVerbs ()
  121. {
  122. if (Verb == "*")
  123. cached_verbs = null;
  124. else
  125. cached_verbs = Verb.Split (',');
  126. return cached_verbs;
  127. }
  128. internal string[] Verbs {
  129. get {
  130. if (cached_verb != Verb) {
  131. cached_verbs = SplitVerbs();
  132. cached_verb = Verb;
  133. }
  134. return cached_verbs;
  135. }
  136. }
  137. FileMatchingInfo[] Paths {
  138. get {
  139. if (cached_files == null) {
  140. cached_files = SplitPaths ();
  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 = null;
  152. t = HttpApplication.LoadType (type_name, false);
  153. if (t == null)
  154. throw new HttpException (String.Format ("Failed to load httpHandler type `{0}'", type_name));
  155. if (typeof (IHttpHandler).IsAssignableFrom (t) ||
  156. typeof (IHttpHandlerFactory).IsAssignableFrom (t))
  157. return t;
  158. throw new HttpException (String.Format ("Type {0} does not implement IHttpHandler or IHttpHandlerFactory", type_name));
  159. }
  160. internal bool PathMatches (string p)
  161. {
  162. int slash = p.LastIndexOf ('/');
  163. string orig = p;
  164. if (slash != -1)
  165. p = p.Substring (slash);
  166. for (int j = Paths.Length; j > 0; ){
  167. j--;
  168. FileMatchingInfo fm = Paths [j];
  169. bool ignoreCase =
  170. #if TARGET_J2EE
  171. true;
  172. #else
  173. false;
  174. #endif
  175. if (fm.MatchExact != null)
  176. return fm.MatchExact.Length == orig.Length && StrUtils.EndsWith (orig, fm.MatchExact, ignoreCase);
  177. if (fm.EndsWith != null)
  178. return StrUtils.EndsWith (p, fm.EndsWith, ignoreCase);
  179. if (fm.MatchExpr == "*")
  180. return true;
  181. /* convert to regexp */
  182. return fm.RegExp.IsMatch (orig);
  183. }
  184. return false;
  185. }
  186. // Loads the handler, possibly delay-loaded.
  187. internal object GetHandlerInstance ()
  188. {
  189. IHttpHandler ihh = instance as IHttpHandler;
  190. if (instance == null || (ihh != null && !ihh.IsReusable)){
  191. if (type == null)
  192. type = LoadType (Type);
  193. instance = Activator.CreateInstance (type);
  194. }
  195. return instance;
  196. }
  197. #endregion
  198. }
  199. }
  200. #endif