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
This commit is contained in:
Renaud Paquay
2011-05-17 13:04:51 -07:00
parent b01a0898a9
commit 8fc1b4d707
2 changed files with 43 additions and 44 deletions

View File

@@ -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<Action> _awaiting = new List<Action>();
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();
}
/// <summary>
/// return true to put request on hold (until call to Signal()) - return false to allow pipeline to execute immediately
/// </summary>
/// <returns></returns>
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;

View File

@@ -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/";
/// <summary>
/// return true to put request on hold (until call to Signal()) - return false to allow pipeline to execute immediately
/// </summary>
/// <param name="httpApplication"></param>
/// <returns></returns>
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");