lib/File/Fetch.pm

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