/usr/share/perl5/Rex/Commands/Download.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
=head1 NAME
Rex::Commands::Download - Download remote files
=head1 DESCRIPTION
With this module you can download a remotefile via sftp, http and ftp from a host to your local computer.
Version <= 1.0: All these functions will not be reported.
=head1 SYNOPSIS
# sftp
task "download", "remoteserver", sub {
download "/remote/file", "localfile";
};
# http
task "download2", sub {
download "http://server/remote/file";
};
=head1 EXPORTED FUNCTIONS
=cut
package Rex::Commands::Download;
use strict;
use warnings;
use Rex::Helper::UserAgent;
use Carp;
our $VERSION = '1.4.1'; # VERSION
use vars qw($has_wget $has_curl $has_lwp);
# check which download type we should use
BEGIN {
if ( $^O !~ m/^MSWin/ ) {
system("which wget >/dev/null 2>&1");
$has_wget = !$?;
system("which curl >/dev/null 2>&1");
$has_curl = !$?;
}
eval {
require Rex::Helper::UserAgent;
$has_lwp = 1;
};
if ( $^O =~ m/^MSWin/ && !$has_lwp ) {
Rex::Logger::info("Please install LWP::UserAgent to allow file downloads.");
}
}
require Rex::Exporter;
use vars qw(@EXPORT);
use base qw(Rex::Exporter);
use Rex::Commands::Fs;
use Rex::Helper::Path;
use Rex::Interface::Fs;
use File::Basename qw(basename);
@EXPORT = qw(download);
=head2 download($remote, [$local])
Perform a download. If no local file is specified it will download the file to the current directory.
task "download", "remoteserver", sub {
download "/remote/file", "localfile";
};
task "download", sub {
download "http://www.rexify.org/index.html", "localfile.html";
};
=cut
sub download {
my ( $remote, $local, %option ) = @_;
unless ($local) {
$local = basename($remote);
}
if ( -d $local ) {
$local = $local . '/' . basename($remote);
}
Rex::Logger::debug("saving file to $local");
$remote = resolv_path($remote);
$local = resolv_path( $local, 1 );
if ( $remote =~ m/^(https?|ftp):\/\// ) {
_http_download( $remote, $local, %option );
}
else {
_sftp_download( $remote, $local, %option );
}
}
sub _sftp_download {
my $remote = shift;
my $local = shift;
my $fs = Rex::Interface::Fs->create;
Rex::Logger::debug("Downloading via SFTP");
unless ( is_file($remote) ) {
Rex::Logger::info("File $remote not found");
die("$remote not found.");
}
unless ( is_readable($remote) ) {
Rex::Logger::info("File $remote is not readable.");
die("$remote is not readable.");
}
$fs->download( $remote, $local );
}
sub _http_download {
my ( $remote, $local, %option ) = @_;
my $content = _get_http( $remote, %option );
open( my $fh, ">", $local ) or die($!);
binmode $fh;
print $fh $content;
close($fh);
}
sub _get_http {
my ( $url, %option ) = @_;
my $html;
if ($has_curl) {
Rex::Logger::debug("Downloading via curl");
if ( exists $option{user} && exists $option{password} ) {
$html =
qx{curl -u '$option{user}:$option{password}' -# -L -k '$url' 2>/dev/null};
}
else {
$html = qx{curl -# -L -k '$url' 2>/dev/null};
}
}
elsif ($has_wget) {
Rex::Logger::debug("Downloading via wget");
if ( exists $option{user} && exists $option{password} ) {
$html =
qx{wget --http-user=$option{user} '--http-password=$option{password}' --no-check-certificate -O - '$url' 2>/dev/null};
}
else {
$html = qx{wget --no-check-certificate -O - '$url' 2>/dev/null};
}
}
elsif ($has_lwp) {
Rex::Logger::debug("Downloading via LWP::UserAgent");
my $ua = Rex::Helper::UserAgent->new;
my $resp = $ua->get( $url, %option );
if ( $resp->is_success ) {
$html = $resp->content;
}
else {
confess "Error downloading $url.";
}
}
else {
die("No tool found to download something. (curl, wget, LWP::Simple)");
}
return $html;
}
1;
|