HandlerItem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. using System;
  11. using System.Collections;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. namespace System.Web.Configuration
  15. {
  16. public class HandlerItem
  17. {
  18. private Type _type;
  19. private string _typeName;
  20. private string _path;
  21. private string _requestType;
  22. private Regex requestRegex;
  23. private Regex pathRegex;
  24. static Hashtable regexCache;
  25. public HandlerItem (string requestType, string path, string type, bool validate)
  26. {
  27. _typeName = type;
  28. _path = path;
  29. _requestType = requestType.Replace (" ", "");
  30. requestRegex = GetRegex (_requestType);
  31. pathRegex = GetRegex (_path);
  32. if (validate)
  33. DoValidation ();
  34. }
  35. public object Create ()
  36. {
  37. if (_type == null)
  38. DoValidation ();
  39. return HttpRuntime.CreateInternalObject (_type);
  40. }
  41. public Type Type
  42. {
  43. get {
  44. if (_type == null)
  45. DoValidation ();
  46. return _type;
  47. }
  48. }
  49. public bool IsMatch (string type, string path)
  50. {
  51. return (MatchVerb (type) && MatchPath (path));
  52. }
  53. bool MatchVerb (string verb)
  54. {
  55. return requestRegex.IsMatch (verb);
  56. }
  57. bool MatchPath (string path)
  58. {
  59. if (pathRegex.IsMatch (path))
  60. return true;
  61. int slash = path.LastIndexOf (path);
  62. if (slash != -1 && path.Length > slash + 1)
  63. return pathRegex.IsMatch (path.Substring (slash + 1));
  64. return false;
  65. }
  66. void DoValidation ()
  67. {
  68. _type = Type.GetType (_typeName, true);
  69. if (typeof (IHttpHandler).IsAssignableFrom (_type))
  70. return;
  71. if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
  72. return;
  73. throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
  74. }
  75. static string ToRegexPattern (string dosPattern)
  76. {
  77. string result = dosPattern.Replace (".", "\\.");
  78. result = result.Replace ("*", ".*");
  79. result = result.Replace ('?', '.');
  80. return result;
  81. }
  82. static Regex GetRegex (string verb)
  83. {
  84. EnsureCache ();
  85. if (regexCache.ContainsKey (verb))
  86. return (Regex) regexCache [verb];
  87. StringBuilder result = new StringBuilder ("\\A");
  88. string [] expressions = verb.Split (',');
  89. int end = expressions.Length;
  90. for (int i = 0; i < end; i++) {
  91. string regex = ToRegexPattern (expressions [i]);
  92. if (i + 1 < end) {
  93. result.AppendFormat ("{0}\\z|\\A", regex);
  94. } else {
  95. result.AppendFormat ("({0})\\z", regex);
  96. }
  97. }
  98. Regex r = new Regex (result.ToString ());
  99. regexCache [verb] = r;
  100. return r;
  101. }
  102. static void EnsureCache ()
  103. {
  104. if (regexCache == null)
  105. regexCache = new Hashtable ();
  106. }
  107. }
  108. }