File Coverage

blib/lib/WWW/RobotRules.pm
Criterion Covered Total %
statement 100 125 80.0
branch 58 76 76.3
condition 7 15 46.6
subroutine 11 16 68.7
pod 3 5 60.0
total 179 237 75.5


line stmt bran cond sub pod time code
1             package WWW::RobotRules;
2 2     2   108006 use strict;
  2         6  
  2         115  
3              
4             our $VERSION = '6.03';
5 0     0 0 0 sub Version { $VERSION; }
6              
7 2     2   1257 use URI ();
  2         16939  
  2         3544  
8              
9             sub new {
10 20     20 1 322120 my ($class, $ua) = @_;
11              
12             # This ugly hack is needed to ensure backwards compatibility.
13             # The "WWW::RobotRules" class is now really abstract.
14 20 50       74 $class = "WWW::RobotRules::InCore" if $class eq "WWW::RobotRules";
15              
16 20         55 my $self = bless {}, $class;
17 20         93 $self->agent($ua);
18 20         69 $self;
19             }
20              
21             sub parse {
22 22     22 1 2765 my ($self, $robot_txt_uri, $txt, $fresh_until) = @_;
23 22         121 $robot_txt_uri = URI->new("$robot_txt_uri");
24 22         19333 my $netloc = $robot_txt_uri->host . ":" . $robot_txt_uri->port;
25              
26 22         1361 $self->clear_rules($netloc);
27 22   66     140 $self->fresh_until($netloc, $fresh_until || (time + 365 * 24 * 3600));
28              
29 22         37 my $ua;
30 22         32 my $is_me = 0; # 1 iff this record is for me
31 22         31 my $is_anon = 0; # 1 iff this record is for *
32 22         33 my $seen_disallow = 0; # watch for missing record separators
33 22         36 my @me_disallowed = (); # rules disallowed for me
34 22         30 my @anon_disallowed = (); # rules disallowed for *
35              
36             # blank lines are significant, so turn CRLF into LF to avoid generating
37             # false ones
38 22         55 $txt =~ s/\015\012/\012/g;
39              
40             # split at \012 (LF) or \015 (CR) (Mac text files have just CR for EOL)
41 22         245 for (split(/[\012\015]/, $txt)) {
42              
43             # Lines containing only a comment are discarded completely, and
44             # therefore do not indicate a record boundary.
45 198 100       488 next if /^\s*\#/;
46              
47 165         277 s/\s*\#.*//; # remove comments at end-of-line
48              
49 165 100       895 if (/^\s*$/) { # blank line
    100          
    100          
    50          
50 37 100       74 last if $is_me; # That was our record. No need to read the rest.
51 31         38 $is_anon = 0;
52 31         44 $seen_disallow = 0;
53             }
54             elsif (/^\s*User-Agent\s*:\s*(.*)/i) {
55 54         137 $ua = $1;
56 54         94 $ua =~ s/\s+$//;
57              
58 54 100       110 if ($seen_disallow) {
59              
60             # treat as start of a new record
61 11         16 $seen_disallow = 0;
62 11 100       26 last if $is_me; # That was our record. No need to read the rest.
63 8         10 $is_anon = 0;
64             }
65              
66 51 50       212 if ($is_me) {
    100          
    100          
67              
68             # This record already had a User-agent that
69             # we matched, so just continue.
70             }
71             elsif ($ua eq '*') {
72 15         62 $is_anon = 1;
73             }
74             elsif ($self->is_me($ua)) {
75 13         25 $is_me = 1;
76             }
77             }
78             elsif (/^\s*Disallow\s*:\s*(.*)/i) {
79 72 50       158 unless (defined $ua) {
80 0 0       0 warn
81             "RobotRules <$robot_txt_uri>: Disallow without preceding User-agent\n"
82             if $^W;
83 0         0 $is_anon = 1; # assume that User-agent: * was intended
84             }
85 72         139 my $disallow = $1;
86 72         120 $disallow =~ s/\s+$//;
87 72         90 $seen_disallow = 1;
88 72 100       177 if (length $disallow) {
89 65         81 my $ignore;
90 65         98 eval {
91 65         208 my $u = URI->new_abs($disallow, $robot_txt_uri);
92 65 100       17320 $ignore++ if $u->scheme ne $robot_txt_uri->scheme;
93 65 100       1758 $ignore++ if lc($u->host) ne lc($robot_txt_uri->host);
94 57 100       2650 $ignore++ if $u->port ne $robot_txt_uri->port;
95 57         2458 $disallow = $u->path_query;
96 57 100       641 $disallow = "/" unless length $disallow;
97             };
98 65 100       159 next if $@;
99 57 100       105 next if $ignore;
100             }
101              
102 51 100       126 if ($is_me) {
    100          
103 13         66 push(@me_disallowed, $disallow);
104             }
105             elsif ($is_anon) {
106 17         45 push(@anon_disallowed, $disallow);
107             }
108             }
109             elsif (/\S\s*:/) {
110              
111             # ignore
112             }
113             else {
114 0 0       0 warn "RobotRules <$robot_txt_uri>: Malformed record: <$_>\n" if $^W;
115             }
116             }
117              
118 22 100       73 if ($is_me) {
119 13         39 $self->push_rules($netloc, @me_disallowed);
120             }
121             else {
122 9         33 $self->push_rules($netloc, @anon_disallowed);
123             }
124             }
125              
126              
127             #
128             # Returns TRUE if the given name matches the
129             # name of this robot
130             #
131             sub is_me {
132 36     36 0 73 my ($self, $ua_line) = @_;
133 36         91 my $me = $self->agent;
134              
135             # See whether my short-name is a substring of the
136             # "User-Agent: ..." line that we were passed:
137              
138 36 100       102 if (index(lc($me), lc($ua_line)) >= 0) {
139 13         35 return 1;
140             }
141             else {
142 23         68 return '';
143             }
144             }
145              
146              
147             sub allowed {
148 53     53 1 26083 my ($self, $uri) = @_;
149 53         284 $uri = URI->new("$uri");
150              
151 53 50 33     4876 return 1 unless $uri->scheme eq 'http' or $uri->scheme eq 'https';
152              
153             # Robots.txt applies to only those schemes.
154              
155 53         977 my $netloc = $uri->host . ":" . $uri->port;
156              
157 53         2839 my $fresh_until = $self->fresh_until($netloc);
158 53 100 100     308 return -1 if !defined($fresh_until) || $fresh_until < time;
159              
160 48         120 my $str = $uri->path_query;
161 48         516 my $rule;
162 48         138 for $rule ($self->rules($netloc)) {
163 41 100       109 return 1 unless length $rule;
164 35 100       143 return 0 if index($str, $rule) == 0;
165             }
166 24         77 return 1;
167             }
168              
169              
170             # The following methods must be provided by the subclass.
171             sub agent;
172             sub visit;
173             sub no_visits;
174             sub last_visit;
175             sub fresh_until;
176             sub push_rules;
177             sub clear_rules;
178             sub rules;
179             sub dump;
180              
181              
182             package WWW::RobotRules::InCore;
183              
184             our @ISA = qw(WWW::RobotRules);
185              
186             sub agent {
187 55     55   104 my ($self, $name) = @_;
188 55         113 my $old = $self->{'ua'};
189 55 100       134 if ($name) {
190              
191             # Strip it so that it's just the short name.
192             # I.e., "FooBot" => "FooBot"
193             # "FooBot/1.2" => "FooBot"
194             # "FooBot/1.2 [http://foobot.int; foo@bot.int]" => "FooBot"
195              
196 20 50       146 $name = $1 if $name =~ m/(\S+)/; # get first word
197 20         76 $name =~ s!/.*!!; # get rid of version
198 20 50 33     55 unless ($old && $old eq $name) {
199 20         32 delete $self->{'loc'}; # all old info is now stale
200 20         55 $self->{'ua'} = $name;
201             }
202             }
203 55         108 $old;
204             }
205              
206              
207             sub visit {
208 0     0   0 my ($self, $netloc, $time) = @_;
209 0 0       0 return unless $netloc;
210 0   0     0 $time ||= time;
211 0         0 $self->{'loc'}{$netloc}{'last'} = $time;
212 0         0 my $count = \$self->{'loc'}{$netloc}{'count'};
213 0 0       0 if (!defined $$count) {
214 0         0 $$count = 1;
215             }
216             else {
217 0         0 $$count++;
218             }
219             }
220              
221              
222             sub no_visits {
223 0     0   0 my ($self, $netloc) = @_;
224 0         0 $self->{'loc'}{$netloc}{'count'};
225             }
226              
227              
228             sub last_visit {
229 0     0   0 my ($self, $netloc) = @_;
230 0         0 $self->{'loc'}{$netloc}{'last'};
231             }
232              
233              
234             sub fresh_until {
235 70     70   152 my ($self, $netloc, $fresh_until) = @_;
236 70         167 my $old = $self->{'loc'}{$netloc}{'fresh'};
237 70 100       152 if (defined $fresh_until) {
238 20         47 $self->{'loc'}{$netloc}{'fresh'} = $fresh_until;
239             }
240 70         124 $old;
241             }
242              
243              
244             sub push_rules {
245 20     20   95 my ($self, $netloc, @rules) = @_;
246 20         31 push(@{$self->{'loc'}{$netloc}{'rules'}}, @rules);
  20         136  
247             }
248              
249              
250             sub clear_rules {
251 20     20   40 my ($self, $netloc) = @_;
252 20         81 delete $self->{'loc'}{$netloc}{'rules'};
253             }
254              
255              
256             sub rules {
257 47     47   76 my ($self, $netloc) = @_;
258 47 50       123 if (defined $self->{'loc'}{$netloc}{'rules'}) {
259 47         58 return @{$self->{'loc'}{$netloc}{'rules'}};
  47         154  
260             }
261             else {
262 0           return ();
263             }
264             }
265              
266              
267             sub dump {
268 0     0     my $self = shift;
269 0           for (keys %$self) {
270 0 0         next if $_ eq 'loc';
271 0           print "$_ = $self->{$_}\n";
272             }
273 0           for (keys %{$self->{'loc'}}) {
  0            
274 0           my @rules = $self->rules($_);
275 0           print "$_: ", join("; ", @rules), "\n";
276             }
277             }
278              
279              
280             1;
281             __END__