blib/lib/Alien/Build/Plugin/Fetch/CurlCommand.pm

filter: Statementstmt 68.1% 75 / 110 Branchbran 36.8% 14 / 38 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 15 / 20 Podpod 100% 3 / 3 totaltotal 59.6% 111 / 186 CC 28 SCAR 25.2
CC
28
Cov
59%
protocol_ok
42.0
__ANON__
17.3
_execute
10.7
CRAP 12.5
« blib/lib/Alien/Build/Plugin/Download/Negotiate.pm ↑ Summary blib/lib/Alien/Build.pm »
1 package Alien::Build::Plugin::Fetch::CurlCommand;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Alien::Build::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Capture::Tiny qw( capture );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util 1.33 qw( any pairmap );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Plugin for fetching files using curl
15 3 our $VERSION = '2.88'; # VERSION
16  
17  
18 sub curl_command
19 4 ▶{
Subroutine curl_command: called CC=2 SCAR=7.6
Pod documented
20 4 ▶ defined $ENV{CURL} ? scalar which($ENV{CURL}) : scalar which('curl');
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'CURL'} ? : 0 4 4
21 }
22  
23 3 has ssl => 0;
24 3 has _see_headers => 0;
25 3 has '+url' => '';
26  
27 # when bootstrapping we have to specify this plugin as a prereq
28 # 1 is the default so that when this plugin is used directly
29 # you also get the prereq
30 3 has bootstrap_ssl => 1;
31  
32  
33 sub protocol_ok
34 0 ▶{
Subroutine protocol_ok: not called CC=6 SCAR=37.4
Pod documented
35 0 my($class, $protocol) = @_;
36 0 my $curl = $class->curl_command;
37 0 ▶ return 0 unless defined $curl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $curl 0 0 0
38 0 ▶ my($out, $err, $exit) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 system $curl, '--version';
40 0 };
41  
42 {
43 # make sure curl supports the -J option.
44 # CentOS 6 for example is recent enough
45 # that it does not. gh#147, gh#148, gh#149
46 0 local $CWD = tempdir( CLEANUP => 1 );
47 0 my $file1 = path('foo/foo.txt');
48 0 $file1->parent->mkpath;
49 0 $file1->spew("hello world\n");
50 0 my $url = 'file://' . $file1->absolute;
51 0 ▶ my($out, $err, $exit) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 system $curl, '-O', '-J', $url;
53 0 };
54 0 my $file2 = $file1->parent->child($file1->basename);
55 0 unlink "$file1";
56 0 unlink "$file2";
57 0 rmdir($file1->parent);
58 0 ▶ return 0 if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 0 0
59 }
60  
61 0 foreach my $line (split /\n/, $out)
62 {
63 0 ▶ if($line =~ /^Protocols:\s*(.*)\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Protocols:\s*(.*)\s*$/) 0 0 0
64 {
65 0 my %proto = map { $_ => 1 } split /\s+/, $1;
66 0 ▶ return $proto{$protocol} if $proto{$protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $proto{$protocol} 0 0 0
67 }
68 }
69 0 return 0;
70 }
71  
72 sub init
73 1 ▶{
Subroutine init: called CC=5 SCAR=17.7
Pod documented
74 1 my($self, $meta) = @_;
75  
76 1 ▶ $meta->prop->{start_url} ||= $self->url;
Condition33%1/3
$meta->prop->{"start_url"} ||= $self->url
l!l&&r!l&&!r
100
MC/DC0%0/2
$meta->prop->{"start_url"} ||= $self->url
$meta->prop->{"start_url"} $self->url
Truth table33%1/3
A ||= B
A= $meta->prop->{"start_url"}B= $self->url
ABresult
1X1
011
000
77 1 $self->url($meta->prop->{start_url});
78 1 ▶ $self->url || Carp::croak('url is a required property');
Branch: 1 branch
BranchTrueFalseTotal
$self->url or Carp::croak("url is a required property") 1 0 1
79  
80 1 ▶ $meta->add_requires('configure', 'Alien::Build::Plugin::Fetch::CurlCommand' => '1.19')
Branch: 1 branch
BranchTrueFalseTotal
if $self->bootstrap_ssl 1 0 1
81 if $self->bootstrap_ssl;
82  
83 $meta->register_hook(
84 3 ▶ fetch => sub {
Subroutine __ANON__: called CC=12 SCAR=28.5
85 3 my($build, $url, %options) = @_;
86 3 ▶ $url ||= $self->url;
Condition66%2/3
$url ||= $self->url
l!l&&r!l&&!r
210
MC/DC0%0/2
$url ||= $self->url
$url $self->url
Truth table66%2/3
A ||= B
A= $urlB= $self->url
ABresult
1X1
011
000
87  
88 3 my($scheme) = $url =~ /^([a-z0-9]+):/i;
89  
90 3 ▶ if($scheme =~ /^https?$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme =~ /^https?$/) { } 3 0 3
91 {
92 3 local $CWD = tempdir( CLEANUP => 1 );
93  
94 3 my @writeout = (
95 "ab-filename :%{filename_effective}",
96 "ab-content_type :%{content_type}",
97 "ab-url :%{url_effective}",
98 );
99  
100 3 $build->log("writeout: $_\\n") for @writeout;
101 3 path('writeout')->spew(join("\\n", @writeout));
102  
103 3 my @headers;
104 3 ▶ if(my $headers = $options{http_headers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = $options{"http_headers"}) 0 3 3
105 {
106 0 ▶ if(ref $headers eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
107 {
108 0 ▶ @headers = pairmap { -H => "$a: $b" } @$headers;
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 }
110 else
111 {
112 0 $build->log("Fetch for $url with http_headers that is not an array reference");
113 }
114 }
115  
116 3 my @command = (
117 $self->curl_command,
118 '-L', '-f', '-O', '-J',
119 -w => '@writeout',
120 @headers,
121 );
122  
123 3 ▶ push @command, -D => 'head' if $self->_see_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_see_headers 0 3 3
124  
125 3 push @command, $url;
126  
127 3 my($stdout, $stderr) = $self->_execute($build, @command);
128  
129 2 ▶ my %h = map { /^ab-(.*?)\s*:(.*)$/ ? ($1 => $2) : () } split /\n/, $stdout;
Branch: 1 branch
BranchTrueFalseTotal
/^ab-(.*?)\s*:(.*)$/ ? : 6 0 6
130  
131 2 ▶ if(-e 'head')
Branch: 1 branch
BranchTrueFalseTotal
if (-e "head") 0 2 2
132 {
133 0 $build->log(" ~ $_ => $h{$_}") for sort keys %h;
134 0 $build->log(" header: $_") for path('headers')->lines;
135 }
136  
137 2 my($type) = split /;/, $h{content_type};
138  
139 2 ▶ if($type eq 'text/html')
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'text/html') { } 1 1 2
140 {
141 return {
142 type => 'html',
143 base => $h{url},
144 1 content => scalar path($h{filename})->slurp,
145 protocol => $scheme,
146 };
147 }
148 else
149 {
150 return {
151 type => 'file',
152 filename => $h{filename},
153 1 path => path($h{filename})->absolute->stringify,
154 protocol => $scheme,
155 };
156 }
157 }
158 # elsif($scheme eq 'ftp')
159 # {
160 # if($url =~ m{/$})
161 # {
162 # my($stdout, $stderr) = $self->_execute($build, $self->curl_command, -l => $url);
163 # chomp $stdout;
164 # return {
165 # type => 'list',
166 # list => [
167 # map { { filename => $_, url => "$url$_" } } sort split /\n/, $stdout,
168 # ],
169 # };
170 # }
171 #
172 # my $first_error;
173 #
174 # {
175 # local $CWD = tempdir( CLEANUP => 1 );
176 #
177 # my($filename) = $url =~ m{/([^/]+)$};
178 # $filename = 'unknown' if (! defined $filename) || ($filename eq '');
179 # my($stdout, $stderr) = eval { $self->_execute($build, $self->curl_command, -o => $filename, $url) };
180 # $first_error = $@;
181 # if($first_error eq '')
182 # {
183 # return {
184 # type => 'file',
185 # filename => $filename,
186 # path => path($filename)->absolute->stringify,
187 # };
188 # }
189 # }
190 #
191 # {
192 # my($stdout, $stderr) = eval { $self->_execute($build, $self->curl_command, -l => "$url/") };
193 # if($@ eq '')
194 # {
195 # chomp $stdout;
196 # return {
197 # type => 'list',
198 # list => [
199 # map { { filename => $_, url => "$url/$_" } } sort split /\n/, $stdout,
200 # ],
201 # };
202 # };
203 # }
204 #
205 # $first_error ||= 'unknown error';
206 # die $first_error;
207 #
208 # }
209 else
210 {
211 0 die "scheme $scheme is not supported by the Fetch::CurlCommand plugin";
212 }
213  
214 },
215 1 ▶ ) if $self->curl_command;
Branch: 1 branch
BranchTrueFalseTotal
if $self->curl_command 1 0 1
216  
217 1 $self;
218 }
219  
220 sub _execute
221 3 ▶{
Subroutine _execute: called CC=7 SCAR=23.7
222 3 my($self, $build, @command) = @_;
223 3 $build->log("+ @command");
224 3 ▶ my($stdout, $stderr, $err) = capture {
Subroutine __ANON__: called CC=1 SCAR=0.0
225 3 system @command;
226 3 $?;
227 3 };
228 3 ▶ if($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 2 3
229 {
230 1 chomp $stderr;
231 1 $build->log($_) for split /\n/, $stderr;
232 0 ▶ if($stderr =~ /Remote filename has no length/ && !!(any { /^-O$/ } @command))
Subroutine __ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr =~ /Remote filename has no length/ and not !&any(sub { /^-O$/; } , @command)) 0 1 1
Condition33%1/3
$stderr =~ /Remote filename has no length/ and not !&any(sub { /^-O$/; } , @command)
!ll&&!rl&&r
100
MC/DC0%0/2
$stderr =~ /Remote filename has no length/ and not !&any(sub { /^-O$/; } , @command)
$stderr =~ /Remote filename has no length/ not !&any(sub { /^-O$/; } , @command)
Truth table33%1/3
A and B
A= $stderr =~ /Remote filename has no length/B= not !&any(sub { /^-O$/; } , @command)
ABresult
0X0
100
111
233 {
234 my @new_command = map {
235 0 ▶ /^-O$/ ? ( -o => 'index.html' ) : /^-J$/ ? () : ($_)
Branch: 2 branches
BranchTrueFalseTotal
/^-O$/ ? : 0 0 0
/^-J$/ ? : 0 0 0
236 } @command;
237 0 return $self->_execute($build, @new_command);
238 }
239 1 die "error in curl fetch";
240 }
241 2 ($stdout, $stderr);
242 }
243  
244 3 1;
245  
246 __END__
« blib/lib/Alien/Build/Plugin/Download/Negotiate.pm ↑ Summary blib/lib/Alien/Build.pm »