e6a4508
File-Fetch-0.18
e6a4508
e6a4508
diff -urN perl-5.10.0.orig/lib/File/Fetch/t/01_File-Fetch.t perl-5.10.0/lib/File/Fetch/t/01_File-Fetch.t
e6a4508
--- perl-5.10.0.orig/lib/File/Fetch/t/01_File-Fetch.t	2007-12-18 11:47:07.000000000 +0100
e6a4508
+++ perl-5.10.0/lib/File/Fetch/t/01_File-Fetch.t	2009-03-10 14:28:48.000000000 +0100
e6a4508
@@ -22,7 +22,7 @@
e6a4508
 
e6a4508
 Some of these tests assume you are connected to the
e6a4508
 internet. If you are not, or if certain protocols or hosts
e6a4508
-are blocked and/or firewalled, these tests will fail due
e6a4508
+are blocked and/or firewalled, these tests could fail due
e6a4508
 to no fault of the module itself.
e6a4508
 
e6a4508
 ###########################################################
e6a4508
@@ -115,6 +115,13 @@
e6a4508
 ) if &File::Fetch::ON_WIN;
e6a4508
 
e6a4508
 
e6a4508
+### sanity tests
e6a4508
+{   like( $File::Fetch::USER_AGENT, qr/$File::Fetch::VERSION/,
e6a4508
+                                "User agent contains version" );
e6a4508
+    like( $File::Fetch::FROM_EMAIL, qr/@/,
e6a4508
+                                q[Email contains '@'] );
e6a4508
+}                                
e6a4508
+
e6a4508
 ### parse uri tests ###
e6a4508
 for my $entry (@map ) {
e6a4508
     my $uri = $entry->{'uri'};
e6a4508
@@ -148,14 +155,14 @@
e6a4508
     my $prefix = &File::Fetch::ON_UNIX ? 'file://' : 'file:///';
e6a4508
     my $uri = $prefix . cwd() .'/'. basename($0);
e6a4508
 
e6a4508
-    for (qw[lwp file]) {
e6a4508
+    for (qw[lwp lftp file]) {
e6a4508
         _fetch_uri( file => $uri, $_ );
e6a4508
     }
e6a4508
 }
e6a4508
 
e6a4508
 ### ftp:// tests ###
e6a4508
 {   my $uri = 'ftp://ftp.funet.fi/pub/CPAN/index.html';
e6a4508
-    for (qw[lwp netftp wget curl ncftp]) {
e6a4508
+    for (qw[lwp netftp wget curl lftp ncftp]) {
e6a4508
 
e6a4508
         ### STUPID STUPID warnings ###
e6a4508
         next if $_ eq 'ncftp' and $File::Fetch::FTP_PASSIVE
e6a4508
@@ -167,9 +174,10 @@
e6a4508
 
e6a4508
 ### http:// tests ###
e6a4508
 {   for my $uri ( 'http://www.cpan.org/index.html',
e6a4508
-                  'http://www.cpan.org/index.html?q=1&y=2'
e6a4508
+                  'http://www.cpan.org/index.html?q=1',
e6a4508
+                  'http://www.cpan.org/index.html?q=1&y=2',
e6a4508
     ) {
e6a4508
-        for (qw[lwp wget curl lynx]) {
e6a4508
+        for (qw[lwp wget curl lftp lynx]) {
e6a4508
             _fetch_uri( http => $uri, $_ );
e6a4508
         }
e6a4508
     }
e6a4508
@@ -206,6 +214,11 @@
e6a4508
             skip "You do not have '$method' installed/available", 3
e6a4508
                 if $File::Fetch::METHOD_FAIL->{$method} &&
e6a4508
                    $File::Fetch::METHOD_FAIL->{$method};
e6a4508
+                
e6a4508
+            ### if the file wasn't fetched, it may be a network/firewall issue                
e6a4508
+            skip "Fetch failed; no network connectivity for '$type'?", 3 
e6a4508
+                unless $file;
e6a4508
+                
e6a4508
             ok( $file,          "   File ($file) fetched with $method ($uri)" );
e6a4508
             ok( $file && -s $file,   
e6a4508
                                 "   File has size" );
e6a4508
diff -urN perl-5.10.0.orig/lib/File/Fetch.pm perl-5.10.0/lib/File/Fetch.pm
e6a4508
--- perl-5.10.0.orig/lib/File/Fetch.pm	2007-12-18 11:47:07.000000000 +0100
e6a4508
+++ perl-5.10.0/lib/File/Fetch.pm	2009-03-10 14:29:10.000000000 +0100
e6a4508
@@ -2,6 +2,7 @@
e6a4508
 
e6a4508
 use strict;
e6a4508
 use FileHandle;
e6a4508
+use File::Temp;
e6a4508
 use File::Copy;
e6a4508
 use File::Spec;
e6a4508
 use File::Spec::Unix;
e6a4508
@@ -9,7 +10,7 @@
e6a4508
 
e6a4508
 use Cwd                         qw[cwd];
e6a4508
 use Carp                        qw[carp];
e6a4508
-use IPC::Cmd                    qw[can_run run];
e6a4508
+use IPC::Cmd                    qw[can_run run QUOTE];
e6a4508
 use File::Path                  qw[mkpath];
e6a4508
 use Params::Check               qw[check];
e6a4508
 use Module::Load::Conditional   qw[can_load];
e6a4508
@@ -20,14 +21,11 @@
e6a4508
                 $FTP_PASSIVE $TIMEOUT $DEBUG $WARN
e6a4508
             ];
e6a4508
 
e6a4508
-use constant QUOTE  => do { $^O eq 'MSWin32' ? q["] : q['] };            
e6a4508
-            
e6a4508
-
e6a4508
-$VERSION        = '0.14';
e6a4508
+$VERSION        = '0.18';
e6a4508
 $VERSION        = eval $VERSION;    # avoid warnings with development releases
e6a4508
 $PREFER_BIN     = 0;                # XXX TODO implement
e6a4508
 $FROM_EMAIL     = 'File-Fetch@example.com';
e6a4508
-$USER_AGENT     = 'File::Fetch/$VERSION';
e6a4508
+$USER_AGENT     = "File::Fetch/$VERSION";
e6a4508
 $BLACKLIST      = [qw|ftp|];
e6a4508
 $METHOD_FAIL    = { };
e6a4508
 $FTP_PASSIVE    = 1;
e6a4508
@@ -37,9 +35,9 @@
e6a4508
 
e6a4508
 ### methods available to fetch the file depending on the scheme
e6a4508
 $METHODS = {
e6a4508
-    http    => [ qw|lwp wget curl lynx| ],
e6a4508
-    ftp     => [ qw|lwp netftp wget curl ncftp ftp| ],
e6a4508
-    file    => [ qw|lwp file| ],
e6a4508
+    http    => [ qw|lwp wget curl lftp lynx| ],
e6a4508
+    ftp     => [ qw|lwp netftp wget curl lftp ncftp ftp| ],
e6a4508
+    file    => [ qw|lwp lftp file| ],
e6a4508
     rsync   => [ qw|rsync| ]
e6a4508
 };
e6a4508
 
e6a4508
@@ -50,11 +48,13 @@
e6a4508
 local $Module::Load::Conditional::VERBOSE   = 0;
e6a4508
 
e6a4508
 ### see what OS we are on, important for file:// uris ###
e6a4508
-use constant ON_WIN         => ($^O eq 'MSWin32');
e6a4508
-use constant ON_VMS         => ($^O eq 'VMS');                                
e6a4508
-use constant ON_UNIX        => (!ON_WIN);
e6a4508
-use constant HAS_VOL        => (ON_WIN);
e6a4508
-use constant HAS_SHARE      => (ON_WIN);
e6a4508
+use constant ON_WIN     => ($^O eq 'MSWin32');
e6a4508
+use constant ON_VMS     => ($^O eq 'VMS');                                
e6a4508
+use constant ON_UNIX    => (!ON_WIN);
e6a4508
+use constant HAS_VOL    => (ON_WIN);
e6a4508
+use constant HAS_SHARE  => (ON_WIN);
e6a4508
+
e6a4508
+
e6a4508
 =pod
e6a4508
 
e6a4508
 =head1 NAME
e6a4508
@@ -146,7 +146,7 @@
e6a4508
 ##########################
e6a4508
 
e6a4508
 {
e6a4508
-    ### template for new() and autogenerated accessors ###
e6a4508
+    ### template for autogenerated accessors ###
e6a4508
     my $Tmpl = {
e6a4508
         scheme          => { default => 'http' },
e6a4508
         host            => { default => 'localhost' },
e6a4508
@@ -626,11 +626,14 @@
e6a4508
         push @$cmd, '--passive-ftp' if $FTP_PASSIVE;
e6a4508
 
e6a4508
         ### set the output document, add the uri ###
e6a4508
-        push @$cmd, '--output-document', 
e6a4508
-                    ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
-                    $IPC::Cmd::USE_IPC_RUN
e6a4508
-                        ? ($to, $self->uri)
e6a4508
-                        : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
+        push @$cmd, '--output-document', $to, $self->uri;
e6a4508
+
e6a4508
+        ### with IPC::Cmd > 0.41, this is fixed in teh library,
e6a4508
+        ### and there's no need for special casing any more.
e6a4508
+        ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
+        # $IPC::Cmd::USE_IPC_RUN
e6a4508
+        #    ? ($to, $self->uri)
e6a4508
+        #    : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
 
e6a4508
         ### shell out ###
e6a4508
         my $captured;
e6a4508
@@ -653,6 +656,81 @@
e6a4508
     }
e6a4508
 }
e6a4508
 
e6a4508
+### /bin/lftp fetch ###
e6a4508
+sub _lftp_fetch {
e6a4508
+    my $self = shift;
e6a4508
+    my %hash = @_;
e6a4508
+
e6a4508
+    my ($to);
e6a4508
+    my $tmpl = {
e6a4508
+        to  => { required => 1, store => \$to }
e6a4508
+    };
e6a4508
+    check( $tmpl, \%hash ) or return;
e6a4508
+
e6a4508
+    ### see if we have a wget binary ###
e6a4508
+    if( my $lftp = can_run('lftp') ) {
e6a4508
+
e6a4508
+        ### no verboseness, thanks ###
e6a4508
+        my $cmd = [ $lftp, '-f' ];
e6a4508
+
e6a4508
+        my $fh = File::Temp->new;
e6a4508
+        
e6a4508
+        my $str;
e6a4508
+        
e6a4508
+        ### if a timeout is set, add it ###
e6a4508
+        $str .= "set net:timeout $TIMEOUT;\n" if $TIMEOUT;
e6a4508
+
e6a4508
+        ### run passive if specified ###
e6a4508
+        $str .= "set ftp:passive-mode 1;\n" if $FTP_PASSIVE;
e6a4508
+
e6a4508
+        ### set the output document, add the uri ###
e6a4508
+        ### quote the URI, because lftp supports certain shell
e6a4508
+        ### expansions, most notably & for backgrounding.
e6a4508
+        ### ' quote does nto work, must be "
e6a4508
+        $str .= q[get ']. $self->uri .q[' -o ]. $to . $/;
e6a4508
+
e6a4508
+        if( $DEBUG ) {
e6a4508
+            my $pp_str = join ' ', split $/, $str;
e6a4508
+            print "# lftp command: $pp_str\n";
e6a4508
+        }              
e6a4508
+
e6a4508
+        ### write straight to the file.
e6a4508
+        $fh->autoflush(1);
e6a4508
+        print $fh $str;
e6a4508
+
e6a4508
+        ### the command needs to be 1 string to be executed
e6a4508
+        push @$cmd, $fh->filename;
e6a4508
+
e6a4508
+        ### with IPC::Cmd > 0.41, this is fixed in teh library,
e6a4508
+        ### and there's no need for special casing any more.
e6a4508
+        ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
+        # $IPC::Cmd::USE_IPC_RUN
e6a4508
+        #    ? ($to, $self->uri)
e6a4508
+        #    : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
+
e6a4508
+
e6a4508
+        ### shell out ###
e6a4508
+        my $captured;
e6a4508
+        unless(run( command => $cmd,
e6a4508
+                    buffer  => \$captured,
e6a4508
+                    verbose => $DEBUG
e6a4508
+        )) {
e6a4508
+            ### wget creates the output document always, even if the fetch
e6a4508
+            ### fails.. so unlink it in that case
e6a4508
+            1 while unlink $to;
e6a4508
+
e6a4508
+            return $self->_error(loc( "Command failed: %1", $captured || '' ));
e6a4508
+        }
e6a4508
+
e6a4508
+        return $to;
e6a4508
+
e6a4508
+    } else {
e6a4508
+        $METHOD_FAIL->{'lftp'} = 1;
e6a4508
+        return;
e6a4508
+    }
e6a4508
+}
e6a4508
+
e6a4508
+
e6a4508
 
e6a4508
 ### /bin/ftp fetch ###
e6a4508
 sub _ftp_fetch {
e6a4508
@@ -717,6 +795,33 @@
e6a4508
                 'lynx' ));
e6a4508
         }            
e6a4508
 
e6a4508
+        ### check if the HTTP resource exists ###
e6a4508
+        if ($self->uri =~ /^https?:\/\//i) {
e6a4508
+            my $cmd = [
e6a4508
+                $lynx,
e6a4508
+                '-head',
e6a4508
+                '-source',
e6a4508
+                "-auth=anonymous:$FROM_EMAIL",
e6a4508
+            ];
e6a4508
+
e6a4508
+            push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
e6a4508
+
e6a4508
+            push @$cmd, $self->uri;
e6a4508
+
e6a4508
+            ### shell out ###
e6a4508
+            my $head;
e6a4508
+            unless(run( command => $cmd,
e6a4508
+                        buffer  => \$head,
e6a4508
+                        verbose => $DEBUG )
e6a4508
+            ) {
e6a4508
+                return $self->_error(loc("Command failed: %1", $head || ''));
e6a4508
+            }
e6a4508
+
e6a4508
+            unless($head =~ /^HTTP\/\d+\.\d+ 200\b/) {
e6a4508
+                return $self->_error(loc("Command failed: %1", $head || ''));
e6a4508
+            }
e6a4508
+        }
e6a4508
+
e6a4508
         ### write to the output file ourselves, since lynx ass_u_mes to much
e6a4508
         my $local = FileHandle->new(">$to")
e6a4508
                         or return $self->_error(loc(
e6a4508
@@ -732,9 +837,14 @@
e6a4508
         push @$cmd, "-connect_timeout=$TIMEOUT" if $TIMEOUT;
e6a4508
 
e6a4508
         ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
-        push @$cmd, $IPC::Cmd::USE_IPC_RUN
e6a4508
-                        ? $self->uri
e6a4508
-                        : QUOTE. $self->uri .QUOTE;
e6a4508
+        push @$cmd, $self->uri;
e6a4508
+        
e6a4508
+        ### with IPC::Cmd > 0.41, this is fixed in teh library,
e6a4508
+        ### and there's no need for special casing any more.
e6a4508
+        ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
+        # $IPC::Cmd::USE_IPC_RUN
e6a4508
+        #    ? $self->uri
e6a4508
+        #    : QUOTE. $self->uri .QUOTE;
e6a4508
 
e6a4508
 
e6a4508
         ### shell out ###
e6a4508
@@ -829,7 +939,7 @@
e6a4508
     if (my $curl = can_run('curl')) {
e6a4508
 
e6a4508
         ### these long opts are self explanatory - I like that -jmb
e6a4508
-	    my $cmd = [ $curl ];
e6a4508
+	    my $cmd = [ $curl, '-q' ];
e6a4508
 
e6a4508
 	    push(@$cmd, '--connect-timeout', $TIMEOUT) if $TIMEOUT;
e6a4508
 
e6a4508
@@ -842,11 +952,15 @@
e6a4508
 
e6a4508
         ### curl doesn't follow 302 (temporarily moved) etc automatically
e6a4508
         ### so we add --location to enable that.
e6a4508
-        push @$cmd, '--fail', '--location', '--output', 
e6a4508
-                    ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
-                    $IPC::Cmd::USE_IPC_RUN
e6a4508
-                        ? ($to, $self->uri)
e6a4508
-                        : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
+        push @$cmd, '--fail', '--location', '--output', $to, $self->uri;
e6a4508
+
e6a4508
+        ### with IPC::Cmd > 0.41, this is fixed in teh library,
e6a4508
+        ### and there's no need for special casing any more.
e6a4508
+        ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
+        # $IPC::Cmd::USE_IPC_RUN
e6a4508
+        #    ? ($to, $self->uri)
e6a4508
+        #    : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
+
e6a4508
 
e6a4508
         my $captured;
e6a4508
         unless(run( command => $cmd,
e6a4508
@@ -960,9 +1074,14 @@
e6a4508
         push(@$cmd, '--quiet') unless $DEBUG;
e6a4508
 
e6a4508
         ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
-        push @$cmd, $IPC::Cmd::USE_IPC_RUN
e6a4508
-                        ? ($self->uri, $to)
e6a4508
-                        : (QUOTE. $self->uri .QUOTE, QUOTE. $to .QUOTE);
e6a4508
+        push @$cmd, $self->uri, $to;
e6a4508
+
e6a4508
+        ### with IPC::Cmd > 0.41, this is fixed in teh library,
e6a4508
+        ### and there's no need for special casing any more.
e6a4508
+        ### DO NOT quote things for IPC::Run, it breaks stuff.
e6a4508
+        # $IPC::Cmd::USE_IPC_RUN
e6a4508
+        #    ? ($to, $self->uri)
e6a4508
+        #    : (QUOTE. $to .QUOTE, QUOTE. $self->uri .QUOTE);
e6a4508
 
e6a4508
         my $captured;
e6a4508
         unless(run( command => $cmd,
e6a4508
@@ -1030,9 +1149,9 @@
e6a4508
 Below is a mapping of what utilities will be used in what order
e6a4508
 for what schemes, if available:
e6a4508
 
e6a4508
-    file    => LWP, file
e6a4508
-    http    => LWP, wget, curl, lynx
e6a4508
-    ftp     => LWP, Net::FTP, wget, curl, ncftp, ftp
e6a4508
+    file    => LWP, lftp, file
e6a4508
+    http    => LWP, wget, curl, lftp, lynx
e6a4508
+    ftp     => LWP, Net::FTP, wget, curl, lftp, ncftp, ftp
e6a4508
     rsync   => rsync
e6a4508
 
e6a4508
 If you'd like to disable the use of one or more of these utilities
e6a4508
@@ -1148,6 +1267,7 @@
e6a4508
     ftp         => ftp
e6a4508
     curl        => curl
e6a4508
     rsync       => rsync
e6a4508
+    lftp        => lftp
e6a4508
 
e6a4508
 =head1 FREQUENTLY ASKED QUESTIONS
e6a4508