Tinyish/Wget.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 41 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 205 SCAR 60.4
CC
20
Cov
0%
build_options
156.0
configure
30.0
_run_wget
6.0
CRAP 420.0
1 package HTTP::Tinyish::Wget;
2 use strict;
3 use warnings;
4 use parent qw(HTTP::Tinyish::Base);
5  
6 use IPC::Run3 qw(run3);
7 use File::Which qw(which);
8  
9 my %supports;
10 my $wget;
11 my $method_supported;
12  
13 sub _run_wget {
14 run3([$wget, @_], \undef, \my $out, \my $err);
15 wantarray ? ($out, $err) : $out;
16 }
17  
18 sub configure {
19 my $class = shift;
20 my %meta;
21  
22 $wget = which('wget');
23  
24 eval {
25 local $ENV{LC_ALL} = 'en_US';
26  
27 $meta{$wget} = _run_wget('--version');
28 unless ($meta{$wget} =~ /GNU Wget 1\.(\d+)/ and $1 >= 12) {
29 die "Wget version is too old. $meta{$wget}";
30 }
31  
32 my $config = $class->new(agent => __PACKAGE__);
33 my @options = grep { $_ ne '--quiet' } $config->build_options("GET");
34  
35 my(undef, $err) = _run_wget(@options, 'https://');
36 if ($err && $err =~ /HTTPS support not compiled/) {
37 $supports{http} = 1;
38 } elsif ($err && $err =~ /Invalid host/) {
39 $supports{http} = $supports{https} = 1;
40 }
41  
42 (undef, $err) = _run_wget('--method', 'GET', 'http://');
43 if ($err && $err =~ /Invalid host/) {
44 $method_supported = $meta{method_supported} = 1;
45 }
46  
47 };
48  
49 \%meta;
50 }
51  
52 sub supports { $supports{$_[1]} }
53  
54 sub new {
55 my($class, %attr) = @_;
56 bless \%attr, $class;
57 }
58  
59 sub request {
60 my($self, $method, $url, $opts) = @_;
61 $opts ||= {};
62  
63 my($stdout, $stderr);
64 eval {
65 run3 [
66 $wget,
67 $self->build_options($method, $url, $opts),
68 $url,
69 '-O', '-',
70 ], \undef, \$stdout, \$stderr;
71 };
72  
73 # wget exit codes: (man wget)
74 # 4 Network failure.
75 # 5 SSL verification failure.
76 # 6 Username/password authentication failure.
77 # 7 Protocol errors.
78 # 8 Server issued an error response.
79 if ($@ or $? && ($? >> 8) <= 5) {
80 return $self->internal_error($url, $@ || $stderr);
81 }
82  
83 my $header = '';
84 $stderr =~ s{^ (\S.*)$}{ $header .= $1."\n" }gem;
85  
86 my $res = { url => $url, content => $stdout };
87 $self->parse_http_header($header, $res);
88 $res;
89 }
90  
91 sub mirror {
92 my($self, $url, $file, $opts) = @_;
93 $opts ||= {};
94  
95 # This doesn't send If-Modified-Since because -O and -N are mutually exclusive :(
96 my($stdout, $stderr);
97 eval {
98 run3 [$wget, $self->build_options("GET", $url, $opts), $url, '-O', $file], \undef, \$stdout, \$stderr;
99 };
100  
101 if ($@ or $?) {
102 return $self->internal_error($url, $@ || $stderr);
103 }
104  
105 $stderr =~ s/^ //gm;
106  
107 my $res = { url => $url, content => $stdout };
108 $self->parse_http_header($stderr, $res);
109 $res;
110 }
111  
112 sub build_options {
113 my($self, $method, $url, $opts) = @_;
114  
115 my @options = (
116 '--retry-connrefused',
117 '--server-response',
118 '--timeout', ($self->{timeout} || 60),
119 '--tries', 1,
120 '--max-redirect', (exists $self->{max_redirect} ? $self->{max_redirect} : 5),
121 '--user-agent', ($self->{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"),
122 );
123  
124 if ($method_supported) {
125 push @options, "--method", $method;
126 } else {
127 if ($method eq 'GET' or $method eq 'POST') {
128 # OK
129 } elsif ($method eq 'HEAD') {
130 push @options, '--spider';
131 } else {
132 die "This version of wget doesn't support specifying HTTP method '$method'";
133 }
134 }
135  
136 if ($self->{agent}) {
137 push @options, '--user-agent', $self->{agent};
138 }
139  
140 my %headers;
141 if ($self->{default_headers}) {
142 %headers = %{$self->{default_headers}};
143 }
144 if ($opts->{headers}) {
145 %headers = (%headers, %{$opts->{headers}});
146 }
147 $self->_translate_headers(\%headers, \@options);
148  
149 if ($supports{https} && !$self->{verify_SSL}) {
150 push @options, '--no-check-certificate';
151 }
152  
153 if ($opts->{content}) {
154 my $content;
155 if (ref $opts->{content} eq 'CODE') {
156 while (my $chunk = $opts->{content}->()) {
157 $content .= $chunk;
158 }
159 } else {
160 $content = $opts->{content};
161 }
162  
163 if ($method_supported) {
164 push @options, '--body-data', $content;
165 } else {
166 push @options, '--post-data', $content;
167 }
168 }
169  
170 @options;
171 }
172  
173 sub _translate_headers {
174 my($self, $headers, $options) = @_;
175  
176 for my $field (keys %$headers) {
177 my $value = $headers->{$field};
178 if (ref $value eq 'ARRAY') {
179 # wget doesn't honor multiple header fields
180 push @$options, '--header', "$field:" . join(",", @$value);
181 } else {
182 push @$options, '--header', "$field:$value";
183 }
184 }
185 }
186  
187 1;