Tinyish/Wget.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 252 CC 22 SCAR 41.7
CC
22
Cov
0%
build_options
156.0
configure
42.0
mirror
12.0
CRAP 65.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 } elsif ($err && $err =~ /wget: unrecognized option '(.*?)'/) {
41 die "This wget ($wget) is too old and doesn't support $1";
42 }
43  
44 (undef, $err) = _run_wget('--method', 'GET', 'http://');
45 if ($err && $err =~ /Invalid host/) {
46 $method_supported = $meta{method_supported} = 1;
47 }
48  
49 };
50  
51 \%meta;
52 }
53  
54 sub supports { $supports{$_[1]} }
55  
56 sub new {
57 my($class, %attr) = @_;
58 bless \%attr, $class;
59 }
60  
61 sub request {
62 my($self, $method, $url, $opts) = @_;
63 $opts ||= {};
64  
65 my($stdout, $stderr);
66 eval {
67 run3 [
68 $wget,
69 $self->build_options($method, $url, $opts),
70 $url,
71 '-O', '-',
72 ], \undef, \$stdout, \$stderr;
73 };
74  
75 # wget exit codes: (man wget)
76 # 4 Network failure.
77 # 5 SSL verification failure.
78 # 6 Username/password authentication failure.
79 # 7 Protocol errors.
80 # 8 Server issued an error response.
81 if ($@ or $? && ($? >> 8) <= 5) {
82 return $self->internal_error($url, $@ || $stderr);
83 }
84  
85 my $header = '';
86 $stderr =~ s{^ (\S.*)$}{ $header .= $1."\n" }gem;
87  
88 my $res = { url => $url, content => $stdout };
89 $self->parse_http_header($header, $res);
90 $res;
91 }
92  
93 sub mirror {
94 my($self, $url, $file, $opts) = @_;
95 $opts ||= {};
96  
97 my $tempfile = $file . int(rand(2**31));
98  
99 # This doesn't send If-Modified-Since because -O and -N are mutually exclusive :(
100 my($stdout, $stderr);
101 eval {
102 run3 [
103 $wget,
104 $self->build_options("GET", $url, $opts),
105 $url,
106 '-O', $tempfile,
107 ], \undef, \$stdout, \$stderr,
108 {
109 binmode_stdout => ":raw",
110 binmode_stderr => ":raw",
111 };
112 };
113  
114 if ($@ or $? && ($? >> 8) <= 5) {
115 unlink $tempfile;
116 return $self->internal_error($url, $@ || $stderr);
117 }
118  
119 $stderr =~ s/^ //gm;
120  
121 my $res = { url => $url, content => $stdout };
122 $self->parse_http_header($stderr, $res);
123  
124 if ($res->{status} eq '200') {
125 rename $tempfile, $file
126 or die "Error replacing $file with $tempfile: $!\n";
127 }
128 unlink $tempfile;
129 $res;
130 }
131  
132 sub build_options {
133 my($self, $method, $url, $opts) = @_;
134  
135 my @options = (
136 '--retry-connrefused',
137 '--server-response',
138 '--timeout', ($self->{timeout} || 60),
139 '--tries', 1,
140 '--max-redirect', (exists $self->{max_redirect} ? $self->{max_redirect} : 5),
141 '--user-agent', ($self->{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"),
142 );
143  
144 if ($method_supported) {
145 push @options, "--method", $method;
146 } else {
147 if ($method eq 'GET' or $method eq 'POST') {
148 # OK
149 } elsif ($method eq 'HEAD') {
150 push @options, '--spider';
151 } else {
152 die "This version of wget doesn't support specifying HTTP method '$method'";
153 }
154 }
155  
156 if ($self->{agent}) {
157 push @options, '--user-agent', $self->{agent};
158 }
159  
160 my %headers;
161 if ($self->{default_headers}) {
162 %headers = %{$self->{default_headers}};
163 }
164 if ($opts->{headers}) {
165 %headers = (%headers, %{$opts->{headers}});
166 }
167 $self->_translate_headers(\%headers, \@options);
168  
169 if ($supports{https} && !$self->{verify_SSL}) {
170 push @options, '--no-check-certificate';
171 }
172  
173 if ($opts->{content}) {
174 my $content;
175 if (ref $opts->{content} eq 'CODE') {
176 while (my $chunk = $opts->{content}->()) {
177 $content .= $chunk;
178 }
179 } else {
180 $content = $opts->{content};
181 }
182  
183 if ($method_supported) {
184 push @options, '--body-data', $content;
185 } else {
186 push @options, '--post-data', $content;
187 }
188 }
189  
190 @options;
191 }
192  
193 sub _translate_headers {
194 my($self, $headers, $options) = @_;
195  
196 for my $field (keys %$headers) {
197 my $value = $headers->{$field};
198 if (ref $value eq 'ARRAY') {
199 # wget doesn't honor multiple header fields
200 push @$options, '--header', "$field:" . join(",", @$value);
201 } else {
202 push @$options, '--header', "$field:$value";
203 }
204 }
205 }
206  
207 1;