1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5

6
namespace TurnerSoftware.RobotsExclusionTools
7
{
8
	public class RobotsPageDefinition
9
	{
10 1
		public IEnumerable<PageAccessEntry> PageAccessEntries { get; set; } = Enumerable.Empty<PageAccessEntry>();
11
		
12
		public bool CanIndex(string userAgent)
13 1
		{
14 1
			return Can("index", userAgent);
15 1
		}
16
		
17
		public bool CanFollowLinks(string userAgent)
18 1
		{
19 1
			return Can("follow", userAgent);
20 1
		}
21
		
22
		public bool Can(string ruleName, string userAgent)
23 1
		{
24 1
			var negatedRule = "no" + ruleName;
25 1
			var entry = GetEntryForUserAgent(userAgent);
26 1
			if (entry != null)
27 1
			{
28 1
				var items = entry.Rules.ToArray();
29
				
30
				//Going through the array backwards gives priority to more specific values (useragent values over global values)
31 1
				for (var i = items.Length - 1; i >= 0; i--)
32 1
				{
33 1
					var rule = items[i];
34 1
					if (rule.RuleName.Equals("all", StringComparison.InvariantCultureIgnoreCase))
35 1
					{
36 1
						return true;
37
					}
38 1
					else if (rule.RuleName.Equals(negatedRule, StringComparison.InvariantCultureIgnoreCase))
39 1
					{
40 1
						return false;
41
					}
42 1
				}
43 1
			}
44 1
			return true;
45 1
		}
46
		
47
		public PageAccessEntry GetEntryForUserAgent(string userAgent)
48 1
		{
49 1
			PageAccessEntry globalEntry = null;
50

51 1
			foreach (var pageAccessEntry in PageAccessEntries)
52 1
			{
53 1
				if (globalEntry == null && pageAccessEntry.UserAgent == "*")
54 1
				{
55 1
					globalEntry = pageAccessEntry;
56 1
				}
57

58 1
				if (userAgent.IndexOf(pageAccessEntry.UserAgent, StringComparison.InvariantCultureIgnoreCase) != -1)
59 1
				{
60 1
					return pageAccessEntry;
61
				}
62 1
			}
63

64 1
			return globalEntry;
65 1
		}
66
	}
67
}

Read our documentation on viewing source code .

Loading