| 1 | package App::Perlbrew::HTTP; | ||||||||||||||||||||
| 2 | 75 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 75 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 75 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 75 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 75 | our @EXPORT_OK = qw(http_user_agent_program http_user_agent_command http_get http_download); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 75 | our $HTTP_VERBOSE = 0; | |||||||||||||||||||
| 10 | 75 | our $HTTP_USER_AGENT_PROGRAM; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | my %commands = ( | ||||||||||||||||||||
| 13 | curl => { | ||||||||||||||||||||
| 14 | test => '--version >/dev/null 2>&1', | ||||||||||||||||||||
| 15 | get => '--silent --location --fail -o - {url}', | ||||||||||||||||||||
| 16 | download => '--silent --location --fail -o {output} {url}', | ||||||||||||||||||||
| 17 | order => 1, | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # Exit code is 22 on 404s etc | ||||||||||||||||||||
| 20 | 0 | ▶ | die_on_error => sub { die 'Page not retrieved; HTTP error code 400 or above.' if ($_[ 0 ] >> 8 == 22); }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||
| 22 | wget => { | ||||||||||||||||||||
| 23 | test => '--version >/dev/null 2>&1', | ||||||||||||||||||||
| 24 | get => '--quiet -O - {url}', | ||||||||||||||||||||
| 25 | download => '--quiet -O {output} {url}', | ||||||||||||||||||||
| 26 | order => 2, | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Exit code is not 0 on error | ||||||||||||||||||||
| 29 | 0 | ▶ | die_on_error => sub { die 'Page not retrieved: fetch failed.' if ($_[ 0 ]); }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||
| 31 | fetch => { | ||||||||||||||||||||
| 32 | test => '--version >/dev/null 2>&1', | ||||||||||||||||||||
| 33 | get => '-o - {url}', | ||||||||||||||||||||
| 34 | download => '-o {output} {url}', | ||||||||||||||||||||
| 35 | order => 3, | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # Exit code is 8 on 404s etc | ||||||||||||||||||||
| 38 | 0 | ▶ | die_on_error => sub { die 'Server issued an error response.' if ($_[ 0 ] >> 8 == 8); }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | 75 | ); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 9 | ▶ | sub http_user_agent_program { | ||||||||||||||||||
|
Subroutine
http_user_agent_program: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||||||||
| 43 | 9 | ▶ | $HTTP_USER_AGENT_PROGRAM ||= do { | ||||||||||||||||||
|
Condition66%2/3
$App::Perlbrew::HTTP::HTTP_USER_AGENT_PROGRAM ||= do {
my $program;
foreach my $p (sort {$commands{$a}{"order"} <=> $commands{$b}{"order"};} keys %commands) {
my $code = system(qq[$p $commands{$p}{"test"}]) >> 8;
if ($code != 127) {
$program = $p;
last;
};
};
unless ($program) {
die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n";
};
$program
}
MC/DC0%0/2
$App::Perlbrew::HTTP::HTTP_USER_AGENT_PROGRAM ||= do {
my $program;
foreach my $p (sort {$commands{$a}{"order"} <=> $commands{$b}{"order"};} keys %commands) {
my $code = system(qq[$p $commands{$p}{"test"}]) >> 8;
if ($code != 127) {
$program = $p;
last;
};
};
unless ($program) {
die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n";
};
$program
}
$App::Perlbrew::HTTP::HTTP_USER_AGENT_PROGRAM
do {
my $program;
foreach my $p (sort {$commands{$a}{"order"} <=> $commands{$b}{"order"};} keys %commands) {
my $code = system(qq[$p $commands{$p}{"test"}]) >> 8;
if ($code != 127) {
$program = $p;
last;
};
};
unless ($program) {
die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n";
};
$program
}
Truth table66%2/3
A ||= B
A= $App::Perlbrew::HTTP::HTTP_USER_AGENT_PROGRAMB= do {
my $program;
foreach my $p (sort {$commands{$a}{"order"} <=> $commands{$b}{"order"};} keys %commands) {
my $code = system(qq[$p $commands{$p}{"test"}]) >> 8;
if ($code != 127) {
$program = $p;
last;
};
};
unless ($program) {
die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n";
};
$program
}
| |||||||||||||||||||||
| 44 | 3 | my $program; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 3 | for my $p (sort {$commands{$a}{order}<=>$commands{$b}{order}} keys %commands) { | |||||||||||||||||||
| 47 | 3 | my $code = system("$p $commands{$p}->{test}") >> 8; | |||||||||||||||||||
| 48 | 3 | ▶ | if ($code != 127) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 3 | $program = $p; | |||||||||||||||||||
| 50 | 3 | last; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 3 | ▶ | unless ($program) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n"; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 3 | $program; | |||||||||||||||||||
| 59 | }; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 9 | ▶ | die "[ERROR] Unrecognized http user agent program: $HTTP_USER_AGENT_PROGRAM. It can only be one of: ".join(",", keys %commands)."\n" unless $commands{$HTTP_USER_AGENT_PROGRAM}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 9 | return $HTTP_USER_AGENT_PROGRAM; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 8 | ▶ | sub http_user_agent_command { | ||||||||||||||||||
|
Subroutine
http_user_agent_command: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 67 | 8 | my ($purpose, $params) = @_; | |||||||||||||||||||
| 68 | 8 | my $ua = http_user_agent_program; | |||||||||||||||||||
| 69 | 8 | my $cmd = $commands{ $ua }->{ $purpose }; | |||||||||||||||||||
| 70 | 8 | for (keys %$params) { | |||||||||||||||||||
| 71 | 10 | $cmd =~ s!{$_}!\Q$params->{$_}\E!g; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 8 | ▶ | if ($HTTP_VERBOSE) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 3 | ▶ | unless ($ua eq "fetch") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 2 | $cmd =~ s/(silent|quiet)/verbose/; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 8 | $cmd = $ua . " " . $cmd; | |||||||||||||||||||
| 81 | 8 | ▶ | return ($ua, $cmd) if wantarray; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 2 | return $cmd; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 3 | ▶ | sub http_download { | ||||||||||||||||||
|
Subroutine
http_download: called CC=6 SCAR=19.2
Pod
undocumented
| |||||||||||||||||||||
| 86 | 3 | my ($url, $path) = @_; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 3 | ▶ | if (-e $path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 1 | die "ERROR: The download target < $path > already exists.\n"; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | my $partial = 0; | |||||||||||||||||||
| 93 | 0 | ▶ | local $SIG{TERM} = local $SIG{INT} = sub { $partial++ }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | my $download_command = http_user_agent_command(download => { url => $url, output => $path }); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | my $status = system($download_command); | |||||||||||||||||||
| 98 | 2 | ▶ | if ($partial) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | $path->unlink; | |||||||||||||||||||
| 100 | 0 | return "ERROR: Interrupted."; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 2 | ▶ | unless ($status == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 2 | $path->unlink; | |||||||||||||||||||
| 104 | 2 | ▶ | if ($? == -1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 105 | 1 | return "ERROR: Failed to execute the command\n\n\t$download_command\n\nReason:\n\n\t$!"; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | elsif ($? & 127) { | ||||||||||||||||||||
| 108 | 0 | return "ERROR: The command died with signal " . ($? & 127) . "\n\n\t$download_command\n\n"; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||
| 111 | 1 | return "ERROR: The command finished with error\n\n\t$download_command\n\nExit code:\n\n\t" . ($? >> 8); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | 0 | return 0; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 0 | ▶ | sub http_get { | ||||||||||||||||||
|
Subroutine
http_get: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 118 | 0 | my ($url, $header, $cb) = @_; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | if (ref($header) eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | $cb = $header; | |||||||||||||||||||
| 122 | 0 | $header = undef; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | my ($program, $command) = http_user_agent_command(get => { url => $url }); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | open my $fh, '-|', $command | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | or die "open() pipe for '$command': $!"; | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | local $/; | |||||||||||||||||||
| 131 | 0 | my $body = <$fh>; | |||||||||||||||||||
| 132 | 0 | close $fh; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # check if the download has failed and die automatically | ||||||||||||||||||||
| 135 | 0 | $commands{ $program }{ die_on_error }->($?); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | ▶ | return $cb ? $cb->($body) : $body; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 75 | 1; | |||||||||||||||||||