lib/File/Fetch.pm

filter: Statementstmt 59.5% 332 / 558 Branchbran 34.2% 91 / 266 Conditioncond 32.1% 26 / 81 MC/DCmcdc 14.2% 7 / 49 Subroutinesub 85.4% 41 / 48 Podpod 100% 4 / 4 totaltotal 49.8% 501 / 1006 CC 168 SCAR 36.0
CC
168
Cov
50%
_lynx_fetch
136.2
_httplite_fetch
104.6
_rsync_fetch
72.0
CRAP 36.6
↑ Summary
1 package File::Fetch;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Unix;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Basename qw[dirname];
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Cwd qw[cwd];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp qw[carp];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IPC::Cmd qw[can_run run QUOTE];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Path qw[mkpath];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Temp qw[tempdir];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Params::Check qw[check];
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Locale::Maketext::Simple Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use vars qw[ $VERBOSE $PREFER_BIN $FROM_EMAIL $USER_AGENT
Subroutine BEGIN: called CC=1 SCAR=0.0
22 $BLACKLIST $METHOD_FAIL $VERSION $METHODS
23 $FTP_PASSIVE $TIMEOUT $DEBUG $WARN $FORCEIPV4
24 2 ];
25  
26 2 $VERSION = '1.08';
27 2 $VERSION = eval $VERSION; # avoid warnings with development releases
28 2 $PREFER_BIN = 0; # XXX TODO implement
29 2 $FROM_EMAIL = 'File-Fetch@example.com';
30 2 $USER_AGENT = "File::Fetch/$VERSION";
31 2 $BLACKLIST = [qw|ftp|];
32 2 ▶push @$BLACKLIST, qw|lftp| if $^O eq 'dragonfly' || $^O eq 'hpux';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "dragonfly" or $^O eq "hpux" 0 2 2
Condition33%1/3
$^O eq "dragonfly" or $^O eq "hpux"
l!l&&r!l&&!r
002
MC/DC0%0/2
$^O eq "dragonfly" or $^O eq "hpux"
$^O eq "dragonfly" $^O eq "hpux"
Truth table33%1/3
A or B
A= $^O eq "dragonfly"B= $^O eq "hpux"
ABresult
1X1
011
000
33 2 $METHOD_FAIL = { };
34 2 $FTP_PASSIVE = 1;
35 2 $TIMEOUT = 0;
36 2 $DEBUG = 0;
37 2 $WARN = 1;
38 2 $FORCEIPV4 = 0;
39  
40 ### methods available to fetch the file depending on the scheme
41 2 $METHODS = {
42 http => [ qw|lwp httptiny wget curl lftp fetch httplite lynx iosock| ],
43 https => [ qw|lwp httptiny wget curl| ],
44 ftp => [ qw|lwp netftp wget curl lftp fetch ncftp ftp| ],
45 file => [ qw|lwp lftp file| ],
46 rsync => [ qw|rsync| ],
47 git => [ qw|git| ],
48 };
49  
50 ### silly warnings ###
51 2 local $Params::Check::VERBOSE = 1;
52 2 local $Params::Check::VERBOSE = 1;
53 2 local $Module::Load::Conditional::VERBOSE = 0;
54 2 local $Module::Load::Conditional::VERBOSE = 0;
55  
56 ### see what OS we are on, important for file:// uris ###
57 2 ▶use constant ON_WIN => ($^O eq 'MSWin32');
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant ON_VMS => ($^O eq 'VMS');
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use constant ON_UNIX => (!ON_WIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use constant HAS_VOL => (ON_WIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use constant HAS_SHARE => (ON_WIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use constant HAS_FETCH => ( $^O =~ m!^(freebsd|netbsd|dragonfly|midnightbsd)$! );
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 =pod
65  
66 =head1 NAME
67  
68 File::Fetch - A generic file fetching mechanism
69  
70 =head1 SYNOPSIS
71  
72 use File::Fetch;
73  
74 ### build a File::Fetch object ###
75 my $ff = File::Fetch->new(uri => 'http://some.where.com/dir/a.txt');
76  
77 ### fetch the uri to cwd() ###
78 my $where = $ff->fetch() or die $ff->error;
79  
80 ### fetch the uri to /tmp ###
81 my $where = $ff->fetch( to => '/tmp' );
82  
83 ### parsed bits from the uri ###
84 $ff->uri;
85 $ff->scheme;
86 $ff->host;
87 $ff->path;
88 $ff->file;
89  
90 =head1 DESCRIPTION
91  
92 File::Fetch is a generic file fetching mechanism.
93  
94 It allows you to fetch any file pointed to by a C<ftp>, C<http>,
95 C<file>, C<git> or C<rsync> uri by a number of different means.
96  
97 See the C<HOW IT WORKS> section further down for details.
98  
99 =head1 ACCESSORS
100  
101 A C<File::Fetch> object has the following accessors
102  
103 =over 4
104  
105 =item $ff->uri
106  
107 The uri you passed to the constructor
108  
109 =item $ff->scheme
110  
111 The scheme from the uri (like 'file', 'http', etc)
112  
113 =item $ff->host
114  
115 The hostname in the uri. Will be empty if host was originally
116 'localhost' for a 'file://' url.
117  
118 =item $ff->vol
119  
120 On operating systems with the concept of a volume the second element
121 of a file:// is considered to the be volume specification for the file.
122 Thus on Win32 this routine returns the volume, on other operating
123 systems this returns nothing.
124  
125 On Windows this value may be empty if the uri is to a network share, in
126 which case the 'share' property will be defined. Additionally, volume
127 specifications that use '|' as ':' will be converted on read to use ':'.
128  
129 On VMS, which has a volume concept, this field will be empty because VMS
130 file specifications are converted to absolute UNIX format and the volume
131 information is transparently included.
132  
133 =item $ff->share
134  
135 On systems with the concept of a network share (currently only Windows) returns
136 the sharename from a file://// url. On other operating systems returns empty.
137  
138 =item $ff->path
139  
140 The path from the uri, will be at least a single '/'.
141  
142 =item $ff->file
143  
144 The name of the remote file. For the local file name, the
145 result of $ff->output_file will be used.
146  
147 =item $ff->file_default
148  
149 The name of the default local file, that $ff->output_file falls back to if
150 it would otherwise return no filename. For example when fetching a URI like
151 http://www.abc.net.au/ the contents retrieved may be from a remote file called
152 'index.html'. The default value of this attribute is literally 'file_default'.
153  
154 =cut
155  
156  
157 ##########################
158 ### Object & Accessors ###
159 ##########################
160  
161 {
162 ### template for autogenerated accessors ###
163 2 my $Tmpl = {
164 scheme => { default => 'http' },
165 host => { default => 'localhost' },
166 path => { default => '/' },
167 file => { required => 1 },
168 uri => { required => 1 },
169 userinfo => { default => '' },
170 vol => { default => '' }, # windows for file:// uris
171 share => { default => '' }, # windows for file:// uris
172 file_default => { default => 'file_default' },
173 tempdir_root => { required => 1 }, # Should be lazy-set at ->new()
174 _error_msg => { no_override => 1 },
175 _error_msg_long => { no_override => 1 },
176 };
177  
178 2 for my $method ( keys %$Tmpl ) {
179 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 617 ▶ *$method = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
181 617 my $self = shift;
182 617 ▶ $self->{$method} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 16 601 617
183 617 return $self->{$method};
184 }
185 24 }
186  
187 49 ▶ sub _create {
Subroutine _create: called CC=6 SCAR=19.6
188 49 my $class = shift;
189 49 my %hash = @_;
190  
191 49 ▶ my $args = check( $Tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($Tmpl, \%hash) or return 49 0 49
192  
193 49 bless $args, $class;
194  
195 49 ▶ if( lc($args->scheme) ne 'file' and not $args->host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $args->scheme ne "file" and not $args->host) 0 49 49
Condition66%2/3
lc $args->scheme ne "file" and not $args->host
!ll&&!rl&&r
6430
MC/DC0%0/2
lc $args->scheme ne "file" and not $args->host
lc $args->scheme ne "file" not $args->host
Truth table66%2/3
A and not(B)
A= lc $args->scheme ne "file"B= not $args->host
ABresult
0X0
100
111
196 0 return $class->_error(loc(
197 "Hostname required when fetching from '%1'",$args->scheme));
198 }
199  
200 49 for (qw[path]) {
201 49 ▶ unless( $args->$_() ) { # 5.5.x needs the ()
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->$_) 0 49 49
202 0 return $class->_error(loc("No '%1' specified",$_));
203 }
204 }
205  
206 49 return $args;
207 }
208 }
209  
210 =item $ff->output_file
211  
212 The name of the output file. This is the same as $ff->file,
213 but any query parameters are stripped off. For example:
214  
215 http://example.com/index.html?x=y
216  
217 would make the output file be C<index.html> rather than
218 C<index.html?x=y>.
219  
220 =back
221  
222 =cut
223  
224 2 ▶sub output_file {
Subroutine output_file: called CC=2 SCAR=7.0
Pod documented
225 106 my $self = shift;
226 106 my $file = $self->file;
227  
228 106 $file =~ s/\?.*$//g;
229  
230 106 ▶ $file ||= $self->file_default;
Condition66%2/3
$file ||= $self->file_default
l!l&&r!l&&!r
82240
MC/DC0%0/2
$file ||= $self->file_default
$file $self->file_default
Truth table66%2/3
A ||= B
A= $fileB= $self->file_default
ABresult
1X1
011
000
231  
232 106 return $file;
233 }
234  
235 ### XXX do this or just point to URI::Escape?
236 # =head2 $esc_uri = $ff->escaped_uri
237 #
238 # =cut
239 #
240 # ### most of this is stolen straight from URI::escape
241 # { ### Build a char->hex map
242 # my %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255;
243 #
244 # sub escaped_uri {
245 # my $self = shift;
246 # my $uri = $self->uri;
247 #
248 # ### Default unsafe characters. RFC 2732 ^(uric - reserved)
249 # $uri =~ s/([^A-Za-z0-9\-_.!~*'()])/
250 # $escapes{$1} || $self->_fail_hi($1)/ge;
251 #
252 # return $uri;
253 # }
254 #
255 # sub _fail_hi {
256 # my $self = shift;
257 # my $char = shift;
258 #
259 # $self->_error(loc(
260 # "Can't escape '%1', try using the '%2' module instead",
261 # sprintf("\\x{%04X}", ord($char)), 'URI::Escape'
262 # ));
263 # }
264 #
265 # sub output_file {
266 #
267 # }
268 #
269 #
270 # }
271  
272 =head1 METHODS
273  
274 =head2 $ff = File::Fetch->new( uri => 'http://some.where.com/dir/file.txt' );
275  
276 Parses the uri and creates a corresponding File::Fetch::Item object,
277 that is ready to be C<fetch>ed and returns it.
278  
279 Returns false on failure.
280  
281 =cut
282  
283 49 ▶sub new {
Subroutine new: called CC=7 SCAR=20.6
Pod documented
284 49 my $class = shift;
285 49 my %hash = @_;
286  
287 49 my ($uri, $file_default, $tempdir_root);
288 49 my $tmpl = {
289 uri => { required => 1, store => \$uri },
290 file_default => { required => 0, store => \$file_default },
291 tempdir_root => { required => 0, store => \$tempdir_root },
292 };
293  
294 49 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 49 0 49
295  
296 ### parse the uri to usable parts ###
297 49 ▶ my $href = $class->_parse_uri( $uri ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $href = $class->_parse_uri($uri) or return 49 0 49
298  
299 49 ▶ $href->{file_default} = $file_default if $file_default;
Branch: 1 branch
BranchTrueFalseTotal
if $file_default 0 49 49
300 49 ▶ $href->{tempdir_root} = File::Spec->rel2abs( $tempdir_root ) if $tempdir_root;
Branch: 1 branch
BranchTrueFalseTotal
if $tempdir_root 0 49 49
301 49 ▶ $href->{tempdir_root} = File::Spec->rel2abs( Cwd::cwd ) if not $href->{tempdir_root};
Branch: 1 branch
BranchTrueFalseTotal
unless $href->{"tempdir_root"} 49 0 49
302  
303 ### make it into a FFI object ###
304 49 ▶ my $ff = $class->_create( %$href ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ff = $class->_create(%$href) or return 49 0 49
305  
306  
307 ### return the object ###
308 49 return $ff;
309 }
310  
311 ### parses an uri to a hash structure:
312 ###
313 ### $class->_parse_uri( 'ftp://ftp.cpan.org/pub/mirror/index.txt' )
314 ###
315 ### becomes:
316 ###
317 ### $href = {
318 ### scheme => 'ftp',
319 ### host => 'ftp.cpan.org',
320 ### path => '/pub/mirror',
321 ### file => 'index.html'
322 ### };
323 ###
324 ### In the case of file:// urls there maybe be additional fields
325 ###
326 ### For systems with volume specifications such as Win32 there will be
327 ### a volume specifier provided in the 'vol' field.
328 ###
329 ### 'vol' => 'volumename'
330 ###
331 ### For windows file shares there may be a 'share' key specified
332 ###
333 ### 'share' => 'sharename'
334 ###
335 ### Note that the rules of what a file:// url means vary by the operating system
336 ### of the host being addressed. Thus file:///d|/foo/bar.txt means the obvious
337 ### 'D:\foo\bar.txt' on windows, but on unix it means '/d|/foo/bar.txt' and
338 ### not '/foo/bar.txt'
339 ###
340 ### Similarly if the host interpreting the url is VMS then
341 ### file:///disk$user/my/notes/note12345.txt' means
342 ### 'DISK$USER:[MY.NOTES]NOTE123456.TXT' but will be returned the same as
343 ### if it is unix where it means /disk$user/my/notes/note12345.txt'.
344 ### Except for some cases in the File::Spec methods, Perl on VMS will generally
345 ### handle UNIX format file specifications.
346 ###
347 ### This means it is impossible to serve certain file:// urls on certain systems.
348 ###
349 ### Thus are the problems with a protocol-less specification. :-(
350 ###
351  
352 57 ▶sub _parse_uri {
Subroutine _parse_uri: called CC=9 SCAR=22.0
353 57 my $self = shift;
354 57 ▶ my $uri = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = shift() or return 57 0 57
355  
356 57 my $href = { uri => $uri };
357  
358 ### find the scheme ###
359 57 $uri =~ s|^(\w+)://||;
360 57 $href->{scheme} = $1;
361  
362 ### See rfc 1738 section 3.10
363 ### https://datatracker.ietf.org/doc/html/rfc1738#section-3.10
364 ### And wikipedia for more on windows file:// urls
365 ### http://en.wikipedia.org/wiki/File://
366 57 ▶ if( $href->{scheme} eq 'file' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($href->{'scheme'} eq 'file') { } 9 48 57
elsif ($href->{'scheme'} eq 'http' or $href->{'scheme'} eq 'https') { } 42 6 48
367  
368 9 my @parts = split '/',$uri;
369  
370 ### file://hostname/...
371 ### file://hostname/...
372 ### normalize file://localhost with file:///
373 9 ▶ $href->{host} = $parts[0] || '';
Condition100%2/2
$parts[0] || ''
l!l
45
MC/DC100%1/1
$parts[0] || ''
$parts[0]
Truth table100%2/2
A
A= $parts[0]
Aresult
11
00
374  
375 ### index in @parts where the path components begin;
376 9 my $index = 1;
377  
378 ### file:////hostname/sharename/blah.txt
379 9 if ( HAS_SHARE and not length $parts[0] and not length $parts[1] ) {
380  
381 $href->{host} = $parts[2] || ''; # avoid warnings
382 $href->{share} = $parts[3] || ''; # avoid warnings
383  
384 $index = 4 # index after the share
385  
386 ### file:///D|/blah.txt
387 ### file:///D:/blah.txt
388 0 } elsif (HAS_VOL) {
389  
390 ### this code comes from dmq's patch, but:
391 ### XXX if volume is empty, wouldn't that be an error? --kane
392 ### if so, our file://localhost test needs to be fixed as wel
393 $href->{vol} = $parts[1] || '';
394  
395 ### correct D| style colume descriptors
396 $href->{vol} =~ s/\A([A-Z])\|\z/$1:/i if ON_WIN;
397  
398 $index = 2; # index after the volume
399 }
400  
401 ### rebuild the path from the leftover parts;
402 9 $href->{path} = join '/', '', splice( @parts, $index, $#parts );
403  
404 ▶ } elsif ( $href->{scheme} eq 'http' || $href->{scheme} eq 'https' ) {
Condition66%2/3
$href->{"scheme"} eq "http" or $href->{"scheme"} eq "https"
l!l&&r!l&&!r
4206
MC/DC50%1/2
$href->{"scheme"} eq "http" or $href->{"scheme"} eq "https"
$href->{"scheme"} eq "http" $href->{"scheme"} eq "https"
Truth table66%2/3
A or B
A= $href->{"scheme"} eq "http"B= $href->{"scheme"} eq "https"
ABresult
1X1
011
000
405 ### using anything but qw() in hash slices may produce warnings
406 ### in older perls :-(
407 42 @{$href}{ qw(userinfo host path) } = $uri =~ m|(?:([^\@:]*:[^\:\@]*)@)?([^/]*)(/.*)?$|s;
408 42 ▶ $href->{path} = '/' unless defined $href->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $href->{"path"} 11 31 42
409 } else {
410 6 @{$href}{ qw(userinfo host path) } = $uri =~ m|(?:([^\@:]*:[^\:\@]*)@)?([^/]*)(/.*)$|s;
411 }
412  
413 ### split the path into file + dir ###
414 57 { my @parts = File::Spec::Unix->splitpath( delete $href->{path} );
415 57 $href->{path} = $parts[1];
416 57 $href->{file} = $parts[2];
417 }
418  
419 ### host will be empty if the target was 'localhost' and the
420 ### scheme was 'file'
421 $href->{host} = '' if ($href->{host} eq 'localhost') and
422 57 ▶ ($href->{scheme} eq 'file');
Branch: 1 branch
BranchTrueFalseTotal
if $href->{"host"} eq "localhost" and $href->{"scheme"} eq "file" 2 55 57
Condition100%3/3
$href->{"host"} eq "localhost" and $href->{"scheme"} eq "file"
!ll&&!rl&&r
5142
MC/DC100%2/2
$href->{"host"} eq "localhost" and $href->{"scheme"} eq "file"
$href->{"host"} eq "localhost" $href->{"scheme"} eq "file"
Truth table100%3/3
A and B
A= $href->{"host"} eq "localhost"B= $href->{"scheme"} eq "file"
ABresult
0X0
100
111
423  
424 57 return $href;
425 }
426  
427 =head2 $where = $ff->fetch( [to => /my/output/dir/ | \$scalar] )
428  
429 Fetches the file you requested and returns the full path to the file.
430  
431 By default it writes to C<cwd()>, but you can override that by specifying
432 the C<to> argument:
433  
434 ### file fetch to /tmp, full path to the file in $where
435 $where = $ff->fetch( to => '/tmp' );
436  
437 ### file slurped into $scalar, full path to the file in $where
438 ### file is downloaded to a temp directory and cleaned up at exit time
439 $where = $ff->fetch( to => \$scalar );
440  
441 Returns the full path to the downloaded file on success, and false
442 on failure.
443  
444 =cut
445  
446 78 ▶sub fetch {
Subroutine fetch: called CC=18 SCAR=31.4
Pod documented
447 78 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 78 0 78
448 78 my %hash = @_;
449  
450 78 my $target;
451 78 my $tmpl = {
452 to => { default => cwd(), store => \$target },
453 };
454  
455 78 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 78 0 78
456  
457 78 my ($to, $fh);
458 ### you want us to slurp the contents
459 78 ▶ if( ref $target and UNIVERSAL::isa( $target, 'SCALAR' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target and UNIVERSAL::isa($target, 'SCALAR')) { } 39 39 78
Condition66%2/3
ref $target and UNIVERSAL::isa($target, "SCALAR")
!ll&&!rl&&r
39039
MC/DC50%1/2
ref $target and UNIVERSAL::isa($target, "SCALAR")
ref $target UNIVERSAL::isa($target, "SCALAR")
Truth table66%2/3
A and B
A= ref $targetB= UNIVERSAL::isa($target, "SCALAR")
ABresult
0X0
100
111
460 39 $to = tempdir( 'FileFetch.XXXXXX', DIR => $self->tempdir_root, CLEANUP => 1 );
461  
462 ### plain old fetch
463 } else {
464 39 $to = $target;
465  
466 ### On VMS force to VMS format so File::Spec will work.
467 39 $to = VMS::Filespec::vmspath($to) if ON_VMS;
468  
469 ### create the path if it doesn't exist yet ###
470 39 ▶ unless( -d $to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $to) 1 38 39
471 1 eval { mkpath( $to ) };
472  
473 1 ▶ return $self->_error(loc("Could not create path '%1'",$to)) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
474 }
475 }
476  
477 ### set passive ftp if required ###
478 78 local $ENV{FTP_PASSIVE} = $FTP_PASSIVE;
479  
480 ### we dont use catfile on win32 because if we are using a cygwin tool
481 ### under cmd.exe they wont understand windows style separators.
482 78 my $out_to = ON_WIN ? $to.'/'.$self->output_file
483 : File::Spec->catfile( $to, $self->output_file );
484  
485 78 for my $method ( @{ $METHODS->{$self->scheme} } ) {
486 78 my $sub = '_'.$method.'_fetch';
487  
488 78 ▶ unless( __PACKAGE__->can($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (__PACKAGE__->can($sub)) 0 78 78
489 0 $self->_error(loc("Cannot call method for '%1' -- WEIRD!",
490 $method));
491 0 next;
492 }
493  
494 ### method is blacklisted ###
495 78 ▶ next if grep { lc $_ eq $method } @$BLACKLIST;
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $_ eq $method;} @$File::Fetch::BLACKLIST 0 78 78
496  
497 ### method is known to fail ###
498 next if ref $METHOD_FAIL->{$method}
499 ? $METHOD_FAIL->{$method}{$self->scheme}
500 78 ▶ : $METHOD_FAIL->{$method};
Branch: 2 branches
BranchTrueFalseTotal
if ref $File::Fetch::METHOD_FAIL->{$method} ? $File::Fetch::METHOD_FAIL->{$method}{$self->scheme} : $File::Fetch::METHOD_FAIL->{$method} 37 41 78
ref $File::Fetch::METHOD_FAIL->{$method} ? : 0 78 78
501  
502 ### there's serious issues with IPC::Run and quoting of command
503 ### line arguments. using quotes in the wrong place breaks things,
504 ### and in the case of say,
505 ### C:\cygwin\bin\wget.EXE --quiet --passive-ftp --output-document
506 ### "index.html" "http://www.cpan.org/index.html?q=1&y=2"
507 ### it doesn't matter how you quote, it always fails.
508 41 local $IPC::Cmd::USE_IPC_RUN = 0;
509  
510 41 ▶ if( my $file = $self->$sub(
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->$sub("to", $out_to)) 28 13 41
511 to => $out_to
512 )){
513  
514 28 ▶ unless( -e $file && -s _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -s _) { } 28 0 28
Condition33%1/3
-e $file and -s _
!ll&&!rl&&r
0028
MC/DC0%0/2
-e $file and -s _
-e $file -s _
Truth table33%1/3
A and B
A= -e $fileB= -s _
ABresult
0X0
100
111
515 0 $self->_error(loc("'%1' said it fetched '%2', ".
516 "but it was not created",$method,$file));
517  
518 ### mark the failure ###
519 0 $METHOD_FAIL->{$method} = 1;
520  
521 0 next;
522  
523 } else {
524  
525 ### slurp mode?
526 28 ▶ if( ref $target and UNIVERSAL::isa( $target, 'SCALAR' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target and UNIVERSAL::isa($target, "SCALAR")) 14 14 28
Condition66%2/3
ref $target and UNIVERSAL::isa($target, "SCALAR")
!ll&&!rl&&r
14014
MC/DC50%1/2
ref $target and UNIVERSAL::isa($target, "SCALAR")
ref $target UNIVERSAL::isa($target, "SCALAR")
Truth table66%2/3
A and B
A= ref $targetB= UNIVERSAL::isa($target, "SCALAR")
ABresult
0X0
100
111
527  
528 ### open the file
529 14 ▶ open my $fh, "<$file" or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$file" or do { $self->_error(loc("Could not open '%1': %2", $file, $!)); return } 14 0 14
530 0 $self->_error(
531 loc("Could not open '%1': %2", $file, $!));
532 0 return;
533 };
534  
535 ### slurp
536 14 $$target = do { local $/; <$fh> };
537  
538 }
539  
540 28 my $abs = File::Spec->rel2abs( $file );
541 28 return $abs;
542  
543 }
544 }
545 }
546  
547  
548 ### if we got here, we looped over all methods, but we weren't able
549 ### to fetch it.
550 50 return;
551 }
552  
553 ########################
554 ### _*_fetch methods ###
555 ########################
556  
557 ### LWP fetching ###
558 10 ▶sub _lwp_fetch {
Subroutine _lwp_fetch: called CC=12 SCAR=29.3
559 10 my $self = shift;
560 10 my %hash = @_;
561  
562 10 my ($to);
563 10 my $tmpl = {
564 to => { required => 1, store => \$to }
565 };
566 10 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 10 0 10
567  
568 ### modules required to download with lwp ###
569 10 my $use_list = {
570 LWP => '0.0',
571 'LWP::UserAgent' => '0.0',
572 'HTTP::Request' => '0.0',
573 'HTTP::Status' => '0.0',
574 URI => '0.0',
575  
576 };
577  
578 ### Fix CVE-2016-1238 ###
579 10 local $Module::Load::Conditional::FORCE_SAFE_INC = 1;
580 10 ▶ unless( can_load( modules => $use_list ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $use_list)) 0 10 10
581 0 $METHOD_FAIL->{'lwp'} = 1;
582 0 return;
583 }
584  
585 10 ▶ if ($self->scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scheme eq "https") 0 10 10
586 0 my $https_use_list = {
587 'LWP::Protocol::https' => '0.0',
588 };
589  
590 0 ▶ unless ( can_load(modules => $https_use_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $https_use_list)) 0 0 0
591 0 $METHOD_FAIL->{'lwp'} = { 'https' => 1 };
592 0 return;
593 }
594 }
595  
596 ### setup the uri object
597 10 my $uri = URI->new( File::Spec::Unix->catfile(
598 $self->path, $self->file
599 ) );
600  
601 ### special rules apply for file:// uris ###
602 10 $uri->scheme( $self->scheme );
603 10 ▶ $uri->host( $self->scheme eq 'file' ? '' : $self->host );
Branch: 1 branch
BranchTrueFalseTotal
$self->scheme eq 'file' ? : 2 8 10
604  
605 10 ▶ if ($self->userinfo) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->userinfo) { } 0 10 10
elsif ($self->scheme ne 'file') { } 8 2 10
606 0 $uri->userinfo($self->userinfo);
607 } elsif ($self->scheme ne 'file') {
608 8 $uri->userinfo("anonymous:$FROM_EMAIL");
609 }
610  
611 ### set up the useragent object
612 10 my $ua = LWP::UserAgent->new();
613 10 ▶ $ua->timeout( $TIMEOUT ) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 10 0 10
614 10 $ua->agent( $USER_AGENT );
615 10 $ua->from( $FROM_EMAIL );
616 10 $ua->env_proxy;
617  
618 10 ▶ my $res = $ua->mirror($uri, $to) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $res = $ua->mirror($uri, $to) or return 10 0 10
619  
620 ### uptodate or fetched ok ###
621 10 ▶ if ( $res->code == 304 or $res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 304 or $res->code == 200) { } 10 0 10
Condition33%1/3
$res->code == 304 or $res->code == 200
l!l&&r!l&&!r
0100
MC/DC0%0/2
$res->code == 304 or $res->code == 200
$res->code == 304 $res->code == 200
Truth table33%1/3
A or B
A= $res->code == 304B= $res->code == 200
ABresult
1X1
011
000
622 10 return $to;
623  
624 } else {
625 0 return $self->_error(loc("Fetch failed! HTTP response: %1 %2 [%3]",
626 $res->code, HTTP::Status::status_message($res->code),
627 $res->status_line));
628 }
629  
630 }
631  
632 ### HTTP::Tiny fetching ###
633 8 ▶sub _httptiny_fetch {
Subroutine _httptiny_fetch: called CC=7 SCAR=22.6
634 8 my $self = shift;
635 8 my %hash = @_;
636  
637 8 my ($to);
638 8 my $tmpl = {
639 to => { required => 1, store => \$to }
640 };
641 8 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 8 0 8
642  
643 8 my $use_list = {
644 'HTTP::Tiny' => '0.008',
645  
646 };
647  
648 ### Fix CVE-2016-1238 ###
649 8 local $Module::Load::Conditional::FORCE_SAFE_INC = 1;
650 8 ▶ unless( can_load(modules => $use_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $use_list)) 0 8 8
651 0 $METHOD_FAIL->{'httptiny'} = 1;
652 0 return;
653 }
654 8 ▶ if ( $self->scheme eq 'https' && !HTTP::Tiny->can_ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scheme eq "https" and not "HTTP::Tiny"->can_ssl) 0 8 8
Condition33%1/3
$self->scheme eq "https" and not "HTTP::Tiny"->can_ssl
!ll&&!rl&&r
800
MC/DC0%0/2
$self->scheme eq "https" and not "HTTP::Tiny"->can_ssl
$self->scheme eq "https" not "HTTP::Tiny"->can_ssl
Truth table33%1/3
A and not(B)
A= $self->scheme eq "https"B= not "HTTP::Tiny"->can_ssl
ABresult
0X0
100
111
655 0 $METHOD_FAIL->{'httptiny'} = 1;
656 0 return;
657 }
658  
659 8 my $uri = $self->uri;
660  
661 8 ▶ my $http = HTTP::Tiny->new( ( $TIMEOUT ? ( timeout => $TIMEOUT ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$File::Fetch::TIMEOUT ? : 8 0 8
662  
663 8 my $rc = $http->mirror( $uri, $to );
664  
665 8 ▶ unless ( $rc->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc->{"success"}) 0 8 8
666  
667 return $self->_error(loc( "Fetch failed! HTTP response: %1 [%2]",
668 0 $rc->{status}, $rc->{reason} ) );
669  
670 }
671  
672 8 return $to;
673  
674 }
675  
676 ### HTTP::Lite fetching ###
677 1 ▶sub _httplite_fetch {
Subroutine _httplite_fetch: called CC=13 SCAR=46.5
678 1 my $self = shift;
679 1 my %hash = @_;
680  
681 1 my ($to);
682 1 my $tmpl = {
683 to => { required => 1, store => \$to }
684 };
685 1 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 1 0 1
686  
687 ### modules required to download with lwp ###
688 1 my $use_list = {
689 'HTTP::Lite' => '2.2',
690 'MIME::Base64' => '0',
691 };
692  
693 ### Fix CVE-2016-1238 ###
694 1 local $Module::Load::Conditional::FORCE_SAFE_INC = 1;
695 1 ▶ unless( can_load(modules => $use_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $use_list)) 1 0 1
696 1 $METHOD_FAIL->{'httplite'} = 1;
697 1 return;
698 }
699  
700 0 my $uri = $self->uri;
701 0 my $retries = 0;
702  
703 0 ▶ RETRIES: while ( $retries++ < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries++ < 5) 0 0 0
704  
705 0 my $http = HTTP::Lite->new();
706 # Naughty naughty but there isn't any accessor/setter
707 0 ▶ $http->{timeout} = $TIMEOUT if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
708 0 $http->http11_mode(1);
709  
710 0 ▶ if ($self->userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->userinfo) 0 0 0
711 0 my $encoded = MIME::Base64::encode($self->userinfo, '');
712 0 $http->add_req_header("Authorization", "Basic $encoded");
713 }
714  
715 0 my $fh = FileHandle->new;
716  
717 0 ▶ unless ( $fh->open($to,'>') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($to, ">")) 0 0 0
718 0 return $self->_error(loc(
719 "Could not open '%1' for writing: %2",$to,$!));
720 }
721  
722 0 $fh->autoflush(1);
723  
724 0 binmode $fh;
725  
726 0 ▶ my $rc = $http->request( $uri, sub { my ($self,$dref,$cbargs) = @_; local $\; print {$cbargs} $$dref }, $fh );
Subroutine __ANON__: not called CC=1 SCAR=6.9
727  
728 0 close $fh;
729  
730 0 ▶ if ( $rc == 301 || $rc == 302 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rc == 301 or $rc == 302) { } 0 0 0
elsif ($rc == 200) { } 0 0 0
Condition0%0/3
$rc == 301 or $rc == 302
l!l&&r!l&&!r
000
MC/DC0%0/2
$rc == 301 or $rc == 302
$rc == 301 $rc == 302
Truth table0%0/3
A or B
A= $rc == 301B= $rc == 302
ABresult
1X1
011
000
731 0 my $loc;
732 0 HEADERS: for ($http->headers_array) {
733 0 ▶ /Location: (\S+)/ and $loc = $1, last HEADERS;
Branch: 1 branch
BranchTrueFalseTotal
/Location: (\S+)/ and $loc = $1, last HEADERS 0 0 0
734 }
735 #$loc or last; # Think we should squeal here.
736 0 ▶ if ($loc =~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc =~ m[^/]) { } 0 0 0
737 0 $uri =~ s{^(\w+?://[^/]+)/.*$}{$1};
738 0 $uri .= $loc;
739 }
740 else {
741 0 $uri = $loc;
742 }
743 0 next RETRIES;
744 }
745 elsif ( $rc == 200 ) {
746 0 return $to;
747 }
748 else {
749 0 return $self->_error(loc("Fetch failed! HTTP response: %1 [%2]",
750 $rc, $http->status_message));
751 }
752  
753 } # Loop for 5 retries.
754  
755 0 return $self->_error("Fetch failed! Gave up after 5 tries");
756  
757 }
758  
759 ### Simple IO::Socket::INET fetching ###
760 8 ▶sub _iosock_fetch {
Subroutine _iosock_fetch: called CC=14 SCAR=30.4
761 8 my $self = shift;
762 8 my %hash = @_;
763  
764 8 my ($to);
765 8 my $tmpl = {
766 to => { required => 1, store => \$to }
767 };
768 8 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 8 0 8
769  
770 8 my $use_list = {
771 'IO::Socket::INET' => '0.0',
772 'IO::Select' => '0.0',
773 };
774  
775 ### Fix CVE-2016-1238 ###
776 8 local $Module::Load::Conditional::FORCE_SAFE_INC = 1;
777 8 ▶ unless( can_load(modules => $use_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $use_list)) 0 8 8
778 0 $METHOD_FAIL->{'iosock'} = 1;
779 0 return;
780 }
781  
782 8 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->host =~ /:/ ? : 0 8 8
783 PeerHost => $self->host,
784 ( $self->host =~ /:/ ? () : ( PeerPort => 80 ) ),
785 );
786  
787 8 ▶ unless ( $sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 8 8
788 0 return $self->_error(loc("Could not open socket to '%1', '%2'",$self->host,$!));
789 }
790  
791 8 my $fh = FileHandle->new;
792  
793 # Check open()
794  
795 8 ▶ unless ( $fh->open($to,'>') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($to, ">")) 0 8 8
796 0 return $self->_error(loc(
797 "Could not open '%1' for writing: %2",$to,$!));
798 }
799  
800 8 $fh->autoflush(1);
801 8 binmode $fh;
802  
803 8 my $path = File::Spec::Unix->catfile( $self->path, $self->file );
804 8 my $req = "GET $path HTTP/1.0\x0d\x0aHost: " . $self->host . "\x0d\x0a\x0d\x0a";
805 8 $sock->send( $req );
806  
807 8 my $select = IO::Select->new( $sock );
808  
809 8 my $resp = '';
810 8 my $normal = 0;
811 8 ▶ while ( $select->can_read( $TIMEOUT || 60 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($File::Fetch::TIMEOUT || 60)) 16 8 24
Condition50%1/2
$File::Fetch::TIMEOUT || 60
l!l
240
MC/DC0%0/1
$File::Fetch::TIMEOUT || 60
$File::Fetch::TIMEOUT
Truth table50%1/2
A
A= $File::Fetch::TIMEOUT
Aresult
11
00
812 16 my $ret = $sock->sysread( $resp, 4096, length($resp) );
813 16 ▶ if ( !defined $ret or $ret == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ret or $ret == 0) 8 8 16
Condition66%2/3
not defined $ret or $ret == 0
l!l&&r!l&&!r
088
MC/DC50%1/2
not defined $ret or $ret == 0
not defined $ret $ret == 0
Truth table66%2/3
not(A) or B
A= not defined $retB= $ret == 0
ABresult
1X1
011
000
814 8 $select->remove( $sock );
815 8 $normal++;
816 }
817 }
818 8 close $sock;
819  
820 8 ▶ unless ( $normal ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($normal) 0 8 8
821 0 ▶ return $self->_error(loc("Socket timed out after '%1' seconds", ( $TIMEOUT || 60 )));
Condition0%0/2
$File::Fetch::TIMEOUT || 60
l!l
00
MC/DC0%0/1
$File::Fetch::TIMEOUT || 60
$File::Fetch::TIMEOUT
Truth table0%0/2
A
A= $File::Fetch::TIMEOUT
Aresult
11
00
822 }
823  
824 # Check the "response"
825 # Strip preceding blank lines apparently they are allowed (RFC 2616 4.1)
826 8 $resp =~ s/^(\x0d?\x0a)+//;
827 # Check it is an HTTP response
828 8 ▶ unless ( $resp =~ m!^HTTP/(\d+)\.(\d+)!i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp =~ m[^HTTP/(\d+)\.(\d+)]i) 0 8 8
829 0 return $self->_error(loc("Did not get a HTTP response from '%1'",$self->host));
830 }
831  
832 # Check for OK
833 8 my ($code) = $resp =~ m!^HTTP/\d+\.\d+\s+(\d+)!i;
834 8 ▶ unless ( $code eq '200' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code eq 200) 8 0 8
835 8 return $self->_error(loc("Got a '%1' from '%2' expected '200'",$code,$self->host));
836 }
837  
838 {
839 0 local $\;
840 0 print $fh +($resp =~ m/\x0d\x0a\x0d\x0a(.*)$/s )[0];
841 }
842 0 close $fh;
843 0 return $to;
844 }
845  
846 ### Net::FTP fetching
847 0 ▶sub _netftp_fetch {
Subroutine _netftp_fetch: not called CC=7 SCAR=40.3
848 0 my $self = shift;
849 0 my %hash = @_;
850  
851 0 my ($to);
852 0 my $tmpl = {
853 to => { required => 1, store => \$to }
854 };
855 0 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 0 0
856  
857 ### required modules ###
858 0 my $use_list = { 'Net::FTP' => 0 };
859  
860 ### Fix CVE-2016-1238 ###
861 0 local $Module::Load::Conditional::FORCE_SAFE_INC = 1;
862 0 ▶ unless( can_load( modules => $use_list ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", $use_list)) 0 0 0
863 0 $METHOD_FAIL->{'netftp'} = 1;
864 0 return;
865 }
866  
867 ### make connection ###
868 0 my $ftp;
869 0 my @options = ($self->host);
870 0 ▶ push(@options, Timeout => $TIMEOUT) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
871 0 ▶ unless( $ftp = Net::FTP->new( @options ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp = "Net::FTP"->new(@options)) 0 0 0
872 0 return $self->_error(loc("Ftp creation failed: %1",$@));
873 }
874  
875 ### login ###
876 0 ▶ unless( $ftp->login( anonymous => $FROM_EMAIL ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->login("anonymous", $File::Fetch::FROM_EMAIL)) 0 0 0
877 0 return $self->_error(loc("Could not login to '%1'",$self->host));
878 }
879  
880 ### set binary mode, just in case ###
881 0 $ftp->binary;
882  
883 ### create the remote path
884 ### remember remote paths are unix paths! [#11483]
885 0 my $remote = File::Spec::Unix->catfile( $self->path, $self->file );
886  
887 ### fetch the file ###
888 0 my $target;
889 0 ▶ unless( $target = $ftp->get( $remote, $to ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target = $ftp->get($remote, $to)) 0 0 0
890 0 return $self->_error(loc("Could not fetch '%1' from '%2'",
891 $remote, $self->host));
892 }
893  
894 ### log out ###
895 0 $ftp->quit;
896  
897 0 return $target;
898  
899 }
900  
901 ### /bin/wget fetch ###
902 1 ▶sub _wget_fetch {
Subroutine _wget_fetch: called CC=9 SCAR=35.6
903 1 my $self = shift;
904 1 my %hash = @_;
905  
906 1 my ($to);
907 1 my $tmpl = {
908 to => { required => 1, store => \$to }
909 };
910 1 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 1 0 1
911  
912 1 my $wget;
913 ### see if we have a wget binary ###
914 1 ▶ unless( $wget = can_run('wget') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wget = can_run("wget")) 1 0 1
915 1 $METHOD_FAIL->{'wget'} = 1;
916 1 return;
917 }
918  
919 ### no verboseness, thanks ###
920 0 my $cmd = [ $wget, '--quiet' ];
921  
922 ### if a timeout is set, add it ###
923 0 ▶ push(@$cmd, '--timeout=' . $TIMEOUT) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
924  
925 ### run passive if specified ###
926 0 ▶ push @$cmd, '--passive-ftp' if $self->scheme eq 'ftp' && $FTP_PASSIVE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->scheme eq "ftp" and $File::Fetch::FTP_PASSIVE 0 0 0
Condition0%0/3
$self->scheme eq "ftp" and $File::Fetch::FTP_PASSIVE
!ll&&!rl&&r
000
MC/DC0%0/2
$self->scheme eq "ftp" and $File::Fetch::FTP_PASSIVE
$self->scheme eq "ftp" $File::Fetch::FTP_PASSIVE
Truth table0%0/3
A and B
A= $self->scheme eq "ftp"B= $File::Fetch::FTP_PASSIVE
ABresult
0X0
100
111
927  
928 ### set the output document, add the uri ###
929 0 push @$cmd, '--output-document', $to, $self->uri;
930  
931 ### with IPC::Cmd > 0.41, this is fixed in teh library,
932 ### and there's no need for special casing any more.
933 ### DO NOT quote things for IPC::Run, it breaks stuff.
934 # $IPC::Cmd::USE_IPC_RUN
935 # ? ($to, $self->uri)
936 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
937  
938 ### shell out ###
939 0 my $captured;
940 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
941 buffer => \$captured,
942 verbose => $DEBUG
943 )) {
944 ### wget creates the output document always, even if the fetch
945 ### fails.. so unlink it in that case
946 0 ▶ 1 while unlink $to;
Branch: 1 branch
BranchTrueFalseTotal
unlink $to and do { '???' } 0 0 0
947  
948 0 ▶ return $self->_error(loc( "Command failed: %1", $captured || '' ));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
949 }
950  
951 0 return $to;
952 }
953  
954 ### /bin/lftp fetch ###
955 1 ▶sub _lftp_fetch {
Subroutine _lftp_fetch: called CC=9 SCAR=37.5
956 1 my $self = shift;
957 1 my %hash = @_;
958  
959 1 my ($to);
960 1 my $tmpl = {
961 to => { required => 1, store => \$to }
962 };
963 1 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 1 0 1
964  
965 ### see if we have a lftp binary ###
966 1 my $lftp;
967 1 ▶ unless( $lftp = can_run('lftp') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lftp = can_run("lftp")) 1 0 1
968 1 $METHOD_FAIL->{'lftp'} = 1;
969 1 return;
970 }
971  
972 ### no verboseness, thanks ###
973 0 my $cmd = [ $lftp, '-f' ];
974  
975 0 my $fh = File::Temp->new;
976  
977 0 my $str;
978  
979 ### if a timeout is set, add it ###
980 0 ▶ $str .= "set net:timeout $TIMEOUT;\n" if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
981  
982 ### lftp can get stuck in a loop of retries without this
983 0 $str .= "set net:reconnect-interval-base 5;\nset net:max-retries 2;\n";
984  
985 ### run passive if specified ###
986 0 ▶ $str .= "set ftp:passive-mode 1;\n" if $FTP_PASSIVE;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::FTP_PASSIVE 0 0 0
987  
988 ### set the output document, add the uri ###
989 ### quote the URI, because lftp supports certain shell
990 ### expansions, most notably & for backgrounding.
991 ### ' quote does nto work, must be "
992 0 $str .= q[get ']. $self->uri .q[' -o ]. $to . $/;
993  
994 0 ▶ if( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Fetch::DEBUG) 0 0 0
995 0 my $pp_str = join ' ', split $/, $str;
996 0 print "# lftp command: $pp_str\n";
997 }
998  
999 ### write straight to the file.
1000 0 $fh->autoflush(1);
1001 0 print $fh $str;
1002  
1003 ### the command needs to be 1 string to be executed
1004 0 push @$cmd, $fh->filename;
1005  
1006 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1007 ### and there's no need for special casing any more.
1008 ### DO NOT quote things for IPC::Run, it breaks stuff.
1009 # $IPC::Cmd::USE_IPC_RUN
1010 # ? ($to, $self->uri)
1011 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
1012  
1013  
1014 ### shell out ###
1015 0 my $captured;
1016 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1017 buffer => \$captured,
1018 verbose => $DEBUG
1019 )) {
1020 ### wget creates the output document always, even if the fetch
1021 ### fails.. so unlink it in that case
1022 0 ▶ 1 while unlink $to;
Branch: 1 branch
BranchTrueFalseTotal
unlink $to and do { '???' } 0 0 0
1023  
1024 0 ▶ return $self->_error(loc( "Command failed: %1", $captured || '' ));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
1025 }
1026  
1027 0 return $to;
1028 }
1029  
1030  
1031  
1032 ### /bin/ftp fetch ###
1033 0 ▶sub _ftp_fetch {
Subroutine _ftp_fetch: not called CC=6 SCAR=37.4
1034 0 my $self = shift;
1035 0 my %hash = @_;
1036  
1037 0 my ($to);
1038 0 my $tmpl = {
1039 to => { required => 1, store => \$to }
1040 };
1041 0 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 0 0
1042  
1043 ### see if we have a ftp binary ###
1044 0 my $ftp;
1045 0 ▶ unless( $ftp = can_run('ftp') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp = can_run("ftp")) 0 0 0
1046 0 $METHOD_FAIL->{'ftp'} = 1;
1047 0 return;
1048 }
1049  
1050 0 my $fh = FileHandle->new;
1051  
1052 0 local $SIG{CHLD} = 'IGNORE';
1053  
1054 0 ▶ unless ($fh->open("$ftp -n", '|-')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open("$ftp -n", "|-")) 0 0 0
1055 0 return $self->_error(loc("%1 creation failed: %2", $ftp, $!));
1056 }
1057  
1058 0 my @dialog = (
1059 "lcd " . dirname($to),
1060 "open " . $self->host,
1061 "user anonymous $FROM_EMAIL",
1062 "cd /",
1063 "cd " . $self->path,
1064 "binary",
1065 "get " . $self->file . " " . $self->output_file,
1066 "quit",
1067 );
1068  
1069 0 foreach (@dialog) { $fh->print($_, "\n") }
1070 0 ▶ $fh->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or return 0 0 0
1071  
1072 0 return $to;
1073 }
1074  
1075 ### lynx is stupid - it decompresses any .gz file it finds to be text
1076 ### use /bin/lynx to fetch files
1077 1 ▶sub _lynx_fetch {
Subroutine _lynx_fetch: called CC=15 SCAR=49.1
1078 1 my $self = shift;
1079 1 my %hash = @_;
1080  
1081 1 my ($to);
1082 1 my $tmpl = {
1083 to => { required => 1, store => \$to }
1084 };
1085 1 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 1 0 1
1086  
1087 ### see if we have a lynx binary ###
1088 1 my $lynx;
1089 1 ▶ unless ( $lynx = can_run('lynx') ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($lynx = can_run("lynx")) 1 0 1
1090 1 $METHOD_FAIL->{'lynx'} = 1;
1091 1 return;
1092 }
1093  
1094 0 ▶ unless( IPC::Cmd->can_capture_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("IPC::Cmd"->can_capture_buffer) 0 0 0
1095 0 $METHOD_FAIL->{'lynx'} = 1;
1096  
1097 0 return $self->_error(loc(
1098 "Can not capture buffers. Can not use '%1' to fetch files",
1099 'lynx' ));
1100 }
1101  
1102 ### check if the HTTP resource exists ###
1103 0 ▶ if ($self->uri =~ /^https?:\/\//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri =~ m[^https?://]i) 0 0 0
1104 0 my $cmd = [
1105 $lynx,
1106 '-head',
1107 '-source',
1108 "-auth=anonymous:$FROM_EMAIL",
1109 ];
1110  
1111 0 ▶ push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
1112  
1113 0 push @$cmd, $self->uri;
1114  
1115 ### shell out ###
1116 0 my $head;
1117 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$head, "verbose", $File::Fetch::DEBUG)) 0 0 0
1118 buffer => \$head,
1119 verbose => $DEBUG )
1120 ) {
1121 0 ▶ return $self->_error(loc("Command failed: %1", $head || ''));
Condition0%0/2
$head || ''
l!l
00
MC/DC0%0/1
$head || ''
$head
Truth table0%0/2
A
A= $head
Aresult
11
00
1122 }
1123  
1124 0 ▶ unless($head =~ /^HTTP\/\d+\.\d+ 200\b/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($head =~ m[^HTTP/\d+\.\d+ 200\b]) 0 0 0
1125 0 ▶ return $self->_error(loc("Command failed: %1", $head || ''));
Condition0%0/2
$head || ''
l!l
00
MC/DC0%0/1
$head || ''
$head
Truth table0%0/2
A
A= $head
Aresult
11
00
1126 }
1127 }
1128  
1129 ### write to the output file ourselves, since lynx ass_u_mes to much
1130 0 ▶ my $local = FileHandle->new( $to, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
my $local = "FileHandle"->new($to, "w") or return $self->_error(loc("Could not open '%1' for writing: %2", $to, $!)) 0 0 0
1131 or return $self->_error(loc(
1132 "Could not open '%1' for writing: %2",$to,$!));
1133  
1134 ### dump to stdout ###
1135 0 my $cmd = [
1136 $lynx,
1137 '-source',
1138 "-auth=anonymous:$FROM_EMAIL",
1139 ];
1140  
1141 0 ▶ push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
1142  
1143 ### DO NOT quote things for IPC::Run, it breaks stuff.
1144 0 push @$cmd, $self->uri;
1145  
1146 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1147 ### and there's no need for special casing any more.
1148 ### DO NOT quote things for IPC::Run, it breaks stuff.
1149 # $IPC::Cmd::USE_IPC_RUN
1150 # ? $self->uri
1151 # : QUOTE. $self->uri .QUOTE;
1152  
1153  
1154 ### shell out ###
1155 0 my $captured;
1156 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1157 buffer => \$captured,
1158 verbose => $DEBUG )
1159 ) {
1160 0 ▶ return $self->_error(loc("Command failed: %1", $captured || ''));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
1161 }
1162  
1163 ### print to local file ###
1164 ### XXX on a 404 with a special error page, $captured will actually
1165 ### hold the contents of that page, and make it *appear* like the
1166 ### request was a success, when really it wasn't :(
1167 ### there doesn't seem to be an option for lynx to change the exit
1168 ### code based on a 4XX status or so.
1169 ### the closest we can come is using --error_file and parsing that,
1170 ### which is very unreliable ;(
1171 0 $local->print( $captured );
1172 0 ▶ $local->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$local->close or return 0 0 0
1173  
1174 0 return $to;
1175 }
1176  
1177 ### use /bin/ncftp to fetch files
1178 0 ▶sub _ncftp_fetch {
Subroutine _ncftp_fetch: not called CC=7 SCAR=40.3
1179 0 my $self = shift;
1180 0 my %hash = @_;
1181  
1182 0 my ($to);
1183 0 my $tmpl = {
1184 to => { required => 1, store => \$to }
1185 };
1186 0 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 0 0
1187  
1188 ### we can only set passive mode in interactive sessions, so bail out
1189 ### if $FTP_PASSIVE is set
1190 0 ▶ return if $FTP_PASSIVE;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::FTP_PASSIVE 0 0 0
1191  
1192 ### see if we have a ncftp binary ###
1193 0 my $ncftp;
1194 0 ▶ unless( $ncftp = can_run('ncftp') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ncftp = can_run("ncftp")) 0 0 0
1195 0 $METHOD_FAIL->{'ncftp'} = 1;
1196 0 return;
1197 }
1198  
1199 0 ▶ my $cmd = [
Branch: 1 branch
BranchTrueFalseTotal
$IPC::Cmd::USE_IPC_RUN ? : 0 0 0
1200 $ncftp,
1201 '-V', # do not be verbose
1202 '-p', $FROM_EMAIL, # email as password
1203 $self->host, # hostname
1204 dirname($to), # local dir for the file
1205 # remote path to the file
1206 ### DO NOT quote things for IPC::Run, it breaks stuff.
1207 $IPC::Cmd::USE_IPC_RUN
1208 ? File::Spec::Unix->catdir( $self->path, $self->file )
1209 : QUOTE. File::Spec::Unix->catdir(
1210 $self->path, $self->file ) .QUOTE
1211  
1212 ];
1213  
1214 ### shell out ###
1215 0 my $captured;
1216 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1217 buffer => \$captured,
1218 verbose => $DEBUG )
1219 ) {
1220 0 ▶ return $self->_error(loc("Command failed: %1", $captured || ''));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
1221 }
1222  
1223 0 return $to;
1224  
1225 }
1226  
1227 ### use /bin/curl to fetch files
1228 8 ▶sub _curl_fetch {
Subroutine _curl_fetch: called CC=10 SCAR=27.5
1229 8 my $self = shift;
1230 8 my %hash = @_;
1231  
1232 8 my ($to);
1233 8 my $tmpl = {
1234 to => { required => 1, store => \$to }
1235 };
1236 8 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 8 0 8
1237 8 my $curl;
1238 8 ▶ unless ( $curl = can_run('curl') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($curl = can_run("curl")) 0 8 8
1239 0 $METHOD_FAIL->{'curl'} = 1;
1240 0 return;
1241 }
1242  
1243 ### these long opts are self explanatory - I like that -jmb
1244 8 my $cmd = [ $curl, '-q' ];
1245  
1246 8 ▶ push(@$cmd, '-4') if $^O eq 'netbsd' && $FORCEIPV4; # only seen this on NetBSD so far
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "netbsd" and $File::Fetch::FORCEIPV4 0 8 8
Condition33%1/3
$^O eq "netbsd" and $File::Fetch::FORCEIPV4
!ll&&!rl&&r
800
MC/DC0%0/2
$^O eq "netbsd" and $File::Fetch::FORCEIPV4
$^O eq "netbsd" $File::Fetch::FORCEIPV4
Truth table33%1/3
A and B
A= $^O eq "netbsd"B= $File::Fetch::FORCEIPV4
ABresult
0X0
100
111
1247  
1248 8 ▶ push(@$cmd, '--connect-timeout', $TIMEOUT) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 8 0 8
1249  
1250 8 ▶ push(@$cmd, '--silent') unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Fetch::DEBUG 8 0 8
1251  
1252 ### curl does the right thing with passive, regardless ###
1253 8 ▶ if ($self->scheme eq 'ftp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scheme eq "ftp") 0 8 8
1254 0 push(@$cmd, '--user', "anonymous:$FROM_EMAIL");
1255 }
1256  
1257 ### curl doesn't follow 302 (temporarily moved) etc automatically
1258 ### so we add --location to enable that.
1259 8 push @$cmd, '--fail', '--location', '--output', $to, $self->uri;
1260  
1261 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1262 ### and there's no need for special casing any more.
1263 ### DO NOT quote things for IPC::Run, it breaks stuff.
1264 # $IPC::Cmd::USE_IPC_RUN
1265 # ? ($to, $self->uri)
1266 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
1267  
1268  
1269 8 my $captured;
1270 8 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 8 8
1271 buffer => \$captured,
1272 verbose => $DEBUG )
1273 ) {
1274  
1275 0 ▶ return $self->_error(loc("Command failed: %1", $captured || ''));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
1276 }
1277  
1278 8 return $to;
1279  
1280 }
1281  
1282 ### /usr/bin/fetch fetch! ###
1283 1 ▶sub _fetch_fetch {
Subroutine _fetch_fetch: called CC=9 SCAR=34.7
1284 1 my $self = shift;
1285 1 my %hash = @_;
1286  
1287 1 my ($to);
1288 1 my $tmpl = {
1289 to => { required => 1, store => \$to }
1290 };
1291 1 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 1 0 1
1292  
1293 ### see if we have a fetch binary ###
1294 1 my $fetch;
1295 1 ▶ unless( HAS_FETCH and $fetch = can_run('fetch') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HAS_FETCH() and $fetch = can_run("fetch")) 1 0 1
Condition33%1/3
&HAS_FETCH() and $fetch = can_run("fetch")
!ll&&!rl&&r
100
MC/DC0%0/2
&HAS_FETCH() and $fetch = can_run("fetch")
&HAS_FETCH() $fetch = can_run("fetch")
Truth table33%1/3
A and B
A= &HAS_FETCH()B= $fetch = can_run("fetch")
ABresult
0X0
100
111
1296 1 $METHOD_FAIL->{'fetch'} = 1;
1297 1 return;
1298 }
1299  
1300 ### no verboseness, thanks ###
1301 0 my $cmd = [ $fetch, '-q' ];
1302  
1303 ### if a timeout is set, add it ###
1304 0 ▶ push(@$cmd, '-T', $TIMEOUT) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
1305  
1306 ### run passive if specified ###
1307 #push @$cmd, '-p' if $FTP_PASSIVE;
1308 0 ▶ local $ENV{'FTP_PASSIVE_MODE'} = 1 if $FTP_PASSIVE;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::FTP_PASSIVE 0 0 0
1309  
1310 ### set the output document, add the uri ###
1311 0 push @$cmd, '-o', $to, $self->uri;
1312  
1313 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1314 ### and there's no need for special casing any more.
1315 ### DO NOT quote things for IPC::Run, it breaks stuff.
1316 # $IPC::Cmd::USE_IPC_RUN
1317 # ? ($to, $self->uri)
1318 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
1319  
1320 ### shell out ###
1321 0 my $captured;
1322 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1323 buffer => \$captured,
1324 verbose => $DEBUG
1325 )) {
1326 ### wget creates the output document always, even if the fetch
1327 ### fails.. so unlink it in that case
1328 0 ▶ 1 while unlink $to;
Branch: 1 branch
BranchTrueFalseTotal
unlink $to and do { '???' } 0 0 0
1329  
1330 0 ▶ return $self->_error(loc( "Command failed: %1", $captured || '' ));
Condition0%0/2
$captured || ''
l!l
00
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= $captured
Aresult
11
00
1331 }
1332  
1333 0 return $to;
1334 }
1335  
1336 ### use File::Copy for fetching file:// urls ###
1337 ###
1338 ### See section 3.10 of RFC 1738 (https://datatracker.ietf.org/doc/html/rfc1738#section-3.10)
1339 ### Also see wikipedia on file:// (http://en.wikipedia.org/wiki/File://)
1340 ###
1341  
1342 2 ▶sub _file_fetch {
Subroutine _file_fetch: called CC=8 SCAR=24.5
1343 2 my $self = shift;
1344 2 my %hash = @_;
1345  
1346 2 my ($to);
1347 2 my $tmpl = {
1348 to => { required => 1, store => \$to }
1349 };
1350 2 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 2 0 2
1351  
1352  
1353  
1354 ### prefix a / on unix systems with a file uri, since it would
1355 ### look somewhat like this:
1356 ### file:///home/kane/file
1357 ### whereas windows file uris for 'c:\some\dir\file' might look like:
1358 ### file:///C:/some/dir/file
1359 ### file:///C|/some/dir/file
1360 ### or for a network share '\\host\share\some\dir\file':
1361 ### file:////host/share/some/dir/file
1362 ###
1363 ### VMS file uri's for 'DISK$USER:[MY.NOTES]NOTE123456.TXT' might look like:
1364 ### file://vms.host.edu/disk$user/my/notes/note12345.txt
1365 ###
1366  
1367 2 my $path = $self->path;
1368 2 my $vol = $self->vol;
1369 2 my $share = $self->share;
1370  
1371 2 my $remote;
1372 2 ▶ if (!$share and $self->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $share and $self->host) 0 2 2
Condition33%1/3
not $share and $self->host
!ll&&!rl&&r
020
MC/DC0%0/2
not $share and $self->host
not $share $self->host
Truth table33%1/3
not(A) and B
A= not $shareB= $self->host
ABresult
0X0
100
111
1373 0 return $self->_error(loc(
1374 "Currently %1 cannot handle hosts in %2 urls",
1375 'File::Fetch', 'file://'
1376 ));
1377 }
1378  
1379 2 ▶ if( $vol ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vol) { } 0 2 2
elsif ($share) { } 0 2 2
1380 0 $path = File::Spec->catdir( split /\//, $path );
1381 0 $remote = File::Spec->catpath( $vol, $path, $self->file);
1382  
1383 } elsif( $share ) {
1384 ### win32 specific, and a share name, so we wont bother with File::Spec
1385 0 $path =~ s|/+|\\|g;
1386 0 $remote = "\\\\".$self->host."\\$share\\$path";
1387  
1388 } else {
1389 ### File::Spec on VMS can not currently handle UNIX syntax.
1390 2 my $file_class = ON_VMS
1391 ? 'File::Spec::Unix'
1392 : 'File::Spec';
1393  
1394 2 $remote = $file_class->catfile( $path, $self->file );
1395 }
1396  
1397 ### File::Copy is littered with 'die' statements :( ###
1398 2 my $rv = eval { File::Copy::copy( $remote, $to ) };
1399  
1400 ### something went wrong ###
1401 2 ▶ if( !$rv or $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rv or $@) 0 2 2
Condition33%1/3
not $rv or $@
l!l&&r!l&&!r
002
MC/DC0%0/2
not $rv or $@
not $rv $@
Truth table33%1/3
not(A) or B
A= not $rvB= $@
ABresult
1X1
011
000
1402 0 return $self->_error(loc("Could not copy '%1' to '%2': %3 %4",
1403 $remote, $to, $!, $@));
1404 }
1405  
1406 2 return $to;
1407 }
1408  
1409 ### use /usr/bin/rsync to fetch files
1410 0 ▶sub _rsync_fetch {
Subroutine _rsync_fetch: not called CC=8 SCAR=42.8
1411 0 my $self = shift;
1412 0 my %hash = @_;
1413  
1414 0 my ($to);
1415 0 my $tmpl = {
1416 to => { required => 1, store => \$to }
1417 };
1418 0 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 0 0
1419 0 my $rsync;
1420 0 ▶ unless ( $rsync = can_run('rsync') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsync = can_run("rsync")) 0 0 0
1421 0 $METHOD_FAIL->{'rsync'} = 1;
1422 0 return;
1423 }
1424  
1425 0 my $cmd = [ $rsync ];
1426  
1427 ### XXX: rsync has no I/O timeouts at all, by default
1428 0 ▶ push(@$cmd, '--timeout=' . $TIMEOUT) if $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Fetch::TIMEOUT 0 0 0
1429  
1430 0 ▶ push(@$cmd, '--quiet') unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Fetch::DEBUG 0 0 0
1431  
1432 ### DO NOT quote things for IPC::Run, it breaks stuff.
1433 0 push @$cmd, $self->uri, $to;
1434  
1435 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1436 ### and there's no need for special casing any more.
1437 ### DO NOT quote things for IPC::Run, it breaks stuff.
1438 # $IPC::Cmd::USE_IPC_RUN
1439 # ? ($to, $self->uri)
1440 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
1441  
1442 0 my $captured;
1443 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1444 buffer => \$captured,
1445 verbose => $DEBUG )
1446 ) {
1447  
1448 0 ▶ return $self->_error(loc("Command %1 failed: %2",
Condition0%0/4
join($", @$cmd) || ''
l!l
00
$captured || ''
l!l
00
MC/DC0%0/1
join($", @$cmd) || ''
join($", @$cmd)
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= join($", @$cmd)
Aresult
11
00
Truth table0%0/2
A
A= $captured
Aresult
11
00
1449 "@$cmd" || '', $captured || ''));
1450 }
1451  
1452 0 return $to;
1453  
1454 }
1455  
1456 ### use git to fetch files
1457 0 ▶sub _git_fetch {
Subroutine _git_fetch: not called CC=7 SCAR=40.3
1458 0 my $self = shift;
1459 0 my %hash = @_;
1460  
1461 0 my ($to);
1462 0 my $tmpl = {
1463 to => { required => 1, store => \$to }
1464 };
1465 0 ▶ check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
check($tmpl, \%hash) or return 0 0 0
1466 0 my $git;
1467 0 ▶ unless ( $git = can_run('git') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($git = can_run("git")) 0 0 0
1468 0 $METHOD_FAIL->{'git'} = 1;
1469 0 return;
1470 }
1471  
1472 0 my $cmd = [ $git, 'clone' ];
1473  
1474 #push(@$cmd, '--timeout=' . $TIMEOUT) if $TIMEOUT;
1475  
1476 0 ▶ push(@$cmd, '--quiet') unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Fetch::DEBUG 0 0 0
1477  
1478 ### DO NOT quote things for IPC::Run, it breaks stuff.
1479 0 push @$cmd, $self->uri, $to;
1480  
1481 ### with IPC::Cmd > 0.41, this is fixed in teh library,
1482 ### and there's no need for special casing any more.
1483 ### DO NOT quote things for IPC::Run, it breaks stuff.
1484 # $IPC::Cmd::USE_IPC_RUN
1485 # ? ($to, $self->uri)
1486 # : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
1487  
1488 0 my $captured;
1489 0 ▶ unless(run( command => $cmd,
Branch: 1 branch
BranchTrueFalseTotal
unless (run("command", $cmd, "buffer", \$captured, "verbose", $File::Fetch::DEBUG)) 0 0 0
1490 buffer => \$captured,
1491 verbose => $DEBUG )
1492 ) {
1493  
1494 0 ▶ return $self->_error(loc("Command %1 failed: %2",
Condition0%0/4
join($", @$cmd) || ''
l!l
00
$captured || ''
l!l
00
MC/DC0%0/1
join($", @$cmd) || ''
join($", @$cmd)
MC/DC0%0/1
$captured || ''
$captured
Truth table0%0/2
A
A= join($", @$cmd)
Aresult
11
00
Truth table0%0/2
A
A= $captured
Aresult
11
00
1495 "@$cmd" || '', $captured || ''));
1496 }
1497  
1498 0 return $to;
1499  
1500 }
1501  
1502 #################################
1503 #
1504 # Error code
1505 #
1506 #################################
1507  
1508 =pod
1509  
1510 =head2 $ff->error([BOOL])
1511  
1512 Returns the last encountered error as string.
1513 Pass it a true value to get the C<Carp::longmess()> output instead.
1514  
1515 =cut
1516  
1517 ### error handling the way Archive::Extract does it
1518 8 ▶sub _error {
Subroutine _error: called CC=3 SCAR=11.2
1519 8 my $self = shift;
1520 8 my $error = shift;
1521  
1522 8 $self->_error_msg( $error );
1523 8 $self->_error_msg_long( Carp::longmess($error) );
1524  
1525 8 ▶ if( $WARN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Fetch::WARN) 8 0 8
1526 8 ▶ carp $DEBUG ? $self->_error_msg_long : $self->_error_msg;
Branch: 1 branch
BranchTrueFalseTotal
$File::Fetch::DEBUG ? : 0 8 8
1527 }
1528  
1529 8 return;
1530 }
1531  
1532 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
1533 0 my $self = shift;
1534 0 ▶ return shift() ? $self->_error_msg_long : $self->_error_msg;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 0 0
1535 }
1536  
1537  
1538 2 1;
1539  
1540 =pod
1541  
1542 =head1 HOW IT WORKS
1543  
1544 File::Fetch is able to fetch a variety of uris, by using several
1545 external programs and modules.
1546  
1547 Below is a mapping of what utilities will be used in what order
1548 for what schemes, if available:
1549  
1550 file => LWP, lftp, file
1551 http => LWP, HTTP::Tiny, wget, curl, lftp, fetch, HTTP::Lite, lynx, iosock
1552 ftp => LWP, Net::FTP, wget, curl, lftp, fetch, ncftp, ftp
1553 rsync => rsync
1554 git => git
1555  
1556 If you'd like to disable the use of one or more of these utilities
1557 and/or modules, see the C<$BLACKLIST> variable further down.
1558  
1559 If a utility or module isn't available, it will be marked in a cache
1560 (see the C<$METHOD_FAIL> variable further down), so it will not be
1561 tried again. The C<fetch> method will only fail when all options are
1562 exhausted, and it was not able to retrieve the file.
1563  
1564 The C<fetch> utility is available on FreeBSD. NetBSD and Dragonfly BSD
1565 may also have it from C<pkgsrc>. We only check for C<fetch> on those
1566 three platforms.
1567  
1568 C<iosock> is a very limited L<IO::Socket::INET> based mechanism for
1569 retrieving C<http> schemed urls. It doesn't follow redirects for instance.
1570  
1571 C<git> only supports C<git://> style urls.
1572  
1573 A special note about fetching files from an ftp uri:
1574  
1575 By default, all ftp connections are done in passive mode. To change
1576 that, see the C<$FTP_PASSIVE> variable further down.
1577  
1578 Furthermore, ftp uris only support anonymous connections, so no
1579 named user/password pair can be passed along.
1580  
1581 C</bin/ftp> is blacklisted by default; see the C<$BLACKLIST> variable
1582 further down.
1583  
1584 =head1 GLOBAL VARIABLES
1585  
1586 The behaviour of File::Fetch can be altered by changing the following
1587 global variables:
1588  
1589 =head2 $File::Fetch::FROM_EMAIL
1590  
1591 This is the email address that will be sent as your anonymous ftp
1592 password.
1593  
1594 Default is C<File-Fetch@example.com>.
1595  
1596 =head2 $File::Fetch::USER_AGENT
1597  
1598 This is the useragent as C<LWP> will report it.
1599  
1600 Default is C<File::Fetch/$VERSION>.
1601  
1602 =head2 $File::Fetch::FTP_PASSIVE
1603  
1604 This variable controls whether the environment variable C<FTP_PASSIVE>
1605 and any passive switches to commandline tools will be set to true.
1606  
1607 Default value is 1.
1608  
1609 Note: When $FTP_PASSIVE is true, C<ncftp> will not be used to fetch
1610 files, since passive mode can only be set interactively for this binary
1611  
1612 =head2 $File::Fetch::TIMEOUT
1613  
1614 When set, controls the network timeout (counted in seconds).
1615  
1616 Default value is 0.
1617  
1618 =head2 $File::Fetch::WARN
1619  
1620 This variable controls whether errors encountered internally by
1621 C<File::Fetch> should be C<carp>'d or not.
1622  
1623 Set to false to silence warnings. Inspect the output of the C<error()>
1624 method manually to see what went wrong.
1625  
1626 Defaults to C<true>.
1627  
1628 =head2 $File::Fetch::DEBUG
1629  
1630 This enables debugging output when calling commandline utilities to
1631 fetch files.
1632 This also enables C<Carp::longmess> errors, instead of the regular
1633 C<carp> errors.
1634  
1635 Good for tracking down why things don't work with your particular
1636 setup.
1637  
1638 Default is 0.
1639  
1640 =head2 $File::Fetch::BLACKLIST
1641  
1642 This is an array ref holding blacklisted modules/utilities for fetching
1643 files with.
1644  
1645 To disallow the use of, for example, C<LWP> and C<Net::FTP>, you could
1646 set $File::Fetch::BLACKLIST to:
1647  
1648 $File::Fetch::BLACKLIST = [qw|lwp netftp|]
1649  
1650 The default blacklist is [qw|ftp|], as C</bin/ftp> is rather unreliable.
1651  
1652 See the note on C<MAPPING> below.
1653  
1654 =head2 $File::Fetch::METHOD_FAIL
1655  
1656 This is a hashref registering what modules/utilities were known to fail
1657 for fetching files (mostly because they weren't installed).
1658  
1659 You can reset this cache by assigning an empty hashref to it, or
1660 individually remove keys.
1661  
1662 See the note on C<MAPPING> below.
1663  
1664 =head1 MAPPING
1665  
1666  
1667 Here's a quick mapping for the utilities/modules, and their names for
1668 the $BLACKLIST, $METHOD_FAIL and other internal functions.
1669  
1670 LWP => lwp
1671 HTTP::Lite => httplite
1672 HTTP::Tiny => httptiny
1673 Net::FTP => netftp
1674 wget => wget
1675 lynx => lynx
1676 ncftp => ncftp
1677 ftp => ftp
1678 curl => curl
1679 rsync => rsync
1680 lftp => lftp
1681 fetch => fetch
1682 IO::Socket => iosock
1683  
1684 =head1 FREQUENTLY ASKED QUESTIONS
1685  
1686 =head2 So how do I use a proxy with File::Fetch?
1687  
1688 C<File::Fetch> currently only supports proxies with LWP::UserAgent.
1689 You will need to set your environment variables accordingly. For
1690 example, to use an ftp proxy:
1691  
1692 $ENV{ftp_proxy} = 'foo.com';
1693  
1694 Refer to the LWP::UserAgent manpage for more details.
1695  
1696 =head2 I used 'lynx' to fetch a file, but its contents is all wrong!
1697  
1698 C<lynx> can only fetch remote files by dumping its contents to C<STDOUT>,
1699 which we in turn capture. If that content is a 'custom' error file
1700 (like, say, a C<404 handler>), you will get that contents instead.
1701  
1702 Sadly, C<lynx> doesn't support any options to return a different exit
1703 code on non-C<200 OK> status, giving us no way to tell the difference
1704 between a 'successful' fetch and a custom error page.
1705  
1706 Therefor, we recommend to only use C<lynx> as a last resort. This is
1707 why it is at the back of our list of methods to try as well.
1708  
1709 =head2 Files I'm trying to fetch have reserved characters or non-ASCII characters in them. What do I do?
1710  
1711 C<File::Fetch> is relatively smart about things. When trying to write
1712 a file to disk, it removes the C<query parameters> (see the
1713 C<output_file> method for details) from the file name before creating
1714 it. In most cases this suffices.
1715  
1716 If you have any other characters you need to escape, please install
1717 the C<URI::Escape> module from CPAN, and pre-encode your URI before
1718 passing it to C<File::Fetch>. You can read about the details of URIs
1719 and URI encoding here:
1720  
1721 L<https://datatracker.ietf.org/doc/html/rfc2396>
1722  
1723 =head1 TODO
1724  
1725 =over 4
1726  
1727 =item Implement $PREFER_BIN
1728  
1729 To indicate to rather use commandline tools than modules
1730  
1731 =back
1732  
1733 =head1 BUG REPORTS
1734  
1735 Please report bugs or other issues to E<lt>bug-file-fetch@rt.cpan.org<gt>.
1736  
1737 =head1 AUTHOR
1738  
1739 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
1740  
1741 =head1 COPYRIGHT
1742  
1743 This library is free software; you may redistribute and/or modify it
1744 under the same terms as Perl itself.
1745  
1746  
1747 =cut
1748  
1749 # Local variables:
1750 # c-indentation-style: bsd
1751 # c-basic-offset: 4
1752 # indent-tabs-mode: nil
1753 # End:
1754 # vim: expandtab shiftwidth=4:
1755  
1756  
1757  
1758  
↑ Summary