Skip to content

Commit

Permalink
Changed lookup logic, handles unencoded urls better
Browse files Browse the repository at this point in the history
  • Loading branch information
evest committed Oct 23, 2015
1 parent f4fae2d commit 30f4126
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ build/
bld/
[Bb]in/
[Oo]bj/
tmp

# Visual Studo 2015 cache/options directory
.vs/
Expand Down
Binary file added src/.nuget/NuGet.exe
Binary file not shown.
12 changes: 4 additions & 8 deletions src/BVNetwork.404Handler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,10 @@
<SolutionDir Condition="$(SolutionDir) == ''">$(MSBuildProjectDirectory)\..\</SolutionDir>
<NuGetExe>$(SolutionDir)\.nuget\NuGet.exe</NuGetExe>
<TmpOutDir>$(SolutionDir)\tmp</TmpOutDir>
<NuspecFile>$(SolutionDir)\BVNetwork.404Handler.nuspec</NuspecFile>
<ProjectFile>$(SolutionDir)\BVNetwork.404Handler.csproj</ProjectFile>
<NuspecFile>$(SolutionDir)\$(ProjectName).nuspec</NuspecFile>
<ProjectFile>$(SolutionDir)\$(ProjectName).csproj</ProjectFile>
</PropertyGroup>
<Target Name="CreateNugetPackage" AfterTargets="Build" Condition="'$(Configuration)' == 'Release' ">
<PropertyGroup>
<Version>
</Version>
</PropertyGroup>
<!-- Create the Versioned out dir for the client resources-->
<!-- Copy -->
<ItemGroup>
Expand All @@ -365,11 +361,11 @@
<Copy SourceFiles="@(ClientResources)" DestinationFiles="@(ClientResources -> '$(TmpOutDir)\content\ClientResources\%(RecursiveDir)%(Filename)%(Extension)')" />
<Copy SourceFiles="@(Views)" DestinationFiles="@(Views -> '$(TmpOutDir)\content\Views\%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- Create the Zip file -->
<ZipDirectory InputPath="$(TmpOutDir)\content" OutputFileName="$(OutDir)\BVNetwork.404Handler.zip" OverwriteExistingFile="true" />
<ZipDirectory InputPath="$(TmpOutDir)\content" OutputFileName="$(OutDir)\$(ProjectName).zip" OverwriteExistingFile="true" />
<!-- Create the package -->
<PropertyGroup>
<NugetCommand>
"$(NuGetExe)" pack "$(NuspecFile)" -OutputDirectory "$(OutDir.TrimEnd('\\'))" -Version "$(Version)" -Properties Configuration=$(Configuration)
"$(NuGetExe)" pack "$(NuspecFile)" -OutputDirectory "$(OutDir.TrimEnd('\\'))" -Properties Configuration=$(Configuration)
</NugetCommand>
</PropertyGroup>
<Exec Command="$(NugetCommand)" />
Expand Down
9 changes: 7 additions & 2 deletions src/BVNetwork.404Handler.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BVNetwork.404Handler", "BVNetwork.404Handler.csproj", "{DFBF0553-7E44-44BF-A59F-F94F9493FC17}"
EndProject
Expand All @@ -12,6 +12,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{526A4D
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7F2A46E7-8995-4E6F-AB53-3B130ABC4047}"
ProjectSection(SolutionItems) = preProject
..\README.md = ..\README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Custom404Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Custom404Handler
{
public const string NotFoundParam = "404;notfound";

private static readonly List<string> _ignoredResourceExtensions = new List<string> { "jpg", "gif", "png", "css", "js", "ico", "swf" };
private static readonly List<string> _ignoredResourceExtensions = new List<string> { "jpg", "gif", "png", "css", "js", "ico", "swf", "woff" };

private static readonly ILogger Logger = LogManager.GetLogger();

Expand All @@ -28,7 +28,7 @@ public static bool HandleRequest(string referer, Uri urlNotFound, out string new
// to the static list of custom redirects
CustomRedirectHandler fnfHandler = CustomRedirectHandler.Current;
CustomRedirect redirect = fnfHandler.CustomRedirects.Find(urlNotFound);
string pathAndQuery = HttpUtility.HtmlEncode(urlNotFound.PathAndQuery);
string pathAndQuery = urlNotFound.PathAndQuery;
newUrl = null;
if (redirect == null)
{
Expand All @@ -53,6 +53,7 @@ public static bool HandleRequest(string referer, Uri urlNotFound, out string new
// log request to database - if logging is turned on.
if (Configuration.Configuration.Logging == LoggerMode.On)
{
// Safe logging
RequestLogger.Instance.LogRequest(pathAndQuery, referer);
}
}
Expand Down
103 changes: 62 additions & 41 deletions src/Core/CustomRedirects/CustomRedirectCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,54 +58,75 @@ public void Remove(CustomRedirect customRedirect)
// TODO: If desired, change parameters to Find method to search based on a property of CustomRedirect.
public CustomRedirect Find(Uri urlNotFound)
{
string pathAndQuery = HttpUtility.HtmlEncode(urlNotFound.PathAndQuery);
// Handle absolute addresses first
string url = urlNotFound.AbsoluteUri;
CustomRedirect foundRedirect = FindInternal(url);

object foundRedirect = _quickLookupTable[urlNotFound.AbsoluteUri] ?? _quickLookupTable[pathAndQuery];
if (foundRedirect != null)
// Common case
if (foundRedirect == null)
{
return foundRedirect as CustomRedirect;
url = urlNotFound.PathAndQuery; ;
foundRedirect = FindInternal(url);
}
else

// Handle legacy databases with encoded values
if (foundRedirect == null)
{
// No exact match could be done, so we'll check if the 404 url
// starts with one of the urls we're matching against. This
// will be kind of a wild card match (even though we only check
// for the start of the url
// Example: http://www.mysite.com/news/mynews.html is not found
// We have defined an "<old>/news</old>" entry in the config
// file. We will get a match on the /news part of /news/myne...
// Depending on the skip wild card append setting, we will either
// redirect using the <new> url as is, or we'll append the 404
// url to the <new> url.
IDictionaryEnumerator _enumerator = _quickLookupTable.GetEnumerator();
while (_enumerator.MoveNext())
{
// See if this "old" url (the one that cannot be found) starts with one
if (pathAndQuery.StartsWith(_enumerator.Key.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
foundRedirect = _quickLookupTable[_enumerator.Key];
CustomRedirect cr = foundRedirect as CustomRedirect;
if (cr.WildCardSkipAppend == true)
{
// We'll redirect without appending the 404 url
return cr;
}
else
{
// We need to append the 404 to the end of the
// new one. Make a copy of the redir object as we
// are changing it.
CustomRedirect redirCopy = new CustomRedirect(cr);
redirCopy.NewUrl = redirCopy.NewUrl + pathAndQuery.Substring(_enumerator.Key.ToString().Length);
return redirCopy;
}
}
}
url = HttpUtility.HtmlEncode(url);
foundRedirect = FindInternal(url);
}
return null;

return foundRedirect;
}

public CustomRedirect FindInProviders(string oldUrl)
private CustomRedirect FindInternal(string url)
{
object foundRedirect = _quickLookupTable[url];
if (foundRedirect != null)
{
return foundRedirect as CustomRedirect;
}
else
{
// No exact match could be done, so we'll check if the 404 url
// starts with one of the urls we're matching against. This
// will be kind of a wild card match (even though we only check
// for the start of the url
// Example: http://www.mysite.com/news/mynews.html is not found
// We have defined an "<old>/news</old>" entry in the config
// file. We will get a match on the /news part of /news/myne...
// Depending on the skip wild card append setting, we will either
// redirect using the <new> url as is, or we'll append the 404
// url to the <new> url.
IDictionaryEnumerator _enumerator = _quickLookupTable.GetEnumerator();
while (_enumerator.MoveNext())
{
// See if this "old" url (the one that cannot be found) starts with one
if (url.StartsWith(_enumerator.Key.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
foundRedirect = _quickLookupTable[_enumerator.Key];
CustomRedirect cr = foundRedirect as CustomRedirect;
if (cr.WildCardSkipAppend == true)
{
// We'll redirect without appending the 404 url
return cr;
}
else
{
// We need to append the 404 to the end of the
// new one. Make a copy of the redir object as we
// are changing it.
CustomRedirect redirCopy = new CustomRedirect(cr);
redirCopy.NewUrl = redirCopy.NewUrl + url.Substring(_enumerator.Key.ToString().Length);
return redirCopy;
}
}
}
}
return null;
}

public CustomRedirect FindInProviders(string oldUrl)
{
// If no exact or wildcard match is found, try to parse the url through the custom providers
if (Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders != null || Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders.Count != 0)
Expand Down

0 comments on commit 30f4126

Please sign in to comment.