blib/lib/HTTP/Tiny.pm

filter: Statementstmt 89.7% 650 / 724 Branchbran 72.0% 366 / 508 Conditioncond 67.2% 205 / 305 MC/DCmcdc 41.3% 69 / 167 Subroutinesub 95.1% 79 / 83 Podpod 86.6% 13 / 15 totaltotal 76.6% 1382 / 1802 CC 372 SCAR 25.2
CC
372
Cov
79%
_prepare_headers_and_cb
30.6
_create_proxy_tunnel
30.0
write_header_lines
22.5
CRAP 12.5
↑ Summary
1 # vim: ts=4 sts=4 sw=4 et:
2 package HTTP::Tiny;
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: A small, simple, correct HTTP/1.1 client
6  
7 30 our $VERSION = '0.086';
8  
9 15 ▶sub _croak { require Carp; Carp::croak(@_) }
Subroutine _croak: called CC=1 SCAR=0.0
10  
11 #pod =method new
12 #pod
13 #pod $http = HTTP::Tiny->new( %attributes );
14 #pod
15 #pod This constructor returns a new HTTP::Tiny object. Valid attributes include:
16 #pod
17 #pod =for :list
18 #pod * C<agent> — A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If
19 #pod C<agent> — ends in a space character, the default user-agent string is
20 #pod appended.
21 #pod * C<cookie_jar> — An instance of L<HTTP::CookieJar> — or equivalent class
22 #pod that supports the C<add> and C<cookie_header> methods
23 #pod * C<default_headers> — A hashref of default headers to apply to requests
24 #pod * C<local_address> — The local IP address to bind to
25 #pod * C<keep_alive> — Whether to reuse the last connection (if for the same
26 #pod scheme, host and port) (defaults to 1)
27 #pod * C<max_redirect> — Maximum number of redirects allowed (defaults to 5)
28 #pod * C<max_size> — Maximum response size in bytes (only when not using a data
29 #pod callback). If defined, requests with responses larger than this will return
30 #pod a 599 status code.
31 #pod * C<http_proxy> — URL of a proxy server to use for HTTP connections
32 #pod (default is C<$ENV{http_proxy}> — if set)
33 #pod * C<https_proxy> — URL of a proxy server to use for HTTPS connections
34 #pod (default is C<$ENV{https_proxy}> — if set)
35 #pod * C<proxy> — URL of a generic proxy server for both HTTP and HTTPS
36 #pod connections (default is C<$ENV{all_proxy}> — if set)
37 #pod * C<no_proxy> — List of domain suffixes that should not be proxied. Must
38 #pod be a comma-separated string or an array reference. (default is
39 #pod C<$ENV{no_proxy}> —)
40 #pod * C<timeout> — Request timeout in seconds (default is 60) If a socket open,
41 #pod read or write takes longer than the timeout, the request response status code
42 #pod will be 599.
43 #pod * C<verify_SSL> — A boolean that indicates whether to validate the TLS/SSL
44 #pod certificate of an C<https> — connection (default is true). Changed from false
45 #pod to true in version 0.083.
46 #pod * C<SSL_options> — A hashref of C<SSL_*> — options to pass through to
47 #pod L<IO::Socket::SSL>
48 #pod * C<$ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT}> - Changes the default
49 #pod certificate verification behavior to not check server identity if set to 1.
50 #pod Only effective if C<verify_SSL> is not set. Added in version 0.083.
51 #pod
52 #pod
53 #pod An accessor/mutator method exists for each attribute.
54 #pod
55 #pod Passing an explicit C<undef> for C<proxy>, C<http_proxy> or C<https_proxy> will
56 #pod prevent getting the corresponding proxies from the environment.
57 #pod
58 #pod Errors during request execution will result in a pseudo-HTTP status code of 599
59 #pod and a reason of "Internal Exception". The content field in the response will
60 #pod contain the text of the error.
61 #pod
62 #pod The C<keep_alive> parameter enables a persistent connection, but only to a
63 #pod single destination scheme, host and port. If any connection-relevant
64 #pod attributes are modified via accessor, or if the process ID or thread ID change,
65 #pod the persistent connection will be dropped. If you want persistent connections
66 #pod across multiple destinations, use multiple HTTP::Tiny objects.
67 #pod
68 #pod See L</SSL SUPPORT> for more on the C<verify_SSL> and C<SSL_options> attributes.
69 #pod
70 #pod =cut
71  
72 30 my @attributes;
73 BEGIN {
74 30 ▶ @attributes = qw(
Subroutine BEGIN: called CC=2 SCAR=7.0
75 cookie_jar default_headers http_proxy https_proxy keep_alive
76 local_address max_redirect max_size proxy no_proxy
77 SSL_options verify_SSL
78 );
79 30 my %persist_ok = map {; $_ => 1 } qw(
80 cookie_jar default_headers max_redirect max_size
81 );
82 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 30 for my $accessor ( @attributes ) {
85 360 *{$accessor} = sub {
86 @_ > 1
87 ? do {
88 1 ▶ delete $_[0]->{handle} if !$persist_ok{$accessor} && $_[1] ne $_[0]->{$accessor};
Branch: 1 branch
BranchTrueFalseTotal
if not $persist_ok{$accessor} and $_[1] ne $_[0]{$accessor} 0 1 1
Condition33%1/3
not $persist_ok{$accessor} and $_[1] ne $_[0]{$accessor}
!ll&&!rl&&r
100
MC/DC0%0/2
not $persist_ok{$accessor} and $_[1] ne $_[0]{$accessor}
not $persist_ok{$accessor} $_[1] ne $_[0]{$accessor}
Truth table33%1/3
not(A) and B
A= not $persist_ok{$accessor}B= $_[1] ne $_[0]{$accessor}
ABresult
0X0
100
111
89 1 $_[0]->{$accessor} = $_[1]
90 }
91 110 ▶ : $_[0]->{$accessor};
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 109 110
92 360 };
93 }
94 }
95  
96 sub agent {
97 182 ▶ my($self, $agent) = @_;
Subroutine agent: called CC=4 SCAR=13.9
Pod undocumented
98 182 ▶ if( @_ > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 176 6 182
99 $self->{agent} =
100 176 ▶ (defined $agent && $agent =~ / $/) ? $agent . $self->_agent : $agent;
Branch: 1 branch
BranchTrueFalseTotal
defined $agent && $agent =~ / $/ ? : 2 174 176
Condition100%3/3
defined $agent and $agent =~ / $/
!ll&&!rl&&r
11732
MC/DC100%2/2
defined $agent and $agent =~ / $/
defined $agent $agent =~ / $/
Truth table100%3/3
A and B
A= defined $agentB= $agent =~ / $/
ABresult
0X0
100
111
101 }
102 182 return $self->{agent};
103 }
104  
105 sub timeout {
106 8 ▶ my ($self, $timeout) = @_;
Subroutine timeout: called CC=3 SCAR=11.0
Pod undocumented
107 8 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 3 5 8
108 3 $self->{timeout} = $timeout;
109 3 ▶ if ($self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle"}) 2 1 3
110 2 $self->{handle}->timeout($timeout);
111 }
112 }
113 8 return $self->{timeout};
114 }
115  
116 sub new {
117 175 ▶ my($class, %args) = @_;
Subroutine new: called CC=9 SCAR=22.0
Pod documented
118  
119 # Support lower case verify_ssl argument, but only if verify_SSL is not
120 # true.
121 175 ▶ if ( exists $args{verify_ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"verify_ssl"}) 7 168 175
122 7 ▶ $args{verify_SSL} ||= $args{verify_ssl};
Condition100%3/3
$args{"verify_SSL"} ||= $args{"verify_ssl"}
l!l&&r!l&&!r
313
MC/DC100%2/2
$args{"verify_SSL"} ||= $args{"verify_ssl"}
$args{"verify_SSL"} $args{"verify_ssl"}
Truth table100%3/3
A ||= B
A= $args{"verify_SSL"}B= $args{"verify_ssl"}
ABresult
1X1
011
000
123 }
124  
125 my $self = {
126 max_redirect => 5,
127 timeout => defined $args{timeout} ? $args{timeout} : 60,
128 keep_alive => 1,
129 verify_SSL => defined $args{verify_SSL} ? $args{verify_SSL} : _verify_SSL_default(),
130 no_proxy => $ENV{no_proxy},
131 175 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'timeout'} ? : 2 173 175
defined $args{'verify_SSL'} ? : 27 148 175
132  
133 175 bless $self, $class;
134  
135 175 ▶ $class->_validate_cookie_jar( $args{cookie_jar} ) if $args{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"cookie_jar"} 15 160 175
136  
137 173 for my $key ( @attributes ) {
138 2076 ▶ $self->{$key} = $args{$key} if exists $args{$key}
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 153 1923 2076
139 }
140  
141 173 ▶ $self->agent( exists $args{agent} ? $args{agent} : $class->_agent );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'agent'} ? : 2 171 173
142  
143 173 $self->_set_proxies;
144  
145 172 return $self;
146 }
147  
148 sub _verify_SSL_default {
149 331 ▶ my ($self) = @_;
Subroutine _verify_SSL_default: called CC=3 SCAR=11.0
150 # Check if insecure default certificate verification behaviour has been
151 # changed by the user by setting PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT=1
152 331 ▶ return (($ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT} || '') eq '1') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'} || '') eq '1' ? : 9 322 331
Condition100%2/2
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'} || ''
l!l
10321
MC/DC100%1/1
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'} || ''
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'}
Truth table100%2/2
A
A= $ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'}
Aresult
11
00
153 }
154  
155 sub _set_proxies {
156 173 ▶ my ($self) = @_;
Subroutine _set_proxies: called CC=16 SCAR=27.7
157  
158 # get proxies from %ENV only if not provided; explicit undef will disable
159 # getting proxies from the environment
160  
161 # generic proxy
162 173 ▶ if (! exists $self->{proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"proxy"}) 170 3 173
163 170 ▶ $self->{proxy} = $ENV{all_proxy} || $ENV{ALL_PROXY};
Condition100%3/3
$ENV{'all_proxy'} || $ENV{'ALL_PROXY'}
l!l&&r!l&&!r
11168
MC/DC100%2/2
$ENV{'all_proxy'} || $ENV{'ALL_PROXY'}
$ENV{'all_proxy'} $ENV{'ALL_PROXY'}
Truth table100%3/3
A || B
A= $ENV{'all_proxy'}B= $ENV{'ALL_PROXY'}
ABresult
1X1
011
000
164 }
165  
166 173 ▶ if ( defined $self->{proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'proxy'}) { } 4 169 173
167 4 $self->_split_proxy( 'generic proxy' => $self->{proxy} ); # validate
168 }
169 else {
170 169 delete $self->{proxy};
171 }
172  
173 # http proxy
174 173 ▶ if (! exists $self->{http_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"http_proxy"}) 172 1 173
175 # under CGI, bypass HTTP_PROXY as request sets it from Proxy header
176 172 ▶ local $ENV{HTTP_PROXY} = ($ENV{CGI_HTTP_PROXY} || "") if $ENV{REQUEST_METHOD};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_METHOD"} 2 170 172
Condition100%2/2
$ENV{'CGI_HTTP_PROXY'} || ''
l!l
11
MC/DC100%1/1
$ENV{'CGI_HTTP_PROXY'} || ''
$ENV{'CGI_HTTP_PROXY'}
Truth table100%2/2
A
A= $ENV{'CGI_HTTP_PROXY'}
Aresult
11
00
177 172 ▶ $self->{http_proxy} = $ENV{http_proxy} || $ENV{HTTP_PROXY} || $self->{proxy};
Condition100%6/6
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
l!l&&r!l&&!r
64162
$ENV{"http_proxy"} or $ENV{"HTTP_PROXY"}
l!l&&r!l&&!r
42166
MC/DC100%3/3
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
$ENV{"http_proxy"} $ENV{"HTTP_PROXY"} $self->{'proxy'}
Truth table100%4/4
A or B || C
A= $ENV{"http_proxy"}B= $ENV{"HTTP_PROXY"}C= $self->{'proxy'}
ABCresult
1XX1
01X1
0011
0000
178 }
179  
180 173 ▶ if ( defined $self->{http_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'http_proxy'}) { } 10 163 173
181 10 $self->_split_proxy( http_proxy => $self->{http_proxy} ); # validate
182 9 $self->{_has_proxy}{http} = 1;
183 }
184 else {
185 163 delete $self->{http_proxy};
186 }
187  
188 # https proxy
189 172 ▶ if (! exists $self->{https_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"https_proxy"}) 171 1 172
190 171 ▶ $self->{https_proxy} = $ENV{https_proxy} || $ENV{HTTPS_PROXY} || $self->{proxy};
Condition100%6/6
$ENV{'https_proxy'} || $ENV{'HTTPS_PROXY'} || $self->{'proxy'}
l!l&&r!l&&!r
24165
$ENV{"https_proxy"} or $ENV{"HTTPS_PROXY"}
l!l&&r!l&&!r
11169
MC/DC100%3/3
$ENV{'https_proxy'} || $ENV{'HTTPS_PROXY'} || $self->{'proxy'}
$ENV{"https_proxy"} $ENV{"HTTPS_PROXY"} $self->{'proxy'}
Truth table100%4/4
A or B || C
A= $ENV{"https_proxy"}B= $ENV{"HTTPS_PROXY"}C= $self->{'proxy'}
ABCresult
1XX1
01X1
0011
0000
191 }
192  
193 172 ▶ if ( $self->{https_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'https_proxy'}) { } 6 166 172
194 6 $self->_split_proxy( https_proxy => $self->{https_proxy} ); # validate
195 6 $self->{_has_proxy}{https} = 1;
196 }
197 else {
198 166 delete $self->{https_proxy};
199 }
200  
201 # Split no_proxy to array reference if not provided as such
202 172 ▶ unless ( ref $self->{no_proxy} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"no_proxy"} eq "ARRAY") 169 3 172
203 $self->{no_proxy} =
204 169 ▶ (defined $self->{no_proxy}) ? [ split /\s*,\s*/, $self->{no_proxy} ] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'no_proxy'} ? : 6 163 169
205 }
206  
207 172 return;
208 }
209  
210 #pod =method get|head|put|post|patch|delete
211 #pod
212 #pod $response = $http->get($url);
213 #pod $response = $http->get($url, \%options);
214 #pod $response = $http->head($url);
215 #pod
216 #pod These methods are shorthand for calling C<request()> for the given method. The
217 #pod URL must have unsafe characters escaped and international domain names encoded.
218 #pod See C<request()> for valid options and a description of the response.
219 #pod
220 #pod The C<success> field of the response will be true if the status code is 2XX.
221 #pod
222 #pod =cut
223  
224 30 for my $sub_name ( qw/get head put post patch delete/ ) {
225 180 my $req_method = uc $sub_name;
226 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 1 ▶ eval <<"HERE"; ## no critic
Subroutine delete: called CC=5 SCAR=20.8
Subroutine get: called CC=5 SCAR=20.8
Subroutine head: called CC=5 SCAR=20.8
Subroutine patch: not called CC=5 SCAR=20.8
Subroutine post: called CC=5 SCAR=20.8
Subroutine put: called CC=5 SCAR=20.8
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 6 branches
BranchTrueFalseTotal
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->delete(URL, [HASHREF])\n") 0 1 1
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->get(URL, [HASHREF])\n") 3 92 95
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->head(URL, [HASHREF])\n") 0 2 2
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->patch(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->post(URL, [HASHREF])\n") 0 2 2
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->put(URL, [HASHREF])\n") 0 8 8
Condition37%18/48
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
100
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
$args || {}
l!l
01
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
8672
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
216
$args || {}
l!l
686
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
200
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
$args || {}
l!l
02
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
000
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
$args || {}
l!l
00
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
020
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
002
$args || {}
l!l
20
@_ == 2 or @_ == 3 and ref $args eq "HASH"
l!l&&r!l&&!r
080
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
008
$args || {}
l!l
80
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/1
$args || {}
$args
MC/DC66%2/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC100%1/1
$args || {}
$args
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/1
$args || {}
$args
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/1
$args || {}
$args
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/1
$args || {}
$args
MC/DC0%0/3
@_ == 2 or @_ == 3 and ref $args eq "HASH"
@_ == 2 @_ == 3 ref $args eq "HASH"
MC/DC0%0/1
$args || {}
$args
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%1/2
A
A= $args
Aresult
11
00
Truth table75%3/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table100%2/2
A
A= $args
Aresult
11
00
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%1/2
A
A= $args
Aresult
11
00
Truth table0%0/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table0%0/2
A
A= $args
Aresult
11
00
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%1/2
A
A= $args
Aresult
11
00
Truth table25%1/4
A or B and C
A= @_ == 2B= @_ == 3C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
Truth table50%1/2
A
A= $args
Aresult
11
00
228 sub $sub_name {
229 my (\$self, \$url, \$args) = \@_;
230 \@_ == 2 || (\@_ == 3 && ref \$args eq 'HASH')
231 or _croak(q/Usage: \$http->$sub_name(URL, [HASHREF])/ . "\n");
232 return \$self->request('$req_method', \$url, \$args || {});
233 }
234 HERE
235 }
236  
237 #pod =method post_form
238 #pod
239 #pod $response = $http->post_form($url, $form_data);
240 #pod $response = $http->post_form($url, $form_data, \%options);
241 #pod
242 #pod This method executes a C<POST> request and sends the key/value pairs from a
243 #pod form data hash or array reference to the given URL with a C<content-type> of
244 #pod C<application/x-www-form-urlencoded>. If data is provided as an array
245 #pod reference, the order is preserved; if provided as a hash reference, the terms
246 #pod are sorted on key and value for consistency. See documentation for the
247 #pod C<www_form_urlencode> method for details on the encoding.
248 #pod
249 #pod The URL must have unsafe characters escaped and international domain names
250 #pod encoded. See C<request()> for valid options and a description of the response.
251 #pod Any C<content-type> header or content in the options hashref will be ignored.
252 #pod
253 #pod The C<success> field of the response will be true if the status code is 2XX.
254 #pod
255 #pod =cut
256  
257 sub post_form {
258 6 ▶ my ($self, $url, $data, $args) = @_;
Subroutine post_form: called CC=6 SCAR=18.5
Pod documented
259 6 ▶ (@_ == 3 || @_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _croak("Usage: \$http->post_form(URL, DATAREF, [HASHREF])\n") 0 6 6
Condition50%3/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
510
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table50%2/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
260 or _croak(q/Usage: $http->post_form(URL, DATAREF, [HASHREF])/ . "\n");
261  
262 6 my $headers = {};
263 6 ▶ while ( my ($key, $value) = each %{$args->{headers} || {}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$args->{"headers"} or {};}) 1 6 7
$args->{"headers"} or {} 5 2 7
264 1 $headers->{lc $key} = $value;
265 }
266  
267 6 return $self->request('POST', $url, {
268 # Any existing 'headers' key in $args will be overridden with a
269 # normalized version below.
270 %$args,
271 content => $self->www_form_urlencode($data),
272 headers => {
273 %$headers,
274 'content-type' => 'application/x-www-form-urlencoded'
275 },
276 }
277 );
278 }
279  
280 #pod =method mirror
281 #pod
282 #pod $response = $http->mirror($url, $file, \%options)
283 #pod if ( $response->{success} ) {
284 #pod print "$file is up to date\n";
285 #pod }
286 #pod
287 #pod Executes a C<GET> request for the URL and saves the response body to the file
288 #pod name provided. The URL must have unsafe characters escaped and international
289 #pod domain names encoded. If the file already exists, the request will include an
290 #pod C<If-Modified-Since> header with the modification timestamp of the file. You
291 #pod may specify a different C<If-Modified-Since> header yourself in the C<<
292 #pod $options->{headers} >> hash.
293 #pod
294 #pod The C<success> field of the response will be true if the status code is 2XX
295 #pod or if the status code is 304 (unmodified).
296 #pod
297 #pod If the file was modified and the server response includes a properly
298 #pod formatted C<Last-Modified> header, the file modification time will
299 #pod be updated accordingly.
300 #pod
301 #pod =cut
302  
303 sub mirror {
304 9 ▶ my ($self, $url, $file, $args) = @_;
Subroutine mirror: called CC=17 SCAR=28.8
Pod documented
305 9 ▶ @_ == 3 || (@_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _croak("Usage: \$http->mirror(URL, FILE, [HASHREF])\n") 4 5 9
Condition100%6/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
414
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
311
MC/DC66%2/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table75%3/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
306 or _croak(q/Usage: $http->mirror(URL, FILE, [HASHREF])/ . "\n");
307  
308 5 ▶ if ( exists $args->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"headers"}) 1 4 5
309 1 my $headers = {};
310 1 ▶ while ( my ($key, $value) = each %{$args->{headers} || {}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$args->{"headers"} or {};}) 1 1 2
$args->{"headers"} or {} 0 2 2
311 1 $headers->{lc $key} = $value;
312 }
313 1 $args->{headers} = $headers;
314 }
315  
316 5 ▶ if ( -e $file and my $mtime = (stat($file))[9] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and my $mtime = (stat $file)[9]) 3 2 5
Condition66%2/3
-e $file and my $mtime = (stat $file)[9]
!ll&&!rl&&r
203
MC/DC50%1/2
-e $file and my $mtime = (stat $file)[9]
-e $file my $mtime = (stat $file)[9]
Truth table66%2/3
A and B
A= -e $fileB= my $mtime = (stat $file)[9]
ABresult
0X0
100
111
317 3 ▶ $args->{headers}{'if-modified-since'} ||= $self->_http_date($mtime);
Condition66%2/3
$args->{"headers"}{"if-modified-since"} ||= $self->_http_date($mtime)
l!l&&r!l&&!r
120
MC/DC0%0/2
$args->{"headers"}{"if-modified-since"} ||= $self->_http_date($mtime)
$args->{"headers"}{"if-modified-since"} $self->_http_date($mtime)
Truth table66%2/3
A ||= B
A= $args->{"headers"}{"if-modified-since"}B= $self->_http_date($mtime)
ABresult
1X1
011
000
318 }
319 5 my $tempfile = $file . int(rand(2**31));
320  
321 5 require Fcntl;
322 5 ▶ sysopen my $fh, $tempfile, Fcntl::O_CREAT()|Fcntl::O_EXCL()|Fcntl::O_WRONLY()
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $tempfile, 193 or _croak("Error: Could not create temporary file $tempfile for downloading: $!\n") 0 5 5
323 or _croak(qq/Error: Could not create temporary file $tempfile for downloading: $!\n/);
324 5 binmode $fh;
325 5 ▶ $args->{data_callback} = sub { print {$fh} $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
326 5 my $response = $self->request('GET', $url, $args);
327 5 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or _croak("Error: Caught error closing temporary file ${tempfile}: $!\n") 0 5 5
328 or _croak(qq/Error: Caught error closing temporary file $tempfile: $!\n/);
329  
330 5 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 3 2 5
331 3 ▶ rename $tempfile, $file
Branch: 1 branch
BranchTrueFalseTotal
rename $tempfile, $file or _croak("Error replacing $file with ${tempfile}: $!\n") 0 3 3
332 or _croak(qq/Error replacing $file with $tempfile: $!\n/);
333 3 my $lm = $response->{headers}{'last-modified'};
334 3 ▶ if ( $lm and my $mtime = $self->_parse_http_date($lm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lm and my $mtime = $self->_parse_http_date($lm)) 3 0 3
Condition33%1/3
$lm and my $mtime = $self->_parse_http_date($lm)
!ll&&!rl&&r
003
MC/DC0%0/2
$lm and my $mtime = $self->_parse_http_date($lm)
$lm my $mtime = $self->_parse_http_date($lm)
Truth table33%1/3
A and B
A= $lmB= my $mtime = $self->_parse_http_date($lm)
ABresult
0X0
100
111
335 3 utime $mtime, $mtime, $file;
336 }
337 }
338 5 ▶ $response->{success} ||= $response->{status} eq '304';
Condition100%3/3
$response->{"success"} ||= $response->{"status"} eq 304
l!l&&r!l&&!r
311
MC/DC100%2/2
$response->{"success"} ||= $response->{"status"} eq 304
$response->{"success"} $response->{"status"} eq 304
Truth table100%3/3
A ||= B
A= $response->{"success"}B= $response->{"status"} eq 304
ABresult
1X1
011
000
339 5 unlink $tempfile;
340 5 return $response;
341 }
342  
343 #pod =method request
344 #pod
345 #pod $response = $http->request($method, $url);
346 #pod $response = $http->request($method, $url, \%options);
347 #pod
348 #pod Executes an HTTP request of the given method type ('GET', 'HEAD', 'POST',
349 #pod 'PUT', etc.) on the given URL. The URL must have unsafe characters escaped and
350 #pod international domain names encoded.
351 #pod
352 #pod B<NOTE>: Method names are B<case-sensitive> per the HTTP/1.1 specification.
353 #pod Don't use C<get> when you really want C<GET>. See L<LIMITATIONS> for
354 #pod how this applies to redirection.
355 #pod
356 #pod If the URL includes a "user:password" stanza, they will be used for Basic-style
357 #pod authorization headers. (Authorization headers will not be included in a
358 #pod redirected request.) For example:
359 #pod
360 #pod $http->request('GET', 'http://Aladdin:open sesame@example.com/');
361 #pod
362 #pod If the "user:password" stanza contains reserved characters, they must
363 #pod be percent-escaped:
364 #pod
365 #pod $http->request('GET', 'http://john%40example.com:password@example.com/');
366 #pod
367 #pod A hashref of options may be appended to modify the request.
368 #pod
369 #pod Valid options are:
370 #pod
371 #pod =for :list
372 #pod * C<headers> —
373 #pod A hashref containing headers to include with the request. If the value for
374 #pod a header is an array reference, the header will be output multiple times with
375 #pod each value in the array. These headers over-write any default headers.
376 #pod * C<content> —
377 #pod A scalar to include as the body of the request OR a code reference
378 #pod that will be called iteratively to produce the body of the request
379 #pod * C<trailer_callback> —
380 #pod A code reference that will be called if it exists to provide a hashref
381 #pod of trailing headers (only used with chunked transfer-encoding)
382 #pod * C<data_callback> —
383 #pod A code reference that will be called for each chunks of the response
384 #pod body received.
385 #pod * C<peer> —
386 #pod Override host resolution and force all connections to go only to a
387 #pod specific peer address, regardless of the URL of the request. This will
388 #pod include any redirections! This options should be used with extreme
389 #pod caution (e.g. debugging or very special circumstances). It can be given as
390 #pod either a scalar or a code reference that will receive the hostname and
391 #pod whose response will be taken as the address.
392 #pod
393 #pod The C<Host> header is generated from the URL in accordance with RFC 2616. It
394 #pod is a fatal error to specify C<Host> in the C<headers> option. Other headers
395 #pod may be ignored or overwritten if necessary for transport compliance.
396 #pod
397 #pod If the C<content> option is a code reference, it will be called iteratively
398 #pod to provide the content body of the request. It should return the empty
399 #pod string or undef when the iterator is exhausted.
400 #pod
401 #pod If the C<content> option is the empty string, no C<content-type> or
402 #pod C<content-length> headers will be generated.
403 #pod
404 #pod If the C<data_callback> option is provided, it will be called iteratively until
405 #pod the entire response body is received. The first argument will be a string
406 #pod containing a chunk of the response body, the second argument will be the
407 #pod in-progress response hash reference, as described below. (This allows
408 #pod customizing the action of the callback based on the C<status> or C<headers>
409 #pod received prior to the content body.)
410 #pod
411 #pod Content data in the request/response is handled as "raw bytes". Any
412 #pod encoding/decoding (with associated headers) are the responsibility of the
413 #pod caller.
414 #pod
415 #pod The C<request> method returns a hashref containing the response. The hashref
416 #pod will have the following keys:
417 #pod
418 #pod =for :list
419 #pod * C<success> —
420 #pod Boolean indicating whether the operation returned a 2XX status code
421 #pod * C<url> —
422 #pod URL that provided the response. This is the URL of the request unless
423 #pod there were redirections, in which case it is the last URL queried
424 #pod in a redirection chain
425 #pod * C<status> —
426 #pod The HTTP status code of the response
427 #pod * C<reason> —
428 #pod The response phrase returned by the server
429 #pod * C<content> —
430 #pod The body of the response. If the response does not have any content
431 #pod or if a data callback is provided to consume the response body,
432 #pod this will be the empty string
433 #pod * C<headers> —
434 #pod A hashref of header fields. All header field names will be normalized
435 #pod to be lower case. If a header is repeated, the value will be an arrayref;
436 #pod it will otherwise be a scalar string containing the value
437 #pod * C<protocol> -
438 #pod If this field exists, it is the protocol of the response
439 #pod such as HTTP/1.0 or HTTP/1.1
440 #pod * C<redirects>
441 #pod If this field exists, it is an arrayref of response hash references from
442 #pod redirects in the same order that redirections occurred. If it does
443 #pod not exist, then no redirections occurred.
444 #pod
445 #pod On an error during the execution of the request, the C<status> field will
446 #pod contain 599, and the C<content> field will contain the text of the error.
447 #pod
448 #pod =cut
449  
450 30 my %idempotent = map { $_ => 1 } qw/GET HEAD PUT DELETE OPTIONS TRACE/;
451  
452 sub request {
453 161 ▶ my ($self, $method, $url, $args) = @_;
Subroutine request: called CC=16 SCAR=30.7
Pod documented
454 161 ▶ @_ == 3 || (@_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _croak("Usage: \$http->request(METHOD, URL, [HASHREF])\n") 4 157 161
Condition100%6/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
281303
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
31129
MC/DC66%2/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table75%3/4
A or B and C
A= @_ == 3B= @_ == 4C= ref $args eq "HASH"
ABCresult
1XX1
0111
00X0
0100
455 or _croak(q/Usage: $http->request(METHOD, URL, [HASHREF])/ . "\n");
456 157 ▶ $args ||= {}; # we keep some state in this during _request
Condition100%2/2
$args ||= {}
l!l
12928
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
457  
458 # RFC 2616 Section 8.1.4 mandates a single retry on broken socket
459 157 my $response;
460 157 for ( 0 .. 1 ) {
461 157 $response = eval { $self->_request($method, $url, $args) };
462 157 ▶ last unless $@ && $idempotent{$method}
Branch: 1 branch
BranchTrueFalseTotal
unless $@ and $idempotent{$method} and $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/ 20 0 20
Condition66%4/6
$@ and $idempotent{$method} and $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/
!ll&&!rl&&r
137200
$@ and $idempotent{$method}
!ll&&!rl&&r
137020
MC/DC0%0/3
$@ and $idempotent{$method} and $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/
$@ $idempotent{$method} $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/
Truth table50%2/4
A and B and C
A= $@B= $idempotent{$method}C= $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/
ABCresult
0XX0
10X0
1100
1111
463 && $@ =~ m{^(?:Socket closed|Unexpected end|SSL read error)};
464 }
465  
466 157 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 20 137 157
467 # maybe we got a response hash thrown from somewhere deep
468 20 ▶ if ( ref $e eq 'HASH' && exists $e->{status} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq "HASH" and exists $e->{"status"}) 0 20 20
Condition33%1/3
ref $e eq "HASH" and exists $e->{"status"}
!ll&&!rl&&r
2000
MC/DC0%0/2
ref $e eq "HASH" and exists $e->{"status"}
ref $e eq "HASH" exists $e->{"status"}
Truth table33%1/3
A and B
A= ref $e eq "HASH"B= exists $e->{"status"}
ABresult
0X0
100
111
469 0 ▶ $e->{redirects} = delete $args->{_redirects} if @{ $args->{_redirects} || []};
Branch: 2 branches
BranchTrueFalseTotal
if @{$args->{"_redirects"} or [];} 0 0 0
$args->{"_redirects"} or [] 0 0 0
470 0 return $e;
471 }
472  
473 # otherwise, stringify it
474 20 $e = "$e";
475 $response = {
476 url => $url,
477 success => q{},
478 status => 599,
479 reason => 'Internal Exception',
480 content => $e,
481 headers => {
482 'content-type' => 'text/plain',
483 'content-length' => length $e,
484 },
485 20 ▶ ( @{$args->{_redirects} || []} ? (redirects => delete $args->{_redirects}) : () ),
Branch: 2 branches
BranchTrueFalseTotal
@{$args->{'_redirects'} or [];} ? : 0 20 20
$args->{"_redirects"} or [] 19 1 20
486 };
487 }
488 157 return $response;
489 }
490  
491 #pod =method www_form_urlencode
492 #pod
493 #pod $params = $http->www_form_urlencode( $data );
494 #pod $response = $http->get("http://example.com/query?$params");
495 #pod
496 #pod This method converts the key/value pairs from a data hash or array reference
497 #pod into a C<x-www-form-urlencoded> string. The keys and values from the data
498 #pod reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an
499 #pod array reference, the key will be repeated with each of the values of the array
500 #pod reference. If data is provided as a hash reference, the key/value pairs in the
501 #pod resulting string will be sorted by key and value for consistent ordering.
502 #pod
503 #pod =cut
504  
505 sub www_form_urlencode {
506 6 ▶ my ($self, $data) = @_;
Subroutine www_form_urlencode: called CC=11 SCAR=25.4
Pod documented
507 6 ▶ (@_ == 2 && ref $data)
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 and ref $data or _croak("Usage: \$http->www_form_urlencode(DATAREF)\n") 0 6 6
Condition33%1/3
@_ == 2 and ref $data
!ll&&!rl&&r
006
MC/DC0%0/2
@_ == 2 and ref $data
@_ == 2 ref $data
Truth table33%1/3
A and B
A= @_ == 2B= ref $data
ABresult
0X0
100
111
508 or _croak(q/Usage: $http->www_form_urlencode(DATAREF)/ . "\n");
509 6 ▶ (ref $data eq 'HASH' || ref $data eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or ref $data eq "ARRAY" or _croak("form data must be a hash or array reference\n") 0 6 6
Condition66%2/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
240
MC/DC0%0/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table66%2/3
A or B
A= ref $data eq "HASH"B= ref $data eq "ARRAY"
ABresult
1X1
011
000
510 or _croak("form data must be a hash or array reference\n");
511  
512 6 ▶ my @params = ref $data eq 'HASH' ? %$data : @$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 2 4 6
513 6 ▶ @params % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 == 0 or _croak("form data reference must have an even number of terms\n") 0 6 6
514 or _croak("form data reference must have an even number of terms\n");
515  
516 6 my @terms;
517 6 ▶ while( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 14 5 19
518 14 my ($key, $value) = splice(@params, 0, 2);
519 14 ▶ _croak("form data keys must not be undef")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 13 14
520 if !defined($key);
521 13 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 13 13
522 0 unshift @params, map { $key => $_ } @$value;
523 }
524 else {
525 13 push @terms, join("=", map { $self->_uri_escape($_) } $key, $value);
526 }
527 }
528  
529 5 ▶ return join("&", (ref $data eq 'ARRAY') ? (@terms) : (sort @terms) );
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 3 2 5
530 }
531  
532 #pod =method can_ssl
533 #pod
534 #pod $ok = HTTP::Tiny->can_ssl;
535 #pod ($ok, $why) = HTTP::Tiny->can_ssl;
536 #pod ($ok, $why) = $http->can_ssl;
537 #pod
538 #pod Indicates if SSL support is available. When called as a class object, it
539 #pod checks for the correct version of L<Net::SSLeay> and L<IO::Socket::SSL>.
540 #pod When called as an object methods, if C<SSL_verify> is true or if C<SSL_verify_mode>
541 #pod is set in C<SSL_options>, it checks that a CA file is available.
542 #pod
543 #pod In scalar context, returns a boolean indicating if SSL is available.
544 #pod In list context, returns the boolean and a (possibly multi-line) string of
545 #pod errors indicating why SSL isn't available.
546 #pod
547 #pod =cut
548  
549 sub can_ssl {
550 33 ▶ my ($self) = @_;
Subroutine can_ssl: called CC=9 SCAR=23.3
Pod documented
551  
552 33 my($ok, $reason) = (1, '');
553  
554 # Need IO::Socket::SSL 1.42 for SSL_create_ctx_callback
555 33 local @INC = @INC;
556 33 ▶ pop @INC if $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 33 0 33
557 33 ▶ unless (eval {require IO::Socket::SSL; IO::Socket::SSL->VERSION(1.42)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require IO::Socket::SSL; "IO::Socket::SSL"->VERSION("1.42") } }) 0 33 33
558 0 $ok = 0;
559 0 $reason .= qq/IO::Socket::SSL 1.42 must be installed for https support\n/;
560 }
561  
562 # Need Net::SSLeay 1.49 for MODE_AUTO_RETRY
563 33 ▶ unless (eval {require Net::SSLeay; Net::SSLeay->VERSION(1.49)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Net::SSLeay; "Net::SSLeay"->VERSION("1.49") } }) 0 33 33
564 0 $ok = 0;
565 0 $reason .= qq/Net::SSLeay 1.49 must be installed for https support\n/;
566 }
567  
568 # If an object, check that SSL config lets us get a CA if necessary
569 33 ▶ if ( ref($self) && ( $self->{verify_SSL} || $self->{SSL_options}{SSL_verify_mode} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->{"verify_SSL"} || $self->{"SSL_options"}{"SSL_verify_mode"}) 3 30 33
Condition50%3/6
ref $self and $self->{"verify_SSL"} || $self->{"SSL_options"}{"SSL_verify_mode"}
!ll&&!rl&&r
3030
$self->{"verify_SSL"} or $self->{"SSL_options"}{"SSL_verify_mode"}
l!l&&r!l&&!r
300
MC/DC0%0/3
ref $self and $self->{"verify_SSL"} || $self->{"SSL_options"}{"SSL_verify_mode"}
ref $self $self->{"verify_SSL"} $self->{"SSL_options"}{"SSL_verify_mode"}
Truth table25%1/4
A and B or C
A= ref $selfB= $self->{"verify_SSL"}C= $self->{"SSL_options"}{"SSL_verify_mode"}
ABCresult
0XX0
1000
11X1
1011
570 my $handle = HTTP::Tiny::Handle->new(
571 SSL_options => $self->{SSL_options},
572 verify_SSL => $self->{verify_SSL},
573 3 );
574 3 ▶ unless ( eval { $handle->_find_CA_file; 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $handle->_find_CA_file; 1 } }) 1 2 3
575 1 $ok = 0;
576 1 $reason .= "$@";
577 }
578 }
579  
580 33 ▶ wantarray ? ($ok, $reason) : $ok;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 30 3 33
581 }
582  
583 #pod =method connected
584 #pod
585 #pod $host = $http->connected;
586 #pod ($host, $port) = $http->connected;
587 #pod
588 #pod Indicates if a connection to a peer is being kept alive, per the C<keep_alive>
589 #pod option.
590 #pod
591 #pod In scalar context, returns the peer host and port, joined with a colon, or
592 #pod C<undef> (if no peer is connected).
593 #pod In list context, returns the peer host and port or an empty list (if no peer
594 #pod is connected).
595 #pod
596 #pod B<Note>: This method cannot reliably be used to discover whether the remote
597 #pod host has closed its end of the socket.
598 #pod
599 #pod =cut
600  
601 sub connected {
602 0 ▶ my ($self) = @_;
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
603  
604 0 ▶ if ( $self->{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle"}) 0 0 0
605 0 return $self->{handle}->connected;
606 }
607 0 return;
608 }
609  
610 #--------------------------------------------------------------------------#
611 # private methods
612 #--------------------------------------------------------------------------#
613  
614 30 my %DefaultPort = (
615 http => 80,
616 https => 443,
617 );
618  
619 sub _agent {
620 175 ▶ my $class = ref($_[0]) || $_[0];
Subroutine _agent: called CC=3 SCAR=11.2
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
31720
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
621 175 (my $default_agent = $class) =~ s{::}{-}g;
622 175 my $version = $class->VERSION;
623 175 ▶ $default_agent .= "/$version" if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 175 0 175
624 175 return $default_agent;
625 }
626  
627 sub _request {
628 170 ▶ my ($self, $method, $url, $args) = @_;
Subroutine _request: called CC=22 SCAR=30.9
629  
630 170 my ($scheme, $host, $port, $path_query, $auth) = $self->_split_url($url);
631  
632 170 ▶ if ($scheme ne 'http' && $scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "http" and $scheme ne "https") 1 169 170
Condition100%3/3
$scheme ne "http" and $scheme ne "https"
!ll&&!rl&&r
139301
MC/DC100%2/2
$scheme ne "http" and $scheme ne "https"
$scheme ne "http" $scheme ne "https"
Truth table100%3/3
A and B
A= $scheme ne "http"B= $scheme ne "https"
ABresult
0X0
100
111
633 1 die(qq/Unsupported URL scheme '$scheme'\n/);
634 }
635  
636 my $request = {
637 method => $method,
638 scheme => $scheme,
639 host => $host,
640 port => $port,
641 169 ▶ host_port => ($port == $DefaultPort{$scheme} ? $host : "$host:$port"),
Branch: 1 branch
BranchTrueFalseTotal
$port == $DefaultPort{$scheme} ? : 167 2 169
642 uri => $path_query,
643 headers => {},
644 };
645  
646 169 ▶ my $peer = $args->{peer} || $host;
Condition33%1/3
$args->{'peer'} || $host
l!l&&r!l&&!r
01690
MC/DC0%0/2
$args->{'peer'} || $host
$args->{'peer'} $host
Truth table33%1/3
A || B
A= $args->{'peer'}B= $host
ABresult
1X1
011
000
647  
648 # Allow 'peer' to be a coderef.
649 169 ▶ if ('CODE' eq ref $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $peer) 0 169 169
650 0 $peer = $peer->($host);
651 }
652  
653 # We remove the cached handle so it is not reused in the case of redirect.
654 # If all is well, it will be recached at the end of _request. We only
655 # reuse for the same scheme, host and port
656 169 my $handle = delete $self->{handle};
657 169 ▶ if ( $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) 13 156 169
658 13 ▶ unless ( $handle->can_reuse( $scheme, $host, $port, $peer ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->can_reuse($scheme, $host, $port, $peer)) 10 3 13
659 10 $handle->close;
660 10 undef $handle;
661 }
662 }
663 169 ▶ $handle ||= $self->_open_handle( $request, $scheme, $host, $port, $peer );
Condition100%3/3
$handle ||= $self->_open_handle($request, $scheme, $host, $port, $peer)
l!l&&r!l&&!r
31642
MC/DC100%2/2
$handle ||= $self->_open_handle($request, $scheme, $host, $port, $peer)
$handle $self->_open_handle($request, $scheme, $host, $port, $peer)
Truth table100%3/3
A ||= B
A= $handleB= $self->_open_handle($request, $scheme, $host, $port, $peer)
ABresult
1X1
011
000
664  
665 155 $self->_prepare_headers_and_cb($request, $args, $url, $auth);
666 154 $handle->write_request($request);
667  
668 153 my $response;
669 155 do { $response = $handle->read_response_header }
670 153 ▶ until (substr($response->{status},0,1) ne '1');
Branch: 1 branch
BranchTrueFalseTotal
substr($response->{"status"}, 0, 1) ne 1 or do { do { $response = $handle->read_response_header } } 2 151 153
671  
672 151 ▶ $self->_update_cookie_jar( $url, $response ) if $self->{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} 34 117 151
673 151 my @redir_args = $self->_maybe_redirect($request, $response, $args);
674  
675 151 my $known_message_length;
676 151 ▶ if ($method eq 'HEAD' || $response->{status} =~ /^[23]04/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'HEAD' or $response->{'status'} =~ /^[23]04/) { } 2 149 151
Condition100%3/3
$method eq "HEAD" or $response->{"status"} =~ /^[23]04/
l!l&&r!l&&!r
11149
MC/DC100%2/2
$method eq "HEAD" or $response->{"status"} =~ /^[23]04/
$method eq "HEAD" $response->{"status"} =~ /^[23]04/
Truth table100%3/3
A or B
A= $method eq "HEAD"B= $response->{"status"} =~ /^[23]04/
ABresult
1X1
011
000
677 # response has no message body
678 2 $known_message_length = 1;
679 }
680 else {
681 # Ignore any data callbacks during redirection.
682 149 ▶ my $cb_args = @redir_args ? +{} : $args;
Branch: 1 branch
BranchTrueFalseTotal
@redir_args ? : 13 136 149
683 149 my $data_cb = $self->_prepare_data_cb($response, $cb_args);
684 149 $known_message_length = $handle->read_body($data_cb, $response);
685 }
686  
687 150 ▶ if ( $self->{keep_alive}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'keep_alive'} and $handle->connected and $known_message_length and $response->{'protocol'} eq 'HTTP/1.1' and ($response->{'headers'}{'connection'} || '') ne 'close') { } 37 113 150
Condition92%13/14
$self->{"keep_alive"} and $handle->connected and $known_message_length and $response->{"protocol"} eq "HTTP/1.1" and ($response->{"headers"}{"connection"} || "") ne "close"
!ll&&!rl&&r
110337
$self->{"keep_alive"} and $handle->connected and $known_message_length and $response->{"protocol"} eq "HTTP/1.1"
!ll&&!rl&&r
108240
$self->{"keep_alive"} and $handle->connected and $known_message_length
!ll&&!rl&&r
106242
$self->{"keep_alive"} and $handle->connected
!ll&&!rl&&r
106044
$response->{'headers'}{'connection'} || ''
l!l
1525
MC/DC80%4/5
$self->{"keep_alive"} and $handle->connected and $known_message_length and $response->{"protocol"} eq "HTTP/1.1" and ($response->{"headers"}{"connection"} || "") ne "close"
$self->{"keep_alive"} $handle->connected $known_message_length $response->{"protocol"} eq "HTTP/1.1" $response->{'headers'}{'connection'}
Truth table83%5/6
A and B and C and D and E
A= $self->{"keep_alive"}B= $handle->connectedC= $known_message_lengthD= $response->{"protocol"} eq "HTTP/1.1"E= $response->{'headers'}{'connection'}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
688 && $handle->connected
689 && $known_message_length
690 && $response->{protocol} eq 'HTTP/1.1'
691 && ($response->{headers}{connection} || '') ne 'close'
692 ) {
693 37 $self->{handle} = $handle;
694 }
695 else {
696 113 $handle->close;
697 }
698  
699 150 $response->{success} = substr( $response->{status}, 0, 1 ) eq '2';
700 150 $response->{url} = $url;
701  
702 # Push the current response onto the stack of redirects if redirecting.
703 150 ▶ if (@redir_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@redir_args) 13 137 150
704 13 push @{$args->{_redirects}}, $response;
705 13 return $self->_request(@redir_args, $args);
706 }
707  
708 # Copy the stack of redirects into the response before returning.
709 $response->{redirects} = delete $args->{_redirects}
710 137 ▶ if @{$args->{_redirects}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$args->{"_redirects"};} 11 126 137
711 137 return $response;
712 }
713  
714 sub _open_handle {
715 166 ▶ my ($self, $request, $scheme, $host, $port, $peer) = @_;
Subroutine _open_handle: called CC=3 SCAR=11.1
716  
717 my $handle = HTTP::Tiny::Handle->new(
718 timeout => $self->{timeout},
719 SSL_options => $self->{SSL_options},
720 verify_SSL => $self->{verify_SSL},
721 local_address => $self->{local_address},
722 keep_alive => $self->{keep_alive}
723 166 );
724  
725 166 ▶ if ($self->{_has_proxy}{$scheme} && ! grep { $host =~ /\Q$_\E$/ } @{$self->{no_proxy}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_has_proxy'}{$scheme} and not grep({$host =~ /\Q$_\E$/;} @{$$self{'no_proxy'};})) { } 2 164 166
Condition66%2/3
$self->{"_has_proxy"}{$scheme} and not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
!ll&&!rl&&r
16402
MC/DC50%1/2
$self->{"_has_proxy"}{$scheme} and not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
$self->{"_has_proxy"}{$scheme} not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
Truth table66%2/3
A and not(B)
A= $self->{"_has_proxy"}{$scheme}B= not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
ABresult
0X0
100
111
726 2 return $self->_proxy_connect( $request, $handle );
727 }
728 else {
729 164 return $handle->connect($scheme, $host, $port, $peer);
730 }
731 }
732  
733 sub _proxy_connect {
734 2 ▶ my ($self, $request, $handle) = @_;
Subroutine _proxy_connect: called CC=8 SCAR=25.1
735  
736 2 my @proxy_vars;
737 2 ▶ if ( $request->{scheme} eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'scheme'} eq 'https') { } 0 2 2
738 0 ▶ _croak(qq{No https_proxy defined}) unless $self->{https_proxy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"https_proxy"} 0 0 0
739 0 @proxy_vars = $self->_split_proxy( https_proxy => $self->{https_proxy} );
740 0 ▶ if ( $proxy_vars[0] eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy_vars[0] eq "https") 0 0 0
741 0 _croak(qq{Can't proxy https over https: $request->{uri} via $self->{https_proxy}});
742 }
743 }
744 else {
745 2 ▶ _croak(qq{No http_proxy defined}) unless $self->{http_proxy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"http_proxy"} 0 2 2
746 2 @proxy_vars = $self->_split_proxy( http_proxy => $self->{http_proxy} );
747 }
748  
749 2 my ($p_scheme, $p_host, $p_port, $p_auth) = @proxy_vars;
750  
751 2 ▶ if ( length $p_auth && ! defined $request->{headers}{'proxy-authorization'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}) 1 1 2
Condition66%2/3
length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}
!ll&&!rl&&r
101
MC/DC50%1/2
length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}
length $p_auth not defined $request->{"headers"}{"proxy-authorization"}
Truth table66%2/3
A and not(B)
A= length $p_authB= not defined $request->{"headers"}{"proxy-authorization"}
ABresult
0X0
100
111
752 1 $self->_add_basic_auth_header( $request, 'proxy-authorization' => $p_auth );
753 }
754  
755 2 $handle->connect($p_scheme, $p_host, $p_port, $p_host);
756  
757 2 ▶ if ($request->{scheme} eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'scheme'} eq 'https') { } 0 2 2
758 0 $self->_create_proxy_tunnel( $request, $handle );
759 }
760 else {
761 # non-tunneled proxy requires absolute URI
762 2 $request->{uri} = "$request->{scheme}://$request->{host_port}$request->{uri}";
763 }
764  
765 2 return $handle;
766 }
767  
768 sub _split_proxy {
769 22 ▶ my ($self, $type, $proxy) = @_;
Subroutine _split_proxy: called CC=6 SCAR=18.8
770  
771 22 my ($scheme, $host, $port, $path_query, $auth) = eval { $self->_split_url($proxy) };
772  
773 22 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme and length $scheme and length $host and length $port and $path_query eq "/") 0 21 21
Condition66%8/12
defined $scheme and length $scheme and length $host and length $port and $path_query eq "/"
!ll&&!rl&&r
1021
defined $scheme and length $scheme and length $host and length $port
!ll&&!rl&&r
1021
defined $scheme and length $scheme and length $host
!ll&&!rl&&r
1021
defined $scheme and length $scheme
!ll&&!rl&&r
1021
MC/DC20%1/5
defined $scheme and length $scheme and length $host and length $port and $path_query eq "/"
defined $scheme length $scheme length $host length $port $path_query eq "/"
Truth table33%2/6
A and B and C and D and E
A= defined $schemeB= length $schemeC= length $hostD= length $portE= $path_query eq "/"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
774 defined($scheme) && length($scheme) && length($host) && length($port)
775 && $path_query eq '/'
776 ) {
777 1 _croak(qq{$type URL must be in format http[s]://[auth@]<host>:<port>/\n});
778 }
779  
780 21 return ($scheme, $host, $port, $auth);
781 }
782  
783 sub _create_proxy_tunnel {
784 0 ▶ my ($self, $request, $handle) = @_;
Subroutine _create_proxy_tunnel: not called CC=5 SCAR=34.0
785  
786 0 $handle->_assert_ssl;
787  
788 my $agent = exists($request->{headers}{'user-agent'})
789 0 ▶ ? $request->{headers}{'user-agent'} : $self->{agent};
Branch: 1 branch
BranchTrueFalseTotal
exists $request->{'headers'}{'user-agent'} ? : 0 0 0
790  
791 0 my $connect_request = {
792 method => 'CONNECT',
793 uri => "$request->{host}:$request->{port}",
794 headers => {
795 host => "$request->{host}:$request->{port}",
796 'user-agent' => $agent,
797 }
798 };
799  
800 0 ▶ if ( $request->{headers}{'proxy-authorization'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"headers"}{"proxy-authorization"}) 0 0 0
801 $connect_request->{headers}{'proxy-authorization'} =
802 0 delete $request->{headers}{'proxy-authorization'};
803 }
804  
805 0 $handle->write_request($connect_request);
806 0 my $response;
807 0 do { $response = $handle->read_response_header }
808 0 ▶ until (substr($response->{status},0,1) ne '1');
Branch: 1 branch
BranchTrueFalseTotal
substr($response->{"status"}, 0, 1) ne 1 or do { do { $response = $handle->read_response_header } } 0 0 0
809  
810 # if CONNECT failed, throw the response so it will be
811 # returned from the original request() method;
812 0 ▶ unless (substr($response->{status},0,1) eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($response->{"status"}, 0, 1) eq 2) 0 0 0
813 0 die $response;
814 }
815  
816 # tunnel established, so start SSL handshake
817 0 $handle->start_ssl( $request->{host} );
818  
819 0 return;
820 }
821  
822 sub _prepare_headers_and_cb {
823 155 ▶ my ($self, $request, $args, $url, $auth) = @_;
Subroutine _prepare_headers_and_cb: called CC=29 SCAR=34.2
824  
825 155 for ($self->{default_headers}, $args->{headers}) {
826 310 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 286 24 310
827 24 ▶ while (my ($k, $v) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 31 24 55
828 31 $request->{headers}{lc $k} = $v;
829 31 $request->{header_case}{lc $k} = $k;
830 }
831 }
832  
833 155 ▶ if (exists $request->{headers}{'host'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"headers"}{"host"}) 1 154 155
834 1 die(qq/The 'Host' header must not be provided as header option\n/);
835 }
836  
837 154 $request->{headers}{'host'} = $request->{host_port};
838 154 ▶ $request->{headers}{'user-agent'} ||= $self->{agent};
Condition33%1/3
$request->{"headers"}{"user-agent"} ||= $self->{"agent"}
l!l&&r!l&&!r
01540
MC/DC0%0/2
$request->{"headers"}{"user-agent"} ||= $self->{"agent"}
$request->{"headers"}{"user-agent"} $self->{"agent"}
Truth table33%1/3
A ||= B
A= $request->{"headers"}{"user-agent"}B= $self->{"agent"}
ABresult
1X1
011
000
839 $request->{headers}{'connection'} = "close"
840 154 ▶ unless $self->{keep_alive};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_alive"} 110 44 154
841  
842 # Some servers error on an empty-body PUT/POST without a content-length
843 154 ▶ if ( $request->{method} eq 'PUT' || $request->{method} eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"method"} eq "PUT" or $request->{"method"} eq "POST") 42 112 154
Condition100%3/3
$request->{"method"} eq "PUT" or $request->{"method"} eq "POST"
l!l&&r!l&&!r
834112
MC/DC100%2/2
$request->{"method"} eq "PUT" or $request->{"method"} eq "POST"
$request->{"method"} eq "PUT" $request->{"method"} eq "POST"
Truth table100%3/3
A or B
A= $request->{"method"} eq "PUT"B= $request->{"method"} eq "POST"
ABresult
1X1
011
000
844 42 ▶ if (!defined($args->{content}) || !length($args->{content}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"content"} and length $args->{"content"}) 16 26 42
Condition100%3/3
defined $args->{"content"} and length $args->{"content"}
!ll&&!rl&&r
15126
MC/DC100%2/2
defined $args->{"content"} and length $args->{"content"}
defined $args->{"content"} length $args->{"content"}
Truth table100%3/3
A and B
A= defined $args->{"content"}B= length $args->{"content"}
ABresult
0X0
100
111
845 16 $request->{headers}{'content-length'} = 0;
846 }
847 }
848  
849 154 ▶ if ( defined $args->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"content"}) 27 127 154
850 27 ▶ if ( ref $args->{content} eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{'content'} eq 'CODE') { } 5 22 27
elsif (length $args->{'content'}) { } 21 1 22
851 5 ▶ if ( exists $request->{'content-length'} && $request->{'content-length'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{'content-length'} and $request->{'content-length'} == 0) { } 0 5 5
Condition33%1/3
exists $request->{"content-length"} and $request->{"content-length"} == 0
!ll&&!rl&&r
500
MC/DC0%0/2
exists $request->{"content-length"} and $request->{"content-length"} == 0
exists $request->{"content-length"} $request->{"content-length"} == 0
Truth table33%1/3
A and B
A= exists $request->{"content-length"}B= $request->{"content-length"} == 0
ABresult
0X0
100
111
852 0 ▶ $request->{cb} = sub { "" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
853 }
854 else {
855 5 ▶ $request->{headers}{'content-type'} ||= "application/octet-stream";
Condition50%1/2
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
l!l
50
MC/DC0%0/1
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
$request->{"headers"}{"content-type"}
Truth table50%1/2
A
A= $request->{"headers"}{"content-type"}
Aresult
11
00
856 $request->{headers}{'transfer-encoding'} = 'chunked'
857 unless exists $request->{headers}{'content-length'}
858 5 ▶ || $request->{headers}{'transfer-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"} 3 2 5
Condition66%2/3
exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
l!l&&r!l&&!r
203
MC/DC50%1/2
exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
exists $request->{"headers"}{"content-length"} $request->{"headers"}{"transfer-encoding"}
Truth table66%2/3
A or B
A= exists $request->{"headers"}{"content-length"}B= $request->{"headers"}{"transfer-encoding"}
ABresult
1X1
011
000
859 5 $request->{cb} = $args->{content};
860 }
861 }
862 elsif ( length $args->{content} ) {
863 21 my $content = $args->{content};
864 21 ▶ if ( $] ge '5.008' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge "5.008") 21 0 21
865 21 ▶ utf8::downgrade($content, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($content, 1) or die "Wide character in request message body\n" 0 21 21
866 or die(qq/Wide character in request message body\n/);
867 }
868 21 ▶ $request->{headers}{'content-type'} ||= "application/octet-stream";
Condition100%2/2
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
l!l
714
MC/DC100%1/1
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
$request->{"headers"}{"content-type"}
Truth table100%2/2
A
A= $request->{"headers"}{"content-type"}
Aresult
11
00
869 $request->{headers}{'content-length'} = length $content
870 unless $request->{headers}{'content-length'}
871 21 ▶ || $request->{headers}{'transfer-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"} 20 1 21
Condition66%2/3
$request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
l!l&&r!l&&!r
1020
MC/DC50%1/2
$request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
$request->{"headers"}{"content-length"} $request->{"headers"}{"transfer-encoding"}
Truth table66%2/3
A or B
A= $request->{"headers"}{"content-length"}B= $request->{"headers"}{"transfer-encoding"}
ABresult
1X1
011
000
872 21 ▶ $request->{cb} = sub { substr $content, 0, length $content, '' };
Subroutine __ANON__: called CC=1 SCAR=0.0
873 }
874 $request->{trailer_cb} = $args->{trailer_callback}
875 27 ▶ if ref $args->{trailer_callback} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"trailer_callback"} eq "CODE" 1 26 27
876 }
877  
878 ### If we have a cookie jar, then maybe add relevant cookies
879 154 ▶ if ( $self->{cookie_jar} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cookie_jar"}) 34 120 154
880 34 my $cookies = $self->cookie_jar->cookie_header( $url );
881 34 ▶ $request->{headers}{cookie} = $cookies if length $cookies;
Branch: 1 branch
BranchTrueFalseTotal
if length $cookies 22 12 34
882 }
883  
884 # if we have Basic auth parameters, add them
885 154 ▶ if ( length $auth && ! defined $request->{headers}{authorization} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $auth and not defined $request->{"headers"}{"authorization"}) 4 150 154
Condition100%3/3
length $auth and not defined $request->{"headers"}{"authorization"}
!ll&&!rl&&r
14914
MC/DC100%2/2
length $auth and not defined $request->{"headers"}{"authorization"}
length $auth not defined $request->{"headers"}{"authorization"}
Truth table100%3/3
A and not(B)
A= length $authB= not defined $request->{"headers"}{"authorization"}
ABresult
0X0
100
111
886 4 $self->_add_basic_auth_header( $request, 'authorization' => $auth );
887 }
888  
889 154 return;
890 }
891  
892 sub _add_basic_auth_header {
893 5 ▶ my ($self, $request, $header, $auth) = @_;
Subroutine _add_basic_auth_header: called CC=1 SCAR=0.0
894 5 require MIME::Base64;
895 5 $request->{headers}{$header} =
896 "Basic " . MIME::Base64::encode_base64($auth, "");
897 5 return;
898 }
899  
900 sub _prepare_data_cb {
901 149 ▶ my ($self, $response, $args) = @_;
Subroutine _prepare_data_cb: called CC=4 SCAR=13.9
902 149 my $data_cb = $args->{data_callback};
903 149 $response->{content} = '';
904  
905 149 ▶ if (!$data_cb || $response->{status} !~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_cb and $response->{"status"} =~ /^2/) 145 4 149
Condition100%3/3
$data_cb and $response->{"status"} =~ /^2/
!ll&&!rl&&r
14414
MC/DC100%2/2
$data_cb and $response->{"status"} =~ /^2/
$data_cb $response->{"status"} =~ /^2/
Truth table100%3/3
A and B
A= $data_cbB= $response->{"status"} =~ /^2/
ABresult
0X0
100
111
906 145 ▶ if (defined $self->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'max_size'}) { } 1 144 145
907 $data_cb = sub {
908 1 ▶ $_[1]->{content} .= $_[0];
Subroutine __ANON__: called CC=2 SCAR=7.2
909 die(qq/Size of response body exceeds the maximum allowed of $self->{max_size}\n/)
910 1 ▶ if length $_[1]->{content} > $self->{max_size};
Branch: 1 branch
BranchTrueFalseTotal
if length $_[1]{"content"} > $self->{"max_size"} 1 0 1
911 1 };
912 }
913 else {
914 144 ▶ $data_cb = sub { $_[1]->{content} .= $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
915 }
916 }
917 149 return $data_cb;
918 }
919  
920 sub _update_cookie_jar {
921 34 ▶ my ($self, $url, $response) = @_;
Subroutine _update_cookie_jar: called CC=4 SCAR=13.9
922  
923 34 my $cookies = $response->{headers}->{'set-cookie'};
924 34 ▶ return unless defined $cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookies 4 30 34
925  
926 30 ▶ my @cookies = ref $cookies ? @$cookies : $cookies;
Branch: 1 branch
BranchTrueFalseTotal
ref $cookies ? : 2 28 30
927  
928 30 $self->cookie_jar->add( $url, $_ ) for @cookies;
929  
930 30 return;
931 }
932  
933 sub _validate_cookie_jar {
934 15 ▶ my ($class, $jar) = @_;
Subroutine _validate_cookie_jar: called CC=4 SCAR=13.9
935  
936 # duck typing
937 15 for my $method ( qw/add cookie_header/ ) {
938 29 ▶ _croak(qq/Cookie jar must provide the '$method' method\n/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $jar and ref($jar)->can($method) 2 27 29
Condition66%2/3
ref $jar and ref($jar)->can($method)
!ll&&!rl&&r
0227
MC/DC50%1/2
ref $jar and ref($jar)->can($method)
ref $jar ref($jar)->can($method)
Truth table66%2/3
A and B
A= ref $jarB= ref($jar)->can($method)
ABresult
0X0
100
111
939 unless ref($jar) && ref($jar)->can($method);
940 }
941  
942 13 return;
943 }
944  
945 sub _maybe_redirect {
946 151 ▶ my ($self, $request, $response, $args) = @_;
Subroutine _maybe_redirect: called CC=9 SCAR=22.0
947 151 my $headers = $response->{headers};
948 151 my ($status, $method) = ($response->{status}, $request->{method});
949 151 ▶ $args->{_redirects} ||= [];
Condition100%2/2
$args->{"_redirects"} ||= []
l!l
13138
MC/DC100%1/1
$args->{"_redirects"} ||= []
$args->{"_redirects"}
Truth table100%2/2
A
A= $args->{"_redirects"}
Aresult
11
00
950  
951 151 ▶ if (($status eq '303' or ($status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/))
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"} and @{$args->{"_redirects"};} < $self->{"max_redirect"}) 13 138 151
Condition83%10/12
$status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"} and @{$args->{"_redirects"};} < $self->{"max_redirect"}
!ll&&!rl&&r
136213
$status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"}
!ll&&!rl&&r
136015
$status eq 303 or $status =~ /^30[1278]/ and $method =~ /^GET|HEAD$/
l!l&&r!l&&!r
223126
$status =~ /^30[1278]/ and $method =~ /^GET|HEAD$/
!ll&&!rl&&r
136013
MC/DC60%3/5
$status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"} and @{$args->{"_redirects"};} < $self->{"max_redirect"}
$status eq 303 $status =~ /^30[1278]/ $method =~ /^GET|HEAD$/ $headers->{"location"} @{$args->{"_redirects"};} < $self->{"max_redirect"}
Truth table50%4/8
A or B and C and D and E
A= $status eq 303B= $status =~ /^30[1278]/C= $method =~ /^GET|HEAD$/D= $headers->{"location"}E= @{$args->{"_redirects"};} < $self->{"max_redirect"}
ABCDEresult
00XXX0
010XX0
1XX0X0
0110X0
1XX100
011100
1XX111
011111
952 and $headers->{location}
953 15 and @{$args->{_redirects}} < $self->{max_redirect}
954 ) {
955 my $location = ($headers->{location} =~ /^\//)
956 ? "$request->{scheme}://$request->{host_port}$headers->{location}"
957 13 ▶ : $headers->{location} ;
Branch: 1 branch
BranchTrueFalseTotal
$headers->{'location'} =~ m[^/] ? : 1 12 13
958 13 ▶ return (($status eq '303' ? 'GET' : $method), $location);
Branch: 1 branch
BranchTrueFalseTotal
$status eq '303' ? : 2 11 13
959 }
960 138 return;
961 }
962  
963 sub _split_url {
964 206 ▶ my $url = pop;
Subroutine _split_url: called CC=9 SCAR=22.0
965  
966 # URI regex adapted from the URI module
967 206 ▶ my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)>
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#]*)] or die "Cannot parse URL: '${url}'\n" 1 205 206
968 or die(qq/Cannot parse URL: '$url'\n/);
969  
970 205 $scheme = lc $scheme;
971 205 ▶ $path_query = "/$path_query" unless $path_query =~ m<\A/>;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 46 159 205
972  
973 205 my $auth = '';
974 205 ▶ if ( (my $i = index $host, '@') != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($host, "\@")) != -1) 12 193 205
975 # user:pass@host
976 12 $auth = substr $host, 0, $i, ''; # take up to the @ for auth
977 12 substr $host, 0, 1, ''; # knock the @ off the host
978  
979 # userinfo might be percent escaped, so recover real auth info
980 12 $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
981 }
982 205 ▶ my $port = $host =~ s/:(\d*)\z// && length $1 ? $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:(\d*)\z// && length $1 ? : 21 184 205
$scheme eq 'http' ? : 151 33 184
$scheme eq 'https' ? : 31 2 33
Condition100%3/3
$host =~ s/:(\d*)\z// and length $1
!ll&&!rl&&r
180421
MC/DC100%2/2
$host =~ s/:(\d*)\z// and length $1
$host =~ s/:(\d*)\z// length $1
Truth table100%3/3
A and B
A= $host =~ s/:(\d*)\z//B= length $1
ABresult
0X0
100
111
983 : $scheme eq 'http' ? 80
984 : $scheme eq 'https' ? 443
985 : undef;
986  
987 205 ▶ return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth);
Branch: 1 branch
BranchTrueFalseTotal
length $host ? : 203 2 205
988 }
989  
990 # Date conversions adapted from HTTP::Date
991 30 my $DoW = "Sun|Mon|Tue|Wed|Thu|Fri|Sat";
992 30 my $MoY = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
993 sub _http_date {
994 3 ▶ my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($_[1]);
Subroutine _http_date: called CC=1 SCAR=0.0
995 3 return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
996 substr($DoW,$wday*4,3),
997 $mday, substr($MoY,$mon*4,3), $year+1900,
998 $hour, $min, $sec
999 );
1000 }
1001  
1002 sub _parse_http_date {
1003 9 ▶ my ($self, $str) = @_;
Subroutine _parse_http_date: called CC=6 SCAR=18.1
1004 9 require Time::Local;
1005 9 my @tl_parts;
1006 9 ▶ if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) { } 5 4 9
elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/) { } 2 2 4
elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/) { } 2 0 2
1007 5 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
1008 }
1009 elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/ ) {
1010 2 @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
1011 }
1012 elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/ ) {
1013 2 @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6);
1014 }
1015 9 return eval {
1016 9 ▶ my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1;
Branch: 1 branch
BranchTrueFalseTotal
@tl_parts ? : 9 0 9
1017 9 ▶ $t < 0 ? undef : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 0 9 9
1018 };
1019 }
1020  
1021 # URI escaping adapted from URI::Escape
1022 # c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
1023 # perl 5.6 ready UTF-8 encoding adapted from JSON::PP
1024 30 my %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255;
1025 30 $escapes{' '}="+";
1026 30 my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
1027  
1028 sub _uri_escape {
1029 26 ▶ my ($self, $str) = @_;
Subroutine _uri_escape: called CC=4 SCAR=15.2
1030 26 ▶ return "" if !defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 24 26
1031 24 ▶ if ( $] ge '5.008' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge '5.008') { } 24 0 24
1032 24 utf8::encode($str);
1033 }
1034 else {
1035 $str = pack("U*", unpack("C*", $str)) # UTF-8 encode a byte string
1036 30 ▶ if ( length $str == do { use bytes; length $str } );
Subroutine BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if length $str == do { use bytes; length $str } 0 0 0
1037 0 $str = pack("C*", unpack("C*", $str)); # clear UTF-8 flag
1038 }
1039 24 $str =~ s/($unsafe_char)/$escapes{$1}/g;
1040 24 return $str;
1041 }
1042  
1043 package
1044 HTTP::Tiny::Handle; # hide from PAUSE/indexers
1045 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1046 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1047  
1048 30 ▶use Errno qw[EINTR EPIPE];
Subroutine BEGIN: called CC=1 SCAR=0.0
1049 30 ▶use IO::Socket qw[SOCK_STREAM];
Subroutine BEGIN: called CC=1 SCAR=0.0
1050 30 ▶use Socket qw[SOL_SOCKET SO_KEEPALIVE];
Subroutine BEGIN: called CC=1 SCAR=0.0
1051  
1052 # PERL_HTTP_TINY_IPV4_ONLY is a private environment variable to force old
1053 # behavior if someone is unable to boostrap CPAN from a new perl install; it is
1054 # not intended for general, per-client use and may be removed in the future
1055 my $SOCKET_CLASS =
1056 $ENV{PERL_HTTP_TINY_IPV4_ONLY} ? 'IO::Socket::INET' :
1057 30 ▶ eval { require IO::Socket::IP; IO::Socket::IP->VERSION(0.32) } ? 'IO::Socket::IP' :
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'PERL_HTTP_TINY_IPV4_ONLY'} ? : 0 30 30
eval { do { require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.32) } } ? : 30 0 30
1058 'IO::Socket::INET';
1059  
1060 sub BUFSIZE () { 32768 } ## no critic
1061  
1062 my $Printable = sub {
1063 3 ▶ local $_ = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1064 3 s/\r/\\r/g;
1065 3 s/\n/\\n/g;
1066 3 s/\t/\\t/g;
1067 3 s/([^\x20-\x7E])/sprintf('\\x%.2X', ord($1))/ge;
1068 3 $_;
1069 30 };
1070  
1071 30 my $Token = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;
1072 30 my $Field_Content = qr/[[:print:]]+ (?: [\x20\x09]+ [[:print:]]+ )*/x;
1073  
1074 sub new {
1075 183 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
1076 183 return bless {
1077 rbuf => '',
1078 timeout => 60,
1079 max_line_size => 16384,
1080 max_header_lines => 64,
1081 verify_SSL => HTTP::Tiny::_verify_SSL_default(),
1082 SSL_options => {},
1083 %args
1084 }, $class;
1085 }
1086  
1087 sub timeout {
1088 2 ▶ my ($self, $timeout) = @_;
Subroutine timeout: called CC=4 SCAR=15.9
1089 2 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 0 2
1090 2 $self->{timeout} = $timeout;
1091 2 ▶ if ( $self->{fh} && $self->{fh}->can('timeout') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"fh"}->can("timeout")) 0 2 2
Condition33%1/3
$self->{"fh"} and $self->{"fh"}->can("timeout")
!ll&&!rl&&r
020
MC/DC0%0/2
$self->{"fh"} and $self->{"fh"}->can("timeout")
$self->{"fh"} $self->{"fh"}->can("timeout")
Truth table33%1/3
A and B
A= $self->{"fh"}B= $self->{"fh"}->can("timeout")
ABresult
0X0
100
111
1092 0 $self->{fh}->timeout($timeout);
1093 }
1094 }
1095 2 return $self->{timeout};
1096 }
1097  
1098 sub connect {
1099 32 ▶ @_ == 5 || die(q/Usage: $handle->connect(scheme, host, port, peer)/ . "\n");
Subroutine connect: called CC=9 SCAR=22.5
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 or die "Usage: \$handle->connect(scheme, host, port, peer)\n" 0 32 32
1100 32 my ($self, $scheme, $host, $port, $peer) = @_;
1101  
1102 32 ▶ if ( $scheme eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "https") 29 3 32
1103 29 $self->_assert_ssl;
1104 }
1105  
1106 $self->{fh} = $SOCKET_CLASS->new(
1107 PeerHost => $peer,
1108 PeerPort => $port,
1109 $self->{local_address} ?
1110 ( LocalAddr => $self->{local_address} ) : (),
1111 Proto => 'tcp',
1112 Type => SOCK_STREAM,
1113 Timeout => $self->{timeout},
1114 32 ▶ ) or die(qq/Could not connect to '$host:$port': $@\n/);
Branch: 2 branches
BranchTrueFalseTotal
$self->{"fh"} = $SOCKET_CLASS->new("PeerHost", $peer, "PeerPort", $port, $self->{"local_address"} ? ("LocalAddr", $self->{"local_address"}) : (), "Proto", "tcp", "Type", 1, "Timeout", $self->{"timeout"}) or die "Could not connect to '${host}:${port}': $@\n" 1 31 32
$self->{'local_address'} ? : 1 31 32
1115  
1116 binmode($self->{fh})
1117 31 ▶ or die(qq/Could not binmode() socket: '$!'\n/);
Branch: 1 branch
BranchTrueFalseTotal
binmode $self->{"fh"} or die "Could not binmode() socket: '$!'\n" 0 31 31
1118  
1119 31 ▶ if ( $self->{keep_alive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_alive"}) 31 0 31
1120 31 ▶ unless ( defined( $self->{fh}->setsockopt( SOL_SOCKET, SO_KEEPALIVE, 1 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}->setsockopt(1, 9, 1)) 0 31 31
1121 0 CORE::close($self->{fh});
1122 0 die(qq/Could not set SO_KEEPALIVE on socket: '$!'\n/);
1123 }
1124 }
1125  
1126 31 ▶ $self->start_ssl($host) if $scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "https" 29 2 31
1127  
1128 18 $self->{scheme} = $scheme;
1129 18 $self->{host} = $host;
1130 18 $self->{peer} = $peer;
1131 18 $self->{port} = $port;
1132 18 $self->{pid} = $$;
1133 18 $self->{tid} = _get_tid();
1134  
1135 18 return $self;
1136 }
1137  
1138 sub connected {
1139 18 ▶ my ($self) = @_;
Subroutine connected: called CC=4 SCAR=17.1
1140 18 ▶ if ( $self->{fh} && $self->{fh}->connected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"fh"}->connected) 18 0 18
Condition33%1/3
$self->{"fh"} and $self->{"fh"}->connected
!ll&&!rl&&r
0018
MC/DC0%0/2
$self->{"fh"} and $self->{"fh"}->connected
$self->{"fh"} $self->{"fh"}->connected
Truth table33%1/3
A and B
A= $self->{"fh"}B= $self->{"fh"}->connected
ABresult
0X0
100
111
1141 return wantarray
1142 ? ( $self->{fh}->peerhost, $self->{fh}->peerport )
1143 18 ▶ : join( ':', $self->{fh}->peerhost, $self->{fh}->peerport );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 18 18
1144 }
1145 0 return;
1146 }
1147  
1148 sub start_ssl {
1149 29 ▶ my ($self, $host) = @_;
Subroutine start_ssl: called CC=4 SCAR=15.2
1150  
1151 # As this might be used via CONNECT after an SSL session
1152 # to a proxy, we shut down any existing SSL before attempting
1153 # the handshake
1154 29 ▶ if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"fh"} eq "IO::Socket::SSL") 0 29 29
1155 0 ▶ unless ( $self->{fh}->stop_SSL ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}->stop_SSL) 0 0 0
1156 0 my $ssl_err = IO::Socket::SSL->errstr;
1157 0 die(qq/Error halting prior SSL connection: $ssl_err/);
1158 }
1159 }
1160  
1161 29 my $ssl_args = $self->_ssl_args($host);
1162 IO::Socket::SSL->start_SSL(
1163 $self->{fh},
1164 %$ssl_args,
1165 SSL_create_ctx_callback => sub {
1166 29 ▶ my $ctx = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1167 29 Net::SSLeay::CTX_set_mode($ctx, Net::SSLeay::MODE_AUTO_RETRY());
1168 },
1169 29 );
1170  
1171 29 ▶ unless ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"fh"} eq "IO::Socket::SSL") 13 16 29
1172 13 my $ssl_err = IO::Socket::SSL->errstr;
1173 13 die(qq/SSL connection failed for $host: $ssl_err\n/);
1174 }
1175 }
1176  
1177 sub close {
1178 2 ▶ @_ == 1 || die(q/Usage: $handle->close()/ . "\n");
Subroutine close: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->close()\n" 0 2 2
1179 2 my ($self) = @_;
1180 CORE::close($self->{fh})
1181 2 ▶ or die(qq/Could not close socket: '$!'\n/);
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $self->{"fh"} or die "Could not close socket: '$!'\n" 0 2 2
1182 }
1183  
1184 sub write {
1185 369 ▶ @_ == 2 || die(q/Usage: $handle->write(buf)/ . "\n");
Subroutine write: called CC=10 SCAR=29.4
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write(buf)\n" 0 369 369
1186 369 my ($self, $buf) = @_;
1187  
1188 369 ▶ if ( $] ge '5.008' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge "5.008") 369 0 369
1189 369 ▶ utf8::downgrade($buf, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($buf, 1) or die "Wide character in write()\n" 0 369 369
1190 or die(qq/Wide character in write()\n/);
1191 }
1192  
1193 369 my $len = length $buf;
1194 369 my $off = 0;
1195  
1196 369 local $SIG{PIPE} = 'IGNORE';
1197  
1198 369 while () {
1199 369 ▶ $self->can_write
Branch: 1 branch
BranchTrueFalseTotal
$self->can_write or die "Timed out while waiting for socket to become ready for writing\n" 0 369 369
1200 or die(qq/Timed out while waiting for socket to become ready for writing\n/);
1201 369 my $r = syswrite($self->{fh}, $buf, $len, $off);
1202 369 ▶ if (defined $r) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $r) { } 369 0 369
elsif ($! == 32) { } 0 0 0
elsif ($! != 4) { } 0 0 0
1203 369 $len -= $r;
1204 369 $off += $r;
1205 369 ▶ last unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 369 0 369
1206 }
1207 elsif ($! == EPIPE) {
1208 0 die(qq/Socket closed by remote server: $!\n/);
1209 }
1210 elsif ($! != EINTR) {
1211 0 ▶ if ($self->{fh}->can('errstr')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fh'}->can('errstr')) { } 0 0 0
1212 0 my $err = $self->{fh}->errstr();
1213 0 die (qq/Could not write to SSL socket: '$err'\n /);
1214 }
1215 else {
1216 0 die(qq/Could not write to socket: '$!'\n/);
1217 }
1218  
1219 }
1220 }
1221 369 return $off;
1222 }
1223  
1224 sub read {
1225 340 ▶ @_ == 2 || @_ == 3 || die(q/Usage: $handle->read(len [, allow_partial])/ . "\n");
Subroutine read: called CC=13 SCAR=29.0
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or die "Usage: \$handle->read(len [, allow_partial])\n" 0 340 340
Condition66%2/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
1032370
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1226 340 my ($self, $len, $allow_partial) = @_;
1227  
1228 340 my $buf = '';
1229 340 my $got = length $self->{rbuf};
1230  
1231 340 ▶ if ($got) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got) 285 55 340
1232 285 ▶ my $take = ($got < $len) ? $got : $len;
Branch: 1 branch
BranchTrueFalseTotal
$got < $len ? : 41 244 285
1233 285 $buf = substr($self->{rbuf}, 0, $take, '');
1234 285 $len -= $take;
1235 }
1236  
1237 # Ignore SIGPIPE because SSL reads can result in writes that might error.
1238 # See "Expecting exactly the same behavior as plain sockets" in
1239 # https://metacpan.org/dist/IO-Socket-SSL/view/lib/IO/Socket/SSL.pod#Common-Usage-Errors
1240 340 local $SIG{PIPE} = 'IGNORE';
1241  
1242 340 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 904 336 1240
1243 904 ▶ $self->can_read
Branch: 1 branch
BranchTrueFalseTotal
$self->can_read or die "Timed out while waiting for socket to become ready for reading\n" 0 904 904
1244 or die(q/Timed out while waiting for socket to become ready for reading/ . "\n");
1245 904 my $r = sysread($self->{fh}, $buf, $len, length $buf);
1246 904 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 904 0 904
elsif ($! != 4) { } 0 0 0
1247 904 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 4 900 904
1248 900 $len -= $r;
1249 }
1250 elsif ($! != EINTR) {
1251 0 ▶ if ($self->{fh}->can('errstr')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fh'}->can('errstr')) { } 0 0 0
1252 0 my $err = $self->{fh}->errstr();
1253 0 die (qq/Could not read from SSL socket: '$err'\n /);
1254 }
1255 else {
1256 0 die(qq/Could not read from socket: '$!'\n/);
1257 }
1258 }
1259 }
1260 340 ▶ if ($len && !$allow_partial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len and not $allow_partial) 0 340 340
Condition66%2/3
$len and not $allow_partial
!ll&&!rl&&r
33640
MC/DC0%0/2
$len and not $allow_partial
$len not $allow_partial
Truth table66%2/3
A and not(B)
A= $lenB= not $allow_partial
ABresult
0X0
100
111
1261 0 die(qq/Unexpected end of stream\n/);
1262 }
1263 340 return $buf;
1264 }
1265  
1266 sub readline {
1267 1162 ▶ @_ == 1 || die(q/Usage: $handle->readline()/ . "\n");
Subroutine readline: called CC=9 SCAR=28.5
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->readline()\n" 0 1162 1162
1268 1162 my ($self) = @_;
1269  
1270 1162 while () {
1271 1377 ▶ if ($self->{rbuf} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rbuf"} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) 1162 215 1377
1272 1162 return $1;
1273 }
1274 215 ▶ if (length $self->{rbuf} >= $self->{max_line_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"rbuf"} >= $self->{"max_line_size"}) 0 215 215
1275 0 die(qq/Line size exceeds the maximum allowed size of $self->{max_line_size}\n/);
1276 }
1277 $self->can_read
1278 215 ▶ or die(qq/Timed out while waiting for socket to become ready for reading\n/);
Branch: 1 branch
BranchTrueFalseTotal
$self->can_read or die "Timed out while waiting for socket to become ready for reading\n" 0 215 215
1279 215 my $r = sysread($self->{fh}, $self->{rbuf}, BUFSIZE, length $self->{rbuf});
1280 215 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 215 0 215
elsif ($! != 4) { } 0 0 0
1281 215 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 215 215
1282 }
1283 elsif ($! != EINTR) {
1284 0 ▶ if ($self->{fh}->can('errstr')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fh'}->can('errstr')) { } 0 0 0
1285 0 my $err = $self->{fh}->errstr();
1286 0 die (qq/Could not read from SSL socket: '$err'\n /);
1287 }
1288 else {
1289 0 die(qq/Could not read from socket: '$!'\n/);
1290 }
1291 }
1292 }
1293 0 die(qq/Unexpected end of stream while looking for line\n/);
1294 }
1295  
1296 sub read_header_lines {
1297 174 ▶ @_ == 1 || @_ == 2 || die(q/Usage: $handle->read_header_lines([headers])/ . "\n");
Subroutine read_header_lines: called CC=14 SCAR=26.8
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or die "Usage: \$handle->read_header_lines([headers])\n" 0 174 174
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
162120
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1298 174 my ($self, $headers) = @_;
1299 174 ▶ $headers ||= {};
Condition100%2/2
$headers ||= {}
l!l
12162
MC/DC100%1/1
$headers ||= {}
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
1300 174 my $lines = 0;
1301 174 my $val;
1302  
1303 174 while () {
1304 886 my $line = $self->readline;
1305  
1306 886 ▶ if (++$lines >= $self->{max_header_lines}) {
Branch: 4 branches
BranchTrueFalseTotal
if (++$lines >= $self->{'max_header_lines'}) { } 0 886 886
elsif ($line =~ /\A ([^\x00-\x1F\x7F:]+) : [\x09\x20]* ([^\x0D\x0A]*)/x) { } 709 177 886
elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) { } 3 174 177
elsif ($line =~ /\A \x0D?\x0A \z/x) { } 174 0 174
1307 0 die(qq/Header lines exceeds maximum number allowed of $self->{max_header_lines}\n/);
1308 }
1309 elsif ($line =~ /\A ([^\x00-\x1F\x7F:]+) : [\x09\x20]* ([^\x0D\x0A]*)/x) {
1310 709 my ($field_name) = lc $1;
1311 709 ▶ if (exists $headers->{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{$field_name}) { } 15 694 709
1312 15 for ($headers->{$field_name}) {
1313 15 ▶ $_ = [$_] unless ref $_ eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 11 4 15
1314 15 push @$_, $2;
1315 15 $val = \$_->[-1];
1316 }
1317 }
1318 else {
1319 694 $val = \($headers->{$field_name} = $2);
1320 }
1321 }
1322 elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) {
1323 3 ▶ $val
Branch: 1 branch
BranchTrueFalseTotal
$val or die "Unexpected header continuation line\n" 0 3 3
1324 or die(qq/Unexpected header continuation line\n/);
1325 3 ▶ next unless length $1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 1 2 3
1326 2 ▶ $$val .= ' ' if length $$val;
Branch: 1 branch
BranchTrueFalseTotal
if length $$val 1 1 2
1327 2 $$val .= $1;
1328 }
1329 elsif ($line =~ /\A \x0D?\x0A \z/x) {
1330 174 last;
1331 }
1332 else {
1333 0 die(q/Malformed header line: / . $Printable->($line) . "\n");
1334 }
1335 }
1336 174 return $headers;
1337 }
1338  
1339 sub write_request {
1340 154 ▶ @_ == 2 || die(q/Usage: $handle->write_request(request)/ . "\n");
Subroutine write_request: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_request(request)\n" 0 154 154
1341 154 my($self, $request) = @_;
1342 154 $self->write_request_header(@{$request}{qw/method uri headers header_case/});
1343 153 ▶ $self->write_body($request) if $request->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $request->{"cb"} 26 127 153
1344 153 return;
1345 }
1346  
1347 # Standard request header names/case from HTTP/1.1 RFCs
1348 30 my @rfc_request_headers = qw(
1349 Accept Accept-Charset Accept-Encoding Accept-Language Authorization
1350 Cache-Control Connection Content-Length Expect From Host
1351 If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since
1352 Max-Forwards Pragma Proxy-Authorization Range Referer TE Trailer
1353 Transfer-Encoding Upgrade User-Agent Via
1354 );
1355  
1356 30 my @other_request_headers = qw(
1357 Content-Encoding Content-MD5 Content-Type Cookie DNT Date Origin
1358 X-XSS-Protection
1359 );
1360  
1361 30 my %HeaderCase = map { lc($_) => $_ } @rfc_request_headers, @other_request_headers;
1362  
1363 # to avoid multiple small writes and hence nagle, you can pass the method line or anything else to
1364 # combine writes.
1365 sub write_header_lines {
1366 158 ▶ (@_ >= 2 && @_ <= 4 && ref $_[1] eq 'HASH') || die(q/Usage: $handle->write_header_lines(headers, [header_case, prefix])/ . "\n");
Subroutine write_header_lines: called CC=21 SCAR=31.1
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 4 and ref $_[1] eq "HASH" or die "Usage: \$handle->write_header_lines(headers, [header_case, prefix])\n" 0 158 158
Condition33%2/6
@_ >= 2 and @_ <= 4 and ref $_[1] eq "HASH"
!ll&&!rl&&r
00158
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
00158
MC/DC0%0/3
@_ >= 2 and @_ <= 4 and ref $_[1] eq "HASH"
@_ >= 2 @_ <= 4 ref $_[1] eq "HASH"
Truth table25%1/4
A and B and C
A= @_ >= 2B= @_ <= 4C= ref $_[1] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
1367 158 my($self, $headers, $header_case, $prefix_data) = @_;
1368 158 ▶ $header_case ||= {};
Condition100%2/2
$header_case ||= {}
l!l
23135
MC/DC100%1/1
$header_case ||= {}
$header_case
Truth table100%2/2
A
A= $header_case
Aresult
11
00
1369  
1370 158 ▶ my $buf = (defined $prefix_data ? $prefix_data : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix_data ? : 154 4 158
1371  
1372 # Per RFC, control fields should be listed first
1373 158 my %seen;
1374 158 for my $k ( qw/host cache-control expect max-forwards pragma range te/ ) {
1375 1106 ▶ next unless exists $headers->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $headers->{$k} 952 154 1106
1376 154 $seen{$k}++;
1377 154 my $field_name = $HeaderCase{$k};
1378 154 my $v = $headers->{$k};
1379 154 ▶ for (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 154 154
1380 154 ▶ $_ = '' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 154 154
1381 154 $buf .= "$field_name: $_\x0D\x0A";
1382 }
1383 }
1384  
1385 # Other headers sent in arbitrary order
1386 158 ▶ while (my ($k, $v) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 533 157 690
1387 533 my $field_name = lc $k;
1388 533 ▶ next if $seen{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$field_name} 153 380 533
1389 380 ▶ if (exists $HeaderCase{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HeaderCase{$field_name}) { } 369 11 380
1390 369 $field_name = $HeaderCase{$field_name};
1391 }
1392 else {
1393 11 ▶ if (exists $header_case->{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $header_case->{$field_name}) { } 5 6 11
1394 5 $field_name = $header_case->{$field_name};
1395 }
1396 else {
1397 6 $field_name =~ s/\b(\w)/\u$1/g;
1398 }
1399 11 ▶ $field_name =~ /\A $Token+ \z/xo
Branch: 1 branch
BranchTrueFalseTotal
$field_name =~ /\A $Token+ \z/ox or die 'Invalid HTTP header field name: ' . &$Printable($field_name) . "\n" 1 10 11
1400 or die(q/Invalid HTTP header field name: / . $Printable->($field_name) . "\n");
1401 10 $HeaderCase{lc $field_name} = $field_name;
1402 }
1403 379 ▶ for (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 3 376 379
1404 # unwrap a field value if pre-wrapped by user
1405 382 s/\x0D?\x0A\s+/ /g;
1406 382 ▶ die(qq/Invalid HTTP header field value ($field_name): / . $Printable->($_). "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "" or /\A $Field_Content \z/ox 0 382 382
Condition66%2/3
$_ eq "" or /\A $Field_Content \z/ox
l!l&&r!l&&!r
13810
MC/DC0%0/2
$_ eq "" or /\A $Field_Content \z/ox
$_ eq "" /\A $Field_Content \z/ox
Truth table66%2/3
A or B
A= $_ eq ""B= /\A $Field_Content \z/ox
ABresult
1X1
011
000
1407 unless $_ eq '' || /\A $Field_Content \z/xo;
1408 382 ▶ $_ = '' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 382 382
1409 382 $buf .= "$field_name: $_\x0D\x0A";
1410 }
1411 }
1412 157 $buf .= "\x0D\x0A";
1413 157 return $self->write($buf);
1414 }
1415  
1416 # return value indicates whether message length was defined; this is generally
1417 # true unless there was no content-length header and we just read until EOF.
1418 # Other message length errors are thrown as exceptions
1419 sub read_body {
1420 149 ▶ @_ == 3 || die(q/Usage: $handle->read_body(callback, response)/ . "\n");
Subroutine read_body: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or die "Usage: \$handle->read_body(callback, response)\n" 0 149 149
1421 149 my ($self, $cb, $response) = @_;
1422 149 ▶ my $te = $response->{headers}{'transfer-encoding'} || '';
Condition100%2/2
$response->{'headers'}{'transfer-encoding'} || ''
l!l
10139
MC/DC100%1/1
$response->{'headers'}{'transfer-encoding'} || ''
$response->{'headers'}{'transfer-encoding'}
Truth table100%2/2
A
A= $response->{'headers'}{'transfer-encoding'}
Aresult
11
00
1423 149 ▶ my $chunked = grep { /chunked/i } ( ref $te eq 'ARRAY' ? @$te : $te ) ;
Branch: 1 branch
BranchTrueFalseTotal
ref $te eq 'ARRAY' ? : 1 148 149
1424 149 ▶ return $chunked
Branch: 1 branch
BranchTrueFalseTotal
$chunked ? : 10 139 149
1425 ? $self->read_chunked_body($cb, $response)
1426 : $self->read_content_body($cb, $response);
1427 }
1428  
1429 sub write_body {
1430 26 ▶ @_ == 2 || die(q/Usage: $handle->write_body(request)/ . "\n");
Subroutine write_body: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_body(request)\n" 0 26 26
1431 26 my ($self, $request) = @_;
1432 26 ▶ if (exists $request->{headers}{'content-length'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{'headers'}{'content-length'}) { } 23 3 26
1433 23 ▶ return unless $request->{headers}{'content-length'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"headers"}{"content-length"} 1 22 23
1434 22 return $self->write_content_body($request);
1435 }
1436 else {
1437 3 return $self->write_chunked_body($request);
1438 }
1439 }
1440  
1441 sub read_content_body {
1442 243 ▶ @_ == 3 || @_ == 4 || die(q/Usage: $handle->read_content_body(callback, response, [read_length])/ . "\n");
Subroutine read_content_body: called CC=8 SCAR=20.8
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 or die "Usage: \$handle->read_content_body(callback, response, [read_length])\n" 0 243 243
Condition66%2/3
@_ == 3 or @_ == 4
l!l&&r!l&&!r
1391040
MC/DC0%0/2
@_ == 3 or @_ == 4
@_ == 3 @_ == 4
Truth table66%2/3
A or B
A= @_ == 3B= @_ == 4
ABresult
1X1
011
000
1443 243 my ($self, $cb, $response, $content_length) = @_;
1444 243 ▶ $content_length ||= $response->{headers}{'content-length'};
Condition100%3/3
$content_length ||= $response->{"headers"}{"content-length"}
l!l&&r!l&&!r
10411821
MC/DC100%2/2
$content_length ||= $response->{"headers"}{"content-length"}
$content_length $response->{"headers"}{"content-length"}
Truth table100%3/3
A ||= B
A= $content_lengthB= $response->{"headers"}{"content-length"}
ABresult
1X1
011
000
1445  
1446 243 ▶ if ( defined $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_length) 241 2 243
1447 241 my $len = $content_length;
1448 241 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 233 240 473
1449 233 ▶ my $read = ($len > BUFSIZE) ? BUFSIZE : $len;
Branch: 1 branch
BranchTrueFalseTotal
$len > 32768 ? : 11 222 233
1450 233 $cb->($self->read($read, 0), $response);
1451 232 $len -= $read;
1452 }
1453 240 return length($self->{rbuf}) == 0;
1454 }
1455  
1456 2 my $chunk;
1457 2 ▶ $cb->($chunk, $response) while length( $chunk = $self->read(BUFSIZE, 1) );
Branch: 1 branch
BranchTrueFalseTotal
length($chunk = $self->read(32768, 1)) and do { &$cb($chunk, $response) } 2 2 4
1458  
1459 2 return;
1460 }
1461  
1462 sub write_content_body {
1463 23 ▶ @_ == 2 || die(q/Usage: $handle->write_content_body(request)/ . "\n");
Subroutine write_content_body: called CC=7 SCAR=19.8
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_content_body(request)\n" 0 23 23
1464 23 my ($self, $request) = @_;
1465  
1466 23 my ($len, $content_length) = (0, $request->{headers}{'content-length'});
1467 23 while () {
1468 173 my $data = $request->{cb}->();
1469  
1470 173 ▶ defined $data && length $data
Branch: 1 branch
BranchTrueFalseTotal
defined $data and length $data or last 23 150 173
Condition100%3/3
defined $data and length $data
!ll&&!rl&&r
221150
MC/DC100%2/2
defined $data and length $data
defined $data length $data
Truth table100%3/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
1471 or last;
1472  
1473 150 ▶ if ( $] ge '5.008' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge "5.008") 150 0 150
1474 150 ▶ utf8::downgrade($data, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($data, 1) or die "Wide character in write_content()\n" 0 150 150
1475 or die(qq/Wide character in write_content()\n/);
1476 }
1477  
1478 150 $len += $self->write($data);
1479 }
1480  
1481 23 ▶ $len == $content_length
Branch: 1 branch
BranchTrueFalseTotal
$len == $content_length or die "Content-Length mismatch (got: $len expected: $content_length)\n" 0 23 23
1482 or die(qq/Content-Length mismatch (got: $len expected: $content_length)\n/);
1483  
1484 23 return $len;
1485 }
1486  
1487 sub read_chunked_body {
1488 12 ▶ @_ == 3 || die(q/Usage: $handle->read_chunked_body(callback, $response)/ . "\n");
Subroutine read_chunked_body: called CC=5 SCAR=16.3
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or die "Usage: \$handle->read_chunked_body(callback, \$response)\n" 0 12 12
1489 12 my ($self, $cb, $response) = @_;
1490  
1491 12 while () {
1492 115 my $head = $self->readline;
1493  
1494 115 ▶ $head =~ /\A ([A-Fa-f0-9]+)/x
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /\A ([A-Fa-f0-9]+)/x or die 'Malformed chunk head: ' . &$Printable($head) . "\n" 0 115 115
1495 or die(q/Malformed chunk head: / . $Printable->($head) . "\n");
1496  
1497 115 ▶ my $len = hex($1)
Branch: 1 branch
BranchTrueFalseTotal
my $len = hex $1 or last 12 103 115
1498 or last;
1499  
1500 103 $self->read_content_body($cb, $response, $len);
1501  
1502 103 ▶ $self->read(2) eq "\x0D\x0A"
Branch: 1 branch
BranchTrueFalseTotal
$self->read(2) eq "\r\n" or die "Malformed chunk: missing CRLF after chunk data\n" 0 103 103
1503 or die(qq/Malformed chunk: missing CRLF after chunk data\n/);
1504 }
1505 12 $self->read_header_lines($response->{headers});
1506 12 return 1;
1507 }
1508  
1509 sub write_chunked_body {
1510 5 ▶ @_ == 2 || die(q/Usage: $handle->write_chunked_body(request)/ . "\n");
Subroutine write_chunked_body: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_chunked_body(request)\n" 0 5 5
1511 5 my ($self, $request) = @_;
1512  
1513 5 my $len = 0;
1514 5 while () {
1515 59 my $data = $request->{cb}->();
1516  
1517 59 ▶ defined $data && length $data
Branch: 1 branch
BranchTrueFalseTotal
defined $data and length $data or last 5 54 59
Condition100%3/3
defined $data and length $data
!ll&&!rl&&r
4154
MC/DC100%2/2
defined $data and length $data
defined $data length $data
Truth table100%3/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
1518 or last;
1519  
1520 54 ▶ if ( $] ge '5.008' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] ge "5.008") 54 0 54
1521 54 ▶ utf8::downgrade($data, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($data, 1) or die "Wide character in write_chunked_body()\n" 0 54 54
1522 or die(qq/Wide character in write_chunked_body()\n/);
1523 }
1524  
1525 54 $len += length $data;
1526  
1527 54 my $chunk = sprintf '%X', length $data;
1528 54 $chunk .= "\x0D\x0A";
1529 54 $chunk .= $data;
1530 54 $chunk .= "\x0D\x0A";
1531  
1532 54 $self->write($chunk);
1533 }
1534 5 $self->write("0\x0D\x0A");
1535 5 ▶ if ( ref $request->{trailer_cb} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request->{'trailer_cb'} eq 'CODE') { } 2 3 5
1536 2 $self->write_header_lines($request->{trailer_cb}->())
1537 }
1538 else {
1539 3 $self->write("\x0D\x0A");
1540 }
1541 5 return $len;
1542 }
1543  
1544 sub read_response_header {
1545 161 ▶ @_ == 1 || die(q/Usage: $handle->read_response_header()/ . "\n");
Subroutine read_response_header: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->read_response_header()\n" 0 161 161
1546 161 my ($self) = @_;
1547  
1548 161 my $line = $self->readline;
1549  
1550 161 ▶ $line =~ /\A (HTTP\/(0*\d+\.0*\d+)) [\x09\x20]+ ([0-9]{3}) (?: [\x09\x20]+ ([^\x0D\x0A]*) )? \x0D?\x0A/x
Branch: 1 branch
BranchTrueFalseTotal
$line =~ m[\A (HTTP/(0*\d+\.0*\d+)) [\x09\x20]+ ([0-9]{3}) (?: [\x09\x20]+ ([^\x0D\x0A]*) )? \x0D?\x0A]x or die 'Malformed Status-Line: ' . &$Printable($line) . "\n" 2 159 161
1551 or die(q/Malformed Status-Line: / . $Printable->($line). "\n");
1552  
1553 159 my ($protocol, $version, $status, $reason) = ($1, $2, $3, $4);
1554 159 ▶ $reason = "" unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 2 157 159
1555  
1556 159 ▶ die (qq/Unsupported HTTP protocol: $protocol\n/)
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /0*1\.0*[01]/ 2 157 159
1557 unless $version =~ /0*1\.0*[01]/;
1558  
1559 return {
1560 157 status => $status,
1561 reason => $reason,
1562 headers => $self->read_header_lines,
1563 protocol => $protocol,
1564 };
1565 }
1566  
1567 sub write_request_header {
1568 154 ▶ @_ == 5 || die(q/Usage: $handle->write_request_header(method, request_uri, headers, header_case)/ . "\n");
Subroutine write_request_header: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 5 or die "Usage: \$handle->write_request_header(method, request_uri, headers, header_case)\n" 0 154 154
1569 154 my ($self, $method, $request_uri, $headers, $header_case) = @_;
1570  
1571 154 return $self->write_header_lines($headers, $header_case, "$method $request_uri HTTP/1.1\x0D\x0A");
1572 }
1573  
1574 sub _do_timeout {
1575 930 ▶ my ($self, $type, $timeout) = @_;
Subroutine _do_timeout: called CC=10 SCAR=29.4
1576 $timeout = $self->{timeout}
1577 930 ▶ unless defined $timeout && $timeout >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout and $timeout >= 0 930 0 930
Condition33%1/3
defined $timeout and $timeout >= 0
!ll&&!rl&&r
93000
MC/DC0%0/2
defined $timeout and $timeout >= 0
defined $timeout $timeout >= 0
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout >= 0
ABresult
0X0
100
111
1578  
1579 930 my $fd = fileno $self->{fh};
1580 930 ▶ defined $fd && $fd >= 0
Branch: 1 branch
BranchTrueFalseTotal
defined $fd and $fd >= 0 or die "select(2): 'Bad file descriptor'\n" 0 930 930
Condition33%1/3
defined $fd and $fd >= 0
!ll&&!rl&&r
00930
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table33%1/3
A and B
A= defined $fdB= $fd >= 0
ABresult
0X0
100
111
1581 or die(qq/select(2): 'Bad file descriptor'\n/);
1582  
1583 930 my $initial = time;
1584 930 my $pending = $timeout;
1585 930 my $nfound;
1586  
1587 930 vec(my $fdset = '', $fd, 1) = 1;
1588  
1589 930 while () {
1590 930 ▶ $nfound = ($type eq 'read')
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'read' ? : 912 18 930
1591 ? select($fdset, undef, undef, $pending)
1592 : select(undef, $fdset, undef, $pending) ;
1593 930 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) 0 930 930
1594 0 ▶ $! == EINTR
Branch: 1 branch
BranchTrueFalseTotal
$! == 4 or die "select(2): '$!'\n" 0 0 0
1595 or die(qq/select(2): '$!'\n/);
1596 0 ▶ redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $timeout or ($pending = $timeout - (time - $initial)) > 0 0 0 0
Condition0%0/3
not $timeout or ($pending = $timeout - (time - $initial)) > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not $timeout or ($pending = $timeout - (time - $initial)) > 0
not $timeout ($pending = $timeout - (time - $initial)) > 0
Truth table0%0/3
not(A) or B
A= not $timeoutB= ($pending = $timeout - (time - $initial)) > 0
ABresult
1X1
011
000
1597 0 $nfound = 0;
1598 }
1599 930 last;
1600 }
1601 930 $! = 0;
1602 930 return $nfound;
1603 }
1604  
1605 sub can_read {
1606 963 ▶ @_ == 1 || @_ == 2 || die(q/Usage: $handle->can_read([timeout])/ . "\n");
Subroutine can_read: called CC=5 SCAR=16.6
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or die "Usage: \$handle->can_read([timeout])\n" 0 963 963
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
96300
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1607 963 my $self = shift;
1608 963 ▶ if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"fh"} eq "IO::Socket::SSL") 933 30 963
1609 933 ▶ return 1 if $self->{fh}->pending;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"}->pending 51 882 933
1610 }
1611 912 return $self->_do_timeout('read', @_)
1612 }
1613  
1614 sub can_write {
1615 18 ▶ @_ == 1 || @_ == 2 || die(q/Usage: $handle->can_write([timeout])/ . "\n");
Subroutine can_write: called CC=3 SCAR=12.5
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or die "Usage: \$handle->can_write([timeout])\n" 0 18 18
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
1800
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1616 18 my $self = shift;
1617 18 return $self->_do_timeout('write', @_)
1618 }
1619  
1620 sub _assert_ssl {
1621 29 ▶ my($ok, $reason) = HTTP::Tiny->can_ssl();
Subroutine _assert_ssl: called CC=2 SCAR=7.2
1622 29 ▶ die $reason unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 29 29
1623 }
1624  
1625 sub can_reuse {
1626 13 ▶ my ($self,$scheme,$host,$port,$peer) = @_;
Subroutine can_reuse: called CC=10 SCAR=24.2
1627 return 0 if
1628 $self->{pid} != $$
1629 || $self->{tid} != _get_tid()
1630 || length($self->{rbuf})
1631 || $scheme ne $self->{scheme}
1632 || $host ne $self->{host}
1633 || $port ne $self->{port}
1634 || $peer ne $self->{peer}
1635 13 ▶ || eval { $self->can_read(0) }
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"} or $peer ne $self->{"peer"} or eval { do { $self->can_read(0) } } or $@ 7 3 10
Condition70%17/24
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"} or $peer ne $self->{"peer"} or eval { do { $self->can_read(0) } } or $@
l!l&&r!l&&!r
1003
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"} or $peer ne $self->{"peer"} or eval { do { $self->can_read(0) } }
l!l&&r!l&&!r
373
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"} or $peer ne $self->{"peer"}
l!l&&r!l&&!r
3010
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"}
l!l&&r!l&&!r
2110
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"}
l!l&&r!l&&!r
1111
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"}
l!l&&r!l&&!r
0112
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"}
l!l&&r!l&&!r
0013
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid()
l!l&&r!l&&!r
0013
MC/DC44%4/9
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"} or $host ne $self->{"host"} or $port ne $self->{"port"} or $peer ne $self->{"peer"} or eval { do { $self->can_read(0) } } or $@
$self->{"pid"} != $$ $self->{"tid"} != _get_tid() length $self->{"rbuf"} $scheme ne $self->{"scheme"} $host ne $self->{"host"} $port ne $self->{"port"} $peer ne $self->{"peer"} eval { do { $self->can_read(0) } } $@
Truth table50%5/10
A or B or C or D or E or F or G or H or I
A= $self->{"pid"} != $$B= $self->{"tid"} != _get_tid()C= length $self->{"rbuf"}D= $scheme ne $self->{"scheme"}E= $host ne $self->{"host"}F= $port ne $self->{"port"}G= $peer ne $self->{"peer"}H= eval { do { $self->can_read(0) } }I= $@
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
1636 || $@ ;
1637 3 return 1;
1638 }
1639  
1640 # Try to find a CA bundle to validate the SSL cert,
1641 # prefer Mozilla::CA or fallback to a system file
1642 sub _find_CA_file {
1643 25 ▶ my $self = shift();
Subroutine _find_CA_file: called CC=8 SCAR=21.7
1644  
1645 my $ca_file =
1646 defined( $self->{SSL_options}->{SSL_ca_file} )
1647 ? $self->{SSL_options}->{SSL_ca_file}
1648 25 ▶ : $ENV{SSL_CERT_FILE};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'SSL_options'}{'SSL_ca_file'} ? : 5 20 25
1649  
1650 25 ▶ if ( defined $ca_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ca_file) 5 20 25
1651 5 ▶ unless ( -r $ca_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $ca_file) 1 4 5
1652 1 die qq/SSL_ca_file '$ca_file' not found or not readable\n/;
1653 }
1654 4 return $ca_file;
1655 }
1656  
1657 20 local @INC = @INC;
1658 20 ▶ pop @INC if $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 18 2 20
1659 return Mozilla::CA::SSL_ca_file()
1660 20 ▶ if eval { require Mozilla::CA; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Mozilla::CA; 1 } } 20 0 20
1661  
1662 # cert list copied from golang src/crypto/x509/root_unix.go
1663 0 foreach my $ca_bundle (
1664 "/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Gentoo etc.
1665 "/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL
1666 "/etc/ssl/ca-bundle.pem", # OpenSUSE
1667 "/etc/openssl/certs/ca-certificates.crt", # NetBSD
1668 "/etc/ssl/cert.pem", # OpenBSD
1669 "/usr/local/share/certs/ca-root-nss.crt", # FreeBSD/DragonFly
1670 "/etc/pki/tls/cacert.pem", # OpenELEC
1671 "/etc/certs/ca-certificates.crt", # Solaris 11.2+
1672 ) {
1673 0 ▶ return $ca_bundle if -e $ca_bundle;
Branch: 1 branch
BranchTrueFalseTotal
if -e $ca_bundle 0 0 0
1674 }
1675  
1676 0 die qq/Couldn't find a CA bundle with which to verify the SSL certificate.\n/
1677 . qq/Try installing Mozilla::CA from CPAN\n/;
1678 }
1679  
1680 # for thread safety, we need to know thread id if threads are loaded
1681 sub _get_tid {
1682 30 ▶ no warnings 'reserved'; # for 'threads'
Subroutine BEGIN: called CC=1 SCAR=0.0
1683 165 ▶ return threads->can("tid") ? threads->tid : 0;
Subroutine _get_tid: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
'threads'->can('tid') ? : 0 165 165
1684 }
1685  
1686 sub _ssl_args {
1687 29 ▶ my ($self, $host) = @_;
Subroutine _ssl_args: called CC=5 SCAR=16.1
1688  
1689 29 my %ssl_args;
1690  
1691 # This test reimplements IO::Socket::SSL::can_client_sni(), which wasn't
1692 # added until IO::Socket::SSL 1.84
1693 29 ▶ if ( Net::SSLeay::OPENSSL_VERSION_NUMBER() >= 0x01000000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::SSLeay::OPENSSL_VERSION_NUMBER() >= 16777216) 29 0 29
1694 29 $ssl_args{SSL_hostname} = $host, # Sane SNI support
1695 }
1696  
1697 29 ▶ if ($self->{verify_SSL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'verify_SSL'}) { } 22 7 29
1698 22 $ssl_args{SSL_verifycn_scheme} = 'http'; # enable CN validation
1699 22 $ssl_args{SSL_verifycn_name} = $host; # set validation hostname
1700 22 $ssl_args{SSL_verify_mode} = 0x01; # enable cert validation
1701 22 $ssl_args{SSL_ca_file} = $self->_find_CA_file;
1702 }
1703 else {
1704 7 $ssl_args{SSL_verifycn_scheme} = 'none'; # disable CN validation
1705 7 $ssl_args{SSL_verify_mode} = 0x00; # disable cert validation
1706 }
1707  
1708 # user options override settings from verify_SSL
1709 29 for my $k ( keys %{$self->{SSL_options}} ) {
1710 10 ▶ $ssl_args{$k} = $self->{SSL_options}{$k} if $k =~ m/^SSL_/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^SSL_/ 10 0 10
1711 }
1712  
1713 29 return \%ssl_args;
1714 }
1715  
1716 30 1;
1717  
1718 __END__
↑ Summary