RobotRules.pm

filter: Statementstmt 80.4% 103 / 128 Branchbran 76.3% 58 / 76 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 68.7% 11 / 16 Podpod 60.0% 3 / 5 totaltotal 74.1% 184 / 248 CC 48 SCAR 27.9
CC
48
Cov
77%
parse
27.9
visit
20.0
dump
20.0
CRAP 16.4
↑ Summary RobotRules/DB_File.pm »
1 package WWW::RobotRules;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '6.03';
5 0 ▶sub Version { $VERSION; }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
6  
7 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
10 20 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 ▶ $class = "WWW::RobotRules::InCore" if $class eq "WWW::RobotRules";
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "WWW::RobotRules" 20 0 20
15  
16 20 my $self = bless {}, $class;
17 20 $self->agent($ua);
18 20 $self;
19 }
20  
21 22 ▶sub parse {
Subroutine parse: called CC=27 SCAR=33.3
Pod documented
22 22 my ($self, $robot_txt_uri, $txt, $fresh_until) = @_;
23 22 $robot_txt_uri = URI->new("$robot_txt_uri");
24 22 my $netloc = $robot_txt_uri->host . ":" . $robot_txt_uri->port;
25  
26 22 $self->clear_rules($netloc);
27 22 ▶ $self->fresh_until($netloc, $fresh_until || (time + 365 * 24 * 3600));
Condition66%2/3
$fresh_until || time + 31536000
l!l&&r!l&&!r
1210
MC/DC0%0/2
$fresh_until || time + 31536000
$fresh_until time + 31536000
Truth table66%2/3
A || B
A= $fresh_untilB= time + 31536000
ABresult
1X1
011
000
28  
29 22 my $ua;
30 22 my $is_me = 0; # 1 iff this record is for me
31 22 my $is_anon = 0; # 1 iff this record is for *
32 22 my $seen_disallow = 0; # watch for missing record separators
33 22 my @me_disallowed = (); # rules disallowed for me
34 22 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 $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 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 ▶ next if /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\#/ 33 165 198
46  
47 165 s/\s*\#.*//; # remove comments at end-of-line
48  
49 165 ▶ if (/^\s*$/) { # blank line
Branch: 4 branches
BranchTrueFalseTotal
if (/^\s*$/) { } 37 128 165
elsif (/^\s*User-Agent\s*:\s*(.*)/i) { } 54 74 128
elsif (/^\s*Disallow\s*:\s*(.*)/i) { } 72 2 74
elsif (/\S\s*:/) { } 2 0 2
50 37 ▶ last if $is_me; # That was our record. No need to read the rest.
Branch: 1 branch
BranchTrueFalseTotal
if $is_me 6 31 37
51 31 $is_anon = 0;
52 31 $seen_disallow = 0;
53 }
54 elsif (/^\s*User-Agent\s*:\s*(.*)/i) {
55 54 $ua = $1;
56 54 $ua =~ s/\s+$//;
57  
58 54 ▶ if ($seen_disallow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_disallow) 11 43 54
59  
60 # treat as start of a new record
61 11 $seen_disallow = 0;
62 11 ▶ last if $is_me; # That was our record. No need to read the rest.
Branch: 1 branch
BranchTrueFalseTotal
if $is_me 3 8 11
63 8 $is_anon = 0;
64 }
65  
66 51 ▶ if ($is_me) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_me) { } 0 51 51
elsif ($ua eq '*') { } 15 36 51
elsif ($self->is_me($ua)) { } 13 23 36
67  
68 # This record already had a User-agent that
69 # we matched, so just continue.
70 }
71 elsif ($ua eq '*') {
72 15 $is_anon = 1;
73 }
74 elsif ($self->is_me($ua)) {
75 13 $is_me = 1;
76 }
77 }
78 elsif (/^\s*Disallow\s*:\s*(.*)/i) {
79 72 ▶ unless (defined $ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 0 72 72
80 0 ▶ warn
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
81 "RobotRules <$robot_txt_uri>: Disallow without preceding User-agent\n"
82 if $^W;
83 0 $is_anon = 1; # assume that User-agent: * was intended
84 }
85 72 my $disallow = $1;
86 72 $disallow =~ s/\s+$//;
87 72 $seen_disallow = 1;
88 72 ▶ if (length $disallow) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $disallow) 65 7 72
89 65 my $ignore;
90 65 eval {
91 65 my $u = URI->new_abs($disallow, $robot_txt_uri);
92 65 ▶ $ignore++ if $u->scheme ne $robot_txt_uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $u->scheme ne $robot_txt_uri->scheme 11 54 65
93 65 ▶ $ignore++ if lc($u->host) ne lc($robot_txt_uri->host);
Branch: 1 branch
BranchTrueFalseTotal
if lc $u->host ne lc $robot_txt_uri->host 7 50 57
94 57 ▶ $ignore++ if $u->port ne $robot_txt_uri->port;
Branch: 1 branch
BranchTrueFalseTotal
if $u->port ne $robot_txt_uri->port 6 51 57
95 57 $disallow = $u->path_query;
96 57 ▶ $disallow = "/" unless length $disallow;
Branch: 1 branch
BranchTrueFalseTotal
unless length $disallow 11 46 57
97 };
98 65 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 8 57 65
99 57 ▶ next if $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 13 44 57
100 }
101  
102 51 ▶ if ($is_me) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_me) { } 13 38 51
elsif ($is_anon) { } 17 21 38
103 13 push(@me_disallowed, $disallow);
104 }
105 elsif ($is_anon) {
106 17 push(@anon_disallowed, $disallow);
107 }
108 }
109 elsif (/\S\s*:/) {
110  
111 # ignore
112 }
113 else {
114 0 ▶ warn "RobotRules <$robot_txt_uri>: Malformed record: <$_>\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
115 }
116 }
117  
118 22 ▶ if ($is_me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_me) { } 13 9 22
119 13 $self->push_rules($netloc, @me_disallowed);
120 }
121 else {
122 9 $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 36 ▶sub is_me {
Subroutine is_me: called CC=2 SCAR=6.9
Pod undocumented
132 36 my ($self, $ua_line) = @_;
133 36 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 ▶ if (index(lc($me), lc($ua_line)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(lc $me, lc $ua_line) != -1) { } 13 23 36
139 13 return 1;
140 }
141 else {
142 23 return '';
143 }
144 }
145  
146  
147 53 ▶sub allowed {
Subroutine allowed: called CC=8 SCAR=20.9
Pod documented
148 53 my ($self, $uri) = @_;
149 53 $uri = URI->new("$uri");
150  
151 53 ▶ return 1 unless $uri->scheme eq 'http' or $uri->scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme eq "http" or $uri->scheme eq "https" 0 53 53
Condition33%1/3
$uri->scheme eq "http" or $uri->scheme eq "https"
l!l&&r!l&&!r
5300
MC/DC0%0/2
$uri->scheme eq "http" or $uri->scheme eq "https"
$uri->scheme eq "http" $uri->scheme eq "https"
Truth table33%1/3
A or B
A= $uri->scheme eq "http"B= $uri->scheme eq "https"
ABresult
1X1
011
000
152  
153 # Robots.txt applies to only those schemes.
154  
155 53 my $netloc = $uri->host . ":" . $uri->port;
156  
157 53 my $fresh_until = $self->fresh_until($netloc);
158 53 ▶ return -1 if !defined($fresh_until) || $fresh_until < time;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fresh_until or $fresh_until < time 5 48 53
Condition100%3/3
not defined $fresh_until or $fresh_until < time
l!l&&r!l&&!r
4148
MC/DC100%2/2
not defined $fresh_until or $fresh_until < time
not defined $fresh_until $fresh_until < time
Truth table100%3/3
not(A) or B
A= not defined $fresh_untilB= $fresh_until < time
ABresult
1X1
011
000
159  
160 48 my $str = $uri->path_query;
161 48 my $rule;
162 48 for $rule ($self->rules($netloc)) {
163 41 ▶ return 1 unless length $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rule 6 35 41
164 35 ▶ return 0 if index($str, $rule) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($str, $rule) == 0 18 17 35
165 }
166 24 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 2 our @ISA = qw(WWW::RobotRules);
185  
186 55 ▶sub agent {
Subroutine agent: called CC=5 SCAR=16.6
187 55 my ($self, $name) = @_;
188 55 my $old = $self->{'ua'};
189 55 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 20 35 55
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 ▶ $name = $1 if $name =~ m/(\S+)/; # get first word
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /(\S+)/ 20 0 20
197 20 $name =~ s!/.*!!; # get rid of version
198 20 ▶ unless ($old && $old eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old and $old eq $name) 20 0 20
Condition33%1/3
$old and $old eq $name
!ll&&!rl&&r
2000
MC/DC0%0/2
$old and $old eq $name
$old $old eq $name
Truth table33%1/3
A and B
A= $oldB= $old eq $name
ABresult
0X0
100
111
199 20 delete $self->{'loc'}; # all old info is now stale
200 20 $self->{'ua'} = $name;
201 }
202 }
203 55 $old;
204 }
205  
206  
207 0 ▶sub visit {
Subroutine visit: not called CC=4 SCAR=30.0
208 0 my ($self, $netloc, $time) = @_;
209 0 ▶ return unless $netloc;
Branch: 1 branch
BranchTrueFalseTotal
unless $netloc 0 0 0
210 0 ▶ $time ||= time;
Condition0%0/2
$time ||= time
l!l
00
MC/DC0%0/1
$time ||= time
$time
Truth table0%0/2
A
A= $time
Aresult
11
00
211 0 $self->{'loc'}{$netloc}{'last'} = $time;
212 0 my $count = \$self->{'loc'}{$netloc}{'count'};
213 0 ▶ if (!defined $$count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$count) { } 0 0 0
214 0 $$count = 1;
215 }
216 else {
217 0 $$count++;
218 }
219 }
220  
221  
222 0 ▶sub no_visits {
Subroutine no_visits: not called CC=1 SCAR=6.9
223 0 my ($self, $netloc) = @_;
224 0 $self->{'loc'}{$netloc}{'count'};
225 }
226  
227  
228 0 ▶sub last_visit {
Subroutine last_visit: not called CC=1 SCAR=6.9
229 0 my ($self, $netloc) = @_;
230 0 $self->{'loc'}{$netloc}{'last'};
231 }
232  
233  
234 70 ▶sub fresh_until {
Subroutine fresh_until: called CC=2 SCAR=6.9
235 70 my ($self, $netloc, $fresh_until) = @_;
236 70 my $old = $self->{'loc'}{$netloc}{'fresh'};
237 70 ▶ if (defined $fresh_until) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fresh_until) 20 50 70
238 20 $self->{'loc'}{$netloc}{'fresh'} = $fresh_until;
239 }
240 70 $old;
241 }
242  
243  
244 20 ▶sub push_rules {
Subroutine push_rules: called CC=1 SCAR=0.0
245 20 my ($self, $netloc, @rules) = @_;
246 20 push(@{$self->{'loc'}{$netloc}{'rules'}}, @rules);
247 }
248  
249  
250 20 ▶sub clear_rules {
Subroutine clear_rules: called CC=1 SCAR=0.0
251 20 my ($self, $netloc) = @_;
252 20 delete $self->{'loc'}{$netloc}{'rules'};
253 }
254  
255  
256 47 ▶sub rules {
Subroutine rules: called CC=2 SCAR=7.4
257 47 my ($self, $netloc) = @_;
258 47 ▶ if (defined $self->{'loc'}{$netloc}{'rules'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'loc'}{$netloc}{'rules'}) { } 47 0 47
259 47 return @{$self->{'loc'}{$netloc}{'rules'}};
260 }
261 else {
262 0 return ();
263 }
264 }
265  
266  
267 0 ▶sub dump {
Subroutine dump: not called CC=4 SCAR=30.0
268 0 my $self = shift;
269 0 for (keys %$self) {
270 0 ▶ next if $_ eq 'loc';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "loc" 0 0 0
271 0 print "$_ = $self->{$_}\n";
272 }
273 0 for (keys %{$self->{'loc'}}) {
274 0 my @rules = $self->rules($_);
275 0 print "$_: ", join("; ", @rules), "\n";
276 }
277 }
278  
279  
280 2 1;
281 __END__
↑ Summary RobotRules/DB_File.pm »