Prog/Aggregator/Spider.pm

filter: Statementstmt 69.5% 258 / 371 Branchbran 45.6% 115 / 252 Conditioncond 34.2% 49 / 143 MC/DCmcdc 7.9% 7 / 88 Subroutinesub 85.7% 30 / 35 Podpod 100% 13 / 13 totaltotal 52.3% 472 / 902 CC 181 SCAR 47.9
CC
181
Cov
55%
_make_request
309.2
_authorize
182.0
init
159.2
CRAP 119.8
« Prog/Aggregator/DBI.pm ↑ Summary Prog/Aggregator.pm »
1 package SWISH::Prog::Aggregator::Spider;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw( SWISH::Prog::Aggregator );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use SWISH::Prog::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use SWISH::Prog::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use SWISH::Prog::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use SWISH::Prog::Aggregator::Spider::UA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use WWW::Sitemap::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #
20 # TODO tests for cookies, non-text urls needing filters
21 #
22 #
23  
24 2 __PACKAGE__->mk_accessors(
25 qw(
26 agent
27 authn_callback
28 credential_timeout
29 credentials
30 delay
31 email
32 file_rules
33 follow_redirects
34 keep_alive
35 link_tags
36 max_depth
37 max_files
38 max_size
39 max_time
40 md5_cache
41 modified_since
42 queue
43 remove_leading_dots
44 same_hosts
45 timeout
46 ua
47 uri_cache
48 use_md5
49 )
50 );
51  
52 #use LWP::Debug qw(+);
53  
54 2 our $VERSION = '0.75';
55  
56 # TODO make these configurable
57 2 my %parser_types = %SWISH::Prog::Utils::ParserTypes;
58 2 my $default_ext = $SWISH::Prog::Utils::ExtRE;
59 2 my $utils = 'SWISH::Prog::Utils';
60  
61 =pod
62  
63 =head1 NAME
64  
65 SWISH::Prog::Aggregator::Spider - web aggregator
66  
67 =head1 SYNOPSIS
68  
69 use SWISH::Prog::Aggregator::Spider;
70 my $spider = SWISH::Prog::Aggregator::Spider->new(
71 indexer => SWISH::Prog::Indexer->new
72 );
73
74 $spider->indexer->start;
75 $spider->crawl( 'http://swish-e.org/' );
76 $spider->indexer->finish;
77  
78 =head1 DESCRIPTION
79  
80 SWISH::Prog::Aggregator::Spider is a web crawler similar to
81 the spider.pl script in the Swish-e 2.4 distribution. Internally,
82 SWISH::Prog::Aggregator::Spider uses LWP::RobotUA to do the hard work.
83 See L<SWISH::Prog::Aggregator::Spider::UA>.
84  
85 =head1 METHODS
86  
87 See L<SWISH::Prog::Aggregator>.
88  
89 =head2 new( I<params> )
90  
91 All I<params> have their own get/set methods too. They include:
92  
93 =over
94  
95 =item agent I<string>
96  
97 Get/set the user-agent string reported by the user agent.
98  
99 =item email I<string>
100  
101 Get/set the email string reported by the user agent.
102  
103 =item use_md5 I<1|0>
104  
105 Flag as to whether each URI's content should be fingerprinted
106 and compared. Useful if the same content is available under multiple
107 URIs and you only want to index it once.
108  
109 =item uri_cache I<cache_object>
110  
111 Get/set the SWISH::Prog::Cache-derived object used to track which URIs have
112 been fetched already.
113  
114 =item md5_cache I<cache_object>
115  
116 If use_md5() is true, this SWISH::Prog::Cache-derived object tracks
117 the URI fingerprints.
118  
119 =item file_rules I<File_Rules_or_ARRAY>
120  
121 Apply L<File::Rules> object in uri_ok(). I<File_Rules_or_ARRAY> should
122 be a L<File::Rules> object or an array of strings suitable to passing
123 to File::Rules->new().
124  
125 =item queue I<queue_object>
126  
127 Get/set the SWISH::Prog::Queue-derived object for tracking which URIs still
128 need to be fetched.
129  
130 =item ua I<lwp_useragent>
131  
132 Get/set the SWISH::Prog::Aggregagor::Spider::UA object.
133  
134 =item max_depth I<n>
135  
136 How many levels of links to follow. B<NOTE:> This value describes the number
137 of links from the first argument passed to I<crawl>.
138  
139 Default is unlimited depth.
140  
141 =item max_time I<n>
142  
143 This optional key will set the max minutes to spider. Spidering
144 for this host will stop after C<max_time> seconds, and move on to the
145 next server, if any. The default is to not limit by time.
146  
147 =item max_files I<n>
148  
149 This optional key sets the max number of files to spider before aborting.
150 The default is to not limit by number of files. This is the number of requests
151 made to the remote server, not the total number of files to index (see C<max_indexed>).
152 This count is displayted at the end of indexing as C<Unique URLs>.
153  
154 This feature can (and perhaps should) be use when spidering a web site where dynamic
155 content may generate unique URLs to prevent run-away spidering.
156  
157 =item max_size I<n>
158  
159 This optional key sets the max size of a file read from the web server.
160 This B<defaults> to 5,000,000 bytes. If the size is exceeded the resource is
161 truncated per LWP::UserAgent.
162  
163 Set max_size to zero for unlimited size.
164  
165 =item modified_since I<date>
166  
167 This optional parameter will skip any URIs that do not report having
168 been modified since I<date>. The C<Last-Modified> HTTP header is used to
169 determine modification time.
170  
171 =item keep_alive I<1|0>
172  
173 This optional parameter will enable keep alive requests. This can dramatically speed
174 up spidering and reduce the load on server being spidered. The default is to not use
175 keep alives, although enabling it will probably be the right thing to do.
176  
177 To get the most out of keep alives, you may want to set up your web server to
178 allow a lot of requests per single connection (i.e MaxKeepAliveRequests on Apache).
179 Apache's default is 100, which should be good.
180  
181 When a connection is not closed the spider does not wait the "delay"
182 time when making the next request. In other words, there is no delay in
183 requesting documents while the connection is open.
184  
185 Note: you must have at least libwww-perl-5.53_90 installed to use this feature.
186  
187 =item delay I<n>
188  
189 Get/set the number of seconds to wait between making requests. Default is
190 5 seconds (a very friendly delay).
191  
192 =item timeout I<n>
193  
194 Get/set the number of seconds to wait before considering the remote
195 server unresponsive. The default is 10.
196  
197 =item authn_callback I<code_ref>
198  
199 CODE reference to fetch username/password credentials when necessary. See also
200 C<credentials>.
201  
202 =item credential_timeout I<n>
203  
204 Number of seconds to wait before skipping manual prompt for username/password.
205  
206 =item credentials I<user:pass>
207  
208 String with C<username>:C<password> pair to be used when prompted by
209 the server.
210  
211 =item follow_redirects I<1|0>
212  
213 By default, 3xx responses from the server will be followed when
214 they are on the same hostname. Set to false (0) to not follow
215 redirects.
216  
217 =item link_tags
218  
219 TODO
220  
221 =item remove_leading_dots I<1|0>
222  
223 Microsoft server hack.
224  
225 =item same_hosts I<array_ref>
226  
227 ARRAY ref of hostnames to be treated as identical to the original
228 host being spidered. By default the spider will not follow
229 links to different hosts.
230  
231 =back
232  
233 =head2 init
234  
235 Initializes a new spider object. Called by new().
236  
237 =cut
238  
239 1 ▶sub init {
Subroutine init: called CC=37 SCAR=50.7
Pod documented
240 1 my $self = shift;
241 1 $self->SUPER::init(@_);
242  
243 # defaults
244 1 ▶ $self->{agent} ||= 'swish-prog-spider http://swish-e.org/';
Condition50%1/2
$self->{"agent"} ||= "swish-prog-spider http://swish-e.org/"
l!l
10
MC/DC0%0/1
$self->{"agent"} ||= "swish-prog-spider http://swish-e.org/"
$self->{"agent"}
Truth table50%1/2
A
A= $self->{"agent"}
Aresult
11
00
245 1 ▶ $self->{email} ||= 'swish@user.failed.to.set.email.invalid';
Condition50%1/2
$self->{"email"} ||= "swish\@user.failed.to.set.email.invalid"
l!l
10
MC/DC0%0/1
$self->{"email"} ||= "swish\@user.failed.to.set.email.invalid"
$self->{"email"}
Truth table50%1/2
A
A= $self->{"email"}
Aresult
11
00
246 1 ▶ $self->{use_cookies} = 1 unless defined $self->{use_cookies};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"use_cookies"} 1 0 1
247 1 ▶ $self->{follow_redirects} = 1 unless defined $self->{follow_redirects};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"follow_redirects"} 1 0 1
248 1 ▶ $self->{max_files} = 0 unless defined $self->{max_files};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_files"} 1 0 1
249 1 ▶ $self->{max_size} = 5_000_000 unless defined $self->{max_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_size"} 0 1 1
250 1 ▶ $self->{max_depth} = undef unless defined( $self->{max_depth} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_depth"} 1 0 1
251 1 ▶ $self->{delay} = 5 unless defined $self->{delay};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"delay"} 0 1 1
252 1 ▶ croak "delay must be expressed in seconds" if $self->{delay} =~ m/\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delay"} =~ /\D/ 0 1 1
253  
254 1 ▶ if ( $self->{modified_since} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"modified_since"}) 1 0 1
255 my $epoch
256 = $self->{modified_since} =~ m/\D/
257 ? str2time( $self->{modified_since} )
258 1 ▶ : $self->{modified_since};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'modified_since'} =~ /\D/ ? : 1 0 1
259  
260 1 ▶ if ( !defined $epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $epoch) 0 1 1
261 0 croak
262 "Invalid datetime in modified_since: $self->{modified_since}";
263 }
264 1 $self->{modified_since} = $epoch;
265 }
266  
267 $self->{credential_timeout} = 30
268 1 ▶ unless exists $self->{credential_timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"credential_timeout"} 1 0 1
269 croak "credential_timeout must be a number"
270 if defined $self->{credential_timeout}
271 1 ▶ and $self->{credential_timeout} =~ m/\D/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"credential_timeout"} and $self->{"credential_timeout"} =~ /\D/ 0 1 1
Condition33%1/3
defined $self->{"credential_timeout"} and $self->{"credential_timeout"} =~ /\D/
!ll&&!rl&&r
010
MC/DC0%0/2
defined $self->{"credential_timeout"} and $self->{"credential_timeout"} =~ /\D/
defined $self->{"credential_timeout"} $self->{"credential_timeout"} =~ /\D/
Truth table33%1/3
A and B
A= defined $self->{"credential_timeout"}B= $self->{"credential_timeout"} =~ /\D/
ABresult
0X0
100
111
272  
273 1 ▶ $self->{queue} ||= SWISH::Prog::Queue->new;
Condition33%1/3
$self->{"queue"} ||= "SWISH::Prog::Queue"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"queue"} ||= "SWISH::Prog::Queue"->new
$self->{"queue"} "SWISH::Prog::Queue"->new
Truth table33%1/3
A ||= B
A= $self->{"queue"}B= "SWISH::Prog::Queue"->new
ABresult
1X1
011
000
274 1 ▶ $self->{uri_cache} ||= SWISH::Prog::Cache->new;
Condition33%1/3
$self->{"uri_cache"} ||= "SWISH::Prog::Cache"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"uri_cache"} ||= "SWISH::Prog::Cache"->new
$self->{"uri_cache"} "SWISH::Prog::Cache"->new
Truth table33%1/3
A ||= B
A= $self->{"uri_cache"}B= "SWISH::Prog::Cache"->new
ABresult
1X1
011
000
275 1 $self->{_auth_cache} = SWISH::Prog::Cache->new; # ALWAYS inmemory cache
276 $self->{ua} ||= SWISH::Prog::Aggregator::Spider::UA->new( $self->{agent},
277 1 ▶ $self->{email}, );
Condition33%1/3
$self->{"ua"} ||= "SWISH::Prog::Aggregator::Spider::UA"->new($self->{"agent"}, $self->{"email"})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"ua"} ||= "SWISH::Prog::Aggregator::Spider::UA"->new($self->{"agent"}, $self->{"email"})
$self->{"ua"} "SWISH::Prog::Aggregator::Spider::UA"->new($self->{"agent"}, $self->{"email"})
Truth table33%1/3
A ||= B
A= $self->{"ua"}B= "SWISH::Prog::Aggregator::Spider::UA"->new($self->{"agent"}, $self->{"email"})
ABresult
1X1
011
000
278  
279 # whitelist which HTML tags we consider "links"
280 # should be subset of what HTML::LinkExtor considers links
281 $self->{link_tags} = [ 'a', 'frame', 'iframe' ]
282 1 ▶ unless ref $self->{link_tags} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"link_tags"} eq "ARRAY" 1 0 1
283 $self->{ua}
284 1 ->set_link_tags( { map { lc($_) => 1 } @{ $self->{link_tags} } } );
285  
286 1 ▶ $self->{timeout} = 10 unless defined $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"timeout"} 1 0 1
287 1 ▶ croak "timeout must be a number" if $self->{timeout} =~ m/\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} =~ /\D/ 0 1 1
288  
289 # we handle our own delay
290 1 $self->{ua}->delay(0);
291  
292 1 $self->{ua}->timeout( $self->{timeout} );
293  
294 # TODO we test this using HEAD request. Set here too?
295 #$self->{ua}->max_size( $self->{max_size} ) if $self->{max_size};
296  
297 1 ▶ if ( $self->{use_cookies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_cookies"}) 1 0 1
298 1 $self->{ua}->cookie_jar( HTTP::Cookies->new() );
299 }
300 1 ▶ if ( $self->{keep_alive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_alive"}) 0 1 1
301 0 ▶ if ( $self->{ua}->can('conn_cache') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ua'}->can('conn_cache')) { } 0 0 0
302 my $keep_alive
303 = $self->{keep_alive} =~ m/^\d+$/
304 ? $self->{keep_alive}
305 0 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'keep_alive'} =~ /^\d+$/ ? : 0 0 0
306 0 $self->{ua}->conn_cache( { total_capacity => $keep_alive } );
307 }
308 else {
309 warn
310 "can't use keep-alive: conn_cache() method not available on ua "
311 0 . ref( $self->{ua} );
312 }
313 }
314  
315 1 $self->{_current_depth} = 1;
316  
317 1 ▶ $self->{same_hosts} ||= [];
Condition50%1/2
$self->{"same_hosts"} ||= []
l!l
10
MC/DC0%0/1
$self->{"same_hosts"} ||= []
$self->{"same_hosts"}
Truth table50%1/2
A
A= $self->{"same_hosts"}
Aresult
11
00
318 1 $self->{same_host_lookup} = { map { $_ => 1 } @{ $self->{same_hosts} } };
319  
320 1 ▶ if ( $self->{use_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_md5"}) 0 1 1
321 0 ▶ eval "require Digest::MD5" or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Digest::MD5" or croak($@) 0 0 0
322 0 ▶ $self->{md5_cache} ||= SWISH::Prog::Cache->new;
Condition0%0/3
$self->{"md5_cache"} ||= "SWISH::Prog::Cache"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"md5_cache"} ||= "SWISH::Prog::Cache"->new
$self->{"md5_cache"} "SWISH::Prog::Cache"->new
Truth table0%0/3
A ||= B
A= $self->{"md5_cache"}B= "SWISH::Prog::Cache"->new
ABresult
1X1
011
000
323 }
324  
325 # if SWISH::Prog::Config defined, use that for some items
326 1 ▶ if ( $self->{indexer} and $self->config ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"indexer"} and $self->config) 0 1 1
Condition33%1/3
$self->{"indexer"} and $self->config
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"indexer"} and $self->config
$self->{"indexer"} $self->config
Truth table33%1/3
A and B
A= $self->{"indexer"}B= $self->config
ABresult
0X0
100
111
327 0 ▶ if ( $self->config->FileRules && !$self->{file_rules} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->FileRules and not $self->{"file_rules"}) 0 0 0
Condition0%0/3
$self->config->FileRules and not $self->{"file_rules"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->FileRules and not $self->{"file_rules"}
$self->config->FileRules not $self->{"file_rules"}
Truth table0%0/3
A and not(B)
A= $self->config->FileRulesB= not $self->{"file_rules"}
ABresult
0X0
100
111
328 $self->{file_rules}
329 0 = File::Rules->new( $self->config->FileRules );
330 }
331 }
332  
333 # make it an object if it is just an array
334 1 ▶ if ( $self->{file_rules} and !blessed( $self->{file_rules} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file_rules"} and not builtin::blessed($self->{"file_rules"})) 1 0 1
Condition33%1/3
$self->{"file_rules"} and not builtin::blessed($self->{"file_rules"})
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"file_rules"} and not builtin::blessed($self->{"file_rules"})
$self->{"file_rules"} not builtin::blessed($self->{"file_rules"})
Truth table33%1/3
A and not(B)
A= $self->{"file_rules"}B= not builtin::blessed($self->{"file_rules"})
ABresult
0X0
100
111
335 1 $self->{file_rules} = File::Rules->new( $self->{file_rules} );
336 }
337  
338 # from spider.pl. not sure if we need it or not.
339 # Lame Microsoft
340 1 ▶ $URI::ABS_REMOTE_LEADING_DOTS = $self->{remove_leading_dots} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'remove_leading_dots'} ? : 0 1 1
341  
342 1 return $self;
343 }
344  
345 =head2 uri_ok( I<uri> )
346  
347 Returns true if I<uri> is acceptable for including in an index.
348 The 'ok-ness' of the I<uri> is based on its base, robot rules,
349 and the spider configuration.
350  
351 =cut
352  
353 17 ▶sub uri_ok {
Subroutine uri_ok: called CC=33 SCAR=41.7
Pod documented
354 17 my $self = shift;
355 17 ▶ my $uri = shift or croak "URI required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or croak("URI required") 17 0 17
356 17 my $str = $uri->canonical->as_string;
357 17 $str =~ s/#.*//; # target anchors create noise
358  
359 17 ▶ if ( $self->verbose > 1 || $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose > 1 or $self->debug) 0 17 17
Condition33%1/3
$self->verbose > 1 or $self->debug
l!l&&r!l&&!r
0017
MC/DC0%0/2
$self->verbose > 1 or $self->debug
$self->verbose > 1 $self->debug
Truth table33%1/3
A or B
A= $self->verbose > 1B= $self->debug
ABresult
1X1
011
000
360 0 $self->write_log_line();
361 0 $self->write_log(
362 uri => $uri,
363 msg => "checking if ok",
364 );
365 }
366  
367 17 ▶ if ( $uri->scheme !~ m,^http, ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme =~ /^http/) 0 17 17
368 0 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, unsupported scheme") 0 0 0
369 uri => $uri,
370 msg => "skipping, unsupported scheme"
371 );
372 0 return 0;
373 }
374  
375 # check if we're on the same host.
376 17 ▶ if ( $uri->rel( $self->{_base} ) eq $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->rel($self->{"_base"}) eq $uri) 3 14 17
377  
378 # not on this host. check our aliases
379 3 ▶ if ( !exists $self->{same_host_lookup}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"same_host_lookup"}{$uri->canonical->authority or ""}) 2 1 3
Condition50%1/2
$uri->canonical->authority || ''
l!l
30
MC/DC0%0/1
$uri->canonical->authority || ''
$uri->canonical->authority
Truth table50%1/2
A
A= $uri->canonical->authority
Aresult
11
00
380 ->{ $uri->canonical->authority || '' } )
381 {
382 2 my $host = $uri->canonical->authority;
383 2 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, different host $host") 0 2 2
384 and $self->write_log(
385 uri => $uri,
386 msg => "skipping, different host $host",
387 );
388 2 return 0;
389 }
390  
391 # in same host lookup, so proceed.
392 }
393  
394 15 my $path = $uri->path;
395 15 my $mime = $utils->mime_type($path);
396  
397 15 ▶ if ( !exists $parser_types{$mime} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parser_types{$mime}) 0 15 15
398 0 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, no parser for $mime") 0 0 0
399 uri => $uri,
400 msg => "skipping, no parser for $mime",
401 );
402 0 return 0;
403 }
404  
405 # check regex
406 15 ▶ if ( $self->file_rules ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_rules) 15 0 15
407  
408 15 ▶ if ( $self->_apply_file_rules( $uri->path_query, $self->file_rules )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_apply_file_rules($uri->path_query, $self->file_rules) and not $self->_apply_file_match($uri->path_query, $self->file_rules)) 2 13 15
Condition66%2/3
$self->_apply_file_rules($uri->path_query, $self->file_rules) and not $self->_apply_file_match($uri->path_query, $self->file_rules)
!ll&&!rl&&r
1302
MC/DC50%1/2
$self->_apply_file_rules($uri->path_query, $self->file_rules) and not $self->_apply_file_match($uri->path_query, $self->file_rules)
$self->_apply_file_rules($uri->path_query, $self->file_rules) not $self->_apply_file_match($uri->path_query, $self->file_rules)
Truth table66%2/3
A and not(B)
A= $self->_apply_file_rules($uri->path_query, $self->file_rules)B= not $self->_apply_file_match($uri->path_query, $self->file_rules)
ABresult
0X0
100
111
409 && !$self->_apply_file_match( $uri->path_query,
410 $self->file_rules ) )
411 {
412 2 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, matched file_rules") 0 2 2
413 uri => $uri,
414 msg => "skipping, matched file_rules",
415 );
416 2 return 0;
417 }
418 }
419  
420 # head request to check max_size and modified_since
421 13 ▶ if ( $self->max_size or $self->modified_since ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_size or $self->modified_since) 13 0 13
Condition33%1/3
$self->max_size or $self->modified_since
l!l&&r!l&&!r
1300
MC/DC0%0/2
$self->max_size or $self->modified_since
$self->max_size $self->modified_since
Truth table33%1/3
A or B
A= $self->max_sizeB= $self->modified_since
ABresult
1X1
011
000
422 13 my %head_args = (
423 uri => $uri,
424 delay => 0, # assume each get() applies the delay
425 debug => $self->debug,
426 verbose => $self->verbose,
427 );
428  
429 13 ▶ if ( my ( $user, $pass ) = $self->_get_user_pass($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_get_user_pass($uri)) 13 0 13
430 13 $head_args{user} = $user;
431 13 $head_args{pass} = $pass;
432 }
433 13 my $resp = $self->ua->head(%head_args);
434  
435 # early abort if resource doesn't exist
436 13 ▶ if ( $resp->status == 404 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->status == 404) 3 10 13
437 3 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, 404 not found") 0 3 3
438 and $self->write_log(
439 uri => $uri,
440 msg => "skipping, 404 not found",
441 );
442 3 return 0;
443 }
444  
445 # redirect? assume ok now and _make_request will check on it later.
446 10 ▶ if ( $resp->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_redirect) 3 7 10
447 3 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "deferring, is_redirect") 0 3 3
448 and $self->write_log(
449 uri => $uri,
450 msg => "deferring, is_redirect",
451 );
452 3 return 1;
453 }
454  
455 7 my $last_mod = $resp->last_modified;
456 7 ▶ if ( $last_mod
Branch: 1 branch
BranchTrueFalseTotal
if ($last_mod and $self->modified_since and $self->modified_since > $last_mod) 1 6 7
Condition66%4/6
$last_mod and $self->modified_since and $self->modified_since > $last_mod
!ll&&!rl&&r
601
$last_mod and $self->modified_since
!ll&&!rl&&r
601
MC/DC33%1/3
$last_mod and $self->modified_since and $self->modified_since > $last_mod
$last_mod $self->modified_since $self->modified_since > $last_mod
Truth table50%2/4
A and B and C
A= $last_modB= $self->modified_sinceC= $self->modified_since > $last_mod
ABCresult
0XX0
10X0
1100
1111
457 and $self->modified_since
458 and $self->modified_since > $last_mod )
459 {
460 1 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", sprintf("skipping, last modified %s (%s < %s)", $resp->header("last-modified"), $last_mod, $self->modified_since)) 0 1 1
461 and $self->write_log(
462 uri => $uri,
463 msg => sprintf(
464 "skipping, last modified %s (%s < %s)",
465 $resp->header('last-modified'), $last_mod,
466 $self->modified_since
467 ),
468 );
469 1 return 0;
470 }
471  
472 6 ▶ if ( $resp->content_length and $self->max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->content_length and $self->max_size) 1 5 6
Condition66%2/3
$resp->content_length and $self->max_size
!ll&&!rl&&r
501
MC/DC50%1/2
$resp->content_length and $self->max_size
$resp->content_length $self->max_size
Truth table66%2/3
A and B
A= $resp->content_lengthB= $self->max_size
ABresult
0X0
100
111
473 1 ▶ if ( $resp->content_length > $self->max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->content_length > $self->max_size) 1 0 1
474 1 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", sprintf("skipping, %s > max_size", $resp->content_length)) 0 1 1
475 and $self->write_log(
476 uri => $uri,
477 msg => sprintf( "skipping, %s > max_size",
478 $resp->content_length ),
479 );
480 1 return 0;
481 }
482 }
483  
484 }
485  
486 5 ▶ ( $self->verbose > 1 || $self->debug ) and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->verbose > 1 || $self->debug and $self->write_log("uri", $uri, "msg", "ok") 0 5 5
Condition33%1/3
$self->verbose > 1 or $self->debug
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->verbose > 1 or $self->debug
$self->verbose > 1 $self->debug
Truth table33%1/3
A or B
A= $self->verbose > 1B= $self->debug
ABresult
1X1
011
000
487 uri => $uri,
488 msg => "ok",
489 );
490 5 return 1;
491 }
492  
493 9 ▶sub _add_links {
Subroutine _add_links: called CC=10 SCAR=23.7
494 9 my ( $self, $parent, @links ) = @_;
495  
496 # calc depth
497 9 ▶ if ( !$self->{_parent} || $self->{_parent} ne $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_parent"} or $self->{"_parent"} ne $parent) 9 0 9
Condition66%2/3
not $self->{"_parent"} or $self->{"_parent"} ne $parent
l!l&&r!l&&!r
180
MC/DC0%0/2
not $self->{"_parent"} or $self->{"_parent"} ne $parent
not $self->{"_parent"} $self->{"_parent"} ne $parent
Truth table66%2/3
not(A) or B
A= not $self->{"_parent"}B= $self->{"_parent"} ne $parent
ABresult
1X1
011
000
498 9 $self->{_current_depth}++;
499 }
500  
501 9 ▶ $self->{_parent} ||= $parent; # first time.
Condition66%2/3
$self->{"_parent"} ||= $parent
l!l&&r!l&&!r
810
MC/DC0%0/2
$self->{"_parent"} ||= $parent
$self->{"_parent"} $parent
Truth table66%2/3
A ||= B
A= $self->{"_parent"}B= $parent
ABresult
1X1
011
000
502  
503 9 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $parent, "msg", sprintf('evaluating %s links', scalar @links)) 0 9 9
504 uri => $parent,
505 msg => sprintf( 'evaluating %s links', scalar(@links) ),
506 );
507  
508 9 for my $l (@links) {
509 19 ▶ my $uri = $l->abs( $self->{_base} ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $l->abs($self->{"_base"}) or next 19 0 19
510 19 $uri = $uri->canonical; # normalize
511 19 ▶ if ( $self->uri_cache->has("$uri") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri_cache->has("$uri")) 2 17 19
512 2 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "skipping, already checked") 0 2 2
513 uri => $uri,
514 msg => "skipping, already checked",
515 );
516 2 next;
517 }
518 17 $self->uri_cache->add( "$uri" => $self->{_current_depth} );
519  
520 17 ▶ if ( $self->uri_ok($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri_ok($uri)) 8 9 17
521 8 $self->add_to_queue($uri);
522 }
523 }
524 }
525  
526 # ported from spider.pl
527 # Do we need to authorize? If so, ask for password and request again.
528 # First we try using any cached value
529 # Then we try using the get_password callback
530 # Then we ask.
531  
532 0 ▶sub _authorize {
Subroutine _authorize: not called CC=13 SCAR=52.0
533 0 my ( $self, $uri, $response ) = @_;
534  
535 0 delete $self->{last_auth}; # since we know that doesn't work
536  
537 0 ▶ if ( $response->header('WWW-Authenticate')
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("WWW-Authenticate") and $response->header("WWW-Authenticate") =~ /realm="([^"]+)"/i) 0 0 0
Condition0%0/3
$response->header("WWW-Authenticate") and $response->header("WWW-Authenticate") =~ /realm="([^"]+)"/i
!ll&&!rl&&r
000
MC/DC0%0/2
$response->header("WWW-Authenticate") and $response->header("WWW-Authenticate") =~ /realm="([^"]+)"/i
$response->header("WWW-Authenticate") $response->header("WWW-Authenticate") =~ /realm="([^"]+)"/i
Truth table0%0/3
A and B
A= $response->header("WWW-Authenticate")B= $response->header("WWW-Authenticate") =~ /realm="([^"]+)"/i
ABresult
0X0
100
111
538 && $response->header('WWW-Authenticate') =~ /realm="([^"]+)"/i )
539 {
540 0 my $realm = $1;
541 0 my $user_pass;
542  
543 # Do we have a cached user/pass for this realm?
544 # only each URI only once
545 0 ▶ unless ( $self->{_request}->{auth}->{$uri}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_request"}{"auth"}{$uri}++) 0 0 0
546 0 my $key = $uri->canonical->host_port . ':' . $realm;
547  
548 0 ▶ if ( $user_pass = $self->{_auth_cache}->get($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_pass = $self->{"_auth_cache"}->get($key)) 0 0 0
549  
550 # If we didn't just try it, try again
551 0 ▶ unless ( $uri->userinfo && $user_pass eq $uri->userinfo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->userinfo and $user_pass eq $uri->userinfo) { } 0 0 0
Condition0%0/3
$uri->userinfo and $user_pass eq $uri->userinfo
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->userinfo and $user_pass eq $uri->userinfo
$uri->userinfo $user_pass eq $uri->userinfo
Truth table0%0/3
A and B
A= $uri->userinfoB= $user_pass eq $uri->userinfo
ABresult
0X0
100
111
552  
553 # add the user/pass to the URI
554 0 $uri->userinfo($user_pass);
555  
556 #warn " >> set userinfo via _auth_cache\n" if $self->debug;
557 0 return 1;
558 }
559 else {
560 # we've tried this before
561 #warn "tried $user_pass before";
562 0 return 0;
563 }
564 }
565 }
566  
567 # now check for a callback password (if $user_pass not set)
568 0 ▶ unless ( $user_pass || $self->{_request}->{auth}->{callback}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_pass or $self->{"_request"}{"auth"}{"callback"}++) 0 0 0
Condition0%0/3
$user_pass or $self->{"_request"}{"auth"}{"callback"}++
l!l&&r!l&&!r
000
MC/DC0%0/2
$user_pass or $self->{"_request"}{"auth"}{"callback"}++
$user_pass $self->{"_request"}{"auth"}{"callback"}++
Truth table0%0/3
A or B
A= $user_passB= $self->{"_request"}{"auth"}{"callback"}++
ABresult
1X1
011
000
569  
570 # Check for a callback function
571 0 ▶ if ( $self->{authn_callback}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"authn_callback"} and ref $self->{"authn_callback"} eq "CODE") 0 0 0
Condition0%0/3
$self->{"authn_callback"} and ref $self->{"authn_callback"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"authn_callback"} and ref $self->{"authn_callback"} eq "CODE"
$self->{"authn_callback"} ref $self->{"authn_callback"} eq "CODE"
Truth table0%0/3
A and B
A= $self->{"authn_callback"}B= ref $self->{"authn_callback"} eq "CODE"
ABresult
0X0
100
111
572 and ref $self->{authn_callback} eq 'CODE' )
573 {
574 $user_pass = $self->{authn_callback}
575 0 ->( $self, $uri, $response, $realm );
576 0 $uri->userinfo($user_pass);
577  
578 #warn " >> set userinfo via authn_callback\n" if $self->debug;
579 0 return 1;
580 }
581 }
582  
583 # otherwise, prompt (over and over)
584 0 ▶ if ( !$user_pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_pass) 0 0 0
585 0 $user_pass = $self->_get_basic_credentials( $uri, $realm );
586 }
587  
588 0 ▶ if ($user_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_pass) 0 0 0
589 0 $uri->userinfo($user_pass);
590 0 $self->{cur_realm} = $realm; # save so we can cache if it's valid
591 0 return 1;
592 }
593 }
594  
595 0 return 0;
596  
597 }
598  
599 # From spider.pl
600 0 ▶sub _get_basic_credentials {
Subroutine _get_basic_credentials: not called CC=11 SCAR=48.8
601 0 my ( $self, $uri, $realm ) = @_;
602  
603 # Exists but undefined means don't ask.
604 return
605 if exists $self->{credential_timeout}
606 0 ▶ && !defined $self->{credential_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"credential_timeout"} and not defined $self->{"credential_timeout"} 0 0 0
Condition0%0/3
exists $self->{"credential_timeout"} and not defined $self->{"credential_timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"credential_timeout"} and not defined $self->{"credential_timeout"}
exists $self->{"credential_timeout"} not defined $self->{"credential_timeout"}
Truth table0%0/3
A and not(B)
A= exists $self->{"credential_timeout"}B= not defined $self->{"credential_timeout"}
ABresult
0X0
100
111
607  
608 0 my $netloc = $uri->canonical->host_port;
609  
610 0 my ( $user, $password );
611  
612 0 eval {
613 0 ▶ local $SIG{ALRM} = sub { die "timed out\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
614  
615 # a zero timeout means don't time out
616 0 ▶ alarm( $self->{credential_timeout} ) unless $^O =~ /Win32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /Win32/i 0 0 0
617  
618 0 ▶ if ( $uri->userinfo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->userinfo) 0 0 0
619 0 print STDERR "\nSorry: invalid username/password\n";
620 0 $uri->userinfo(undef);
621 }
622  
623 0 print STDERR
624 "Need Authentication for $uri at realm '$realm'\n(<Enter> skips)\nUsername: ";
625 0 $user = <STDIN>;
626 0 ▶ chomp($user) if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
627 0 ▶ die "No Username specified\n" unless length $user;
Branch: 1 branch
BranchTrueFalseTotal
unless length $user 0 0 0
628  
629 0 ▶ alarm( $self->{credential_timeout} ) unless $^O =~ /Win32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /Win32/i 0 0 0
630  
631 0 print STDERR "Password: ";
632 0 system("stty -echo");
633 0 $password = <STDIN>;
634 0 system("stty echo");
635 0 print STDERR "\n"; # because we disabled echo
636 0 chomp($password);
637 0 ▶ alarm(0) unless $^O =~ /Win32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /Win32/i 0 0 0
638 };
639  
640 0 ▶ alarm(0) unless $^O =~ /Win32/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $^O =~ /Win32/i 0 0 0
641  
642 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
643  
644 0 return join ':', $user, $password;
645  
646 }
647  
648 =head2 add_to_queue( I<uri> )
649  
650 Add I<uri> to the queue.
651  
652 =cut
653  
654 9 ▶sub add_to_queue {
Subroutine add_to_queue: called CC=2 SCAR=7.1
Pod documented
655 9 my $self = shift;
656 9 ▶ my $uri = shift or croak "uri required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or croak("uri required") 9 0 9
657 9 return $self->queue->put($uri);
658 }
659  
660 =head2 next_from_queue
661  
662 Return next I<uri> from queue.
663  
664 =cut
665  
666 9 ▶sub next_from_queue {
Subroutine next_from_queue: called CC=1 SCAR=0.0
Pod documented
667 9 my $self = shift;
668 9 return $self->queue->get();
669 }
670  
671 =head2 left_in_queue
672  
673 Returns queue()->size().
674  
675 =cut
676  
677 10 ▶sub left_in_queue {
Subroutine left_in_queue: called CC=1 SCAR=0.0
Pod documented
678 10 return shift->queue->size();
679 }
680  
681 =head2 remove_from_queue( I<uri> )
682  
683 Calls queue()->remove(I<uri>).
684  
685 =cut
686  
687 9 ▶sub remove_from_queue {
Subroutine remove_from_queue: called CC=2 SCAR=7.1
Pod documented
688 9 my $self = shift;
689 9 ▶ my $uri = shift or croak "uri required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or croak("uri required") 9 0 9
690 9 return $self->queue->remove($uri);
691 }
692  
693 =head2 get_doc
694  
695 Returns the next URI from the queue() as a SWISH::Prog::Doc object,
696 or the error message if there was one.
697  
698 Returns undef if the queue is empty or max_depth() has been reached.
699  
700 =cut
701  
702 10 ▶sub get_doc {
Subroutine get_doc: called CC=7 SCAR=21.1
Pod documented
703 10 my $self = shift;
704  
705 # return unless we have something in the queue
706 10 ▶ return unless $self->left_in_queue();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->left_in_queue 1 9 10
707  
708 # pop the queue and make it a URI
709 9 my $uri = $self->next_from_queue();
710 9 my $depth = $self->uri_cache->get("$uri");
711  
712 9 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", sprintf("depth:%d max_depth:%s", $depth, $self->max_depth || "undef")) 0 9 9
Condition0%0/2
$self->max_depth || 'undef'
l!l
00
MC/DC0%0/1
$self->max_depth || 'undef'
$self->max_depth
Truth table0%0/2
A
A= $self->max_depth
Aresult
11
00
713 and $self->write_log(
714 uri => $uri,
715 msg => sprintf(
716 "depth:%d max_depth:%s",
717 $depth, ( $self->max_depth || 'undef' )
718 ),
719 );
720  
721 9 ▶ return if defined $self->max_depth && $depth > $self->max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->max_depth and $depth > $self->max_depth 0 9 9
Condition33%1/3
defined $self->max_depth and $depth > $self->max_depth
!ll&&!rl&&r
900
MC/DC0%0/2
defined $self->max_depth and $depth > $self->max_depth
defined $self->max_depth $depth > $self->max_depth
Truth table33%1/3
A and B
A= defined $self->max_depthB= $depth > $self->max_depth
ABresult
0X0
100
111
722  
723 9 $self->{_cur_depth} = $depth;
724  
725 9 my $doc = $self->_make_request($uri);
726  
727 9 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 9 0 9
728 9 $self->remove_from_queue($uri);
729 }
730  
731 9 return $doc;
732 }
733  
734 =head2 get_authorized_doc( I<uri>, I<response> )
735  
736 Called internally when the server returns a 401 or 403 response.
737 Will attempt to determine the correct credentials for I<uri>
738 based on the previous attempt in I<response> and what you
739 have configured in B<credentials>, B<authn_callback> or when
740 manually prompted.
741  
742 =cut
743  
744 0 ▶sub get_authorized_doc {
Subroutine get_authorized_doc: not called CC=4 SCAR=30.0
Pod documented
745 0 my $self = shift;
746 0 ▶ my $uri = shift or croak "uri required";
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or croak("uri required") 0 0 0
747 0 ▶ my $response = shift or croak "response required";
Branch: 1 branch
BranchTrueFalseTotal
my $response = shift() or croak("response required") 0 0 0
748  
749 # set up credentials
750 0 ▶ $self->_authorize( $uri, $response->http_response ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_authorize($uri, $response->http_response) or return 0 0 0
751  
752 0 return $self->_make_request($uri);
753 }
754  
755 9 ▶sub _make_request {
Subroutine _make_request: called CC=43 SCAR=57.3
756 9 my ( $self, $uri ) = @_;
757  
758 # get our useragent
759 9 my $ua = $self->ua;
760 9 my $delay = 0;
761 9 ▶ if ( $self->{keep_alive} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'keep_alive'}) { } 0 9 9
elsif (not $self->{'delay'} && $self->{'_last_response_time'}) { } 9 0 9
762 0 $delay = 0;
763 }
764 ▶ elsif ( !$self->{delay} or !$self->{_last_response_time} ) {
Condition33%1/3
$self->{'delay'} && $self->{'_last_response_time'}
!ll&&!rl&&r
900
MC/DC0%0/2
$self->{'delay'} && $self->{'_last_response_time'}
$self->{'delay'} $self->{'_last_response_time'}
Truth table33%1/3
A && B
A= $self->{'delay'}B= $self->{'_last_response_time'}
ABresult
0X0
100
111
765 9 $delay = 0;
766 }
767 else {
768 0 my $elapsed = time() - $self->{_last_response_time};
769 0 $delay = $self->{delay} - $elapsed;
770 0 ▶ $delay = 0 if $delay < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 0 0
771 0 ▶ $self->debug
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "elapsed:$elapsed delay:$delay") 0 0 0
772 and $self->write_log(
773 uri => $uri,
774 msg => "elapsed:$elapsed delay:$delay",
775 );
776 }
777  
778 9 ▶ $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 9 9
779 uri => $uri,
780 msg => "GET delay:$delay",
781 ) if $self->verbose;
782  
783 9 my %get_args = (
784 uri => $uri,
785 delay => $delay,
786 debug => $self->debug,
787 verbose => $self->verbose,
788 );
789  
790 9 ▶ if ( my ( $user, $pass ) = $self->_get_user_pass($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_get_user_pass($uri)) 9 0 9
791 9 $get_args{user} = $user;
792 9 $get_args{pass} = $pass;
793 }
794  
795 # fetch the uri. $ua handles delay internally.
796 9 my $response = $ua->get(%get_args);
797 9 my $http_response = $response->http_response;
798  
799 # flag current time for next delay calc.
800 9 $self->{_last_response_time} = time();
801  
802 # redirect? follow, conditionally.
803 9 ▶ if ( $response->is_redirect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) 3 6 9
804 3 my $location = $response->header('location');
805 3 ▶ if ( !$location ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location) 0 3 3
806 0 $self->write_log(
807 uri => $uri,
808 msg => "skipping, redirect without a Location header",
809 );
810 0 return $response->status;
811 }
812 $self->debug
813 3 ▶ and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "redirect: $location") 0 3 3
814 uri => $uri,
815 msg => "redirect: $location",
816 );
817 3 ▶ if ( $self->follow_redirects ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->follow_redirects) 3 0 3
818 3 $self->_add_links( $uri,
819 URI->new_abs( $location, $http_response->base ) );
820 }
821 3 return $response->status;
822 }
823  
824 6 ▶ if ( $response->ct ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->ct) 6 0 6
825 6 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", 'content-type: ' . $response->ct) 0 6 6
826 uri => $uri,
827 msg => 'content-type: ' . $response->ct,
828 );
829 }
830  
831 # add its links to the queue.
832 # If the resource looks like an XML feed of some kind,
833 # glean its links differently than if it is an HTML response.
834 6 ▶ if ( my $feed = $self->looks_like_feed($http_response) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $feed = $self->looks_like_feed($http_response)) { } 0 6 6
elsif (my $sitemap = $self->looks_like_sitemap($http_response)) { } 0 6 6
835 0 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "looks like feed") 0 0 0
836 uri => $uri,
837 msg => 'looks like feed'
838 );
839 0 my @links;
840 0 for my $entry ( $feed->entries ) {
841 0 push @links, URI->new( $entry->link );
842 }
843 0 $self->_add_links( $uri, @links );
844  
845 # we don't want the feed content, we want the links.
846 # TODO make this optional
847 0 return $response->status;
848 }
849 elsif ( my $sitemap = $self->looks_like_sitemap($http_response) ) {
850 0 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $uri, "msg", "looks like sitemap") 0 0 0
851 uri => $uri,
852 msg => 'looks like sitemap',
853 );
854 0 my @links;
855 0 for my $url ( $sitemap->urls ) {
856 0 push @links, URI->new( $url->loc );
857 }
858 0 $self->_add_links( $uri, @links );
859  
860 # we don't want the feed content, we want the links.
861 # TODO make this optional
862 0 return $response->status;
863 }
864 else {
865 6 $self->_add_links( $uri, $response->links );
866 }
867  
868 # return $uri as a Doc object
869 6 ▶ my $use_uri = $response->success ? $ua->uri : $uri;
Branch: 1 branch
BranchTrueFalseTotal
$response->success ? : 5 1 6
870 my $meta = {
871 org_uri => $uri,
872 ret_uri => ( $use_uri || $uri ),
873 depth => delete $self->{_cur_depth},
874 6 ▶ status => $response->status,
Branch: 3 branches
BranchTrueFalseTotal
$response->success ? : 5 1 6
$response->is_html ? : 4 1 5
$response->success ? : 5 1 6
Condition60%3/5
$use_uri || $uri
l!l&&r!l&&!r
600
$response->title or "No title: $use_uri"
l!l
22
MC/DC0%0/2
$use_uri || $uri
$use_uri $uri
MC/DC100%1/1
$response->title or "No title: $use_uri"
$response->title
Truth table33%1/3
A || B
A= $use_uriB= $uri
ABresult
1X1
011
000
Truth table100%2/2
A
A= $response->title
Aresult
11
00
875 success => $response->success,
876 is_html => $response->is_html,
877 title => (
878 $response->success
879 ? ( $response->is_html
880 ? ( $response->title || "No title: $use_uri" )
881 : $use_uri
882 )
883 : "Failed: $use_uri"
884 ),
885 ct => ( $response->success ? $response->ct : "Unknown" ),
886 };
887  
888 6 my $headers = $http_response->headers;
889 6 my $buf = $response->content;
890  
891 6 ▶ if ( $self->{use_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_md5"}) 0 6 6
892 0 ▶ my $fingerprint = $response->header('Content-MD5')
Condition0%0/3
$response->header('Content-MD5') || Digest::MD5::md5_base64($buf)
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->header('Content-MD5') || Digest::MD5::md5_base64($buf)
$response->header('Content-MD5') Digest::MD5::md5_base64($buf)
Truth table0%0/3
A || B
A= $response->header('Content-MD5')B= Digest::MD5::md5_base64($buf)
ABresult
1X1
011
000
893 || Digest::MD5::md5_base64($buf);
894 0 ▶ if ( $self->md5_cache->has($fingerprint) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->md5_cache->has($fingerprint)) 0 0 0
895 0 return "duplicate content for "
896 . $self->md5_cache->get($fingerprint);
897 }
898 0 $self->md5_cache->add( $fingerprint => $uri );
899 }
900  
901 6 ▶ if ( $response->success ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($response->success) { } 5 1 6
elsif ($response->status == 401) { } 0 1 1
elsif ($response->status == 403 and $http_response->status_line =~ /robots.txt/) { } 0 1 1
elsif ($response->status == 403) { } 0 1 1
902  
903 5 my $content_type = $meta->{ct};
904 5 ▶ if ( !exists $parser_types{$content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parser_types{$content_type}) 0 5 5
905 0 $self->write_log(
906 uri => $uri,
907 msg => "no parser for $content_type",
908 );
909 }
910 5 my $charset = $headers->content_type;
911 5 $charset =~ s/;?$meta->{ct};?//;
912 5 ▶ my $encoding = $headers->content_encoding || $charset;
Condition33%1/3
$headers->content_encoding || $charset
l!l&&r!l&&!r
005
MC/DC0%0/2
$headers->content_encoding || $charset
$headers->content_encoding $charset
Truth table33%1/3
A || B
A= $headers->content_encodingB= $charset
ABresult
1X1
011
000
913 my %doc = (
914 url => $meta->{org_uri},
915 modtime => ( $headers->last_modified || $headers->date ),
916 type => $meta->{ct},
917 5 ▶ content => ( $encoding =~ m/utf-8/i ? to_utf8($buf) : $buf ),
Branch: 1 branch
BranchTrueFalseTotal
$encoding =~ /utf-8/i ? : 0 5 5
Condition33%2/6
$headers->last_modified || $headers->date
l!l&&r!l&&!r
005
$headers->content_length || length pack('C0a*', $buf)
l!l&&r!l&&!r
050
MC/DC0%0/2
$headers->last_modified || $headers->date
$headers->last_modified $headers->date
MC/DC0%0/2
$headers->content_length || length pack('C0a*', $buf)
$headers->content_length length pack('C0a*', $buf)
Truth table33%1/3
A || B
A= $headers->last_modifiedB= $headers->date
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $headers->content_lengthB= length pack('C0a*', $buf)
ABresult
1X1
011
000
918 size => $headers->content_length || length( pack 'C0a*', $buf ),
919 charset => $encoding,
920 );
921  
922 # cache whatever credentials were used so we can re-use
923 5 ▶ if ( $self->{cur_realm} and $uri->userinfo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cur_realm"} and $uri->userinfo) 0 5 5
Condition33%1/3
$self->{"cur_realm"} and $uri->userinfo
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"cur_realm"} and $uri->userinfo
$self->{"cur_realm"} $uri->userinfo
Truth table33%1/3
A and B
A= $self->{"cur_realm"}B= $uri->userinfo
ABresult
0X0
100
111
924 0 my $key = $uri->canonical->host_port . ':' . $self->{cur_realm};
925 0 $self->{_auth_cache}->add( $key => $uri->userinfo );
926  
927 # not too sure of the best logic here
928 0 my $path = $uri->path;
929 0 $path =~ s!/[^/]*$!!;
930 $self->{last_auth} = {
931 0 path => $path,
932 auth => $uri->userinfo,
933 };
934 }
935  
936 # return doc
937 5 return $self->doc_class->new(%doc);
938  
939 }
940 elsif ( $response->status == 401 ) {
941  
942 # authorize and try again
943 0 $self->write_log(
944 uri => $uri,
945 msg => sprintf( "authn denied, retrying, %s",
946 $response->status_line ),
947 );
948 0 ▶ return $self->get_authorized_doc( $uri, $response )
Condition0%0/3
$self->get_authorized_doc($uri, $response) || $response->status
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_authorized_doc($uri, $response) || $response->status
$self->get_authorized_doc($uri, $response) $response->status
Truth table0%0/3
A || B
A= $self->get_authorized_doc($uri, $response)B= $response->status
ABresult
1X1
011
000
949 || $response->status;
950 }
951 ▶ elsif ($response->status == 403
Condition33%1/3
$response->status == 403 and $http_response->status_line =~ /robots.txt/
!ll&&!rl&&r
100
MC/DC0%0/2
$response->status == 403 and $http_response->status_line =~ /robots.txt/
$response->status == 403 $http_response->status_line =~ /robots.txt/
Truth table33%1/3
A and B
A= $response->status == 403B= $http_response->status_line =~ /robots.txt/
ABresult
0X0
100
111
952 && $http_response->status_line =~ m/robots.txt/ )
953 {
954  
955 # ignore
956 0 $self->write_log(
957 uri => $uri,
958 msg => sprintf( "skipped, %s", $http_response->status_line ),
959 );
960 0 ▶ return $self->get_authorized_doc( $uri, $response )
Condition0%0/3
$self->get_authorized_doc($uri, $response) || $response->status
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_authorized_doc($uri, $response) || $response->status
$self->get_authorized_doc($uri, $response) $response->status
Truth table0%0/3
A || B
A= $self->get_authorized_doc($uri, $response)B= $response->status
ABresult
1X1
011
000
961 || $response->status;
962 }
963 elsif ( $response->status == 403 ) {
964  
965 # authorize and try again
966 0 $self->write_log(
967 uri => $uri,
968 msg => sprintf( "retrying, %s", $http_response->status_line ),
969 );
970 0 return $self->get_authorized_doc( $uri, $response );
971 }
972 else {
973  
974 1 $self->write_log(
975 uri => $uri,
976 msg => $http_response->status_line,
977 );
978 1 return $response->status;
979 }
980  
981 0 return; # never get here.
982 }
983  
984 22 ▶sub _get_user_pass {
Subroutine _get_user_pass: called CC=6 SCAR=24.1
985 22 my $self = shift;
986 22 my $uri = shift;
987  
988 # Set basic auth if defined - use URI specific first, then credentials.
989 # this doesn't track what should have authorization
990 22 my $last_auth;
991 22 ▶ if ( $self->{last_auth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_auth"}) 0 22 22
992 0 my $path = $uri->path;
993 0 $path =~ s!/[^/]*$!!;
994 $last_auth = $self->{last_auth}->{auth}
995 0 ▶ if $self->{last_auth}->{path} eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_auth"}{"path"} eq $path 0 0 0
996 }
997  
998 22 ▶ my ( $user, $pass ) = split /:/,
Condition37%3/8
$last_auth || $uri->userinfo || $self->credentials || ''
l!l
220
$last_auth or $uri->userinfo or $self->credentials
l!l&&r!l&&!r
0220
$last_auth or $uri->userinfo
l!l&&r!l&&!r
0022
MC/DC0%0/3
$last_auth || $uri->userinfo || $self->credentials || ''
$last_auth $uri->userinfo $self->credentials
Truth table25%1/4
A or B or C
A= $last_authB= $uri->userinfoC= $self->credentials
ABCresult
1XX1
01X1
0011
0000
999 ( $last_auth || $uri->userinfo || $self->credentials || '' );
1000  
1001 22 return ( $user, $pass );
1002 }
1003  
1004 =head2 looks_like_feed( I<http_response> )
1005  
1006 Called internally to perform naive heuristics on I<http_response>
1007 to determine whether it looks like an XML feed of some kind,
1008 rather than a HTML page.
1009  
1010 =cut
1011  
1012 6 ▶sub looks_like_feed {
Subroutine looks_like_feed: called CC=8 SCAR=24.7
Pod documented
1013 6 my $self = shift;
1014 6 ▶ my $response = shift or croak "response required";
Branch: 1 branch
BranchTrueFalseTotal
my $response = shift() or croak("response required") 6 0 6
1015 6 my $headers = $response->headers;
1016 6 my $ct = $headers->content_type;
1017 6 ▶ if ( $ct eq 'text/html' or $ct eq 'application/xhtml+xml' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/html" or $ct eq "application/xhtml+xml") 4 2 6
Condition66%2/3
$ct eq "text/html" or $ct eq "application/xhtml+xml"
l!l&&r!l&&!r
402
MC/DC50%1/2
$ct eq "text/html" or $ct eq "application/xhtml+xml"
$ct eq "text/html" $ct eq "application/xhtml+xml"
Truth table66%2/3
A or B
A= $ct eq "text/html"B= $ct eq "application/xhtml+xml"
ABresult
1X1
011
000
1018 4 return 0;
1019 }
1020 2 ▶ if ( $ct eq 'text/xml'
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/xml" or $ct eq "application/rss+xml" or $ct eq "application/rdf+xml" or $ct eq "application/atom+xml") 0 2 2
Condition33%3/9
$ct eq "text/xml" or $ct eq "application/rss+xml" or $ct eq "application/rdf+xml" or $ct eq "application/atom+xml"
l!l&&r!l&&!r
002
$ct eq "text/xml" or $ct eq "application/rss+xml" or $ct eq "application/rdf+xml"
l!l&&r!l&&!r
002
$ct eq "text/xml" or $ct eq "application/rss+xml"
l!l&&r!l&&!r
002
MC/DC0%0/4
$ct eq "text/xml" or $ct eq "application/rss+xml" or $ct eq "application/rdf+xml" or $ct eq "application/atom+xml"
$ct eq "text/xml" $ct eq "application/rss+xml" $ct eq "application/rdf+xml" $ct eq "application/atom+xml"
Truth table20%1/5
A or B or C or D
A= $ct eq "text/xml"B= $ct eq "application/rss+xml"C= $ct eq "application/rdf+xml"D= $ct eq "application/atom+xml"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1021 or $ct eq 'application/rss+xml'
1022 or $ct eq 'application/rdf+xml'
1023 or $ct eq 'application/atom+xml' )
1024 {
1025 0 my $xml = $response->decoded_content; # TODO or content()
1026 0 return XML::Feed->parse( \$xml );
1027 }
1028  
1029 2 return 0;
1030 }
1031  
1032 =head2 looks_like_sitemap( I<http_response> )
1033  
1034 Called internally to perform naive heuristics on I<http_response>
1035 to determine whether it looks like a XML sitemap feed,
1036 rather than a HTML page.
1037  
1038 =cut
1039  
1040 6 ▶sub looks_like_sitemap {
Subroutine looks_like_sitemap: called CC=7 SCAR=19.8
Pod documented
1041 6 my $self = shift;
1042 6 ▶ my $response = shift or croak "response required";
Branch: 1 branch
BranchTrueFalseTotal
my $response = shift() or croak("response required") 6 0 6
1043 6 my $headers = $response->headers;
1044 6 my $ct = $headers->content_type;
1045 6 ▶ if ( $ct eq 'text/html' or $ct eq 'application/xhtml+xml' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/html" or $ct eq "application/xhtml+xml") 4 2 6
Condition66%2/3
$ct eq "text/html" or $ct eq "application/xhtml+xml"
l!l&&r!l&&!r
402
MC/DC50%1/2
$ct eq "text/html" or $ct eq "application/xhtml+xml"
$ct eq "text/html" $ct eq "application/xhtml+xml"
Truth table66%2/3
A or B
A= $ct eq "text/html"B= $ct eq "application/xhtml+xml"
ABresult
1X1
011
000
1046 4 return 0;
1047 }
1048 2 ▶ if ( $ct eq 'text/xml'
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/xml" or $ct eq "application/xml") 1 1 2
Condition66%2/3
$ct eq "text/xml" or $ct eq "application/xml"
l!l&&r!l&&!r
011
MC/DC50%1/2
$ct eq "text/xml" or $ct eq "application/xml"
$ct eq "text/xml" $ct eq "application/xml"
Truth table66%2/3
A or B
A= $ct eq "text/xml"B= $ct eq "application/xml"
ABresult
1X1
011
000
1049 or $ct eq 'application/xml' )
1050 {
1051 1 my $xml = $response->decoded_content; # TODO or content()
1052 1 my $sitemap = WWW::Sitemap::XML->new();
1053 1 eval { $sitemap->load( string => $xml ); };
1054 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1055 1 return 0;
1056 }
1057 0 return $sitemap;
1058 }
1059  
1060 1 return 0;
1061 }
1062  
1063 =head2 crawl( I<uri> )
1064  
1065 Implements the required crawl() method. Recursively fetches I<uri>
1066 and its child links to a depth set in max_depth().
1067  
1068 Will quit after max_files() unless max_files==0.
1069  
1070 Will quit after max_time() seconds unless max_time==0.
1071  
1072 =cut
1073  
1074 1 ▶sub crawl {
Subroutine crawl: called CC=11 SCAR=25.3
Pod documented
1075 1 my $self = shift;
1076 1 my @urls = @_;
1077  
1078 1 my $indexer = $self->indexer; # may be undef
1079  
1080 1 for my $url (@urls) {
1081 1 my $started = time();
1082 1 ▶ $self->debug and $self->write_log(
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log("uri", $url, "msg", "crawling") 0 1 1
1083 uri => $url,
1084 msg => "crawling",
1085 );
1086  
1087 1 my $uri = URI->new($url)->canonical;
1088 1 $self->uri_cache->add( "$uri" => 1 );
1089 1 $self->add_to_queue($uri);
1090 1 $self->{_base} = $uri->as_string;
1091 1 ▶ while ( my $doc = $self->get_doc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $self->get_doc) 9 1 10
1092 9 ▶ $self->debug and $self->write_log_line();
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and $self->write_log_line 0 9 9
1093 9 ▶ next unless blessed($doc);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($doc) 4 5 9
1094  
1095 # indexer not required
1096 5 ▶ $indexer->process($doc) if $indexer;
Branch: 1 branch
BranchTrueFalseTotal
if $indexer 0 5 5
1097  
1098 5 $self->_increment_count;
1099  
1100 # abort if we've met any max_* conditions
1101 5 ▶ last if $self->max_files and $self->count >= $self->max_files;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_files and $self->count >= $self->max_files 0 5 5
Condition33%1/3
$self->max_files and $self->count >= $self->max_files
!ll&&!rl&&r
500
MC/DC0%0/2
$self->max_files and $self->count >= $self->max_files
$self->max_files $self->count >= $self->max_files
Truth table33%1/3
A and B
A= $self->max_filesB= $self->count >= $self->max_files
ABresult
0X0
100
111
1102 last
1103 5 ▶ if $self->max_time
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_time and time - $started > $self->max_time 0 5 5
Condition33%1/3
$self->max_time and time - $started > $self->max_time
!ll&&!rl&&r
500
MC/DC0%0/2
$self->max_time and time - $started > $self->max_time
$self->max_time time - $started > $self->max_time
Truth table33%1/3
A and B
A= $self->max_timeB= time - $started > $self->max_time
ABresult
0X0
100
111
1104 and ( time() - $started ) > $self->max_time;
1105 }
1106 }
1107  
1108 1 return $self->count;
1109 }
1110  
1111 =head2 write_log( I<args> )
1112  
1113 Passes I<args> to SWISH::Prog::Utils::write_log().
1114  
1115 =cut
1116  
1117 1 ▶sub write_log {
Subroutine write_log: called CC=1 SCAR=0.0
Pod documented
1118 1 SWISH::Prog::Utils::write_log(@_);
1119 }
1120  
1121 =head2 write_log_line([I<char>, I<width>])
1122  
1123 Pass through to SWISH::Prog::Utils::write_log_line().
1124  
1125 =cut
1126  
1127 0 ▶sub write_log_line {
Subroutine write_log_line: not called CC=1 SCAR=6.9
Pod documented
1128 0 SWISH::Prog::Utils::write_log_line(@_);
1129 }
1130  
1131 2 1;
1132  
1133 __END__
« Prog/Aggregator/DBI.pm ↑ Summary Prog/Aggregator.pm »