Tinyish/Curl.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 167 SCAR 54.8
CC
15
Cov
0%
build_options
72.0
configure
20.0
request
12.0
CRAP 240.0
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  
64 if ($@ or $?) {
65 return $self->internal_error($url, $@ || $error);
66 }
67  
68 my $res = { url => $url, content => $output };
69 $self->parse_http_header( _slurp($temp), $res );
70 $res;
71 }
72  
73 sub mirror {
74 my($self, $url, $file, $opts) = @_;
75 $opts ||= {};
76  
77 my(undef, $temp) = File::Temp::tempfile(UNLINK => 1);
78  
79 my($output, $error);
80 eval {
81 run3 [
82 $curl,
83 $self->build_options($url, $opts),
84 '-z', $file,
85 '-o', $file,
86 '--dump-header', $temp,
87 '--remote-time',
88 $url,
89 ], \undef, \$output, \$error;
90 };
91  
92 if ($@ or $?) {
93 return $self->internal_error($url, $@ || $error);
94 }
95  
96 my $res = { url => $url, content => $output };
97 $self->parse_http_header( _slurp($temp), $res );
98 $res;
99 }
100  
101 sub build_options {
102 my($self, $url, $opts) = @_;
103  
104 my @options = (
105 '--silent',
106 '--show-error',
107 '--max-time', ($self->{timeout} || 60),
108 '--user-agent', ($self->{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"),
109 );
110 if (my $max_redirect = exists $self->{max_redirect} ? $self->{max_redirect} : 5) {
111 push @options, '--location', '--max-redirs', $max_redirect;
112 }
113  
114 my %headers;
115 if ($self->{default_headers}) {
116 %headers = %{$self->{default_headers}};
117 }
118 if ($opts->{headers}) {
119 %headers = (%headers, %{$opts->{headers}});
120 }
121 $self->_translate_headers(\%headers, \@options);
122  
123 unless ($self->{verify_SSL}) {
124 push @options, '--insecure';
125 }
126  
127 if ($opts->{content}) {
128 my $content;
129 if (ref $opts->{content} eq 'CODE') {
130 while (my $chunk = $opts->{content}->()) {
131 $content .= $chunk;
132 }
133 } else {
134 $content = $opts->{content};
135 }
136 push @options, '--data', $content;
137 }
138  
139 @options;
140 }
141  
142 sub _translate_headers {
143 my($self, $headers, $options) = @_;
144  
145 for my $field (keys %$headers) {
146 my $value = $headers->{$field};
147 if (ref $value eq 'ARRAY') {
148 push @$options, map { ('-H', "$field:$_") } @$value;
149 } else {
150 push @$options, '-H', "$field:$value";
151 }
152 }
153 }
154  
155 1;