File Coverage

blib/lib/WWW/RobotRules.pm
Criterion Covered Total %
statement 103 128 80.4
branch 58 76 76.3
condition 7 15 46.6
subroutine 12 17 70.5
pod 3 5 60.0
total 183 241 75.9


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