HandlerItem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. namespace System.Web.Configuration
  35. {
  36. class HandlerItem
  37. {
  38. private Type _type;
  39. private string _typeName;
  40. private string _path;
  41. private string _requestType;
  42. private Regex requestRegex;
  43. private Regex pathRegex;
  44. static Hashtable regexCache;
  45. public HandlerItem (string requestType, string path, string type, bool validate)
  46. {
  47. _typeName = type;
  48. _path = path;
  49. _requestType = requestType.Replace (" ", "");
  50. requestRegex = GetRegex (_requestType);
  51. pathRegex = GetRegex (_path);
  52. if (validate)
  53. DoValidation ();
  54. }
  55. public object Create ()
  56. {
  57. if (_type == null)
  58. DoValidation ();
  59. return HttpRuntime.CreateInternalObject (_type);
  60. }
  61. public Type Type
  62. {
  63. get {
  64. if (_type == null)
  65. DoValidation ();
  66. return _type;
  67. }
  68. }
  69. public bool IsMatch (string type, string path)
  70. {
  71. return (MatchVerb (type) && MatchPath (path));
  72. }
  73. bool MatchVerb (string verb)
  74. {
  75. return requestRegex.IsMatch (verb);
  76. }
  77. bool MatchPath (string path)
  78. {
  79. if (pathRegex.IsMatch (path))
  80. return true;
  81. int slash = path.LastIndexOf ('/');
  82. if (slash != -1 && path.Length > slash + 1)
  83. return pathRegex.IsMatch (path.Substring (slash + 1));
  84. return false;
  85. }
  86. void DoValidation ()
  87. {
  88. _type = Type.GetType (_typeName, true);
  89. if (typeof (IHttpHandler).IsAssignableFrom (_type))
  90. return;
  91. if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
  92. return;
  93. throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
  94. }
  95. static string ToRegexPattern (string dosPattern)
  96. {
  97. string result = dosPattern.Replace (".", "\\.");
  98. result = result.Replace ("*", ".*");
  99. result = result.Replace ('?', '.');
  100. return result;
  101. }
  102. static Regex GetRegex (string verb)
  103. {
  104. EnsureCache ();
  105. if (regexCache.ContainsKey (verb))
  106. return (Regex) regexCache [verb];
  107. StringBuilder result = new StringBuilder ("\\A");
  108. string [] expressions = verb.Split (',');
  109. int end = expressions.Length;
  110. for (int i = 0; i < end; i++) {
  111. string regex = ToRegexPattern (expressions [i]);
  112. if (i + 1 < end) {
  113. result.AppendFormat ("{0}\\z|\\A", regex);
  114. } else {
  115. result.AppendFormat ("({0})\\z", regex);
  116. }
  117. }
  118. Regex r = new Regex (result.ToString ());
  119. regexCache [verb] = r;
  120. return r;
  121. }
  122. static void EnsureCache ()
  123. {
  124. if (regexCache == null)
  125. regexCache = new Hashtable ();
  126. }
  127. }
  128. }