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

filter: Statementstmt 44.7% 38 / 85 Branchbran 9.3% 3 / 32 Conditioncond 13.7% 4 / 29 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 31.6% 55 / 174 CC 31 SCAR 61.6
CC
31
Cov
31%
__ANON__
702.0
init
26.9
BEGIN
1.0
CRAP 472.3
↑ Summary blib/lib/Alien/Build/Plugin/Core/Setup.pm »
1 package Alien::Build::Plugin::Fetch::HTTPTiny;
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::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Alien::Build::Util qw( _ssl_reqs );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Plugin for fetching files using HTTP::Tiny
12 3 our $VERSION = '2.88'; # VERSION
13  
14  
15 3 has '+url' => '';
16  
17  
18 3 has ssl => 0;
19  
20 # ignored for compatability
21 3 has bootstrap_ssl => 1;
22  
23 sub init
24 9 ▶{
Subroutine init: called CC=6 SCAR=32.9
Pod documented
25 9 my($self, $meta) = @_;
26  
27 9 $meta->add_requires('share' => 'HTTP::Tiny' => '0.044' );
28 9 $meta->add_requires('share' => 'URI' => '0' );
29 9 $meta->add_requires('share' => 'Mozilla::CA' => '0' );
30  
31 9 ▶ $meta->prop->{start_url} ||= $self->url;
Condition66%2/3
$meta->prop->{"start_url"} ||= $self->url
l!l&&r!l&&!r
540
MC/DC0%0/2
$meta->prop->{"start_url"} ||= $self->url
$meta->prop->{"start_url"} $self->url
Truth table66%2/3
A ||= B
A= $meta->prop->{"start_url"}B= $self->url
ABresult
1X1
011
000
32 9 $self->url($meta->prop->{start_url});
33 9 ▶ $self->url || Carp::croak('url is a required property');
Branch: 1 branch
BranchTrueFalseTotal
$self->url or Carp::croak("url is a required property") 9 0 9
34  
35 9 ▶ if($self->url =~ /^https:/ || $self->ssl)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url =~ /^https:/ or $self->ssl) 4 5 9
Condition66%2/3
$self->url =~ /^https:/ or $self->ssl
l!l&&r!l&&!r
405
MC/DC50%1/2
$self->url =~ /^https:/ or $self->ssl
$self->url =~ /^https:/ $self->ssl
Truth table66%2/3
A or B
A= $self->url =~ /^https:/B= $self->ssl
ABresult
1X1
011
000
36 {
37 4 my $reqs = _ssl_reqs;
38 4 foreach my $mod (sort keys %$reqs)
39 {
40 8 $meta->add_requires('share' => $mod => $reqs->{$mod});
41 }
42 }
43  
44 0 ▶ $meta->register_hook( fetch => sub {
Subroutine __ANON__: not called CC=26 SCAR=65.5
45 0 my($build, $url, %options) = @_;
46 0 ▶ $url ||= $self->url;
Condition0%0/3
$url ||= $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$url ||= $self->url
$url $self->url
Truth table0%0/3
A ||= B
A= $urlB= $self->url
ABresult
1X1
011
000
47  
48 0 ▶ $url = URI->new($url) unless ref($url) && $url->isa('URI');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $url and $url->isa("URI") 0 0 0
Condition0%0/3
ref $url and $url->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $url and $url->isa("URI")
ref $url $url->isa("URI")
Truth table0%0/3
A and B
A= ref $urlB= $url->isa("URI")
ABresult
0X0
100
111
49  
50 0 my %headers;
51 0 ▶ if(my $headers = $options{http_headers})
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = $options{"http_headers"}) 0 0 0
52 {
53 0 ▶ if(ref $headers eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
54 {
55 0 my @headers = @$headers;
56 0 ▶ while(@headers)
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 0 0
57 {
58 0 my $key = shift @headers;
59 0 my $value = shift @headers;
60 0 ▶ unless(defined $key && defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and defined $value) 0 0 0
Condition0%0/3
defined $key and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and defined $value
defined $key defined $value
Truth table0%0/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
61 {
62 0 $build->log("Fetch for $url with http_headers contains undef key or value");
63 0 next;
64 }
65 0 push @{ $headers{$key} }, $value;
66 }
67 }
68 else
69 {
70 0 $build->log("Fetch for $url with http_headers that is not an array reference");
71 }
72 }
73  
74 0 my $ua = HTTP::Tiny->new(
75 0 ▶ agent => "Alien-Build/@{[ $Alien::Build::VERSION || 'dev' ]} ",
Branch: 1 branch
BranchTrueFalseTotal
$build->download_rule =~ /encrypt/ ? : 0 0 0
Condition0%0/2
$Alien::Build::VERSION || 'dev'
l!l
00
MC/DC0%0/1
$Alien::Build::VERSION || 'dev'
$Alien::Build::VERSION
Truth table0%0/2
A
A= $Alien::Build::VERSION
Aresult
11
00
76 verify_SSL => $build->download_rule =~ /encrypt/ ? 1 : 0,
77 );
78 0 my $res = $ua->get($url, { headers => \%headers });
79  
80 0 ▶ unless($res->{success})
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
81 {
82 0 ▶ my $status = $res->{status} || '---';
Condition0%0/2
$res->{'status'} || '---'
l!l
00
MC/DC0%0/1
$res->{'status'} || '---'
$res->{'status'}
Truth table0%0/2
A
A= $res->{'status'}
Aresult
11
00
83 0 ▶ my $reason = $res->{reason} || 'unknown';
Condition0%0/2
$res->{'reason'} || 'unknown'
l!l
00
MC/DC0%0/1
$res->{'reason'} || 'unknown'
$res->{'reason'}
Truth table0%0/2
A
A= $res->{'reason'}
Aresult
11
00
84  
85 0 $build->log("$status $reason fetching $url");
86 0 ▶ if($status == 599)
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 599) 0 0 0
87 {
88 0 $build->log("exception: $_") for split /\n/, $res->{content};
89  
90 0 my($can_ssl, $why_ssl) = HTTP::Tiny->can_ssl;
91 0 ▶ if(! $can_ssl)
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_ssl) 0 0 0
92 {
93 0 ▶ if($res->{redirects}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"redirects"}) 0 0 0
94 0 foreach my $redirect (@{ $res->{redirects} })
95 {
96 0 ▶ if(defined $redirect->{headers}->{location} && $redirect->{headers}->{location} =~ /^https:/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redirect->{"headers"}{"location"} and $redirect->{"headers"}{"location"} =~ /^https:/) 0 0 0
Condition0%0/3
defined $redirect->{"headers"}{"location"} and $redirect->{"headers"}{"location"} =~ /^https:/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $redirect->{"headers"}{"location"} and $redirect->{"headers"}{"location"} =~ /^https:/
defined $redirect->{"headers"}{"location"} $redirect->{"headers"}{"location"} =~ /^https:/
Truth table0%0/3
A and B
A= defined $redirect->{"headers"}{"location"}B= $redirect->{"headers"}{"location"} =~ /^https:/
ABresult
0X0
100
111
97 {
98 0 $build->log("An attempt at a SSL URL https was made, but your HTTP::Tiny does not appear to be able to use https.");
99 0 $build->log("Please see: https://metacpan.org/pod/Alien::Build::Manual::FAQ#599-Internal-Exception-errors-downloading-packages-from-the-internet");
100 }
101 }
102 }
103 }
104 }
105  
106 0 die "error fetching $url: $status $reason";
107 }
108  
109 0 my($type) = split /;/, $res->{headers}->{'content-type'};
110 0 $type = lc $type;
111 0 my $base = URI->new($res->{url});
112 0 my $filename = File::Basename::basename do { my $name = $base->path; $name =~ s{/$}{}; $name };
113  
114 # TODO: this doesn't get exercised by t/bin/httpd
115 0 ▶ if(my $disposition = $res->{headers}->{"content-disposition"})
Branch: 1 branch
BranchTrueFalseTotal
if (my $disposition = $res->{"headers"}{"content-disposition"}) 0 0 0
116 {
117 # Note: from memory without quotes does not match the spec,
118 # but many servers actually return this sort of value.
119 0 ▶ if($disposition =~ /filename="([^"]+)"/ || $disposition =~ /filename=([^\s]+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($disposition =~ /filename="([^"]+)"/ or $disposition =~ /filename=([^\s]+)/) 0 0 0
Condition0%0/3
$disposition =~ /filename="([^"]+)"/ or $disposition =~ /filename=([^\s]+)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$disposition =~ /filename="([^"]+)"/ or $disposition =~ /filename=([^\s]+)/
$disposition =~ /filename="([^"]+)"/ $disposition =~ /filename=([^\s]+)/
Truth table0%0/3
A or B
A= $disposition =~ /filename="([^"]+)"/B= $disposition =~ /filename=([^\s]+)/
ABresult
1X1
011
000
120 {
121 0 $filename = $1;
122 }
123 }
124  
125 0 ▶ if($type eq 'text/html')
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'text/html') { } 0 0 0
126 {
127 return {
128 type => 'html',
129 base => $base->as_string,
130 content => $res->{content},
131 0 protocol => $url->scheme,
132 };
133 }
134 else
135 {
136 return {
137 type => 'file',
138 filename => $filename || 'downloadedfile',
139 content => $res->{content},
140 0 ▶ protocol => $url->scheme,
Condition0%0/2
$filename || 'downloadedfile'
l!l
00
MC/DC0%0/1
$filename || 'downloadedfile'
$filename
Truth table0%0/2
A
A= $filename
Aresult
11
00
141 };
142 }
143  
144 9 });
145  
146 9 $self;
147 }
148  
149 3 1;
150  
151 __END__
↑ Summary blib/lib/Alien/Build/Plugin/Core/Setup.pm »