From 8fc1b4d707e3ad3a92c3985d0e2e176d0ebb02c3 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Tue, 17 May 2011 13:04:51 -0700 Subject: [PATCH] Refactoring: moving code out of WarmupHttpModule class --HG-- branch : 1.x extra : transplant_source : %86%0A%26%0DP%29e%99%0E%87%5D%F5%7E%F5X%C0i%9E%2C%01 --- src/Orchard.Startup/WarmupHttpModule.cs | 45 +------------------------ src/Orchard.Startup/WarmupUtility.cs | 42 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/Orchard.Startup/WarmupHttpModule.cs b/src/Orchard.Startup/WarmupHttpModule.cs index 5ab2facf9..bfdb50e71 100644 --- a/src/Orchard.Startup/WarmupHttpModule.cs +++ b/src/Orchard.Startup/WarmupHttpModule.cs @@ -1,20 +1,14 @@ using System; using System.Collections.Generic; -using System.IO; using System.Threading; using System.Web; -using System.Web.Hosting; namespace Orchard.WarmupStarter { public class WarmupHttpModule : IHttpModule { - private const string WarmupFilesPath = "~/App_Data/Warmup/"; private HttpApplication _context; private static object _synLock = new object(); private static IList _awaiting = new List(); - public WarmupHttpModule() { - } - public void Init(HttpApplication context) { _context = context; context.AddOnBeginRequestAsync(BeginBeginRequest, EndBeginRequest, null); @@ -92,7 +86,7 @@ namespace Orchard.WarmupStarter { var asyncResult = new WarmupAsyncResult(cb); // host is available, process every requests, or file is processed - if (!InWarmup() || DoBeginRequest()) { + if (!InWarmup() || WarmupUtility.DoBeginRequest(_context)) { asyncResult.Done(); } else { @@ -107,43 +101,6 @@ namespace Orchard.WarmupStarter { ((WarmupAsyncResult)ar).Wait(); } - /// - /// return true to put request on hold (until call to Signal()) - return false to allow pipeline to execute immediately - /// - /// - private bool DoBeginRequest() { - // use the url as it was requested by the client - // the real url might be different if it has been translated (proxy, load balancing, ...) - var url = ToUrlString(_context.Request); - var virtualFileCopy = WarmupUtility.EncodeUrl(url.Trim('/')); - var localCopy = Path.Combine(HostingEnvironment.MapPath(WarmupFilesPath), virtualFileCopy); - - if (File.Exists(localCopy)) { - // result should not be cached, even on proxies - _context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); - _context.Response.Cache.SetValidUntilExpires(false); - _context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); - _context.Response.Cache.SetCacheability(HttpCacheability.NoCache); - _context.Response.Cache.SetNoStore(); - - _context.Response.WriteFile(localCopy); - _context.Response.End(); - return true; - } - - // there is no local copy and the file exists - // serve the static file - if (File.Exists(_context.Request.PhysicalPath)) { - return true; - } - - return false; - } - - public static string ToUrlString(HttpRequest request) { - return string.Format("{0}://{1}{2}", request.Url.Scheme, request.Headers["Host"], request.RawUrl); - } - private class WarmupAsyncResult : IAsyncResult { private readonly EventWaitHandle _eventWaitHandle = new AutoResetEvent(false); private readonly AsyncCallback _cb; diff --git a/src/Orchard.Startup/WarmupUtility.cs b/src/Orchard.Startup/WarmupUtility.cs index 2c79c60d0..4abb789f3 100644 --- a/src/Orchard.Startup/WarmupUtility.cs +++ b/src/Orchard.Startup/WarmupUtility.cs @@ -1,8 +1,50 @@ using System; +using System.IO; using System.Text; +using System.Web; +using System.Web.Hosting; namespace Orchard.WarmupStarter { public static class WarmupUtility { + public static readonly string WarmupFilesPath = "~/App_Data/Warmup/"; + /// + /// return true to put request on hold (until call to Signal()) - return false to allow pipeline to execute immediately + /// + /// + /// + public static bool DoBeginRequest(HttpApplication httpApplication) { + // use the url as it was requested by the client + // the real url might be different if it has been translated (proxy, load balancing, ...) + var url = ToUrlString(httpApplication.Request); + var virtualFileCopy = WarmupUtility.EncodeUrl(url.Trim('/')); + var localCopy = Path.Combine(HostingEnvironment.MapPath(WarmupFilesPath), virtualFileCopy); + + if (File.Exists(localCopy)) { + // result should not be cached, even on proxies + httpApplication.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); + httpApplication.Response.Cache.SetValidUntilExpires(false); + httpApplication.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); + httpApplication.Response.Cache.SetCacheability(HttpCacheability.NoCache); + httpApplication.Response.Cache.SetNoStore(); + + httpApplication.Response.WriteFile(localCopy); + httpApplication.Response.End(); + return true; + } + + // there is no local copy and the file exists + // serve the static file + if (File.Exists(httpApplication.Request.PhysicalPath)) { + return true; + } + + return false; + } + + public static string ToUrlString(HttpRequest request) { + return string.Format("{0}://{1}{2}", request.Url.Scheme, request.Headers["Host"], request.RawUrl); + } + public static string EncodeUrl(string url) { if (String.IsNullOrWhiteSpace(url)) { throw new ArgumentException("url can't be empty");