RobotRulesParser.pm

filter: Statementstmt 97.5% 200 / 205 Branchbran 93.9% 77 / 82 Conditioncond 61.4% 35 / 57 MC/DCmcdc 25.9% 7 / 27 Subroutinesub 90.3% 28 / 31 Podpod 0.0% 0 / 1 totaltotal 86.1% 347 / 403 CC 67 SCAR 28.9
CC
67
Cov
91%
parse_content
39.5
_handle_user_agent
12.0
_tokenize
8.0
CRAP 18.0
↑ Summary RobotRules.pm »
1 =head1 NAME
2  
3 CrawlerCommons::RobotRulesParser - parser for robots.txt files
4  
5 =head1 SYNOPSIS
6  
7 use CrawlerCommons::RobotRulesParser;
8  
9 my $rules_parser = CrawlerCommons::RobotRulesParser->new;
10
11 my $content = "User-agent: *\r\nDisallow: *images";
12 my $content_type = "text/plain";
13 my $robot_names = "any-old-robot";
14 my $url = "http://domain.com/";
15  
16 my $robot_rules =
17 $rules_parser->parse_content($url, $content, $content_type, $robot_names);
18  
19 say "We're allowed to crawl the index :)"
20 if $robot_rules->is_allowed( "https://www.domain.com/index.html");
21  
22 say "Not allowed to crawl: $_" unless $robot_rules->is_allowed( $_ )
23 for ("http://www.domain.com/images/some_file.png",
24 "http://www.domain.com/images/another_file.png");
25  
26 =head1 DESCRIPTION
27  
28 This module is a fairly close reproduction of the Crawler-Commons
29 L<SimpleRobotRulesParser|http://crawler-commons.github.io/crawler-commons/0.7/crawlercommons/robots/SimpleRobotRulesParser.html>
30  
31 From BaseRobotsParser javadoc:
32  
33 Parse the robots.txt file in <i>content</i>, and return rules appropriate
34 for processing paths by <i>userAgent</i>. Note that multiple agent names
35 may be provided as comma-separated values; the order of these shouldn't
36 matter, as the file is parsed in order, and each agent name found in the
37 file will be compared to every agent name found in robotNames.
38 Also note that names are lower-cased before comparison, and that any
39 robot name you pass shouldn't contain commas or spaces; if the name has
40 spaces, it will be split into multiple names, each of which will be
41 compared against agent names in the robots.txt file. An agent name is
42 considered a match if it's a prefix match on the provided robot name. For
43 example, if you pass in "Mozilla Crawlerbot-super 1.0", this would match
44 "crawlerbot" as the agent name, because of splitting on spaces,
45 lower-casing, and the prefix match rule.
46  
47 The method failedFetch is not implemented.
48  
49 =cut
50  
51 ###############################################################################
52 package CrawlerCommons::RobotRulesParser;
53  
54  
55 # MODULE IMPORTS
56 ########################################
57 # Pragmas
58 #------------------#
59 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 # CPAN/Core
65 #------------------#
66 2 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 # Moose Setup
72 #------------------#
73 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # Moose Pragmas
77 #------------------#
78 2 with 'MooseX::Log::Log4perl';
79  
80 # Custom Modules
81 #------------------#
82 2 ▶use CrawlerCommons::RobotDirective;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use CrawlerCommons::ParseState;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use CrawlerCommons::RobotRules;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use CrawlerCommons::RobotToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 # VARIABLES/CONSTANTS
88 ########################################
89 # Constants
90 #------------------#
91 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
92 2 ▶const my $TEST => $ENV{TEST} // 0;
Condition50%1/2
$ENV{"TEST"} // 0
l!l
02
MC/DC0%0/1
$ENV{"TEST"} // 0
$ENV{"TEST"}
Truth table50%1/2
A
A= $ENV{"TEST"}
Aresult
11
00
93  
94 2 const my $BLANK_DIRECTIVE_PATTERN=> qr![ \t]+(.*)!o;
95 2 const my $COLON_DIRECTIVE_PATTERN=> qr![ \t]*:[ \t]*(.*)!o;
96  
97 2 const my $MAX_CRAWL_DELAY => 300000;
98 2 const my $MAX_WARNINGS => 5;
99 2 const my $SIMPLE_HTML_PATTERN => qr!<(?:html|head|body)\s*>!is;
100 2 const my $USER_AGENT_PATTERN => qr!user-agent:!i;
101  
102 # Variables
103 #------------------#
104  
105 # setup
106 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
107 2 require Log::Log4perl;
108 2 ▶ Log::Log4perl->easy_init($Log::Log4perl::ERROR)
Branch: 1 branch
BranchTrueFalseTotal
unless $Log::Log4perl::Logger::INITIALIZED 0 2 2
109 unless $Log::Log4perl::Logger::INITIALIZED;
110 }
111  
112 =head1 VERSION
113  
114 Version 0.03
115  
116 =cut
117  
118 2 our $VERSION = '0.03';
119  
120  
121  
122 # MOOSE ATTRIBUTES
123 ########################################
124 # Class
125 #-----------------------------------------------------------------------------#
126 #-----------------------------------------------------------------------------#
127  
128 # Instance
129 #-----------------------------------------------------------------------------#
130 2 has 'num_warnings' => (
131 default => 0,
132 handles => {
133 increment_warnings => 'inc',
134 },
135 is => 'ro',
136 isa => 'Int',
137 traits => ['Counter']
138 );
139 #-----------------------------------------------------------------------------#
140  
141  
142 =head1 METHODS
143  
144 =cut
145  
146 # METHODS
147 ########################################
148 # Construction
149 #------------------#
150 #-----------------------------------------------------------------------------#
151 #-----------------------------------------------------------------------------#
152  
153 # Class Methods
154 #------------------#
155 #-----------------------------------------------------------------------------#
156 #-----------------------------------------------------------------------------#
157  
158 # Instance Methods
159 #------------------#
160 #-----------------------------------------------------------------------------#
161 =head2 C<< my $robot_rules = $rules_parser->parse_content($url, $content, $content_type, $robot_name) >>
162  
163 Parsers robots.txt data in C<$content> for the User-agent(s) specified in
164 C<$robot_name> returning a C<CrawlerCommons::RobotRules> object corresponding
165 to the rules defined for C<$robot_name>.
166  
167 =over
168  
169 =item * C<$url>
170  
171 URL string that's parsed in a URI object to provide scheme, authority, and path
172 for sitemap directive values. If the directive's value begins with a '/', it
173 overrides the path value provided by this URL context string.
174  
175 =item * C<$content>
176  
177 The text content of the robots.txt file to be parsed.
178  
179 =item * C<$content_type>
180  
181 The content-type of the robots.txt content to be parsed. Assumes text/plain by
182 default. If type is text/html, the parser will attempt to strip-out html tags
183 from the content.
184  
185 =item * C<$robot_name>
186  
187 A string signifying for which user-agent(s) the rules should be extracted.
188  
189 =back
190  
191 =cut
192 73 ▶sub parse_content {
Subroutine parse_content: called CC=37 SCAR=36.8
Pod undocumented
193 73 my ($self, $url, $content, $content_type, $robot_name) = @_;
194  
195 73 ▶ return CrawlerCommons::RobotRules->new(
Branch: 1 branch
BranchTrueFalseTotal
if ($content // "") eq "" 2 71 73
Condition50%1/2
$content // ""
l!l
730
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
196 _mode => $CrawlerCommons::RobotRules::ALLOW_ALL)
197 if ( ($content // '') eq '' );
198  
199 71 my $content_len = length( $content );
200 71 my $offset = 0;
201  
202 # handle UTF-8, UTF-16LE, UTF-16BE content
203 71 ▶ if ( ($content_len >= 3) && (substr($content, 0, 1) eq "\xEF") &&
Branch: 3 branches
BranchTrueFalseTotal
if ($content_len >= 3 and substr($content, 0, 1) eq "\357" and substr($content, 1, 1) eq "\273" and substr($content, 2, 1) eq "\277") { } 1 70 71
elsif ($content_len >= 2 and substr($content, 0, 1) eq "\377" and substr($content, 1, 1) eq "\376") { } 1 69 70
elsif ($content_len >= 2 and substr($content, 0, 1) eq "\376" and substr($content, 1, 1) eq "\377") { } 1 136 137
Condition66%6/9
$content_len >= 3 and substr($content, 0, 1) eq "\357" and substr($content, 1, 1) eq "\273" and substr($content, 2, 1) eq "\277"
!ll&&!rl&&r
7001
$content_len >= 3 and substr($content, 0, 1) eq "\357" and substr($content, 1, 1) eq "\273"
!ll&&!rl&&r
7001
$content_len >= 3 and substr($content, 0, 1) eq "\357"
!ll&&!rl&&r
0701
MC/DC25%1/4
$content_len >= 3 and substr($content, 0, 1) eq "\357" and substr($content, 1, 1) eq "\273" and substr($content, 2, 1) eq "\277"
$content_len >= 3 substr($content, 0, 1) eq "\357" substr($content, 1, 1) eq "\273" substr($content, 2, 1) eq "\277"
Truth table40%2/5
A and B and C and D
A= $content_len >= 3B= substr($content, 0, 1) eq "\357"C= substr($content, 1, 1) eq "\273"D= substr($content, 2, 1) eq "\277"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
204 (substr($content, 1, 1) eq "\xBB") &&
205 (substr($content, 2, 1) eq "\xBF") ) {
206 1 $offset = 3;
207 1 $content_len -= 3;
208 1 $content = substr( $content, 3);
209 1 $content = decode('UTF-8', $content);
210 }
211 ▶ elsif ( ($content_len >= 2) && (substr($content, 0, 1) eq "\xFF") &&
Condition66%4/6
$content_len >= 2 and substr($content, 0, 1) eq "\377" and substr($content, 1, 1) eq "\376"
!ll&&!rl&&r
6901
$content_len >= 2 and substr($content, 0, 1) eq "\377"
!ll&&!rl&&r
0691
MC/DC33%1/3
$content_len >= 2 and substr($content, 0, 1) eq "\377" and substr($content, 1, 1) eq "\376"
$content_len >= 2 substr($content, 0, 1) eq "\377" substr($content, 1, 1) eq "\376"
Truth table50%2/4
A and B and C
A= $content_len >= 2B= substr($content, 0, 1) eq "\377"C= substr($content, 1, 1) eq "\376"
ABCresult
0XX0
10X0
1100
1111
212 (substr($content, 1, 1) eq "\xFE") ) {
213 1 $offset = 2;
214 1 $content_len -= 2;
215 1 $content = substr( $content, 2);
216 1 $content = decode('UTF-16LE', $content);
217 }
218 ▶ elsif ( ($content_len >= 2) && (substr($content, 0, 1) eq "\xFE") &&
Condition66%4/6
$content_len >= 2 and substr($content, 0, 1) eq "\376" and substr($content, 1, 1) eq "\377"
!ll&&!rl&&r
6801
$content_len >= 2 and substr($content, 0, 1) eq "\376"
!ll&&!rl&&r
0681
MC/DC33%1/3
$content_len >= 2 and substr($content, 0, 1) eq "\376" and substr($content, 1, 1) eq "\377"
$content_len >= 2 substr($content, 0, 1) eq "\376" substr($content, 1, 1) eq "\377"
Truth table50%2/4
A and B and C
A= $content_len >= 2B= substr($content, 0, 1) eq "\376"C= substr($content, 1, 1) eq "\377"
ABCresult
0XX0
10X0
1100
1111
219 (substr($content, 1, 1) eq "\xFF") ) {
220 1 $offset = 2;
221 1 $content_len -= 2;
222 1 $content = substr( $content, 2);
223 1 $content = decode('UTF-16BE', $content);
224 }
225  
226 # set flags that trigger the stripping of '<' and '>' from content
227 71 ▶ my $is_html_type = ($content_type // '') ne '' &&
Branch: 1 branch
BranchTrueFalseTotal
($content_type // '') ne '' && lc($content_type // '') =~ m[^text/html] ? : 0 71 71
Condition42%3/7
($content_type // "") ne "" and lc($content_type // "") =~ m[^text/html]
!ll&&!rl&&r
0710
$content_type // ""
l!l
710
$content_type // ""
l!l
710
MC/DC0%0/2
($content_type // "") ne "" and lc($content_type // "") =~ m[^text/html]
$content_type $content_type
Truth table33%1/3
A and B
A= $content_typeB= $content_type
ABresult
0X0
100
111
228 lc( $content_type // '') =~ m!^text/html! ? 1 : 0;
229  
230 71 my $has_html = 0;
231 71 ▶ if ( $is_html_type || ($content // '') =~ $SIMPLE_HTML_PATTERN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_html_type or ($content // "") =~ /$SIMPLE_HTML_PATTERN/) 3 68 71
Condition60%3/5
$is_html_type or ($content // "") =~ /$SIMPLE_HTML_PATTERN/
l!l&&r!l&&!r
0368
$content // ""
l!l
710
MC/DC0%0/2
$is_html_type or ($content // "") =~ /$SIMPLE_HTML_PATTERN/
$is_html_type $content
Truth table33%1/3
A or B
A= $is_html_typeB= $content
ABresult
1X1
011
000
232 3 ▶ if ( ($content // '') !~ $USER_AGENT_PATTERN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not +($content // '') =~ /$USER_AGENT_PATTERN/) { } 1 2 3
Condition50%1/2
$content // ""
l!l
30
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
233 1 $self->log->warn( "Found non-robots.txt HTML file: $url");
234  
235 1 return CrawlerCommons::RobotRules->new(
236 _mode => $CrawlerCommons::RobotRules::ALLOW_ALL);
237 }
238  
239 else {
240 2 ▶ if ( $is_html_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_html_type) { } 0 2 2
241 0 $self->log->info(
242 "HTML content type returned for robots.txt file: $url");
243 }
244 else {
245 2 $self->log->warn("Found HTML in robots.txt file: $url");
246 }
247  
248 2 $has_html = 1;
249 }
250 }
251  
252 70 my $parse_state =
253 CrawlerCommons::ParseState->new(
254 url => $url, target_name => lc($robot_name) );
255  
256 # DEBUG
257 70 $self->log->trace(Data::Dumper->Dump([$parse_state],['parse_state1']));
258  
259 70 for my $line ( split( m!(?:\n|\r|\r\n|\x0085|\x2028|\x2029)!, $content) ) {
260 1835 $self->log->trace("Input Line: [$line]\n");
261  
262 # strip html tags
263 1835 ▶ $line =~ s!<[^>]+>!!g if $has_html;
Branch: 1 branch
BranchTrueFalseTotal
if $has_html 30 1805 1835
264  
265 # trim comments
266 1835 ▶ if (my $hash_idx = index( $line, '#') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash_idx = index($line, "#")) 1799 36 1835
267 1799 ▶ $line = substr($line, 0, $hash_idx ) if $hash_idx >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hash_idx >= 0 13 1786 1799
268 }
269  
270 # trim whitespace
271 1835 $line =~ s!^\s+|\s+$!!;
272 1835 ▶ next if length( $line ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $line == 0 274 1561 1835
273  
274 1561 my $robot_token = $self->_tokenize( $line );
275  
276 1561 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_user_agent) 923 638 1561
277 923 $self->_handle_user_agent( $parse_state, $robot_token );
278 923 next;
279 } if $robot_token->directive->is_user_agent;
280  
281 638 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_disallow) 494 144 638
282 494 $self->_handle_disallow( $parse_state, $robot_token );
283 494 next;
284 } if $robot_token->directive->is_disallow;
285  
286 144 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_allow) 57 87 144
287 57 $self->_handle_allow( $parse_state, $robot_token );
288 57 next;
289 } if $robot_token->directive->is_allow;
290  
291 87 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_crawl_delay) 17 70 87
292 17 $self->_handle_crawl_delay( $parse_state, $robot_token );
293 17 next;
294 } if $robot_token->directive->is_crawl_delay;
295  
296 70 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_sitemap) 19 51 70
297 19 $self->_handle_sitemap( $parse_state, $robot_token );
298 19 next;
299 } if $robot_token->directive->is_sitemap;
300  
301 51 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_http) 1 50 51
302 1 $self->_handle_http( $parse_state, $robot_token );
303 1 next;
304 } if $robot_token->directive->is_http;
305  
306 50 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_missing) 13 37 50
307 13 $self->_report_warning(
308 sprintf(
309 "Unknown line in robots.txt file (size %d): %s",
310 length( $content ),
311 $line
312 ),
313 $url
314 );
315 13 $parse_state->is_finished_agent_fields( 1 );
316 13 next;
317 } if $robot_token->directive->is_missing;
318  
319 37 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_token->directive->is_unknown) 26 11 37
320 26 $self->_report_warning(
321 sprintf(
322 "Unknown directive in robots.txt file: %s",
323 $line
324 ),
325 $url
326 );
327 26 $parse_state->is_finished_agent_fields( 1 );
328 26 next;
329 } if $robot_token->directive->is_unknown;
330 }
331  
332 70 $self->log->trace(Data::Dumper->Dump([$parse_state],['parse_state2']));
333  
334 70 my $robot_rules = $parse_state->current_rules();
335 70 ▶ if ( $robot_rules->crawl_delay > $MAX_CRAWL_DELAY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($robot_rules->crawl_delay > $MAX_CRAWL_DELAY) { } 1 69 70
336 1 return CrawlerCommons::RobotRules->new(
337 _mode => $CrawlerCommons::RobotRules::ALLOW_NONE );
338 }
339 else {
340 69 $robot_rules->sort_rules;
341 69 return $robot_rules;
342 }
343 }
344 #-----------------------------------------------------------------------------#
345  
346 # Private Methods
347 #------------------#
348 #-----------------------------------------------------------------------------#
349 551 ▶sub _handle_allow_or_disallow {
Subroutine _handle_allow_or_disallow: called CC=4 SCAR=13.9
350 551 my ($self, $state, $token, $allow_or_disallow ) = @_;
351  
352 551 $self->log->trace(Data::Dumper->Dump([\@_],['_handle_allow_or_disallow']));
353  
354 551 ▶ return if $state->is_skip_agents;
Branch: 1 branch
BranchTrueFalseTotal
if $state->is_skip_agents 138 413 551
355  
356 413 $state->is_finished_agent_fields( 1 );
357  
358 413 ▶ return unless $state->is_adding_rules;
Branch: 1 branch
BranchTrueFalseTotal
unless $state->is_adding_rules 148 265 413
359  
360 265 ▶ my $path = $token->data // '';
Condition50%1/2
$token->data // ""
l!l
2650
MC/DC0%0/1
$token->data // ""
$token->data
Truth table50%1/2
A
A= $token->data
Aresult
11
00
361 265 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
362 265 $path = uri_unescape( $path );
363 265 utf8::encode( $path );
364 265 ▶ if ( length( $path ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path == 0) { } 11 254 265
365 11 $state->clear_rules;
366 }
367 else {
368 254 $state->add_rule( $path, $allow_or_disallow );
369 }
370 }
371 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
372 0 $self->_report_warning(
373 "Error parsing robot rules - can't decode path: $path\n$_",
374 $state->url
375 );
376 265 };
377 }
378 #-----------------------------------------------------------------------------#
379 57 ▶sub _handle_allow { shift->_handle_allow_or_disallow( @_, 1 ); }
Subroutine _handle_allow: called CC=1 SCAR=0.0
380 #-----------------------------------------------------------------------------#
381 17 ▶sub _handle_crawl_delay {
Subroutine _handle_crawl_delay: called CC=3 SCAR=11.0
382 17 my ($self, $state, $token) = @_;
383  
384 17 $self->log->trace(Data::Dumper->Dump([$state, $token],['state','token']));
385  
386 17 ▶ return if $state->is_skip_agents;
Branch: 1 branch
BranchTrueFalseTotal
if $state->is_skip_agents 5 12 17
387  
388 12 $state->is_finished_agent_fields( 1 );
389  
390 12 ▶ return unless $state->is_adding_rules;
Branch: 1 branch
BranchTrueFalseTotal
unless $state->is_adding_rules 3 9 12
391  
392 9 my $delay = $token->data;
393 9 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
394 9 my $delay_ms = $delay * 1000;
395 9 $state->set_crawl_delay( $delay_ms );
396 }
397 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
398 0 $self->_report_warning(
399 "Error parsing robot rules - can't decode crawl delay: $delay",
400 $state->url
401 );
402 9 };
403 }
404 #-----------------------------------------------------------------------------#
405 494 ▶sub _handle_disallow { shift->_handle_allow_or_disallow( @_, 0 ); }
Subroutine _handle_disallow: called CC=1 SCAR=0.0
406 #-----------------------------------------------------------------------------#
407 1 ▶sub _handle_http {
Subroutine _handle_http: called CC=2 SCAR=7.2
408 1 my ($self, $state, $token) = @_;
409 1 my $url_fragment = $token->data;
410 1 ▶ if ( index( $url_fragment, 'sitemap' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index $url_fragment, 'sitemap') { } 1 0 1
411 1 my $fixed_token = CrawlerCommons::RobotToken->new(
412 data => 'http:' . $url_fragment,
413 directive =>
414 CrawlerCommons::RobotDirective
415 ->get_directive('sitemap'),
416 );
417 1 $self->_handle_sitemap( $state, $fixed_token );
418 }
419 else {
420 0 $self->_report_warning(
421 "Fournd raw non-sitemap URL: http:$url_fragment", $state->url);
422 }
423 }
424 #-----------------------------------------------------------------------------#
425 20 ▶sub _handle_sitemap {
Subroutine _handle_sitemap: called CC=1 SCAR=0.1
426 20 my ($self, $state, $token) = @_;
427 20 my $sitemap = $token->data;
428 20 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.3
429 20 my $sitemap_url = URI->new_abs( $sitemap, URI->new( $state->url ) );
430 20 ▶ my $host = $sitemap_url->host() // '';
Condition50%1/2
$sitemap_url->host // ""
l!l
200
MC/DC0%0/1
$sitemap_url->host // ""
$sitemap_url->host
Truth table50%1/2
A
A= $sitemap_url->host
Aresult
11
00
431  
432 20 $self->log->trace(<<"DUMP");
433 # _handle_sitemap
434 ###################
435 sitemap $sitemap
436 sitemap_url $sitemap_url
437 host $host
438 20 url ${\$state->url}
439 DUMP
440  
441 20 ▶ $state->add_sitemap( $sitemap_url->as_string ) if ( $host ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $host ne "" 20 0 20
442 }
443 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
444 0 $self->_report_warning( "Invalid URL with sitemap directive: $sitemap",
445 $state->url );
446 20 };
447 }
448 #-----------------------------------------------------------------------------#
449 923 ▶sub _handle_user_agent {
Subroutine _handle_user_agent: called CC=12 SCAR=24.9
450 923 my ($self, $state, $token) = @_;
451 923 ▶ if ( $state->is_matched_real_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->is_matched_real_name) 121 802 923
452 121 ▶ $state->is_skip_agents( 1 ) if $state->is_finished_agent_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $state->is_finished_agent_fields 41 80 121
453 121 return;
454 }
455  
456 802 ▶ if ( $state->is_finished_agent_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->is_finished_agent_fields) 62 740 802
457 62 $state->is_finished_agent_fields( 0 );
458 62 $state->is_adding_rules( 0 );
459 }
460  
461 802 for my $target_name ( split(/,/, lc( $state->target_name ) ) ) {
462 806 for my $agent_name ( split( m! |\t|,!, $token->data ) ) {
463 1362 ▶ ( $agent_name = lc( $agent_name // '' ) ) =~ s!^\s+|\s+$!!g;
Condition50%1/2
$agent_name // ""
l!l
13620
MC/DC0%0/1
$agent_name // ""
$agent_name
Truth table50%1/2
A
A= $agent_name
Aresult
11
00
464  
465 1362 ▶ if ( $agent_name eq '*' && !$state->is_matched_wildcard ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($agent_name eq '*' and not $state->is_matched_wildcard) { } 39 1323 1362
elsif ($agent_name ne '') { } 1315 8 1323
Condition100%3/3
$agent_name eq "*" and not $state->is_matched_wildcard
!ll&&!rl&&r
1322139
MC/DC100%2/2
$agent_name eq "*" and not $state->is_matched_wildcard
$agent_name eq "*" not $state->is_matched_wildcard
Truth table100%3/3
A and not(B)
A= $agent_name eq "*"B= not $state->is_matched_wildcard
ABresult
0X0
100
111
466 39 $state->is_matched_wildcard( 1 );
467 39 $state->is_adding_rules( 1 );
468 }
469 elsif ($agent_name ne '') {
470 1315 for my $target_name_split ( split(/ /, $target_name) ) {
471 1340 ▶ if (index( $target_name_split, $agent_name ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($target_name_split, $agent_name) == 0) 33 1307 1340
472 33 $state->is_matched_real_name( 1 );
473 33 $state->is_adding_rules( 1 );
474 33 $state->clear_rules;
475 33 last;
476 }
477 }
478 }
479 }
480 }
481 }
482 #-----------------------------------------------------------------------------#
483 39 ▶sub _report_warning {
Subroutine _report_warning: called CC=3 SCAR=11.0
484 39 my ($self, $msg, $url) = @_;
485 39 $self->increment_warnings;
486  
487 39 my $warning_count = $self->num_warnings;
488 39 ▶ $self->log->warn("Problem processing robots.txt for $url")
Branch: 1 branch
BranchTrueFalseTotal
if $warning_count == 1 21 18 39
489 if $warning_count == 1;
490  
491 39 ▶ $self->log->warn( $msg ) if $warning_count < $MAX_WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
if $warning_count < $MAX_WARNINGS 34 5 39
492 }
493 #-----------------------------------------------------------------------------#
494 1561 ▶sub _tokenize {
Subroutine _tokenize: called CC=8 SCAR=20.8
495 1561 my ($self, $line) = @_;
496  
497 1561 $self->log->trace("Parsing line: [$line]");
498  
499 1561 my $lower_line = lc( $line );
500 1561 my ($directive) = ($lower_line =~ m!^([^:\s]+)!);
501 1561 ▶ $directive //= '';
Condition50%1/2
$directive //= ""
l!l
15610
MC/DC0%0/1
$directive //= ""
$directive
Truth table50%1/2
A
A= $directive
Aresult
11
00
502  
503 1561 ▶ if ( $directive =~ m!^acap\-! ||
Branch: 1 branch
BranchTrueFalseTotal
if ($directive =~ /^acap\-/ or 'CrawlerCommons::RobotDirective'->directive_exists($directive)) { } 1522 39 1561
Condition100%3/3
$directive =~ /^acap\-/ or "CrawlerCommons::RobotDirective"->directive_exists($directive)
l!l&&r!l&&!r
2152039
MC/DC100%2/2
$directive =~ /^acap\-/ or "CrawlerCommons::RobotDirective"->directive_exists($directive)
$directive =~ /^acap\-/ "CrawlerCommons::RobotDirective"->directive_exists($directive)
Truth table100%3/3
A or B
A= $directive =~ /^acap\-/B= "CrawlerCommons::RobotDirective"->directive_exists($directive)
ABresult
1X1
011
000
504 CrawlerCommons::RobotDirective->directive_exists( $directive ) ){
505  
506 1522 my $data_portion = substr($line, length( $directive ));
507 1522 ( my $data ) = ( $data_portion =~ m!$COLON_DIRECTIVE_PATTERN! );
508 1522 ▶ ( $data ) = ( $data_portion =~ m!$BLANK_DIRECTIVE_PATTERN! )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 10 1512 1522
509 unless defined $data;
510 1522 ▶ $data //= '';
Condition50%1/2
$data //= ""
l!l
15220
MC/DC0%0/1
$data //= ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
511 1522 $data =~ s!^\s+|\s+$!!;
512  
513 1522 $self->log->trace(<<"DUMP");
514 # _tokenize dump
515 #################
516 line [$line]
517 directive [$directive]
518 data_portion [$data_portion]
519 data [$data]
520 DUMP
521  
522 1522 ▶ my $robot_directive =
Branch: 1 branch
BranchTrueFalseTotal
$directive =~ /^acap-/i ? : 2 1520 1522
523 CrawlerCommons::RobotDirective->get_directive(
524 $directive =~ m!^acap-!i ? 'acap-' : $directive );
525  
526 1522 return CrawlerCommons::RobotToken->new(
527 data => $data, directive => $robot_directive
528 );
529 }
530 else {
531 39 ▶ my $robot_directive =
Branch: 1 branch
BranchTrueFalseTotal
$lower_line =~ /[ \t]*:[ \t]*(.*)/ ? : 26 13 39
532 CrawlerCommons::RobotDirective->get_directive(
533 $lower_line =~ m![ \t]*:[ \t]*(.*)! ? 'unknown' : 'missing' );
534  
535 39 return CrawlerCommons::RobotToken->new(
536 data => $line, directive => $robot_directive
537 );
538 }
539 }
540 #-----------------------------------------------------------------------------#
541  
542 ###############################################################################
543  
544 2 __PACKAGE__->meta->make_immutable;
545  
546 ###############################################################################
547  
548 =head1 AUTHOR
549  
550 Adam K Robinson <akrobinson74@gmail.com>
551  
552 =head1 COPYRIGHT AND LICENSE
553  
554 This software is copyright (c) 2017 by Adam K Robinson.
555  
556 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
557  
558 =cut
559  
560 2 1;
561  
562 __END__
↑ Summary RobotRules.pm »