our
$VERSION
=
'1.16.0'
;
use
vars
qw($has_wget $has_curl $has_lwp)
;
BEGIN {
if
( $^O !~ m/^MSWin/ ) {
system
(
"which wget >/dev/null 2>&1"
);
$has_wget
= !$?;
system
(
"which curl >/dev/null 2>&1"
);
$has_curl
= !$?;
}
eval
{
$has_lwp
= 1;
};
if
( $^O =~ m/^MSWin/ && !
$has_lwp
) {
Rex::Logger::info(
"Please install LWP::UserAgent to allow file downloads."
);
}
}
@EXPORT
=
qw(download)
;
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}' -
}
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;