RobotRules.pm

filter: Statementstmt 80.9% 106 / 131 Branchbran 76.3% 58 / 76 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 70.5% 12 / 17 Podpod 60.0% 3 / 5 totaltotal 74.0% 188 / 254 CC 48 SCAR 27.8
CC
48
Cov
77%
parse
27.9
visit
20.0
dump
20.0
CRAP 16.1
↑ Summary RobotRules/AnyDBM_File.pm »
1 package WWW::RobotRules;
2  
3 2 $VERSION = "6.02";
4 0 ▶sub Version { $VERSION; }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 sub new {
12 20 ▶ my($class, $ua) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
13  
14 # This ugly hack is needed to ensure backwards compatibility.
15 # The "WWW::RobotRules" class is now really abstract.
16 20 ▶ $class = "WWW::RobotRules::InCore" if $class eq "WWW::RobotRules";
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "WWW::RobotRules" 20 0 20
17  
18 20 my $self = bless { }, $class;
19 20 $self->agent($ua);
20 20 $self;
21 }
22  
23  
24 sub parse {
25 22 ▶ my($self, $robot_txt_uri, $txt, $fresh_until) = @_;
Subroutine parse: called CC=27 SCAR=33.3
Pod documented
26 22 $robot_txt_uri = URI->new("$robot_txt_uri");
27 22 my $netloc = $robot_txt_uri->host . ":" . $robot_txt_uri->port;
28  
29 22 $self->clear_rules($netloc);
30 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
31  
32 22 my $ua;
33 22 my $is_me = 0; # 1 iff this record is for me
34 22 my $is_anon = 0; # 1 iff this record is for *
35 22 my $seen_disallow = 0; # watch for missing record separators
36 22 my @me_disallowed = (); # rules disallowed for me
37 22 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 $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 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 ▶ next if /^\s*\#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\#/ 33 165 198
49  
50 165 s/\s*\#.*//; # remove comments at end-of-line
51  
52 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
53 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
54 31 $is_anon = 0;
55 31 $seen_disallow = 0;
56 }
57 elsif (/^\s*User-Agent\s*:\s*(.*)/i) {
58 54 $ua = $1;
59 54 $ua =~ s/\s+$//;
60  
61 54 ▶ if ($seen_disallow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_disallow) 11 43 54
62 # treat as start of a new record
63 11 $seen_disallow = 0;
64 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
65 8 $is_anon = 0;
66 }
67  
68 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
69 # This record already had a User-agent that
70 # we matched, so just continue.
71 }
72 elsif ($ua eq '*') {
73 15 $is_anon = 1;
74 }
75 elsif($self->is_me($ua)) {
76 13 $is_me = 1;
77 }
78 }
79 elsif (/^\s*Disallow\s*:\s*(.*)/i) {
80 72 ▶ unless (defined $ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 0 72 72
81 0 ▶ warn "RobotRules <$robot_txt_uri>: Disallow without preceding User-agent\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
82 0 $is_anon = 1; # assume that User-agent: * was intended
83 }
84 72 my $disallow = $1;
85 72 $disallow =~ s/\s+$//;
86 72 $seen_disallow = 1;
87 72 ▶ if (length $disallow) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $disallow) 65 7 72
88 65 my $ignore;
89 65 eval {
90 65 my $u = URI->new_abs($disallow, $robot_txt_uri);
91 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
92 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
93 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
94 57 $disallow = $u->path_query;
95 57 ▶ $disallow = "/" unless length $disallow;
Branch: 1 branch
BranchTrueFalseTotal
unless length $disallow 11 46 57
96 };
97 65 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 8 57 65
98 57 ▶ next if $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore 13 44 57
99 }
100  
101 51 ▶ if ($is_me) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_me) { } 13 38 51
elsif ($is_anon) { } 17 21 38
102 13 push(@me_disallowed, $disallow);
103 }
104 elsif ($is_anon) {
105 17 push(@anon_disallowed, $disallow);
106 }
107 }
108 elsif (/\S\s*:/) {
109 # ignore
110 }
111 else {
112 0 ▶ warn "RobotRules <$robot_txt_uri>: Malformed record: <$_>\n" if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
113 }
114 }
115  
116 22 ▶ if ($is_me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_me) { } 13 9 22
117 13 $self->push_rules($netloc, @me_disallowed);
118 }
119 else {
120 9 $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 ▶ my($self, $ua_line) = @_;
Subroutine is_me: called CC=2 SCAR=6.9
Pod undocumented
131 36 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 ▶ if(index(lc($me), lc($ua_line)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(lc $me, lc $ua_line) != -1) { } 13 23 36
137 13 return 1;
138 }
139 else {
140 23 return '';
141 }
142 }
143  
144  
145 sub allowed {
146 53 ▶ my($self, $uri) = @_;
Subroutine allowed: called CC=8 SCAR=20.9
Pod documented
147 53 $uri = URI->new("$uri");
148  
149 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
150 # Robots.txt applies to only those schemes.
151  
152 53 my $netloc = $uri->host . ":" . $uri->port;
153  
154 53 my $fresh_until = $self->fresh_until($netloc);
155 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
156  
157 48 my $str = $uri->path_query;
158 48 my $rule;
159 48 for $rule ($self->rules($netloc)) {
160 41 ▶ return 1 unless length $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rule 6 35 41
161 35 ▶ return 0 if index($str, $rule) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($str, $rule) == 0 18 17 35
162 }
163 24 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 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
183 2 @ISA = qw(WWW::RobotRules);
184  
185  
186  
187 sub agent {
188 55 ▶ my ($self, $name) = @_;
Subroutine agent: called CC=5 SCAR=16.6
189 55 my $old = $self->{'ua'};
190 55 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 20 35 55
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 sub visit {
208 0 ▶ my($self, $netloc, $time) = @_;
Subroutine visit: not called CC=4 SCAR=30.0
209 0 ▶ return unless $netloc;
Branch: 1 branch
BranchTrueFalseTotal
unless $netloc 0 0 0
210 0 ▶ $time ||= time;
Condition0%0/3
$time ||= time
l!l&&r!l&&!r
000
MC/DC0%0/2
$time ||= time
$time time
Truth table0%0/3
A ||= B
A= $timeB= time
ABresult
1X1
011
000
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 sub no_visits {
223 0 ▶ my ($self, $netloc) = @_;
Subroutine no_visits: not called CC=1 SCAR=6.9
224 0 $self->{'loc'}{$netloc}{'count'};
225 }
226  
227  
228 sub last_visit {
229 0 ▶ my ($self, $netloc) = @_;
Subroutine last_visit: not called CC=1 SCAR=6.9
230 0 $self->{'loc'}{$netloc}{'last'};
231 }
232  
233  
234 sub fresh_until {
235 70 ▶ my ($self, $netloc, $fresh_until) = @_;
Subroutine fresh_until: called CC=2 SCAR=6.9
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 sub push_rules {
245 20 ▶ my($self, $netloc, @rules) = @_;
Subroutine push_rules: called CC=1 SCAR=0.0
246 20 push (@{$self->{'loc'}{$netloc}{'rules'}}, @rules);
247 }
248  
249  
250 sub clear_rules {
251 20 ▶ my($self, $netloc) = @_;
Subroutine clear_rules: called CC=1 SCAR=0.0
252 20 delete $self->{'loc'}{$netloc}{'rules'};
253 }
254  
255  
256 sub rules {
257 47 ▶ my($self, $netloc) = @_;
Subroutine rules: called CC=2 SCAR=7.4
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 sub dump
268 {
269 0 ▶ my $self = shift;
Subroutine dump: not called CC=4 SCAR=30.0
270 0 for (keys %$self) {
271 0 ▶ next if $_ eq 'loc';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "loc" 0 0 0
272 0 print "$_ = $self->{$_}\n";
273 }
274 0 for (keys %{$self->{'loc'}}) {
275 0 my @rules = $self->rules($_);
276 0 print "$_: ", join("; ", @rules), "\n";
277 }
278 }
279  
280  
281 2 1;
282  
283 __END__
↑ Summary RobotRules/AnyDBM_File.pm »