HandlerItem.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. return pathRegex.IsMatch (path);
  60. }
  61. void DoValidation ()
  62. {
  63. _type = Type.GetType (_typeName, true);
  64. if (typeof (IHttpHandler).IsAssignableFrom (_type))
  65. return;
  66. if (typeof (IHttpHandlerFactory).IsAssignableFrom (_type))
  67. return;
  68. throw new HttpException (HttpRuntime.FormatResourceString ("type_not_factory_or_handler"));
  69. }
  70. static string ToRegexPattern (string dosPattern)
  71. {
  72. string result = dosPattern.Replace (".", "\\.");
  73. result = result.Replace ("*", ".*");
  74. result = result.Replace ('?', '.');
  75. return result;
  76. }
  77. static Regex GetRegex (string verb)
  78. {
  79. EnsureCache ();
  80. if (regexCache.ContainsKey (verb))
  81. return (Regex) regexCache [verb];
  82. StringBuilder result = new StringBuilder ("\\A");
  83. string [] expressions = verb.Split (',');
  84. int end = expressions.Length;
  85. for (int i = 0; i < end; i++) {
  86. string regex = ToRegexPattern (expressions [i]);
  87. if (i + 1 < end) {
  88. result.AppendFormat ("{0}\\z|\\A", regex);
  89. } else {
  90. result.AppendFormat ("({0})\\z", regex);
  91. }
  92. }
  93. Regex r = new Regex (result.ToString ());
  94. regexCache [verb] = r;
  95. return r;
  96. }
  97. static void EnsureCache ()
  98. {
  99. if (regexCache == null)
  100. regexCache = new Hashtable ();
  101. }
  102. }
  103. }