Skip to content

Commit afaa1af

Browse files
committed
Null pointer fixed in PathMatch
Fixes #51 Additionally removes a minor allocation source via the use of the PathComparisonUtility
1 parent 7ac4df9 commit afaa1af

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/TurnerSoftware.RobotsExclusionTools/Helpers/PathComparisonUtility.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
53

64
namespace TurnerSoftware.RobotsExclusionTools.Helpers
75
{
8-
public class PathComparisonUtility
6+
public static class PathComparisonUtility
97
{
10-
public bool IsAllowed(SiteAccessEntry accessEntry, Uri requestUri)
8+
public static bool IsAllowed(SiteAccessEntry accessEntry, Uri requestUri)
119
{
1210
var requestPath = requestUri.PathAndQuery;
1311

@@ -38,14 +36,14 @@ public bool IsAllowed(SiteAccessEntry accessEntry, Uri requestUri)
3836
return true;
3937
}
4038

41-
public bool PathMatch(string sourceRecord, string uriPath, StringComparison comparison)
39+
public static bool PathMatch(string sourceRecord, string uriPath, StringComparison comparison)
4240
{
4341
var sourcePieces = sourceRecord.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
4442
var lastPiece = sourcePieces.LastOrDefault();
4543
var mustMatchToEnd = false;
4644
var mustMatchToStart = true;
4745

48-
if (lastPiece.EndsWith("$"))
46+
if (lastPiece != null && lastPiece.EndsWith("$"))
4947
{
5048
//Remove the last dollar sign from the last piece
5149
lastPiece = lastPiece.Substring(0, lastPiece.Length - 1);

src/TurnerSoftware.RobotsExclusionTools/RobotsFile.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public bool IsAllowedAccess(Uri uri, string userAgent)
5353
}
5454

5555
var entry = GetEntryForUserAgent(userAgent);
56-
var pathComparisonUtility = new PathComparisonUtility();
57-
return pathComparisonUtility.IsAllowed(entry, uri);
56+
return PathComparisonUtility.IsAllowed(entry, uri);
5857
}
5958

6059
public SiteAccessEntry GetEntryForUserAgent(string userAgent)

tests/TurnerSoftware.RobotsExclusionTools.Tests/Resources/RobotsFile/Comprehensive-Example.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Disallow: /org/plan.html
3636
User-agent: PathMustEndWith
3737
Disallow: /org/plan.html$
3838
Disallow: /org/planb.html*$
39+
40+
User-agent: OnlyWildcard
41+
Disallow: /org/plan.html
42+
Allow: *
3943

4044
Sitemap: http://www.example.org/sitemap2.xml
4145

tests/TurnerSoftware.RobotsExclusionTools.Tests/RobotsFile/PathWildcardTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,20 @@ public void PathMustEndWith()
9797
Assert.IsFalse(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/planb.html"), userAgent));
9898
Assert.IsFalse(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/planb.html?foo=bar"), userAgent));
9999
}
100+
101+
[TestMethod]
102+
public void OnlyWildcard()
103+
{
104+
var robotsFile = GetRobotsFile("Comprehensive-Example.txt");
105+
var userAgent = "OnlyWildcard";
106+
107+
Assert.IsTrue(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/"), userAgent));
108+
Assert.IsTrue(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/about.html"), userAgent));
109+
Assert.IsFalse(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/plan.html?"), userAgent));
110+
Assert.IsFalse(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/plan.html?foo="), userAgent));
111+
Assert.IsFalse(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/plan.html"), userAgent));
112+
Assert.IsTrue(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/planb.html"), userAgent));
113+
Assert.IsTrue(robotsFile.IsAllowedAccess(new Uri("http://www.example.org/org/planb.html?foo=bar"), userAgent));
114+
}
100115
}
101116
}

0 commit comments

Comments
 (0)