| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Alien::Build::Plugin::Fetch::LWP; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 1459 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 57 |  | 
| 4 | 2 |  |  | 2 |  | 8 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 48 |  | 
| 5 | 2 |  |  | 2 |  | 40 | use 5.008004; | 
|  | 2 |  |  |  |  | 7 |  | 
| 6 | 2 |  |  | 2 |  | 353 | use Alien::Build::Plugin; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 7 | 2 |  |  | 2 |  | 11 | use Carp (); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 876 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | # ABSTRACT: Plugin for fetching files using LWP | 
| 10 |  |  |  |  |  |  | our $VERSION = '2.47'; # VERSION | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | has '+url' => ''; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | has ssl => 0; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub init | 
| 19 |  |  |  |  |  |  | { | 
| 20 | 4 |  |  | 4 | 1 | 25 | my($self, $meta) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 4 |  |  |  |  | 13 | $meta->add_requires('share' => 'LWP::UserAgent' => 0 ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 4 |  | 66 |  |  | 9 | $meta->prop->{start_url} ||= $self->url; | 
| 25 | 4 |  |  |  |  | 10 | $self->url($meta->prop->{start_url}); | 
| 26 | 4 | 50 |  |  |  | 9 | $self->url || Carp::croak('url is a required property'); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 4 | 50 | 33 |  |  | 8 | if($self->url =~ /^https:/ || $self->ssl) | 
| 29 |  |  |  |  |  |  | { | 
| 30 | 0 |  |  |  |  | 0 | $meta->add_requires('share' => 'LWP::Protocol::https' => 0 ); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | $meta->register_hook( fetch => sub { | 
| 34 | 0 |  |  | 0 |  | 0 | my($build, $url, %options) = @_; | 
| 35 | 0 |  | 0 |  |  | 0 | $url ||= $self->url; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 |  |  |  |  | 0 | my @headers; | 
| 38 | 0 | 0 |  |  |  | 0 | if(my $headers = $options{http_headers}) | 
| 39 |  |  |  |  |  |  | { | 
| 40 | 0 | 0 |  |  |  | 0 | if(ref $headers eq 'ARRAY') | 
| 41 |  |  |  |  |  |  | { | 
| 42 | 0 |  |  |  |  | 0 | @headers = @$headers; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | else | 
| 45 |  |  |  |  |  |  | { | 
| 46 | 0 |  |  |  |  | 0 | $build->log("Fetch for $url with http_headers that is not an array reference"); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  |  |  | 0 | my $ua = LWP::UserAgent->new; | 
| 51 | 0 |  |  |  |  | 0 | $ua->env_proxy; | 
| 52 | 0 |  |  |  |  | 0 | my $res = $ua->get($url, @headers); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 |  |  |  | 0 | die "error fetching $url: @{[ $res->status_line ]}" | 
|  | 0 |  |  |  |  | 0 |  | 
| 55 |  |  |  |  |  |  | unless $res->is_success; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  | 0 | my($type, $charset) = $res->content_type_charset; | 
| 58 | 0 |  |  |  |  | 0 | my $base            = $res->base; | 
| 59 | 0 |  |  |  |  | 0 | my $filename        = $res->filename; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 | 0 |  |  |  | 0 | if($type eq 'text/html') | 
|  |  | 0 |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | { | 
| 63 |  |  |  |  |  |  | return { | 
| 64 | 0 |  | 0 |  |  | 0 | type    => 'html', | 
| 65 |  |  |  |  |  |  | charset => $charset, | 
| 66 |  |  |  |  |  |  | base    => "$base", | 
| 67 |  |  |  |  |  |  | content => $res->decoded_content || $res->content, | 
| 68 |  |  |  |  |  |  | }; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | elsif($type eq 'text/ftp-dir-listing') | 
| 71 |  |  |  |  |  |  | { | 
| 72 |  |  |  |  |  |  | return { | 
| 73 | 0 |  | 0 |  |  | 0 | type => 'dir_listing', | 
| 74 |  |  |  |  |  |  | base => "$base", | 
| 75 |  |  |  |  |  |  | content => $res->decoded_content || $res->content, | 
| 76 |  |  |  |  |  |  | }; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | else | 
| 79 |  |  |  |  |  |  | { | 
| 80 |  |  |  |  |  |  | return { | 
| 81 | 0 |  | 0 |  |  | 0 | type     => 'file', | 
| 82 |  |  |  |  |  |  | filename => $filename || 'downloadedfile', | 
| 83 |  |  |  |  |  |  | content  => $res->content, | 
| 84 |  |  |  |  |  |  | }; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 4 |  |  |  |  | 30 | }); | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 4 |  |  |  |  | 7 | $self; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | 1; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | __END__ |