1
|
|
using System;
|
2
|
|
using System.Collections.Generic;
|
3
|
|
using System.Linq;
|
4
|
|
using System.Text;
|
5
|
|
|
6
|
|
namespace TurnerSoftware.RobotsExclusionTools.Helpers
|
7
|
|
{
|
8
|
|
public class PathComparisonUtility
|
9
|
|
{
|
10
|
|
public bool IsAllowed(SiteAccessEntry accessEntry, Uri requestUri)
|
11
|
1
|
{
|
12
|
1
|
var requestPath = requestUri.PathAndQuery;
|
13
|
|
|
14
|
|
//Robots file is always unrestricted
|
15
|
1
|
if (requestUri.LocalPath == "/robots.txt")
|
16
|
1
|
{
|
17
|
1
|
return true;
|
18
|
|
}
|
19
|
|
|
20
|
|
//If no entry is defined, the robot is allowed access by default
|
21
|
1
|
if (accessEntry == null)
|
22
|
1
|
{
|
23
|
1
|
return true;
|
24
|
|
}
|
25
|
|
|
26
|
1
|
foreach (var rule in accessEntry.PathRules)
|
27
|
1
|
{
|
28
|
1
|
if (rule.RuleType == PathRuleType.Disallow && rule.Path == string.Empty)
|
29
|
1
|
{
|
30
|
1
|
return true;
|
31
|
|
}
|
32
|
1
|
else if (PathMatch(rule.Path, requestPath, StringComparison.InvariantCulture))
|
33
|
1
|
{
|
34
|
1
|
return rule.RuleType == PathRuleType.Allow;
|
35
|
|
}
|
36
|
1
|
}
|
37
|
|
|
38
|
1
|
return true;
|
39
|
1
|
}
|
40
|
|
|
41
|
|
public bool PathMatch(string sourceRecord, string uriPath, StringComparison comparison)
|
42
|
1
|
{
|
43
|
1
|
var sourcePieces = sourceRecord.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
|
44
|
1
|
var lastPiece = sourcePieces.LastOrDefault();
|
45
|
1
|
var mustMatchToEnd = false;
|
46
|
1
|
var mustMatchToStart = true;
|
47
|
|
|
48
|
1
|
if (lastPiece.EndsWith("$"))
|
49
|
1
|
{
|
50
|
|
//Remove the last dollar sign from the last piece
|
51
|
1
|
lastPiece = lastPiece.Substring(0, lastPiece.Length - 1);
|
52
|
1
|
sourcePieces[sourcePieces.Length - 1] = lastPiece;
|
53
|
1
|
mustMatchToEnd = true;
|
54
|
1
|
}
|
55
|
|
|
56
|
1
|
if (sourceRecord.StartsWith("*"))
|
57
|
1
|
{
|
58
|
1
|
mustMatchToStart = false;
|
59
|
1
|
}
|
60
|
|
|
61
|
1
|
var offsetPosition = 0;
|
62
|
|
|
63
|
1
|
for (int i = 0, l = sourcePieces.Length; i < l; i++)
|
64
|
1
|
{
|
65
|
1
|
var piece = sourcePieces[i];
|
66
|
1
|
var indexPosition = uriPath.IndexOf(piece, offsetPosition, comparison);
|
67
|
|
|
68
|
1
|
if (mustMatchToStart && offsetPosition == 0 && indexPosition > 0)
|
69
|
1
|
{
|
70
|
1
|
return false;
|
71
|
|
}
|
72
|
|
|
73
|
1
|
if (indexPosition >= offsetPosition)
|
74
|
1
|
{
|
75
|
1
|
offsetPosition = piece.Length;
|
76
|
1
|
}
|
77
|
|
else
|
78
|
1
|
{
|
79
|
1
|
return false;
|
80
|
|
}
|
81
|
|
|
82
|
1
|
}
|
83
|
|
|
84
|
1
|
if (mustMatchToEnd)
|
85
|
1
|
{
|
86
|
1
|
var endOffset = uriPath.Length - lastPiece.Length;
|
87
|
1
|
if (uriPath.IndexOf(lastPiece, endOffset, comparison) == -1)
|
88
|
1
|
{
|
89
|
1
|
return false;
|
90
|
|
}
|
91
|
1
|
}
|
92
|
|
|
93
|
1
|
return true;
|
94
|
1
|
}
|
95
|
|
}
|
96
|
|
}
|