File Coverage

blib/lib/Alien/Build/Plugin/Fetch/Wget.pm
Criterion Covered Total %
statement 63 76 82.8
branch 13 24 54.1
condition 3 6 50.0
subroutine 16 16 100.0
pod 1 1 100.0
total 96 123 78.0


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::Fetch::Wget;
2              
3 2     2   1337 use strict;
  2         5  
  2         58  
4 2     2   11 use warnings;
  2         3  
  2         43  
5 2     2   30 use 5.008004;
  2         6  
6 2     2   396 use Alien::Build::Plugin;
  2         12  
  2         15  
7 2     2   14 use File::Temp qw( tempdir );
  2         4  
  2         104  
8 2     2   27 use Path::Tiny qw( path );
  2         4  
  2         85  
9 2     2   14 use File::Which qw( which );
  2         4  
  2         103  
10 2     2   11 use Capture::Tiny qw( capture capture_merged );
  2         4  
  2         114  
11 2     2   431 use File::chdir;
  2         2766  
  2         207  
12 2     2   15 use List::Util qw( pairmap );
  2         5  
  2         1704  
13              
14             # ABSTRACT: Plugin for fetching files using wget
15             our $VERSION = '2.45'; # VERSION
16              
17              
18             sub _wget
19             {
20 2 50   2   97 my $wget = defined $ENV{WGET} ? which($ENV{WGET}) : which('wget');
21 2 50       203 return undef unless defined $wget;
22 2     2   71 my $output = capture_merged { system $wget, '--help' };
  2         2638  
23              
24             # The wget that BusyBox implements does not follow that same interface
25             # as GNU wget and may not check ssl certs which is not good.
26 2 50       14174 return undef if $output =~ /BusyBox/;
27 2         23 return $wget;
28             }
29              
30             has wget_command => sub { _wget() };
31             has ssl => 0;
32              
33             # when bootstrapping we have to specify this plugin as a prereq
34             # 1 is the default so that when this plugin is used directly
35             # you also get the prereq
36             has bootstrap_ssl => 1;
37              
38             sub init
39             {
40 1     1 1 4 my($self, $meta) = @_;
41              
42 1 50       4 $meta->add_requires('configure', 'Alien::Build::Plugin::Fetch::Wget' => '1.19')
43             if $self->bootstrap_ssl;
44              
45             $meta->register_hook(
46             fetch => sub {
47 3     3   10 my($build, $url, %options) = @_;
48 3   66     12 $url ||= $meta->prop->{start_url};
49              
50 3         30 my($scheme) = $url =~ /^([a-z0-9]+):/i;
51              
52 3 50 33     15 if($scheme eq 'http' || $scheme eq 'https')
53             {
54 3         12 local $CWD = tempdir( CLEANUP => 1 );
55              
56 3         1107 my @headers;
57 3 50       13 if(my $headers = $options{http_headers})
58             {
59 0 0       0 if(ref $headers eq 'ARRAY')
60             {
61 0         0 my @copy = @$headers;
62 0         0 my %headers;
63 0         0 while(@copy)
64             {
65 0         0 my $key = shift @copy;
66 0         0 my $value = shift @copy;
67 0         0 push @{ $headers{$key} }, $value;
  0         0  
68             }
69 0         0 @headers = pairmap { "--header=$a: @{[ join ', ', @$b ]}" } %headers;
  0         0  
  0         0  
70             }
71             else
72             {
73 0         0 $build->log("Fetch for $url with http_headers that is not an array reference");
74             }
75             }
76              
77 3         21 my($stdout, $stderr) = $self->_execute(
78             $build,
79             $self->wget_command,
80             '-k', '--content-disposition', '-S',
81             @headers,
82             $url,
83             );
84              
85 2         11 my($path) = path('.')->children;
86 2 50       311 die "no file found after wget" unless $path;
87 2         18 my($type) = $stderr =~ /Content-Type:\s*(.*?)$/m;
88 2 50       12 $type =~ s/;.*$// if $type;
89 2 100       7 if($type eq 'text/html')
90             {
91             return {
92 1         7 type => 'html',
93             base => $url,
94             content => scalar $path->slurp,
95             };
96             }
97             else
98             {
99             return {
100 1         6 type => 'file',
101             filename => $path->basename,
102             path => $path->absolute->stringify,
103             };
104             }
105             }
106             else
107             {
108 0         0 die "scheme $scheme is not supported by the Fetch::Wget plugin";
109             }
110             },
111 1 50       4 ) if $self->wget_command;
112             }
113              
114             sub _execute
115             {
116 3     3   18 my($self, $build, @command) = @_;
117 3         27 $build->log("+ @command");
118             my($stdout, $stderr, $err) = capture {
119 3     3   2998 system @command;
120 3         1270 $?;
121 3         108 };
122 3 100       2230 if($err)
123             {
124 1         4 chomp $stderr;
125 1         9 $stderr = [split /\n/, $stderr]->[-1];
126 1         17 die "error in wget fetch: $stderr";
127             }
128 2         10 ($stdout, $stderr);
129             }
130              
131             1;
132              
133             __END__