From fe16835648f89b81ab695fa4aeb77d23a866bcf6 Mon Sep 17 00:00:00 2001 From: Stefan Kip Date: Tue, 17 Nov 2015 09:05:22 +0100 Subject: [PATCH] Revert "Fixes issue #90" This reverts commit 78db283f19ff5bb832f5937507cc496bb4e842c6. Added UrlTrackerContentFinder Fixed small cache bug --- ContentFinders/UrlTrackerContentFinder.cs | 17 +++++++ Modules/UrlTrackerModule.cs | 58 +++++++++++------------ Properties/AssemblyInfo.cs | 2 +- README.md | 4 ++ Repositories/UrlTrackerRepository.cs | 3 +- UI/UrlTrackerInfo.aspx | 8 ++++ UrlTracker.csproj | 1 + UrlTrackerApplicationEventHandler.cs | 11 ++++- 8 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 ContentFinders/UrlTrackerContentFinder.cs diff --git a/ContentFinders/UrlTrackerContentFinder.cs b/ContentFinders/UrlTrackerContentFinder.cs new file mode 100644 index 0000000..5c6b6ff --- /dev/null +++ b/ContentFinders/UrlTrackerContentFinder.cs @@ -0,0 +1,17 @@ +using InfoCaster.Umbraco.UrlTracker.Modules; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Umbraco.Web.Routing; + +namespace InfoCaster.Umbraco.UrlTracker.ContentFinders +{ + public class UrlTrackerContentFinder : IContentFinder + { + public bool TryFindContent(PublishedContentRequest contentRequest) + { + return UrlTrackerModule.UrlTrackerDo("UrlTrackerContentFinder"); + } + } +} \ No newline at end of file diff --git a/Modules/UrlTrackerModule.cs b/Modules/UrlTrackerModule.cs index d636c37..b456ef8 100644 --- a/Modules/UrlTrackerModule.cs +++ b/Modules/UrlTrackerModule.cs @@ -37,9 +37,8 @@ public void Dispose() { } public void Init(HttpApplication context) { context.AcquireRequestState += context_AcquireRequestState; - context.PostRequestHandlerExecute += context_PostRequestHandlerExecute; - LoggingHelper.LogInformation("UrlTracker HttpModule | Subscribed to AcquireRequestState and EndRequest events"); + LoggingHelper.LogInformation("UrlTracker HttpModule | Subscribed to AcquireRequestState event"); } #endregion @@ -72,16 +71,10 @@ void context_AcquireRequestState(object sender, EventArgs e) } if (_execute) - UrlTrackerDo("AcquireRequestState", ignoreHttpStatusCode: true); + UrlTrackerDo("AcquireRequestState", forcedRedirects: true); } - void context_PostRequestHandlerExecute(object sender, EventArgs e) - { - if (_execute) - UrlTrackerDo("EndRequest"); - } - - static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = false) + public static bool UrlTrackerDo(string source, bool forcedRedirects = false) { HttpContext context = HttpContext.Current; HttpRequest request = context.Request; @@ -93,15 +86,15 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa response.Write(UrlTrackerSettings.HttpModuleCheck); response.StatusCode = 200; response.End(); - return; + return true; } - LoggingHelper.LogInformation("UrlTracker HttpModule | {0} start", callingEventName); + LoggingHelper.LogInformation("UrlTracker HttpModule | {0} start", source); if (UrlTrackerSettings.IsDisabled) { LoggingHelper.LogInformation("UrlTracker HttpModule | UrlTracker is disabled by config"); - return; + return false; } string url = request.RawUrl; @@ -110,18 +103,19 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa LoggingHelper.LogInformation("UrlTracker HttpModule | Incoming URL is: {0}", url); - if (_urlTrackerInstalled && (response.StatusCode == (int)HttpStatusCode.NotFound || ignoreHttpStatusCode)) + //if (_urlTrackerInstalled && (response.StatusCode == (int)HttpStatusCode.NotFound || ignoreHttpStatusCode)) + if (_urlTrackerInstalled) { - if (response.StatusCode == (int)HttpStatusCode.NotFound) - LoggingHelper.LogInformation("UrlTracker HttpModule | Response statusCode is 404, continue URL matching"); - else + if (forcedRedirects) LoggingHelper.LogInformation("UrlTracker HttpModule | Checking for forced redirects (AcquireRequestState), continue URL matching"); + else + LoggingHelper.LogInformation("UrlTracker HttpModule | No content found, continue URL matching"); string urlWithoutQueryString = url; if (InfoCaster.Umbraco.UrlTracker.Helpers.UmbracoHelper.IsReservedPathOrUrl(url)) { LoggingHelper.LogInformation("UrlTracker HttpModule | URL is an umbraco reserved path or url, ignore request"); - return; + return false; } //bool urlHasQueryString = request.QueryString.HasKeys() && url.Contains('?'); @@ -193,7 +187,7 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa int? redirectHttpCode = null; bool redirectPassThroughQueryString = true; - if (!ignoreHttpStatusCode) + if (!forcedRedirects) { // Normal matching (database) LoadUrlTrackerMatchesFromDatabase(request, urlWithoutQueryString, urlHasQueryString, shortestUrl, rootNodeId, ref redirectUrl, ref redirectHttpCode, ref redirectPassThroughQueryString); @@ -207,12 +201,12 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa string query; if (!redirectHttpCode.HasValue) { - if (!ignoreHttpStatusCode) + if (!forcedRedirects) { // Normal matching (database) // Regex matching query = "SELECT * FROM icUrlTracker WHERE Is404 = 0 AND ForceRedirect = @forceRedirect AND (RedirectRootNodeId = @redirectRootNodeId OR RedirectRootNodeId = -1) AND OldRegex IS NOT NULL ORDER BY Inserted DESC"; - using (IRecordsReader reader = _sqlHelper.ExecuteReader(query, _sqlHelper.CreateParameter("forceRedirect", ignoreHttpStatusCode ? 1 : 0), _sqlHelper.CreateParameter("redirectRootNodeId", rootNodeId))) + using (IRecordsReader reader = _sqlHelper.ExecuteReader(query, _sqlHelper.CreateParameter("forceRedirect", forcedRedirects ? 1 : 0), _sqlHelper.CreateParameter("redirectRootNodeId", rootNodeId))) { Regex regex; while (reader.Read()) @@ -260,11 +254,11 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa else { // Forced matching (cache) - List forcedRedirects = UrlTrackerRepository.GetForcedRedirects().Where(x => !string.IsNullOrEmpty(x.OldRegex)).ToList(); - if (forcedRedirects == null || !forcedRedirects.Any()) - return; + List forcedRedirectsList = UrlTrackerRepository.GetForcedRedirects().Where(x => !string.IsNullOrEmpty(x.OldRegex)).ToList(); + if (forcedRedirectsList == null || !forcedRedirectsList.Any()) + return false; - foreach (var match in forcedRedirects.Select(x => new { UrlTrackerModel = x, Regex = new Regex(x.OldRegex) }).Where(x => x.Regex.IsMatch(url))) + foreach (var match in forcedRedirectsList.Select(x => new { UrlTrackerModel = x, Regex = new Regex(x.OldRegex) }).Where(x => x.Regex.IsMatch(url))) { LoggingHelper.LogInformation("UrlTracker HttpModule | Regex match found"); if (match.UrlTrackerModel.RedirectNodeId.HasValue) @@ -324,7 +318,7 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa if (redirectUri == new Uri(string.Format("{0}{1}{2}{3}/{4}", request.Url.Scheme, Uri.SchemeDelimiter, request.Url.Host, request.Url.Port != 80 && UrlTrackerSettings.AppendPortNumber ? string.Concat(":", request.Url.Port) : string.Empty, request.RawUrl.StartsWith("/") ? request.RawUrl.Substring(1) : request.RawUrl))) { LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect URL is the same as Request.RawUrl; don't redirect"); - return; + return false; } if (request.Url.Host.Equals(redirectUri.Host, StringComparison.OrdinalIgnoreCase)) { @@ -347,8 +341,10 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa } response.End(); + + return true; } - else if (!ignoreHttpStatusCode) + else if (!forcedRedirects) { // Log 404 if (!UrlTrackerSettings.IsNotFoundTrackingDisabled && !UrlTrackerSettings.NotFoundUrlsToIgnore.Contains(urlWithoutQueryString) && !UmbracoHelper.IsReservedPathOrUrl(urlWithoutQueryString) && request.Headers["X-UrlTracker-Ignore404"] != "1") @@ -386,12 +382,14 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa LoggingHelper.LogInformation("UrlTracker HttpModule | No match found, url is ignored because the 'X-UrlTracker-Ignore404' header was set to '1'. URL: {0}", urlWithoutQueryString); } else - LoggingHelper.LogInformation("UrlTracker HttpModule | No match found in {0}", callingEventName); + LoggingHelper.LogInformation("UrlTracker HttpModule | No match found in {0}", source); } else - LoggingHelper.LogInformation("UrlTracker HttpModule | Response statuscode is not 404, UrlTracker won't do anything"); + LoggingHelper.LogInformation("UrlTracker HttpModule | UrlTracker isn't installed, don't do anything"); + + LoggingHelper.LogInformation("UrlTracker HttpModule | {0} end", source); - LoggingHelper.LogInformation("UrlTracker HttpModule | {0} end", callingEventName); + return false; } static void LoadUrlTrackerMatchesFromDatabase(HttpRequest request, string urlWithoutQueryString, bool urlHasQueryString, string shortestUrl, int rootNodeId, ref string redirectUrl, ref int? redirectHttpCode, ref bool redirectPassThroughQueryString) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 760c153..7c684a2 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -30,7 +30,7 @@ // Build Number // Revision // -[assembly: AssemblyVersion("3.9.*")] +[assembly: AssemblyVersion("3.10.*")] // SQL [assembly: WebResource("InfoCaster.Umbraco.UrlTracker.SQL.MicrosoftSqlServer.create-table-1.sql", "text/plain")] diff --git a/README.md b/README.md index 77d0423..87e97d4 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,10 @@ Set to true to disable tracking not found (404) requests. Set to false to disable appending a port number to redirect URLs ## Changelog ## +* 3.10 [2015/11/17] + * [Improvement] Switched to a ContentFinder for normal URL matching + * [BugFix] NullReferenceException with 7.3 Beta 3 and Multiple root nodes (#90) + * [BugFix] ForcedRedirects cache list didn't get updated when saving an entry * 3.9 [2015/07/09] * [BugFix] Not allowed root "/" in old url ([#79](https://github.com/kipusoep/UrlTracker/issues/79)) * [BugFix] AdvanceView is throwing an exception when having a single domain configured in umbraco ([#86](https://github.com/kipusoep/UrlTracker/pull/86)) diff --git a/Repositories/UrlTrackerRepository.cs b/Repositories/UrlTrackerRepository.cs index 59e5077..25e59b1 100644 --- a/Repositories/UrlTrackerRepository.cs +++ b/Repositories/UrlTrackerRepository.cs @@ -420,8 +420,7 @@ public static void UpdateUrlTrackerEntry(UrlTrackerModel urlTrackerModel) string query = "UPDATE icUrlTracker SET OldUrl = @oldUrl, OldUrlQueryString = @oldUrlQueryString, OldRegex = @oldRegex, RedirectRootNodeId = @redirectRootNodeId, RedirectNodeId = @redirectNodeId, RedirectUrl = @redirectUrl, RedirectHttpCode = @redirectHttpCode, RedirectPassThroughQueryString = @redirectPassThroughQueryString, ForceRedirect = @forceRedirect, Notes = @notes, Is404 = @is404 WHERE Id = @id"; _sqlHelper.ExecuteNonQuery(query, _sqlHelper.CreateStringParameter("oldUrl", urlTrackerModel.OldUrl), _sqlHelper.CreateStringParameter("oldUrlQueryString", urlTrackerModel.OldUrlQueryString), _sqlHelper.CreateStringParameter("oldRegex", urlTrackerModel.OldRegex), _sqlHelper.CreateParameter("redirectRootNodeId", urlTrackerModel.RedirectRootNodeId), _sqlHelper.CreateNullableParameter("redirectNodeId", urlTrackerModel.RedirectNodeId), _sqlHelper.CreateStringParameter("redirectUrl", urlTrackerModel.RedirectUrl), _sqlHelper.CreateParameter("redirectHttpCode", urlTrackerModel.RedirectHttpCode), _sqlHelper.CreateParameter("redirectPassThroughQueryString", urlTrackerModel.RedirectPassThroughQueryString), _sqlHelper.CreateParameter("forceRedirect", urlTrackerModel.ForceRedirect), _sqlHelper.CreateStringParameter("notes", urlTrackerModel.Notes), _sqlHelper.CreateParameter("is404", urlTrackerModel.Is404), _sqlHelper.CreateParameter("id", urlTrackerModel.Id)); - if (urlTrackerModel.ForceRedirect) - ReloadForcedRedirectsCache(); + ReloadForcedRedirectsCache(); } #endregion diff --git a/UI/UrlTrackerInfo.aspx b/UI/UrlTrackerInfo.aspx index 919dcff..fa616f3 100644 --- a/UI/UrlTrackerInfo.aspx +++ b/UI/UrlTrackerInfo.aspx @@ -86,6 +86,14 @@
    +
  • + 3.10 [2015/11/17] +
      +
    • [Improvement][Breaking] Switched to a ContentFinder for normal URL matching
    • +
    • [BugFix] NullReferenceException with 7.3 Beta 3 and Multiple root nodes (#90)
    • +
    • [BugFix] ForcedRedirects cache list didn't get updated when saving an entry
    • +
    +
  • 3.9 [2015/07/09]
      diff --git a/UrlTracker.csproj b/UrlTracker.csproj index 2e6132b..d996340 100644 --- a/UrlTracker.csproj +++ b/UrlTracker.csproj @@ -148,6 +148,7 @@ + diff --git a/UrlTrackerApplicationEventHandler.cs b/UrlTrackerApplicationEventHandler.cs index cd112fa..87d3245 100644 --- a/UrlTrackerApplicationEventHandler.cs +++ b/UrlTrackerApplicationEventHandler.cs @@ -1,4 +1,5 @@ -using InfoCaster.Umbraco.UrlTracker.Extensions; +using InfoCaster.Umbraco.UrlTracker.ContentFinders; +using InfoCaster.Umbraco.UrlTracker.Extensions; using InfoCaster.Umbraco.UrlTracker.Helpers; using InfoCaster.Umbraco.UrlTracker.Models; using InfoCaster.Umbraco.UrlTracker.Repositories; @@ -18,6 +19,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Publishing; using Umbraco.Core.Services; +using Umbraco.Web.Routing; using Umbraco.Web.UI.Pages; namespace InfoCaster.Umbraco.UrlTracker @@ -35,6 +37,13 @@ protected ClientTools ClientTools } } + protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) + { + base.ApplicationStarting(umbracoApplication, applicationContext); + + ContentFinderResolver.Current.InsertTypeBefore(); + } + protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { base.ApplicationStarted(umbracoApplication, applicationContext);