blib/lib/HTTP/Tiny.pm

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