RobotRules.pm

filter: Statementstmt 98.5% 131 / 133 Branchbran 93.7% 45 / 48 Conditioncond 79.4% 31 / 39 MC/DCmcdc 62.5% 15 / 24 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 4 totaltotal 90.2% 250 / 277 CC 38 SCAR 23.6
CC
38
Cov
94%
_rule_matches
24.0
__ANON__
8.0
is_allowed
6.0
CRAP 10.6
« RobotRulesParser.pm ↑ Summary RobotDirective.pm »
1 =head1 NAME
2  
3 CrawlerCommons::RobotRules - the result of a parsed robots.txt
4  
5 =head1 SYNOPSIS
6  
7 use CrawlerCommons::RobotRules;
8 use CrawlerCommons::RobotRulesParser;
9  
10 my $rules_parser = CrawlerCommons::RobotRulesParser->new;
11
12 my $content = "User-agent: *\r\nDisallow: *images";
13 my $content_type = "text/plain";
14 my $robot_names = "any-old-robot";
15 my $url = "http://domain.com/";
16  
17 my $robot_rules =
18 $rules_parser->parse_content($url, $content, $content_type, $robot_names);
19  
20 # obtain the 'mode' of the robot rules object
21 say "Anything Goes!!!!" if $robot_rules->is_allow_all;
22 say "Nothing to see here!" if $robot_rules->is_allow_none;
23 say "Default robot rules mode..." if $robot_rules->is_allow_some;
24  
25 # are we allowed to crawl a URL (returns 1 if so, 0 if not)
26 say "We're allowed to crawl the index :)"
27 if $robot_rules->is_allowed( "https://www.domain.com/index.html");
28  
29 say "Not allowed to crawl: $_" unless $robot_rules->is_allowed( $_ )
30 for ("http://www.domain.com/images/some_file.png",
31 "http://www.domain.com/images/another_file.png");
32  
33 =head1 DESCRIPTION
34  
35 This object is the result of parsing a single robots.txt file
36  
37 =cut
38  
39 ###############################################################################
40 package CrawlerCommons::RobotRules;
41  
42 # MODULE IMPORTS
43 ########################################
44 # Pragmas
45 #------------------#
46 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # CPAN/Core
52 #------------------#
53 2 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # Moose Setup
59 #------------------#
60 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 # Moose Pragmas
64 #------------------#
65 2 with 'MooseX::Log::Log4perl';
66  
67 # Custom Modules
68 #------------------#
69  
70  
71 # VARIABLES/CONSTANTS
72 ########################################
73 # Debug Constants
74 #------------------#
75 2 ▶const my $DEBUG => $ENV{DEBUG} // 0;
Condition50%1/2
$ENV{"DEBUG"} // 0
l!l
02
MC/DC0%0/1
$ENV{"DEBUG"} // 0
$ENV{"DEBUG"}
Truth table50%1/2
A
A= $ENV{"DEBUG"}
Aresult
11
00
76 2 ▶const my $TEST => $ENV{TEST} // 1;
Condition50%1/2
$ENV{"TEST"} // 1
l!l
02
MC/DC0%0/1
$ENV{"TEST"} // 1
$ENV{"TEST"}
Truth table50%1/2
A
A= $ENV{"TEST"}
Aresult
11
00
77  
78 2 const our $ALLOW_ALL => 'allow_all';
79 2 const our $ALLOW_NONE => 'allow_none';
80 2 const our $ALLOW_SOME => 'allow_some';
81 2 const my $ROBOT_RULES_MODES =>
82 ["$ALLOW_ALL", "$ALLOW_NONE", "$ALLOW_SOME"];
83 2 const our $UNSET_CRAWL_DELAY => 0xffffffff * -1;
84  
85 # Constants
86 #------------------#
87  
88 # Variables
89 #------------------#
90 =head1 VERSION
91  
92 Version 0.03
93  
94 =cut
95  
96 2 our $VERSION = '0.03';
97  
98 # setup logging, if not present
99 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
100 2 require Log::Log4perl;
101 2 ▶ Log::Log4perl->easy_init($Log::Log4perl::ERROR)
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Log4perl::Logger::INITIALIZED 1 1 2
102 unless $Log::Log4perl::Logger::INITIALIZED;
103 }
104  
105  
106 # ATTRIBUTES
107 ########################################
108 # Class
109 #------------------#
110 #-----------------------------------------------------------------------------#
111 #-----------------------------------------------------------------------------#
112  
113 # Instance
114 #------------------#
115 #-----------------------------------------------------------------------------#
116 2 has 'crawl_delay' => (
117 default => $UNSET_CRAWL_DELAY,
118 is => 'rw',
119 isa => 'Int',
120 writer => 'set_crawl_delay',
121 );
122 #-----------------------------------------------------------------------------#
123 2 has '_defer_visits' => (
124 default => 0,
125 is => 'rw',
126 isa => 'Bool',
127 traits => ['Bool'],
128 );
129 #-----------------------------------------------------------------------------#
130 2 has '_mode' => (
131 enum => $ROBOT_RULES_MODES,
132 handles => 1,
133 is => 'ro',
134 required => 1,
135 traits => ['Enumeration'],
136 );
137 #-----------------------------------------------------------------------------#
138 has '_rules' => (
139 74 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
140 2 handles => {
141 '_add_rule' => 'push',
142 'clear_rules' => 'clear',
143 '_get_rules' => 'elements',
144 },
145 is => 'ro',
146 isa => 'ArrayRef[CrawlerCommons::RobotRule]',
147 traits => ['Array'],
148 writer => '_set_rules',
149 );
150 #-----------------------------------------------------------------------------#
151 has '_sitemaps' => (
152 74 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
153 2 handles => {
154 _add_sitemap => 'push',
155 get_sitemap => 'get',
156 get_sitemaps => 'elements',
157 sitemaps_size => 'count',
158 },
159 is => 'ro',
160 isa => 'ArrayRef[Str]',
161 traits => ['Array'],
162 );
163 #-----------------------------------------------------------------------------#
164  
165 =head1 METHODS
166  
167 =cut
168  
169 # METHODS
170 ########################################
171 # Constructor
172 #------------------#
173 #-----------------------------------------------------------------------------#
174 #-----------------------------------------------------------------------------#
175  
176 # Class Methods
177 #------------------#
178 #-----------------------------------------------------------------------------#
179 #-----------------------------------------------------------------------------#
180  
181 # Instance Methods
182 #------------------#
183 #-----------------------------------------------------------------------------#
184 254 ▶sub add_rule {
Subroutine add_rule: called CC=3 SCAR=11.4
Pod undocumented
185 254 my ($self, $prefix, $allow) = @_;
186 254 ▶ $allow = 1 if !$allow && length($prefix) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $allow and length $prefix == 0 0 254 254
Condition66%2/3
not $allow and length $prefix == 0
!ll&&!rl&&r
232310
MC/DC0%0/2
not $allow and length $prefix == 0
not $allow length $prefix == 0
Truth table66%2/3
not(A) and B
A= not $allowB= length $prefix == 0
ABresult
0X0
100
111
187 254 $self->_add_rule(
188 CrawlerCommons::RobotRule->new( _prefix => $prefix, _allow => $allow )
189 );
190 }
191 #-----------------------------------------------------------------------------#
192 20 ▶sub add_sitemap {
Subroutine add_sitemap: called CC=1 SCAR=0.0
Pod undocumented
193 20 my ($self, $sitemap) = @_;
194 20 $self->_add_sitemap( $sitemap );
195 }
196 #-----------------------------------------------------------------------------#
197 =head2 C<< my $true_or_false = $robot_rules->is_allowed( $url ) >>
198  
199 Returns 1 if we're allowed to crawl the URL represented by C<$url> and 0
200 otherwise. Will return 1 if the method C<is_allow_all()> returns true,
201 otherwise, if C<is_allow_none> is false, returns 1 if there is an allow rule or
202 no disallow rule for this URL.
203  
204 =over
205  
206 =item * C<$url>
207  
208 The URL whose path is used to search for a matching rule within the object for
209 evaluation.
210  
211 =back
212  
213 =cut
214  
215 286 ▶sub is_allowed {
Subroutine is_allowed: called CC=6 SCAR=17.9
Pod undocumented
216 286 my ($self, $url) = @_;
217 286 ▶ return 0 if $self->is_allow_none;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_allow_none 1 285 286
218 285 ▶ return 1 if $self->is_allow_all;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_allow_all 3 282 285
219 282 my $path_with_query = $self->_get_path( $url, 1);
220  
221 # always allow robots.txt
222 282 ▶ return 1 if $path_with_query eq '/robots.txt';
Branch: 1 branch
BranchTrueFalseTotal
if $path_with_query eq "/robots.txt" 5 277 282
223  
224 277 for my $rule ($self->_get_rules) {
225 733 ▶ return $rule->_allow
Branch: 1 branch
BranchTrueFalseTotal
if $self->_rule_matches($path_with_query, $rule->_prefix) 121 612 733
226 if $self->_rule_matches( $path_with_query, $rule->_prefix );
227 }
228  
229 156 return 1;
230 }
231 #-----------------------------------------------------------------------------#
232 69 ▶sub sort_rules {
Subroutine sort_rules: called CC=2 SCAR=6.9
Pod undocumented
233 69 my $self = shift;
234  
235 $self->_set_rules(
236 748 ▶ [ sort {length( $b->_prefix ) <=> length( $a->_prefix ) ||
Condition100%3/3
length $b->_prefix <=> length $a->_prefix || $b->_allow <=> $a->_allow
l!l&&r!l&&!r
5567185
MC/DC100%2/2
length $b->_prefix <=> length $a->_prefix || $b->_allow <=> $a->_allow
length $b->_prefix <=> length $a->_prefix $b->_allow <=> $a->_allow
Truth table100%3/3
A || B
A= length $b->_prefix <=> length $a->_prefixB= $b->_allow <=> $a->_allow
ABresult
1X1
011
000
237 69 $b->_allow <=> $a->_allow} @{ $self->_rules }
238 ]
239 );
240  
241 }
242 #-----------------------------------------------------------------------------#
243  
244 # Private Methods
245 #------------------#
246 #-----------------------------------------------------------------------------#
247 282 ▶sub _get_path() {
Subroutine _get_path: called CC=1 SCAR=0.5
248 282 my ($self, $url, $with_query) = @_;
249  
250 282 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=20.8
251 282 my $uri = URI->new( $url );
252 282 my $path = $uri->path();
253 282 ▶ my $path_query = $uri->path_query() // '';
Condition50%1/2
$uri->path_query // ""
l!l
2820
MC/DC0%0/1
$uri->path_query // ""
$uri->path_query
Truth table50%1/2
A
A= $uri->path_query
Aresult
11
00
254  
255 282 ▶ $path = $path_query if ($with_query && $path_query ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $with_query and $path_query ne "" 282 0 282
Condition33%1/3
$with_query and $path_query ne ""
!ll&&!rl&&r
00282
MC/DC0%0/2
$with_query and $path_query ne ""
$with_query $path_query ne ""
Truth table33%1/3
A and B
A= $with_queryB= $path_query ne ""
ABresult
0X0
100
111
256  
257 282 ▶ if (not(defined($path)) || $path eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path or $path eq '') { } 0 282 282
Condition33%1/3
not defined $path or $path eq ""
l!l&&r!l&&!r
00282
MC/DC0%0/2
not defined $path or $path eq ""
not defined $path $path eq ""
Truth table33%1/3
not(A) or B
A= not defined $pathB= $path eq ""
ABresult
1X1
011
000
258 0 return '/';
259 }
260 else {
261 282 $path = uri_unescape( $path );
262 282 utf8::encode( $path );
263 282 return $path;
264 }
265 }
266 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 0 return '/';
268 282 };
269 }
270 #-----------------------------------------------------------------------------#
271 733 ▶sub _rule_matches {
Subroutine _rule_matches: called CC=24 SCAR=31.8
272 733 my ($self, $text, $pattern) = @_;
273 733 my $pattern_pos = my $text_pos = 0;
274 733 my $pattern_end = length( $pattern );
275 733 my $text_end = length( $text );
276  
277 733 ▶ my $contains_end_char = $pattern =~ m!\$! ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ /\$/ ? : 6 727 733
278 733 ▶ $pattern_end -= 1 if $contains_end_char;
Branch: 1 branch
BranchTrueFalseTotal
if $contains_end_char 6 727 733
279  
280 733 ▶ while ( ( $pattern_pos < $pattern_end ) && ( $text_pos < $text_end ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern_pos < $pattern_end and $text_pos < $text_end) 781 214 995
Condition100%3/3
$pattern_pos < $pattern_end and $text_pos < $text_end
!ll&&!rl&&r
11698781
MC/DC100%2/2
$pattern_pos < $pattern_end and $text_pos < $text_end
$pattern_pos < $pattern_end $text_pos < $text_end
Truth table100%3/3
A and B
A= $pattern_pos < $pattern_endB= $text_pos < $text_end
ABresult
0X0
100
111
281 781 my $wildcard_pos = index( $pattern, '*', $pattern_pos );
282 781 ▶ $wildcard_pos = $pattern_end if $wildcard_pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $wildcard_pos == -1 693 88 781
283  
284 781 $self->log->trace( <<"DUMP" );
285 # _rule_matches wildcard...
286 ############################
287 pattern $pattern
288 pattern_end $pattern_end
289 wildcard_pos $wildcard_pos
290 DUMP
291  
292 781 ▶ if ( $wildcard_pos == $pattern_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wildcard_pos == $pattern_pos) { } 31 750 781
293 31 $pattern_pos += 1;
294 31 ▶ return 1 if $pattern_pos >= $pattern_end;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern_pos >= $pattern_end 7 24 31
295  
296 24 my $pattern_piece_end = index( $pattern, '*', $pattern_pos);
297 24 ▶ $pattern_piece_end = $pattern_end if $pattern_piece_end == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern_piece_end == -1 21 3 24
298  
299 24 my $matched = 0;
300 24 my $pattern_piece_len = $pattern_piece_end - $pattern_pos;
301 24 ▶ while ( ( $text_pos + $pattern_piece_len <= $text_end )
Branch: 1 branch
BranchTrueFalseTotal
if ($text_pos + $pattern_piece_len <= $text_end and not $matched) 199 24 223
Condition100%3/3
$text_pos + $pattern_piece_len <= $text_end and not $matched
!ll&&!rl&&r
618199
MC/DC100%2/2
$text_pos + $pattern_piece_len <= $text_end and not $matched
$text_pos + $pattern_piece_len <= $text_end not $matched
Truth table100%3/3
A and not(B)
A= $text_pos + $pattern_piece_len <= $text_endB= not $matched
ABresult
0X0
100
111
302 && !$matched ) {
303  
304 199 $matched = 1;
305  
306 199 ▶ for ( my $i = 0; $i < $pattern_piece_len && $matched; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pattern_piece_len and $matched) 261 199 460
Condition100%3/3
$i < $pattern_piece_len and $matched
!ll&&!rl&&r
18181261
MC/DC100%2/2
$i < $pattern_piece_len and $matched
$i < $pattern_piece_len $matched
Truth table100%3/3
A and B
A= $i < $pattern_piece_lenB= $matched
ABresult
0X0
100
111
307 261 ▶ $matched = 0
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $text_pos + $i, 1) ne substr($pattern, $pattern_pos + $i, 1) 181 80 261
308 if substr( $text, $text_pos + $i, 1 ) ne
309 substr( $pattern, $pattern_pos + $i, 1 );
310 }
311  
312 199 ▶ $text_pos += 1 unless $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 181 18 199
313 }
314  
315 24 ▶ return 0 unless $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 6 18 24
316 }
317  
318 else {
319 750 ▶ while ( ( $pattern_pos < $wildcard_pos ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern_pos < $wildcard_pos and $text_pos < $text_end) 1992 244 2236
Condition100%3/3
$pattern_pos < $wildcard_pos and $text_pos < $text_end
!ll&&!rl&&r
148961992
MC/DC100%2/2
$pattern_pos < $wildcard_pos and $text_pos < $text_end
$pattern_pos < $wildcard_pos $text_pos < $text_end
Truth table100%3/3
A and B
A= $pattern_pos < $wildcard_posB= $text_pos < $text_end
ABresult
0X0
100
111
320 ( $text_pos < $text_end ) ) {
321  
322 1992 $self->log->trace( <<"DUMP" );
323 # _rule_matches dump
324 #####################
325 text $text
326 text_pos $text_pos
327 pattern $pattern
328 pattern_pos $pattern_pos
329 DUMP
330 1992 ▶ return 0 if substr( $text, $text_pos++, 1) ne
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $text_pos++, 1) ne substr($pattern, $pattern_pos++, 1) 506 1486 1992
331 substr( $pattern, $pattern_pos++, 1);
332 }
333 }
334 }
335  
336 214 ▶ while ( ( $pattern_pos < $pattern_end ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern_pos < $pattern_end and substr($pattern, $pattern_pos, 1) eq "*") 2 214 216
Condition100%3/3
$pattern_pos < $pattern_end and substr($pattern, $pattern_pos, 1) eq "*"
!ll&&!rl&&r
117972
MC/DC100%2/2
$pattern_pos < $pattern_end and substr($pattern, $pattern_pos, 1) eq "*"
$pattern_pos < $pattern_end substr($pattern, $pattern_pos, 1) eq "*"
Truth table100%3/3
A and B
A= $pattern_pos < $pattern_endB= substr($pattern, $pattern_pos, 1) eq "*"
ABresult
0X0
100
111
337 ( substr( $pattern, $pattern_pos, 1 ) eq '*' ) ) {
338 2 $pattern_pos++;
339 }
340  
341 214 ▶ return ( $pattern_pos == $pattern_end ) &&
Branch: 1 branch
BranchTrueFalseTotal
$pattern_pos == $pattern_end && ($text_pos == $text_end || !$contains_end_char) ? : 114 100 214
Condition100%6/6
$pattern_pos == $pattern_end and $text_pos == $text_end || !$contains_end_char
!ll&&!rl&&r
973114
$text_pos == $text_end or not $contains_end_char
l!l&&r!l&&!r
47673
MC/DC100%3/3
$pattern_pos == $pattern_end and $text_pos == $text_end || !$contains_end_char
$pattern_pos == $pattern_end $text_pos == $text_end not $contains_end_char
Truth table100%4/4
A and B or not(C)
A= $pattern_pos == $pattern_endB= $text_pos == $text_endC= not $contains_end_char
ABCresult
0XX0
1000
11X1
1011
342 ( ( $text_pos == $text_end ) || !$contains_end_char ) ? 1 : 0;
343 }
344 #-----------------------------------------------------------------------------#
345 ###############################################################################
346  
347 2 __PACKAGE__->meta->make_immutable;
348  
349 ###############################################################################
350  
351 =pod
352  
353  
354 =cut
355  
356 ###############################################################################
357 package CrawlerCommons::RobotRule;
358  
359 # MODULE IMPORTS
360 ########################################
361 # Pragmas
362 #------------------#
363 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
364 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
365 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
366 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
367  
368 # CPAN/Core
369 #------------------#
370 2 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
371 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 # Moose Setup
374 #------------------#
375 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
377  
378 # Moose Pragmas
379 #------------------#
380  
381 # Custom Modules
382 #------------------#
383  
384  
385  
386 # VARIABLES/CONSTANTS
387 ########################################
388 # Debug Constants
389 #------------------#
390  
391 # Constants
392 #------------------#
393  
394 # Variables
395 #------------------#
396  
397 # ATTRIBUTES
398 ########################################
399 # Class
400 #------------------#
401 #-----------------------------------------------------------------------------#
402 #-----------------------------------------------------------------------------#
403  
404 # Instance
405 #------------------#
406 #-----------------------------------------------------------------------------#
407 2 has '_allow' => (
408 is => 'ro',
409 isa => 'Bool',
410 required => 1,
411 );
412 #-----------------------------------------------------------------------------#
413 2 has '_prefix' => (
414 is => 'ro',
415 isa => 'Str',
416 );
417 #-----------------------------------------------------------------------------#
418  
419 # METHODS
420 ########################################
421 # Constructor
422 #------------------#
423 #-----------------------------------------------------------------------------#
424 #-----------------------------------------------------------------------------#
425  
426 # Class Methods
427 #------------------#
428 #-----------------------------------------------------------------------------#
429 #-----------------------------------------------------------------------------#
430  
431 # Instance Methods
432 #------------------#
433 #-----------------------------------------------------------------------------#
434 #-----------------------------------------------------------------------------#
435  
436 # Private Methods
437 #------------------#
438 #-----------------------------------------------------------------------------#
439 #-----------------------------------------------------------------------------#
440 ###############################################################################
441  
442 2 __PACKAGE__->meta->make_immutable;
443  
444 ###############################################################################
445  
446 =head1 AUTHOR
447  
448 Adam Robinson <akrobinson74@gmail.com>
449  
450 =cut
451  
452 2 1;
453  
454 __END__
« RobotRulesParser.pm ↑ Summary RobotDirective.pm »