From fbf94bbf01a90af481e9ffe80c63ae7f7eaaa707 Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Mon, 6 Aug 2012 23:04:08 +0600 Subject: [PATCH] Fixed module with client side perf check, added unit test --- src/Umbraco.Tests/UmbracoModuleTests.cs | 17 +++++++- src/Umbraco.Web/LegacyRequestInitializer.cs | 21 ++++++---- src/Umbraco.Web/UmbracoModule.cs | 46 ++++++++++++--------- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Tests/UmbracoModuleTests.cs b/src/Umbraco.Tests/UmbracoModuleTests.cs index 27eb37ec71..e4d36eaa34 100644 --- a/src/Umbraco.Tests/UmbracoModuleTests.cs +++ b/src/Umbraco.Tests/UmbracoModuleTests.cs @@ -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); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Web/LegacyRequestInitializer.cs b/src/Umbraco.Web/LegacyRequestInitializer.cs index bc54e36f43..a89e55c054 100644 --- a/src/Umbraco.Web/LegacyRequestInitializer.cs +++ b/src/Umbraco.Web/LegacyRequestInitializer.cs @@ -1,3 +1,6 @@ +using System; +using System.Web; + namespace Umbraco.Web { /// @@ -5,27 +8,29 @@ namespace Umbraco.Web /// 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
+ /// 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. + /// + /// + /// + internal bool IsClientSideRequest(Uri url) + { + var toIgnore = new[] {".js", ".css", ".ico"}; + return toIgnore.Any(x => Path.GetExtension(url.LocalPath).InvariantEquals(x)); + } + /// /// Checks the current request and ensures that it is routable based on the structure of the request and URI /// @@ -29,7 +43,7 @@ namespace Umbraco.Web /// /// /// - 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("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("End processing request, not transfering to handler"); + LogHelper.Debug("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("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();