HandlerItem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Web.Configuration.HandlerItem
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // Gonzalo Paniagua Javier ([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.Text;
  33. using System.Text.RegularExpressions;
  34. using System.Threading;
  35. namespace System.Web.Configuration
  36. {
  37. class HandlerItem
  38. {
  39. private Type _type;
  40. private string _typeName;
  41. private string _path;
  42. private string _requestType;
  43. private Regex requestRegex;
  44. private Regex pathRegex;
  45. static Hashtable regexCache;
  46. object instance;
  47. bool validated;
  48. public HandlerItem (string requestType, string path, string type, bool validate)
  49. {
  50. _typeName = type;
  51. _path = path;
  52. _requestType = requestType.Replace (" ", "");
  53. requestRegex = GetRegex (_requestType);
  54. pathRegex = GetRegex (_path);
  55. if (validate)
  56. DoValidation ();
  57. }
  58. public object GetInstance ()
  59. {
  60. object obj = Interlocked.CompareExchange (ref instance, null, null);
  61. if (obj != null)
  62. return obj;
  63. DoValidation ();
  64. obj = HttpRuntime.CreateInternalObject (_type);
  65. IHttpHandler hnd = obj as IHttpHandler;
  66. if (hnd != null && hnd.IsReusable)
  67. Interlocked.CompareExchange (ref instance, hnd, null);
  68. return obj;
  69. }
  70. public Type Type
  71. {
  72. get {
  73. DoValidation ();
  74. return _type;
  75. }
  76. }
  77. public bool IsMatch (string type, string path)
  78. {
  79. return (MatchVerb (type) && MatchPath (path));
  80. }
  81. bool MatchVerb (string verb)
  82. {
  83. return requestRegex.IsMatch (verb);
  84. }
  85. bool MatchPath (string path)
  86. {
  87. if (pathRegex.IsMatch (path))
  88. return true;
  89. int slash = path.LastIndexOf ('/');
  90. if (slash != -1 && path.Length > slash + 1)
  91. return pathRegex.IsMatch (path.Substring (slash + 1));
  92. return false;
  93. }
  94. void DoValidation ()
  95. {
  96. if (validated)
  97. return;
  98. lock (this) {
  99. Type t = Type.GetType (_typeName, true);
  100. _type = t;
  101. validated = true;
  102. }
  103. if (typeof (IHttpHandler).IsAssignableFrom (_type))
  104. return;
  105. if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
  106. return;
  107. throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
  108. }
  109. static string ToRegexPattern (string dosPattern)
  110. {
  111. string result = dosPattern.Replace (".", "\\.");
  112. result = result.Replace ("*", ".*");
  113. result = result.Replace ('?', '.');
  114. return result;
  115. }
  116. static Regex GetRegex (string verb)
  117. {
  118. EnsureCache ();
  119. if (regexCache.ContainsKey (verb))
  120. return (Regex) regexCache [verb];
  121. StringBuilder result = new StringBuilder ("\\A");
  122. string [] expressions = verb.Split (',');
  123. int end = expressions.Length;
  124. for (int i = 0; i < end; i++) {
  125. string regex = ToRegexPattern (expressions [i]);
  126. if (i + 1 < end) {
  127. result.AppendFormat ("{0}\\z|\\A", regex);
  128. } else {
  129. result.AppendFormat ("({0})\\z", regex);
  130. }
  131. }
  132. Regex r = new Regex (result.ToString ());
  133. regexCache [verb] = r;
  134. return r;
  135. }
  136. static void EnsureCache ()
  137. {
  138. if (regexCache == null)
  139. regexCache = new Hashtable ();
  140. }
  141. }
  142. }