RewriterFactoryHandler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using System.Web.UI;
  4. using System.Web;
  5. using URLRewriter.Config;
  6. using System.Text.RegularExpressions;
  7. namespace URLRewriter
  8. {
  9. /// <summary>
  10. /// Provides an HttpHandler that performs redirection.
  11. /// </summary>
  12. /// <remarks>The RewriterFactoryHandler checks the rewriting rules, rewrites the path if needed, and then
  13. /// delegates the responsibility of processing the ASP.NET page to the <b>PageParser</b> class (the same one
  14. /// used by the <b>PageHandlerFactory</b> class).</remarks>
  15. public class RewriterFactoryHandler : IHttpHandlerFactory
  16. {
  17. /// <summary>
  18. /// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of
  19. /// GetHandler is to return an instance of an HttpHandler that can process the page.
  20. /// </summary>
  21. /// <param name="context">The HttpContext for this request.</param>
  22. /// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>
  23. /// <param name="url">The RawUrl of the requested resource.</param>
  24. /// <param name="pathTranslated">The physical path to the requested resource.</param>
  25. /// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
  26. /// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates
  27. /// to.</returns>
  28. public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
  29. {
  30. // log info to the Trace object...
  31. context.Trace.Write("RewriterFactoryHandler", "Entering RewriterFactoryHandler");
  32. string sendToUrl = url;
  33. string filePath = pathTranslated;
  34. // get the configuration rules
  35. RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
  36. // iterate through the rules
  37. for(int i = 0; i < rules.Count; i++)
  38. {
  39. // Get the pattern to look for (and resolve its URL)
  40. string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].LookFor) + "$";
  41. // Create a regular expression object that ignores case...
  42. Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
  43. // Check to see if we've found a match
  44. if (re.IsMatch(url))
  45. {
  46. // do any replacement needed
  47. sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(url, rules[i].SendTo));
  48. // log info to the Trace object...
  49. context.Trace.Write("RewriterFactoryHandler", "Found match, rewriting to " + sendToUrl);
  50. // Rewrite the path, getting the querystring-less url and the physical file path
  51. string sendToUrlLessQString;
  52. RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath);
  53. // return a compiled version of the page
  54. context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); // log info to the Trace object...
  55. return PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context);
  56. }
  57. }
  58. // if we reached this point, we didn't find a rewrite match
  59. context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); // log info to the Trace object...
  60. return PageParser.GetCompiledPageInstance(url, filePath, context);
  61. }
  62. public virtual void ReleaseHandler(IHttpHandler handler)
  63. {
  64. }
  65. }
  66. }