| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Alien::Build::Plugin::Fetch::HTTPTiny; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 1612 | use strict; | 
|  | 3 |  |  |  |  | 11 |  | 
|  | 3 |  |  |  |  | 103 |  | 
| 4 | 3 |  |  | 3 |  | 17 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 89 |  | 
| 5 | 3 |  |  | 3 |  | 58 | use 5.008004; | 
|  | 3 |  |  |  |  | 11 |  | 
| 6 | 3 |  |  | 3 |  | 444 | use Alien::Build::Plugin; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 24 |  | 
| 7 | 3 |  |  | 3 |  | 19 | use File::Basename (); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 73 |  | 
| 8 | 3 |  |  | 3 |  | 18 | use Alien::Build::Util qw( _ssl_reqs ); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 175 |  | 
| 9 | 3 |  |  | 3 |  | 103 | use Carp (); | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 3003 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # ABSTRACT: Plugin for fetching files using HTTP::Tiny | 
| 12 |  |  |  |  |  |  | our $VERSION = '2.46'; # VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has '+url' => ''; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has ssl => 0; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # ignored for compatability | 
| 21 |  |  |  |  |  |  | has bootstrap_ssl => 1; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub init | 
| 24 |  |  |  |  |  |  | { | 
| 25 | 9 |  |  | 9 | 1 | 46 | my($self, $meta) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 9 |  |  |  |  | 38 | $meta->add_requires('share' => 'HTTP::Tiny' => '0.044' ); | 
| 28 | 9 |  |  |  |  | 31 | $meta->add_requires('share' => 'URI' => 0 ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 9 |  | 66 |  |  | 26 | $meta->prop->{start_url} ||= $self->url; | 
| 31 | 9 |  |  |  |  | 26 | $self->url($meta->prop->{start_url}); | 
| 32 | 9 | 50 |  |  |  | 25 | $self->url || Carp::croak('url is a required property'); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 9 | 100 | 66 |  |  | 22 | if($self->url =~ /^https:/ || $self->ssl) | 
| 35 |  |  |  |  |  |  | { | 
| 36 | 4 |  |  |  |  | 21 | my $reqs = _ssl_reqs; | 
| 37 | 4 |  |  |  |  | 24 | foreach my $mod (sort keys %$reqs) | 
| 38 |  |  |  |  |  |  | { | 
| 39 | 8 |  |  |  |  | 22 | $meta->add_requires('share' => $mod => $reqs->{$mod}); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | $meta->register_hook( fetch => sub { | 
| 44 | 0 |  |  | 0 |  | 0 | my($build, $url, %options) = @_; | 
| 45 | 0 |  | 0 |  |  | 0 | $url ||= $self->url; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 |  |  |  |  | 0 | my %headers; | 
| 48 | 0 | 0 |  |  |  | 0 | if(my $headers = $options{http_headers}) | 
| 49 |  |  |  |  |  |  | { | 
| 50 | 0 | 0 |  |  |  | 0 | if(ref $headers eq 'ARRAY') | 
| 51 |  |  |  |  |  |  | { | 
| 52 | 0 |  |  |  |  | 0 | my @headers = @$headers; | 
| 53 | 0 |  |  |  |  | 0 | while(@headers) | 
| 54 |  |  |  |  |  |  | { | 
| 55 | 0 |  |  |  |  | 0 | my $key = shift @headers; | 
| 56 | 0 |  |  |  |  | 0 | my $value = shift @headers; | 
| 57 | 0 | 0 | 0 |  |  | 0 | unless(defined $key && defined $value) | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 0 |  |  |  |  | 0 | $build->log("Fetch for $url with http_headers contains undef key or value"); | 
| 60 | 0 |  |  |  |  | 0 | next; | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 0 |  |  |  |  | 0 | push @{ $headers{$key} }, $value; | 
|  | 0 |  |  |  |  | 0 |  | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | else | 
| 66 |  |  |  |  |  |  | { | 
| 67 | 0 |  |  |  |  | 0 | $build->log("Fetch for $url with http_headers that is not an array reference"); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  | 0 |  |  | 0 | my $ua = HTTP::Tiny->new( agent => "Alien-Build/@{[ $Alien::Build::VERSION || 'dev' ]} " ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 72 | 0 |  |  |  |  | 0 | my $res = $ua->get($url, { headers => \%headers }); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  | 0 | unless($res->{success}) | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 0 |  | 0 |  |  | 0 | my $status = $res->{status} || '---'; | 
| 77 | 0 |  | 0 |  |  | 0 | my $reason = $res->{reason} || 'unknown'; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  | 0 | $build->log("$status $reason fetching $url"); | 
| 80 | 0 | 0 |  |  |  | 0 | if($status == 599) | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 0 |  |  |  |  | 0 | $build->log("exception: $_") for split /\n/, $res->{content}; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  | 0 | my($can_ssl, $why_ssl) = HTTP::Tiny->can_ssl; | 
| 85 | 0 | 0 |  |  |  | 0 | if(! $can_ssl) | 
| 86 |  |  |  |  |  |  | { | 
| 87 | 0 | 0 |  |  |  | 0 | if($res->{redirects}) { | 
| 88 | 0 |  |  |  |  | 0 | foreach my $redirect (@{ $res->{redirects} }) | 
|  | 0 |  |  |  |  | 0 |  | 
| 89 |  |  |  |  |  |  | { | 
| 90 | 0 | 0 | 0 |  |  | 0 | if(defined $redirect->{headers}->{location} && $redirect->{headers}->{location} =~ /^https:/) | 
| 91 |  |  |  |  |  |  | { | 
| 92 | 0 |  |  |  |  | 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."); | 
| 93 | 0 |  |  |  |  | 0 | $build->log("Please see: https://metacpan.org/pod/Alien::Build::Manual::FAQ#599-Internal-Exception-errors-downloading-packages-from-the-internet"); | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  | 0 | die "error fetching $url: $status $reason"; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  | 0 | my($type) = split /;/, $res->{headers}->{'content-type'}; | 
| 104 | 0 |  |  |  |  | 0 | $type = lc $type; | 
| 105 | 0 |  |  |  |  | 0 | my $base            = URI->new($res->{url}); | 
| 106 | 0 |  |  |  |  | 0 | my $filename        = File::Basename::basename do { my $name = $base->path; $name =~ s{/$}{}; $name }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | # TODO: this doesn't get exercised by t/bin/httpd | 
| 109 | 0 | 0 |  |  |  | 0 | if(my $disposition = $res->{headers}->{"content-disposition"}) | 
| 110 |  |  |  |  |  |  | { | 
| 111 |  |  |  |  |  |  | # Note: from memory without quotes does not match the spec, | 
| 112 |  |  |  |  |  |  | # but many servers actually return this sort of value. | 
| 113 | 0 | 0 | 0 |  |  | 0 | if($disposition =~ /filename="([^"]+)"/ || $disposition =~ /filename=([^\s]+)/) | 
| 114 |  |  |  |  |  |  | { | 
| 115 | 0 |  |  |  |  | 0 | $filename = $1; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 | 0 |  |  |  | 0 | if($type eq 'text/html') | 
| 120 |  |  |  |  |  |  | { | 
| 121 |  |  |  |  |  |  | return { | 
| 122 |  |  |  |  |  |  | type    => 'html', | 
| 123 |  |  |  |  |  |  | base    => $base->as_string, | 
| 124 |  |  |  |  |  |  | content => $res->{content}, | 
| 125 | 0 |  |  |  |  | 0 | }; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | else | 
| 128 |  |  |  |  |  |  | { | 
| 129 |  |  |  |  |  |  | return { | 
| 130 |  |  |  |  |  |  | type     => 'file', | 
| 131 |  |  |  |  |  |  | filename => $filename || 'downloadedfile', | 
| 132 |  |  |  |  |  |  | content  => $res->{content}, | 
| 133 | 0 |  | 0 |  |  | 0 | }; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 9 |  |  |  |  | 109 | }); | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 9 |  |  |  |  | 22 | $self; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | 1; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | __END__ |