Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Revert "Fixes issue #90"
Browse files Browse the repository at this point in the history
This reverts commit 78db283.
Added UrlTrackerContentFinder
Fixed small cache bug
  • Loading branch information
Stefan Kip committed Nov 17, 2015
1 parent 3c4f5c6 commit fe16835
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
17 changes: 17 additions & 0 deletions 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");
}
}
}
58 changes: 28 additions & 30 deletions Modules/UrlTrackerModule.cs
Expand Up @@ -37,9 +37,8 @@ public class UrlTrackerModule : IHttpModule
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

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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('?');
Expand Down Expand Up @@ -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);
Expand All @@ -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())
Expand Down Expand Up @@ -260,11 +254,11 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa
else
{
// Forced matching (cache)
List<UrlTrackerModel> forcedRedirects = UrlTrackerRepository.GetForcedRedirects().Where(x => !string.IsNullOrEmpty(x.OldRegex)).ToList();
if (forcedRedirects == null || !forcedRedirects.Any())
return;
List<UrlTrackerModel> 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)
Expand Down Expand Up @@ -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))
{
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Expand Up @@ -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")]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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 (<a target="_blank" href="https://github.com/kipusoep/UrlTracker/issues/90">#90</a>)
* [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))
Expand Down
3 changes: 1 addition & 2 deletions Repositories/UrlTrackerRepository.cs
Expand Up @@ -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<int?>("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

Expand Down
8 changes: 8 additions & 0 deletions UI/UrlTrackerInfo.aspx
Expand Up @@ -86,6 +86,14 @@
</div>
<div class="tab-pane" id="changeLog">
<ul>
<li>
3.10 [2015/11/17]
<ul>
<li>[Improvement][Breaking] Switched to a ContentFinder for normal URL matching</li>
<li>[BugFix] NullReferenceException with 7.3 Beta 3 and Multiple root nodes (<a target="_blank" href="https://github.com/kipusoep/UrlTracker/issues/90">#90</a>)</li>
<li>[BugFix] ForcedRedirects cache list didn't get updated when saving an entry</li>
</ul>
</li>
<li>
3.9 [2015/07/09]
<ul>
Expand Down
1 change: 1 addition & 0 deletions UrlTracker.csproj
Expand Up @@ -148,6 +148,7 @@
<EmbeddedResource Include="UI\res\css\info.css" />
</ItemGroup>
<ItemGroup>
<Compile Include="ContentFinders\UrlTrackerContentFinder.cs" />
<Compile Include="Exceptions\DashboardAlreadyInstalledException.cs" />
<Compile Include="Helpers\ContextHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
11 changes: 10 additions & 1 deletion 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;
Expand All @@ -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
Expand All @@ -35,6 +37,13 @@ protected ClientTools ClientTools
}
}

protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);

ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, UrlTrackerContentFinder>();
}

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
Expand Down

0 comments on commit fe16835

Please sign in to comment.