Alien-ROOT
view release on metacpan or search on metacpan
inc/inc_File-Fetch/File/Fetch.pm view on Meta::CPAN
my $cmd = [
$lynx,
'-head',
'-source',
"-auth=anonymous:$FROM_EMAIL",
];
push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
push @$cmd, $self->uri;
### shell out ###
my $head;
unless(run( command => $cmd,
buffer => \$head,
verbose => $DEBUG )
) {
return $self->_error(loc("Command failed: %1", $head || ''));
}
unless($head =~ /^HTTP\/\d+\.\d+ 200\b/) {
return $self->_error(loc("Command failed: %1", $head || ''));
}
}
### write to the output file ourselves, since lynx ass_u_mes to much
my $local = FileHandle->new(">$to")
or return $self->_error(loc(
"Could not open '%1' for writing: %2",$to,$!));
### dump to stdout ###
my $cmd = [
$lynx,
'-source',
"-auth=anonymous:$FROM_EMAIL",
];
push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
### DO NOT quote things for IPC::Run, it breaks stuff.
push @$cmd, $self->uri;
### with IPC::Cmd > 0.41, this is fixed in teh library,
### and there's no need for special casing any more.
### DO NOT quote things for IPC::Run, it breaks stuff.
# $IPC::Cmd::USE_IPC_RUN
# ? $self->uri
# : QUOTE. $self->uri .QUOTE;
### shell out ###
my $captured;
unless(run( command => $cmd,
buffer => \$captured,
verbose => $DEBUG )
) {
return $self->_error(loc("Command failed: %1", $captured || ''));
}
### print to local file ###
### XXX on a 404 with a special error page, $captured will actually
### hold the contents of that page, and make it *appear* like the
### request was a success, when really it wasn't :(
### there doesn't seem to be an option for lynx to change the exit
### code based on a 4XX status or so.
### the closest we can come is using --error_file and parsing that,
### which is very unreliable ;(
$local->print( $captured );
$local->close or return;
return $to;
} else {
$METHOD_FAIL->{'lynx'} = 1;
return;
}
}
### use /bin/ncftp to fetch files
sub _ncftp_fetch {
my $self = shift;
my %hash = @_;
my ($to);
my $tmpl = {
to => { required => 1, store => \$to }
};
check( $tmpl, \%hash ) or return;
### we can only set passive mode in interactive sessions, so bail out
### if $FTP_PASSIVE is set
return if $FTP_PASSIVE;
### see if we have a ncftp binary ###
if( my $ncftp = can_run('ncftp') ) {
my $cmd = [
$ncftp,
'-V', # do not be verbose
'-p', $FROM_EMAIL, # email as password
$self->host, # hostname
dirname($to), # local dir for the file
# remote path to the file
### DO NOT quote things for IPC::Run, it breaks stuff.
$IPC::Cmd::USE_IPC_RUN
? File::Spec::Unix->catdir( $self->path, $self->file )
: QUOTE. File::Spec::Unix->catdir(
$self->path, $self->file ) .QUOTE
];
### shell out ###
my $captured;
unless(run( command => $cmd,
buffer => \$captured,
verbose => $DEBUG )
) {
return $self->_error(loc("Command failed: %1", $captured || ''));
}
return $to;
inc/inc_File-Fetch/File/Fetch.pm view on Meta::CPAN
=head2 $File::Fetch::BLACKLIST
This is an array ref holding blacklisted modules/utilities for fetching
files with.
To disallow the use of, for example, C<LWP> and C<Net::FTP>, you could
set $File::Fetch::BLACKLIST to:
$File::Fetch::BLACKLIST = [qw|lwp netftp|]
The default blacklist is [qw|ftp|], as C</bin/ftp> is rather unreliable.
See the note on C<MAPPING> below.
=head2 $File::Fetch::METHOD_FAIL
This is a hashref registering what modules/utilities were known to fail
for fetching files (mostly because they weren't installed).
You can reset this cache by assigning an empty hashref to it, or
individually remove keys.
See the note on C<MAPPING> below.
=head1 MAPPING
Here's a quick mapping for the utilities/modules, and their names for
the $BLACKLIST, $METHOD_FAIL and other internal functions.
LWP => lwp
HTTP::Lite => httplite
HTTP::Tiny => httptiny
Net::FTP => netftp
wget => wget
lynx => lynx
ncftp => ncftp
ftp => ftp
curl => curl
rsync => rsync
lftp => lftp
fetch => fetch
IO::Socket => iosock
=head1 FREQUENTLY ASKED QUESTIONS
=head2 So how do I use a proxy with File::Fetch?
C<File::Fetch> currently only supports proxies with LWP::UserAgent.
You will need to set your environment variables accordingly. For
example, to use an ftp proxy:
$ENV{ftp_proxy} = 'foo.com';
Refer to the LWP::UserAgent manpage for more details.
=head2 I used 'lynx' to fetch a file, but its contents is all wrong!
C<lynx> can only fetch remote files by dumping its contents to C<STDOUT>,
which we in turn capture. If that content is a 'custom' error file
(like, say, a C<404 handler>), you will get that contents instead.
Sadly, C<lynx> doesn't support any options to return a different exit
code on non-C<200 OK> status, giving us no way to tell the difference
between a 'successful' fetch and a custom error page.
Therefor, we recommend to only use C<lynx> as a last resort. This is
why it is at the back of our list of methods to try as well.
=head2 Files I'm trying to fetch have reserved characters or non-ASCII characters in them. What do I do?
C<File::Fetch> is relatively smart about things. When trying to write
a file to disk, it removes the C<query parameters> (see the
C<output_file> method for details) from the file name before creating
it. In most cases this suffices.
If you have any other characters you need to escape, please install
the C<URI::Escape> module from CPAN, and pre-encode your URI before
passing it to C<File::Fetch>. You can read about the details of URIs
and URI encoding here:
http://www.faqs.org/rfcs/rfc2396.html
=head1 TODO
=over 4
=item Implement $PREFER_BIN
To indicate to rather use commandline tools than modules
=back
=head1 BUG REPORTS
Please report bugs or other issues to E<lt>bug-file-fetch@rt.cpan.org<gt>.
=head1 AUTHOR
This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
=head1 COPYRIGHT
This library is free software; you may redistribute and/or modify it
under the same terms as Perl itself.
=cut
# Local variables:
# c-indentation-style: bsd
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
# vim: expandtab shiftwidth=4:
( run in 1.638 second using v1.01-cache-2.11-cpan-39bf76dae61 )