Skip to content

Commit 30f4126

Browse files
committed
Changed lookup logic, handles unencoded urls better
1 parent f4fae2d commit 30f4126

File tree

6 files changed

+77
-53
lines changed

6 files changed

+77
-53
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ build/
2121
bld/
2222
[Bb]in/
2323
[Oo]bj/
24+
tmp
2425

2526
# Visual Studo 2015 cache/options directory
2627
.vs/

src/.nuget/NuGet.exe

636 KB
Binary file not shown.

src/BVNetwork.404Handler.csproj

+4-8
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,10 @@
347347
<SolutionDir Condition="$(SolutionDir) == ''">$(MSBuildProjectDirectory)\..\</SolutionDir>
348348
<NuGetExe>$(SolutionDir)\.nuget\NuGet.exe</NuGetExe>
349349
<TmpOutDir>$(SolutionDir)\tmp</TmpOutDir>
350-
<NuspecFile>$(SolutionDir)\BVNetwork.404Handler.nuspec</NuspecFile>
351-
<ProjectFile>$(SolutionDir)\BVNetwork.404Handler.csproj</ProjectFile>
350+
<NuspecFile>$(SolutionDir)\$(ProjectName).nuspec</NuspecFile>
351+
<ProjectFile>$(SolutionDir)\$(ProjectName).csproj</ProjectFile>
352352
</PropertyGroup>
353353
<Target Name="CreateNugetPackage" AfterTargets="Build" Condition="'$(Configuration)' == 'Release' ">
354-
<PropertyGroup>
355-
<Version>
356-
</Version>
357-
</PropertyGroup>
358354
<!-- Create the Versioned out dir for the client resources-->
359355
<!-- Copy -->
360356
<ItemGroup>
@@ -365,11 +361,11 @@
365361
<Copy SourceFiles="@(ClientResources)" DestinationFiles="@(ClientResources -> '$(TmpOutDir)\content\ClientResources\%(RecursiveDir)%(Filename)%(Extension)')" />
366362
<Copy SourceFiles="@(Views)" DestinationFiles="@(Views -> '$(TmpOutDir)\content\Views\%(RecursiveDir)%(Filename)%(Extension)')" />
367363
<!-- Create the Zip file -->
368-
<ZipDirectory InputPath="$(TmpOutDir)\content" OutputFileName="$(OutDir)\BVNetwork.404Handler.zip" OverwriteExistingFile="true" />
364+
<ZipDirectory InputPath="$(TmpOutDir)\content" OutputFileName="$(OutDir)\$(ProjectName).zip" OverwriteExistingFile="true" />
369365
<!-- Create the package -->
370366
<PropertyGroup>
371367
<NugetCommand>
372-
"$(NuGetExe)" pack "$(NuspecFile)" -OutputDirectory "$(OutDir.TrimEnd('\\'))" -Version "$(Version)" -Properties Configuration=$(Configuration)
368+
"$(NuGetExe)" pack "$(NuspecFile)" -OutputDirectory "$(OutDir.TrimEnd('\\'))" -Properties Configuration=$(Configuration)
373369
</NugetCommand>
374370
</PropertyGroup>
375371
<Exec Command="$(NugetCommand)" />

src/BVNetwork.404Handler.sln

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.31101.0
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BVNetwork.404Handler", "BVNetwork.404Handler.csproj", "{DFBF0553-7E44-44BF-A59F-F94F9493FC17}"
77
EndProject
@@ -12,6 +12,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{526A4D
1212
.nuget\NuGet.targets = .nuget\NuGet.targets
1313
EndProjectSection
1414
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7F2A46E7-8995-4E6F-AB53-3B130ABC4047}"
16+
ProjectSection(SolutionItems) = preProject
17+
..\README.md = ..\README.md
18+
EndProjectSection
19+
EndProject
1520
Global
1621
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1722
Debug|Any CPU = Debug|Any CPU

src/Core/Custom404Handler.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Custom404Handler
1818
{
1919
public const string NotFoundParam = "404;notfound";
2020

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

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

@@ -28,7 +28,7 @@ public static bool HandleRequest(string referer, Uri urlNotFound, out string new
2828
// to the static list of custom redirects
2929
CustomRedirectHandler fnfHandler = CustomRedirectHandler.Current;
3030
CustomRedirect redirect = fnfHandler.CustomRedirects.Find(urlNotFound);
31-
string pathAndQuery = HttpUtility.HtmlEncode(urlNotFound.PathAndQuery);
31+
string pathAndQuery = urlNotFound.PathAndQuery;
3232
newUrl = null;
3333
if (redirect == null)
3434
{
@@ -53,6 +53,7 @@ public static bool HandleRequest(string referer, Uri urlNotFound, out string new
5353
// log request to database - if logging is turned on.
5454
if (Configuration.Configuration.Logging == LoggerMode.On)
5555
{
56+
// Safe logging
5657
RequestLogger.Instance.LogRequest(pathAndQuery, referer);
5758
}
5859
}

src/Core/CustomRedirects/CustomRedirectCollection.cs

+62-41
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,75 @@ public void Remove(CustomRedirect customRedirect)
5858
// TODO: If desired, change parameters to Find method to search based on a property of CustomRedirect.
5959
public CustomRedirect Find(Uri urlNotFound)
6060
{
61-
string pathAndQuery = HttpUtility.HtmlEncode(urlNotFound.PathAndQuery);
61+
// Handle absolute addresses first
62+
string url = urlNotFound.AbsoluteUri;
63+
CustomRedirect foundRedirect = FindInternal(url);
6264

63-
object foundRedirect = _quickLookupTable[urlNotFound.AbsoluteUri] ?? _quickLookupTable[pathAndQuery];
64-
if (foundRedirect != null)
65+
// Common case
66+
if (foundRedirect == null)
6567
{
66-
return foundRedirect as CustomRedirect;
68+
url = urlNotFound.PathAndQuery; ;
69+
foundRedirect = FindInternal(url);
6770
}
68-
else
71+
72+
// Handle legacy databases with encoded values
73+
if (foundRedirect == null)
6974
{
70-
// No exact match could be done, so we'll check if the 404 url
71-
// starts with one of the urls we're matching against. This
72-
// will be kind of a wild card match (even though we only check
73-
// for the start of the url
74-
// Example: http://www.mysite.com/news/mynews.html is not found
75-
// We have defined an "<old>/news</old>" entry in the config
76-
// file. We will get a match on the /news part of /news/myne...
77-
// Depending on the skip wild card append setting, we will either
78-
// redirect using the <new> url as is, or we'll append the 404
79-
// url to the <new> url.
80-
IDictionaryEnumerator _enumerator = _quickLookupTable.GetEnumerator();
81-
while (_enumerator.MoveNext())
82-
{
83-
// See if this "old" url (the one that cannot be found) starts with one
84-
if (pathAndQuery.StartsWith(_enumerator.Key.ToString(), StringComparison.InvariantCultureIgnoreCase))
85-
{
86-
foundRedirect = _quickLookupTable[_enumerator.Key];
87-
CustomRedirect cr = foundRedirect as CustomRedirect;
88-
if (cr.WildCardSkipAppend == true)
89-
{
90-
// We'll redirect without appending the 404 url
91-
return cr;
92-
}
93-
else
94-
{
95-
// We need to append the 404 to the end of the
96-
// new one. Make a copy of the redir object as we
97-
// are changing it.
98-
CustomRedirect redirCopy = new CustomRedirect(cr);
99-
redirCopy.NewUrl = redirCopy.NewUrl + pathAndQuery.Substring(_enumerator.Key.ToString().Length);
100-
return redirCopy;
101-
}
102-
}
103-
}
75+
url = HttpUtility.HtmlEncode(url);
76+
foundRedirect = FindInternal(url);
10477
}
105-
return null;
78+
79+
return foundRedirect;
10680
}
10781

108-
public CustomRedirect FindInProviders(string oldUrl)
82+
private CustomRedirect FindInternal(string url)
83+
{
84+
object foundRedirect = _quickLookupTable[url];
85+
if (foundRedirect != null)
86+
{
87+
return foundRedirect as CustomRedirect;
88+
}
89+
else
90+
{
91+
// No exact match could be done, so we'll check if the 404 url
92+
// starts with one of the urls we're matching against. This
93+
// will be kind of a wild card match (even though we only check
94+
// for the start of the url
95+
// Example: http://www.mysite.com/news/mynews.html is not found
96+
// We have defined an "<old>/news</old>" entry in the config
97+
// file. We will get a match on the /news part of /news/myne...
98+
// Depending on the skip wild card append setting, we will either
99+
// redirect using the <new> url as is, or we'll append the 404
100+
// url to the <new> url.
101+
IDictionaryEnumerator _enumerator = _quickLookupTable.GetEnumerator();
102+
while (_enumerator.MoveNext())
103+
{
104+
// See if this "old" url (the one that cannot be found) starts with one
105+
if (url.StartsWith(_enumerator.Key.ToString(), StringComparison.InvariantCultureIgnoreCase))
106+
{
107+
foundRedirect = _quickLookupTable[_enumerator.Key];
108+
CustomRedirect cr = foundRedirect as CustomRedirect;
109+
if (cr.WildCardSkipAppend == true)
110+
{
111+
// We'll redirect without appending the 404 url
112+
return cr;
113+
}
114+
else
115+
{
116+
// We need to append the 404 to the end of the
117+
// new one. Make a copy of the redir object as we
118+
// are changing it.
119+
CustomRedirect redirCopy = new CustomRedirect(cr);
120+
redirCopy.NewUrl = redirCopy.NewUrl + url.Substring(_enumerator.Key.ToString().Length);
121+
return redirCopy;
122+
}
123+
}
124+
}
125+
}
126+
return null;
127+
}
128+
129+
public CustomRedirect FindInProviders(string oldUrl)
109130
{
110131
// If no exact or wildcard match is found, try to parse the url through the custom providers
111132
if (Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders != null || Bvn404HandlerConfiguration.Instance.Bvn404HandlerProviders.Count != 0)

0 commit comments

Comments
 (0)