lib/App/Perlbrew/HTTP.pm

filter: Statementstmt 72.2% 52 / 72 Branchbran 47.0% 16 / 34 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 4 totaltotal 60.6% 77 / 127 CC 21 SCAR 19.5
CC
21
Cov
64%
http_get
20.0
http_download
6.8
http_user_agent_program
6.4
CRAP 7.1
« lib/App/perlbrew.pm ↑ Summary lib/App/Perlbrew/Util.pm »
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
BranchTrueFalseTotal
if $_[0] >> 8 == 22 0 0 0
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
BranchTrueFalseTotal
if $_[0] 0 0 0
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
BranchTrueFalseTotal
if $_[0] >> 8 == 8 0 0 0
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 }
l!l&&r!l&&!r
630
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 }
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($code != 127) 3 0 3
49 3 $program = $p;
50 3 last;
51 }
52 }
53  
54 3 ▶ unless ($program) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($program) 0 3 3
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
BranchTrueFalseTotal
unless $commands{$App::Perlbrew::HTTP::HTTP_USER_AGENT_PROGRAM} 0 9 9
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
BranchTrueFalseTotal
if ($App::Perlbrew::HTTP::HTTP_VERBOSE) 3 5 8
75 3 ▶ unless ($ua eq "fetch") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua eq "fetch") 2 1 3
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
BranchTrueFalseTotal
if wantarray 6 2 8
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
BranchTrueFalseTotal
if (-e $path) 1 2 3
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
BranchTrueFalseTotal
if ($partial) 0 2 2
99 0 $path->unlink;
100 0 return "ERROR: Interrupted.";
101 }
102 2 ▶ unless ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status == 0) 2 0 2
103 2 $path->unlink;
104 2 ▶ if ($? == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 1 1 2
elsif ($? & 127) { } 0 1 1
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
BranchTrueFalseTotal
if (ref $header eq "CODE") 0 0 0
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
BranchTrueFalseTotal
open my $fh, "-|", $command or die "open() pipe for '${command}': $!" 0 0 0
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
BranchTrueFalseTotal
$cb ? : 0 0 0
138 }
139  
140 75 1;
« lib/App/perlbrew.pm ↑ Summary lib/App/Perlbrew/Util.pm »