Fixed module with client side perf check, added unit test

This commit is contained in:
shannon@ShandemVaio
2012-08-06 23:04:08 +06:00
parent 19205f1435
commit fbf94bbf01
3 changed files with 56 additions and 28 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Configuration;
using NUnit.Framework;
using Umbraco.Core;
@@ -58,10 +59,24 @@ namespace Umbraco.Tests
var uri = httpContext.Request.Url;
var lpath = uri.AbsolutePath.ToLower();
var result = _module.EnsureRequestRoutable(uri, lpath, httpContext);
var result = _module.EnsureUmbracoRoutablePage(uri, lpath, httpContext);
Assert.AreEqual(assert, result);
}
[TestCase("/favicon.ico", true)]
[TestCase("/umbraco_client/Tree/treeIcons.css", true)]
[TestCase("/umbraco_client/Tree/Themes/umbraco/style.css?cdv=37", true)]
[TestCase("/umbraco_client/scrollingmenu/style.css?cdv=37", true)]
[TestCase("/base/somebasehandler", false)]
[TestCase("/", false)]
[TestCase("/home.aspx", false)]
public void Is_Client_Side_Request(string url, bool assert)
{
var uri = new Uri("http://test.com" + url);
var result = _module.IsClientSideRequest(uri);
Assert.AreEqual(assert, result);
}
}
}

View File

@@ -1,3 +1,6 @@
using System;
using System.Web;
namespace Umbraco.Web
{
/// <summary>
@@ -5,27 +8,29 @@ namespace Umbraco.Web
/// </summary>
internal class LegacyRequestInitializer
{
private readonly UmbracoContext _umbracoContext;
private readonly Uri _requestUrl;
private readonly HttpContextBase _httpContext;
public LegacyRequestInitializer(UmbracoContext umbracoContext)
public LegacyRequestInitializer(Uri requestUrl, HttpContextBase httpContext)
{
_umbracoContext = umbracoContext;
_requestUrl = requestUrl;
_httpContext = httpContext;
}
public void InitializeRequest()
public void InitializeRequest()
{
var uri = _umbracoContext.RequestUrl;
var uri = _requestUrl;
// legacy - umbOriginalUrl used by default.aspx to rewritepath so forms are happy
// legacy - umbOriginalUrl used by presentation/umbraco/urlRewriter/UrlRewriterFormWriter which handles <form action="..."
// legacy - umbOriginalUrl also in Umbraco's back-end!
_umbracoContext.HttpContext.Items["umbOriginalUrl"] = uri.AbsolutePath;
_httpContext.Items["umbOriginalUrl"] = uri.AbsolutePath;
// legacy - umbPage used by default.aspx to get the "clean url"... whatever... fixme - we prob. don't want it anymore
_umbracoContext.HttpContext.Items["UmbPage"] = uri.AbsolutePath;
_httpContext.Items["UmbPage"] = uri.AbsolutePath;
// legacy - virtualUrl used by presentation/template.cs to handle <form action="..."
// legacy - virtualUrl used by presentation/umbraco/urlRewriter/UrlRewriterFormWriter which handles <form action="..." too
// but, what if we RewritePath as soon as default.aspx begins, shouldn't it clear the form action?
_umbracoContext.HttpContext.Items["VirtualUrl"] = uri.PathAndQuery; //String.Format("{0}{1}{2}", uri.AbsolutePath, string.IsNullOrWhiteSpace(docreq.QueryString) ? "" : "?", docreq.QueryString);
_httpContext.Items["VirtualUrl"] = uri.PathAndQuery; //String.Format("{0}{1}{2}", uri.AbsolutePath, string.IsNullOrWhiteSpace(docreq.QueryString) ? "" : "?", docreq.QueryString);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
@@ -22,6 +23,19 @@ namespace Umbraco.Web
public class UmbracoModule : IHttpModule
{
/// <summary>
/// This is a performance tweak to check if this is a .css, .js or .ico file request since
/// .Net will pass these requests through to the module when in integrated mode.
/// We want to ignore all of these requests immediately.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
internal bool IsClientSideRequest(Uri url)
{
var toIgnore = new[] {".js", ".css", ".ico"};
return toIgnore.Any(x => Path.GetExtension(url.LocalPath).InvariantEquals(x));
}
/// <summary>
/// Checks the current request and ensures that it is routable based on the structure of the request and URI
/// </summary>
@@ -29,7 +43,7 @@ namespace Umbraco.Web
/// <param name="lpath"></param>
/// <param name="httpContext"></param>
/// <returns></returns>
internal bool EnsureRequestRoutable(Uri uri, string lpath, HttpContextBase httpContext)
internal bool EnsureUmbracoRoutablePage(Uri uri, string lpath, HttpContextBase httpContext)
{
// ensure this is a document request
if (!EnsureDocumentRequest(httpContext, uri, lpath))
@@ -55,16 +69,14 @@ namespace Umbraco.Web
{
LogHelper.Debug<UmbracoModule>("Start processing request");
var uri = httpContext.Request.Url;
var lpath = uri.AbsolutePath.ToLower();
//do not continue if this request is not routablehttpContextFactory
if (!EnsureRequestRoutable(uri, lpath, httpContext))
if (IsClientSideRequest(uri))
{
LogHelper.Debug<UmbracoModule>("End processing request, not transfering to handler");
LogHelper.Debug<UmbracoModule>("End processing request, not transfering to handler, this is a client side file request {0}", () => uri);
return;
}
}
// add Umbraco's signature header
if (!UmbracoSettings.RemoveUmbracoVersionHeader)
@@ -101,9 +113,8 @@ namespace Umbraco.Web
// initialize the DocumentRequest on the UmbracoContext (this is circular dependency but i think in this case is ok)
umbracoContext.DocumentRequest = docreq;
//create the LegacyRequestInitializer (one per http request as it relies on the umbraco context!)
var legacyRequestInitializer = new LegacyRequestInitializer(umbracoContext);
//create the LegacyRequestInitializer
var legacyRequestInitializer = new LegacyRequestInitializer(httpContext.Request.Url, httpContext);
// legacy - initialize legacy stuff
legacyRequestInitializer.InitializeRequest();
@@ -114,20 +125,17 @@ namespace Umbraco.Web
//
// to trigger Umbraco's not-found, one should configure IIS and/or ASP.NET custom 404 errors
// so that they point to a non-existing page eg /redirect-404.aspx
//do not continue if this request is not a front-end routable page
if (!EnsureUmbracoRoutablePage(uri, lpath, httpContext))
{
LogHelper.Debug<UmbracoModule>("End processing request, not transfering to handler {0}", () => uri);
return;
}
// legacy - no idea what this is
LegacyCleanUmbPageFromQueryString(ref uri, ref lpath);
//TODO: Before this happens, we need to :
// * move all of this logic into a custom IHttpHandler (maybe)
// ** This is because of TransferRequest actual makes an internal request and the HttpContext is reset
// which means that all HttpContext.Items setting need to occur after
// * need to figure out if we can execute default.aspx as a handler from our IHttpHandler instead of rewriting to it
// * same goes for MVC
// * perhaps its even possible to do all of this without any Rewriting or TransferRequest!
//**THERE** we should create the doc request
// before, we're not sure we handling a doc request
docreq.LookupDomain();