AnyUA/Backend/Net/Curl/Easy.pm

filter: Statementstmt 13.6% 17 / 125 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 41.6% 5 / 12 Podpod 100% 1 / 1 totaltotal 10.7% 23 / 215 CC 36 SCAR 46.7
CC
36
Cov
9%
request
240.0
_munge_response
110.0
__ANON__
30.0
CRAP 106.8
↑ Summary AnyUA/Backend/AnyEvent/HTTP.pm »
1 package HTTP::AnyUA::Backend::Net::Curl::Easy;
2 # ABSTRACT: A unified programming interface for Net::Curl::Easy
3  
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.904'; # VERSION
9  
10 1 ▶use parent 'HTTP::AnyUA::Backend';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use HTTP::AnyUA::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 0 ▶sub request {
Subroutine request: not called CC=15 SCAR=54.8
Pod documented
17 0 my $self = shift;
18 0 my ($method, $url, $args) = @_;
19  
20 0 my $ua = $self->ua;
21  
22 # reset
23 0 $ua->setopt(Net::Curl::Easy::CURLOPT_HTTPGET(), 0);
24 0 $ua->setopt(Net::Curl::Easy::CURLOPT_NOBODY(), 0);
25 0 $ua->setopt(Net::Curl::Easy::CURLOPT_READFUNCTION(), undef);
26 0 $ua->setopt(Net::Curl::Easy::CURLOPT_POSTFIELDS(), undef);
27 0 $ua->setopt(Net::Curl::Easy::CURLOPT_POSTFIELDSIZE(), 0);
28  
29 0 ▶ if ($method eq 'GET') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
elsif ($method eq 'HEAD') { } 0 0 0
30 0 $ua->setopt(Net::Curl::Easy::CURLOPT_HTTPGET(), 1);
31 }
32 elsif ($method eq 'HEAD') {
33 0 $ua->setopt(Net::Curl::Easy::CURLOPT_NOBODY(), 1);
34 }
35  
36 0 ▶ if (my $content = $args->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = $args->{"content"}) 0 0 0
37 0 ▶ if (ref($content) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 0 0 0
38 0 my $content_length;
39 0 ▶ for my $header (keys %{$args->{headers} || {}}) {
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
40 0 ▶ if (lc($header) eq 'content-length') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "content-length") 0 0 0
41 0 $content_length = $args->{headers}{$header};
42 0 last;
43 }
44 }
45  
46 0 ▶ if ($content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length) { } 0 0 0
47 0 my $chunk;
48 0 ▶ $ua->setopt(Net::Curl::Easy::CURLOPT_READFUNCTION(), sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
49 0 my $ua = shift;
50 0 my $maxlen = shift;
51  
52 0 ▶ if (!$chunk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk) 0 0 0
53 0 $chunk = $content->();
54 0 ▶ return 0 if !$chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk 0 0 0
55 }
56  
57 0 my $part = substr($chunk, 0, $maxlen, '');
58 0 return \$part;
59 0 });
60 0 $ua->setopt(Net::Curl::Easy::CURLOPT_POSTFIELDSIZE(), $content_length);
61 }
62 else {
63 # if we don't know the length we have to just read it all in
64 0 $content = HTTP::AnyUA::Util::coderef_content_to_string($content);
65 }
66 }
67 0 ▶ if (ref($content) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content ne "CODE") 0 0 0
68 0 $ua->setopt(Net::Curl::Easy::CURLOPT_POSTFIELDS(), $content);
69 0 $ua->setopt(Net::Curl::Easy::CURLOPT_POSTFIELDSIZE(), length $content);
70 }
71 }
72  
73 0 $ua->setopt(Net::Curl::Easy::CURLOPT_URL(), $url);
74 0 $ua->setopt(Net::Curl::Easy::CURLOPT_CUSTOMREQUEST(), $method);
75  
76 # munge headers
77 0 my @headers;
78 0 ▶ for my $header (keys %{$args->{headers} || {}}) {
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
79 0 my $value = $args->{headers}{$header};
80 0 ▶ my @values = ref($value) eq 'ARRAY' ? @$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
81 0 for my $v (@values) {
82 0 push @headers, "${header}: $v";
83 }
84 }
85 0 ▶ $ua->setopt(Net::Curl::Easy::CURLOPT_HTTPHEADER(), \@headers) if @headers;
Branch: 1 branch
BranchTrueFalseTotal
if @headers 0 0 0
86  
87 0 my @hdrdata;
88  
89 0 ▶ $ua->setopt(Net::Curl::Easy::CURLOPT_HEADERFUNCTION(), sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
90 0 my $ua = shift;
91 0 my $data = shift;
92 0 my $size = length $data;
93  
94 0 my %headers = _parse_header($data);
95  
96 0 ▶ if ($headers{Status}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers{"Status"}) 0 0 0
97 0 push @hdrdata, {};
98 }
99  
100 0 my $resp_headers = $hdrdata[-1];
101  
102 0 for my $key (keys %headers) {
103 0 ▶ if (!$resp_headers->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp_headers->{$key}) { } 0 0 0
104 0 $resp_headers->{$key} = $headers{$key};
105 }
106 else {
107 0 ▶ if (ref($resp_headers->{$key}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resp_headers->{$key} ne "ARRAY") 0 0 0
108 0 $resp_headers->{$key} = [$resp_headers->{$key}];
109 }
110 0 push @{$resp_headers->{$key}}, $headers{$key};
111 }
112 }
113  
114 0 return $size;
115 0 });
116  
117 0 my $resp_body = '';
118  
119 0 my $data_cb = $args->{data_callback};
120 0 my $copy = $self;
121 0 Scalar::Util::weaken($copy);
122 0 ▶ $ua->setopt(Net::Curl::Easy::CURLOPT_WRITEFUNCTION(), sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
123 0 my $ua = shift;
124 0 my $data = shift;
125 0 my $fh = shift;
126 0 my $size = length $data;
127  
128 0 ▶ if ($data_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_cb) { } 0 0 0
129 0 my $resp = $copy->_munge_response(undef, undef, [@hdrdata], $data_cb);
130 0 $data_cb->($data, $resp);
131 }
132 else {
133 0 print $fh $data;
134 }
135  
136 0 return $size;
137 0 });
138 0 open(my $fileb, '>', \$resp_body);
139 0 $ua->setopt(Net::Curl::Easy::CURLOPT_WRITEDATA(), $fileb);
140  
141 0 eval { $ua->perform };
142 0 my $ret = $@;
143  
144 0 return $self->_munge_response($ret, $resp_body, [@hdrdata], $data_cb);
145 }
146  
147  
148 0 ▶sub _munge_response {
Subroutine _munge_response: not called CC=10 SCAR=47.0
149 0 my $self = shift;
150 0 my $error = shift;
151 0 my $body = shift;
152 0 my $hdrdata = shift;
153 0 my $data_cb = shift;
154  
155 0 ▶ my %headers = %{pop @$hdrdata || {}};
Condition0%0/2
pop @$hdrdata || {}
l!l
00
MC/DC0%0/1
pop @$hdrdata || {}
pop @$hdrdata
Truth table0%0/2
A
A= pop @$hdrdata
Aresult
11
00
156  
157 0 ▶ my $code = delete $headers{Status} || $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE()) || 599;
Condition0%0/5
delete $headers{'Status'} || $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE()) || 599
l!l
00
delete $headers{"Status"} or $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE())
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $headers{'Status'} || $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE()) || 599
delete $headers{"Status"} $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE())
Truth table0%0/3
A or B
A= delete $headers{"Status"}B= $self->ua->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE())
ABresult
1X1
011
000
158 0 my $reason = delete $headers{Reason};
159 0 my $url = $self->ua->getinfo(Net::Curl::Easy::CURLINFO_EFFECTIVE_URL());
160  
161 0 ▶ my $resp = {
Condition0%0/3
200 <= $code && $code <= 299
!ll&&!rl&&r
000
MC/DC0%0/2
200 <= $code && $code <= 299
200 <= $code $code <= 299
Truth table0%0/3
A && B
A= 200 <= $codeB= $code <= 299
ABresult
0X0
100
111
162 success => 200 <= $code && $code <= 299,
163 url => $url,
164 status => $code,
165 reason => $reason,
166 headers => \%headers,
167 };
168  
169 0 ▶ my $version = delete $headers{HTTPVersion} || _http_version($self->ua->getinfo(Net::Curl::Easy::CURLINFO_HTTP_VERSION()));
Condition0%0/3
delete $headers{'HTTPVersion'} || _http_version($self->ua->getinfo(Net::Curl::Easy::CURLINFO_HTTP_VERSION()))
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $headers{'HTTPVersion'} || _http_version($self->ua->getinfo(Net::Curl::Easy::CURLINFO_HTTP_VERSION()))
delete $headers{'HTTPVersion'} _http_version($self->ua->getinfo(Net::Curl::Easy::CURLINFO_HTTP_VERSION()))
Truth table0%0/3
A || B
A= delete $headers{'HTTPVersion'}B= _http_version($self->ua->getinfo(Net::Curl::Easy::CURLINFO_HTTP_VERSION()))
ABresult
1X1
011
000
170 0 ▶ $resp->{protocol} = "HTTP/$version" if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 0 0 0
171  
172 # We have the headers for the redirect chain in $hdrdata, but we don't have the contents, and we
173 # would also need to reconstruct the URLs.
174  
175 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
176 0 my $err = $self->ua->strerror($error);
177 0 return HTTP::AnyUA::Util::internal_exception($err, $resp);
178 }
179  
180 0 ▶ $resp->{content} = $body if $body && !$data_cb;
Branch: 1 branch
BranchTrueFalseTotal
if $body and not $data_cb 0 0 0
Condition0%0/3
$body and not $data_cb
!ll&&!rl&&r
000
MC/DC0%0/2
$body and not $data_cb
$body not $data_cb
Truth table0%0/3
A and not(B)
A= $bodyB= not $data_cb
ABresult
0X0
100
111
181  
182 0 return $resp;
183 }
184  
185 # get the HTTP version according to the user agent object
186 0 ▶sub _http_version {
Subroutine _http_version: not called CC=4 SCAR=30.0
187 0 my $version = shift;
188 0 ▶ return $version == Net::Curl::Easy::CURL_HTTP_VERSION_1_0() ? '1.0' :
Branch: 3 branches
BranchTrueFalseTotal
$version == Net::Curl::Easy::CURL_HTTP_VERSION_1_0() ? : 0 0 0
$version == Net::Curl::Easy::CURL_HTTP_VERSION_1_1() ? : 0 0 0
$version == Net::Curl::Easy::CURL_HTTP_VERSION_2_0() ? : 0 0 0
189 $version == Net::Curl::Easy::CURL_HTTP_VERSION_1_1() ? '1.1' :
190 $version == Net::Curl::Easy::CURL_HTTP_VERSION_2_0() ? '2.0' : '';
191 }
192  
193 # parse a header line (or status line) and return as key-value pairs
194 0 ▶sub _parse_header {
Subroutine _parse_header: not called CC=3 SCAR=24.8
195 0 my $data = shift;
196  
197 0 $data =~ s/[\x0A\x0D]*$//;
198  
199 0 ▶ if ($data =~ m!^HTTP/([0-9.]+) [\x09\x20]+ (\d{3}) [\x09\x20]+ ([^\x0A\x0D]*)!x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m[^HTTP/([0-9.]+) [\x09\x20]+ (\d{3}) [\x09\x20]+ ([^\x0A\x0D]*)]x) 0 0 0
200 return (
201 0 HTTPVersion => $1,
202 Status => $2,
203 Reason => $3,
204 );
205 }
206  
207 0 my ($key, $val) = split(/:\s*/, $data, 2);
208 0 ▶ return if !$key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
209 0 return (lc($key) => $val);
210 }
211  
212 # no Net::Curl::Easy;
213  
214 1 1;
215  
216 __END__
↑ Summary AnyUA/Backend/AnyEvent/HTTP.pm »