download.pl: use curl in preference to wget

Because wget doesn't know how to do Negotiate authentication with a proxy
and curl does, use curl if it's present. The user is expected to have a
~/.curlrc that sets the options necessary for any proxy authentication.

A ~/.curlrc is completely optional however and curl will work in exactly
the same manner as wget without one.

Signed-off-by: Brian J. Murrell <brian@interlinx.bc.ca>
[Jo-Philipp Wich: Rework code to detect curl usability by checking --version,
                  Use vararg style open() to bypass the shell when downloading,
                  Use Text::ParseWords to decompose env vars into arguments]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Brian J. Murrell 2016-11-20 16:01:33 -05:00 committed by Jo-Philipp Wich
parent 558680012d
commit fc6b6f4583

View File

@ -11,6 +11,7 @@ use strict;
use warnings;
use File::Basename;
use File::Copy;
use Text::ParseWords;
@ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
@ -73,12 +74,28 @@ sub hash_cmd() {
return undef;
}
sub download_cmd($) {
my $url = shift;
my $have_curl = 0;
if (open CURL, '-|', 'curl', '--version') {
if (defined(my $line = readline CURL)) {
$have_curl = 1 if $line =~ /^curl /;
}
close CURL;
}
return $have_curl
? (qw(curl --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
: (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
;
}
my $hash_cmd = hash_cmd();
sub download
{
my $mirror = shift;
my $options = $ENV{WGET_OPTIONS} || "";
$mirror =~ s!/$!!;
@ -125,18 +142,19 @@ sub download
}
};
} else {
open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
my @cmd = download_cmd("$mirror/$url_filename");
open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
$hash_cmd and do {
open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
};
open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
my $buffer;
while (read WGET, $buffer, 1048576) {
while (read FETCH_FD, $buffer, 1048576) {
$hash_cmd and print MD5SUM $buffer;
print OUTPUT $buffer;
}
$hash_cmd and close MD5SUM;
close WGET;
close FETCH_FD;
close OUTPUT;
if ($? >> 8) {