| 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
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
| |||||||||||||||||||||||||||||
| 32 | 9 | $self->url($meta->prop->{start_url}); | |||||||||||||||||||||||||||
| 33 | 9 | ▶ | $self->url || Carp::croak('url is a required property'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | ▶ | if($self->url =~ /^https:/ || $self->ssl) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->url =~ /^https:/ or $self->ssl
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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$url ||= $self->url
$url
$self->url
Truth table0%0/3
A ||= B
A= $urlB= $self->url
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $url = URI->new($url) unless ref($url) && $url->isa('URI'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $url and $url->isa("URI")
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")
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my %headers; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if(my $headers = $options{http_headers}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if(ref $headers eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 0 | my @headers = @$headers; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | while(@headers) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 0 | my $key = shift @headers; | |||||||||||||||||||||||||||
| 59 | 0 | my $value = shift @headers; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | unless(defined $key && defined $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key and defined $value
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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/2
$Alien::Build::VERSION || 'dev'
MC/DC0%0/1
$Alien::Build::VERSION || 'dev'
$Alien::Build::VERSION
Truth table0%0/2
A
A= $Alien::Build::VERSION
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $status = $res->{status} || '---'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res->{'status'} || '---'
MC/DC0%0/1
$res->{'status'} || '---'
$res->{'status'}
Truth table0%0/2
A
A= $res->{'status'}
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $reason = $res->{reason} || 'unknown'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res->{'reason'} || 'unknown'
MC/DC0%0/1
$res->{'reason'} || 'unknown'
$res->{'reason'}
Truth table0%0/2
A
A= $res->{'reason'}
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $build->log("$status $reason fetching $url"); | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | if($status == 599) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if($res->{redirects}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | foreach my $redirect (@{ $res->{redirects} }) | |||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if(defined $redirect->{headers}->{location} && $redirect->{headers}->{location} =~ /^https:/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $redirect->{"headers"}{"location"} and $redirect->{"headers"}{"location"} =~ /^https:/
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:/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$disposition =~ /filename="([^"]+)"/ or $disposition =~ /filename=([^\s]+)/
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]+)/
| |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | $filename = $1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if($type eq 'text/html') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$filename || 'downloadedfile'
$filename
Truth table0%0/2
A
A= $filename
| |||||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 9 | }); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | $self; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 3 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||