Tinyish/Curl.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 190 CC 16 SCAR 32.6
CC
16
Cov
0%
build_options
72.0
configure
20.0
request
12.0
CRAP 26.1
1 package HTTP::Tinyish::Curl;
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 use File::Temp ();
9  
10 my %supports;
11 my $curl;
12  
13 sub _slurp {
14 open my $fh, "<", shift or die $!;
15 local $/;
16 <$fh>;
17 }
18  
19 sub configure {
20 my $class = shift;
21  
22 my %meta;
23 $curl = which('curl');
24  
25 eval {
26 run3([$curl, '--version'], \undef, \my $version, \my $error);
27 if ($version =~ /^Protocols: (.*)/m) {
28 my %protocols = map { $_ => 1 } split /\s/, $1;
29 $supports{http} = 1 if $protocols{http};
30 $supports{https} = 1 if $protocols{https};
31 }
32  
33 $meta{$curl} = $version;
34 };
35  
36 \%meta;
37 }
38  
39 sub supports { $supports{$_[1]} }
40  
41 sub new {
42 my($class, %attr) = @_;
43 bless \%attr, $class;
44 }
45  
46 sub request {
47 my($self, $method, $url, $opts) = @_;
48 $opts ||= {};
49  
50 my(undef, $temp) = File::Temp::tempfile(UNLINK => 1);
51  
52 my($output, $error);
53 eval {
54 run3 [
55 $curl,
56 '-X', $method,
57 ($method eq 'HEAD' ? ('--head') : ()),
58 $self->build_options($url, $opts),
59 '--dump-header', $temp,
60 $url,
61 ], \undef, \$output, \$error,
62 {
63 binmode_stdout => ":raw",
64 binmode_stderr => ":raw",
65 };
66 };
67  
68 if ($@ or $?) {
69 return $self->internal_error($url, $@ || $error);
70 }
71  
72 my $res = { url => $url, content => $output };
73 $self->parse_http_header( _slurp($temp), $res );
74 $res;
75 }
76  
77 sub mirror {
78 my($self, $url, $file, $opts) = @_;
79 $opts ||= {};
80  
81 my(undef, $temp) = File::Temp::tempfile(UNLINK => 1);
82 my $tempfile = $file . int(rand(2**31));
83  
84 my($output, $error);
85 eval {
86 run3 [
87 $curl,
88 $self->build_options($url, $opts),
89 '-z', $file,
90 '-o', $tempfile,
91 '--dump-header', $temp,
92 '--remote-time',
93 $url,
94 ], \undef, \$output, \$error,
95 {
96 binmode_stdout => ":raw",
97 binmode_stderr => ":raw",
98 };
99 };
100  
101 if ($@ or $?) {
102 unlink $tempfile;
103 return $self->internal_error($url, $@ || $error);
104 }
105  
106 my $res = { url => $url, content => $output };
107 $self->parse_http_header( _slurp($temp), $res );
108  
109 if ($res->{status} eq '200') {
110 rename $tempfile, $file
111 or die "Error replacing $file with $tempfile: $!\n";
112 }
113 unlink $tempfile;
114 $res;
115 }
116  
117 sub build_options {
118 my($self, $url, $opts) = @_;
119  
120 my @options = (
121 '--silent',
122 '--show-error',
123 '--max-time', ($self->{timeout} || 60),
124 '--user-agent', ($self->{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"),
125 );
126 if (my $max_redirect = exists $self->{max_redirect} ? $self->{max_redirect} : 5) {
127 push @options, '--location', '--max-redirs', $max_redirect;
128 }
129  
130 my %headers;
131 if ($self->{default_headers}) {
132 %headers = %{$self->{default_headers}};
133 }
134 if ($opts->{headers}) {
135 %headers = (%headers, %{$opts->{headers}});
136 }
137 $self->_translate_headers(\%headers, \@options);
138  
139 unless ($self->{verify_SSL}) {
140 push @options, '--insecure';
141 }
142  
143 if ($opts->{content}) {
144 my $content;
145 if (ref $opts->{content} eq 'CODE') {
146 while (my $chunk = $opts->{content}->()) {
147 $content .= $chunk;
148 }
149 } else {
150 $content = $opts->{content};
151 }
152 push @options, '--data', $content;
153 }
154  
155 @options;
156 }
157  
158 sub _translate_headers {
159 my($self, $headers, $options) = @_;
160  
161 for my $field (keys %$headers) {
162 my $value = $headers->{$field};
163 if (ref $value eq 'ARRAY') {
164 push @$options, map { ('-H', "$field:$_") } @$value;
165 } else {
166 push @$options, '-H', "$field:$value";
167 }
168 }
169 }
170  
171 1;