/usr/local/lib/perl5/5.44.0/HTTP/Tiny.pm

filter: Statementstmt 9.2% 69 / 746 Branchbran 0.3% 2 / 514 Conditioncond 0.0% 0 / 340 MC/DCmcdc 0.0% 0 / 185 Subroutinesub 15.1% 13 / 86 Podpod 86.6% 13 / 15 totaltotal 5.1% 97 / 1886 CC 388 SCAR 52.9
CC
388
Cov
4%
_prepare_headers_and_cb
992.0
write_header_lines
552.0
_request
506.0
CRAP 198.3
« /usr/local/lib/perl5/5.44.0/base.pm ↑ Summary /usr/local/lib/perl5/5.44.0/x86_64-linux/Compress/Raw/Zlib.pm »
1 # vim: ts=4 sts=4 sw=4 et:
2 package HTTP::Tiny;
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: A small, simple, correct HTTP/1.1 client
6  
7 5 our $VERSION = '0.096';
8  
9 0 ▶sub _croak { require Carp; Carp::croak(@_) }
Subroutine _croak: not called CC=1 SCAR=6.9
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 5 my @attributes;
92 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.7
93 5 @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 5 my %persist_ok = map {; $_ => 1 } qw(
99 cookie_jar default_headers max_redirect max_size
100 );
101 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 5 for my $accessor ( @attributes ) {
104 70 ▶ *{$accessor} = sub {
Subroutine __ANON__: not called CC=4 SCAR=27.7
105 @_ > 1
106 ? do {
107 0 ▶ 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 0 0
Condition0%0/3
not $persist_ok{$accessor} and $_[1] ne $_[0]{$accessor}
!ll&&!rl&&r
000
MC/DC0%0/2
not $persist_ok{$accessor} and $_[1] ne $_[0]{$accessor}
not $persist_ok{$accessor} $_[1] ne $_[0]{$accessor}
Truth table0%0/3
not(A) and B
A= not $persist_ok{$accessor}B= $_[1] ne $_[0]{$accessor}
ABresult
0X0
100
111
108 0 $_[0]->{$accessor} = $_[1]
109 }
110 0 ▶ : $_[0]->{$accessor};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
111 70 };
112 }
113 }
114  
115 0 ▶sub agent {
Subroutine agent: not called CC=4 SCAR=30.0
Pod undocumented
116 0 my($self, $agent) = @_;
117 0 ▶ if( @_ > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
118 $self->{agent} =
119 0 ▶ (defined $agent && $agent =~ / $/) ? $agent . $self->_agent : $agent;
Branch: 1 branch
BranchTrueFalseTotal
defined $agent && $agent =~ / $/ ? : 0 0 0
Condition0%0/3
defined $agent and $agent =~ / $/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $agent and $agent =~ / $/
defined $agent $agent =~ / $/
Truth table0%0/3
A and B
A= defined $agentB= $agent =~ / $/
ABresult
0X0
100
111
120 }
121 0 return $self->{agent};
122 }
123  
124 0 ▶sub timeout {
Subroutine timeout: not called CC=3 SCAR=24.8
Pod undocumented
125 0 my ($self, $timeout) = @_;
126 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
127 0 $self->{timeout} = $timeout;
128 0 ▶ if ($self->{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle"}) 0 0 0
129 0 $self->{handle}->timeout($timeout);
130 }
131 }
132 0 return $self->{timeout};
133 }
134  
135 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
136 0 my($class, %args) = @_;
137  
138 # Support lower case verify_ssl argument, but only if verify_SSL is not
139 # true.
140 0 ▶ if ( exists $args{verify_ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"verify_ssl"}) 0 0 0
141 0 ▶ $args{verify_SSL} ||= $args{verify_ssl};
Condition0%0/3
$args{"verify_SSL"} ||= $args{"verify_ssl"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"verify_SSL"} ||= $args{"verify_ssl"}
$args{"verify_SSL"} $args{"verify_ssl"}
Truth table0%0/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 0 ▶ };
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'timeout'} ? : 0 0 0
defined $args{'verify_SSL'} ? : 0 0 0
152  
153 0 bless $self, $class;
154  
155 0 ▶ $class->_validate_cookie_jar( $args{cookie_jar} ) if $args{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"cookie_jar"} 0 0 0
156  
157 0 for my $key ( @attributes ) {
158 0 ▶ $self->{$key} = $args{$key} if exists $args{$key}
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} 0 0 0
159 }
160  
161 0 ▶ $self->agent( exists $args{agent} ? $args{agent} : $class->_agent );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'agent'} ? : 0 0 0
162  
163 0 $self->_set_proxies;
164  
165 0 return $self;
166 }
167  
168 0 ▶sub _verify_SSL_default {
Subroutine _verify_SSL_default: not called CC=3 SCAR=24.8
169 0 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 0 ▶ 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' ? : 0 0 0
Condition0%0/2
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'} || ''
l!l
00
MC/DC0%0/1
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'} || ''
$ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'}
Truth table0%0/2
A
A= $ENV{'PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT'}
Aresult
11
00
173 }
174  
175 0 ▶sub _set_proxies {
Subroutine _set_proxies: not called CC=16 SCAR=56.1
176 0 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 0 ▶ if (! exists $self->{proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"proxy"}) 0 0 0
183 0 ▶ $self->{proxy} = $ENV{all_proxy} || $ENV{ALL_PROXY};
Condition0%0/3
$ENV{'all_proxy'} || $ENV{'ALL_PROXY'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'all_proxy'} || $ENV{'ALL_PROXY'}
$ENV{'all_proxy'} $ENV{'ALL_PROXY'}
Truth table0%0/3
A || B
A= $ENV{'all_proxy'}B= $ENV{'ALL_PROXY'}
ABresult
1X1
011
000
184 }
185  
186 0 ▶ if ( defined $self->{proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'proxy'}) { } 0 0 0
187 0 $self->_split_proxy( 'generic proxy' => $self->{proxy} ); # validate
188 }
189 else {
190 0 delete $self->{proxy};
191 }
192  
193 # http proxy
194 0 ▶ if (! exists $self->{http_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"http_proxy"}) 0 0 0
195 # under CGI, bypass HTTP_PROXY as request sets it from Proxy header
196 0 ▶ local $ENV{HTTP_PROXY} = ($ENV{CGI_HTTP_PROXY} || "") if $ENV{REQUEST_METHOD};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_METHOD"} 0 0 0
Condition0%0/2
$ENV{'CGI_HTTP_PROXY'} || ''
l!l
00
MC/DC0%0/1
$ENV{'CGI_HTTP_PROXY'} || ''
$ENV{'CGI_HTTP_PROXY'}
Truth table0%0/2
A
A= $ENV{'CGI_HTTP_PROXY'}
Aresult
11
00
197 0 ▶ $self->{http_proxy} = $ENV{http_proxy} || $ENV{HTTP_PROXY} || $self->{proxy};
Condition0%0/6
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
l!l&&r!l&&!r
000
$ENV{"http_proxy"} or $ENV{"HTTP_PROXY"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'http_proxy'} || $ENV{'HTTP_PROXY'} || $self->{'proxy'}
$ENV{"http_proxy"} $ENV{"HTTP_PROXY"} $self->{'proxy'}
Truth table0%0/4
A or B || C
A= $ENV{"http_proxy"}B= $ENV{"HTTP_PROXY"}C= $self->{'proxy'}
ABCresult
1XX1
01X1
0011
0000
198 }
199  
200 0 ▶ if ( defined $self->{http_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'http_proxy'}) { } 0 0 0
201 0 $self->_split_proxy( http_proxy => $self->{http_proxy} ); # validate
202 0 $self->{_has_proxy}{http} = 1;
203 }
204 else {
205 0 delete $self->{http_proxy};
206 }
207  
208 # https proxy
209 0 ▶ if (! exists $self->{https_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"https_proxy"}) 0 0 0
210 0 ▶ $self->{https_proxy} = $ENV{https_proxy} || $ENV{HTTPS_PROXY} || $self->{proxy};
Condition0%0/6
$ENV{'https_proxy'} || $ENV{'HTTPS_PROXY'} || $self->{'proxy'}
l!l&&r!l&&!r
000
$ENV{"https_proxy"} or $ENV{"HTTPS_PROXY"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'https_proxy'} || $ENV{'HTTPS_PROXY'} || $self->{'proxy'}
$ENV{"https_proxy"} $ENV{"HTTPS_PROXY"} $self->{'proxy'}
Truth table0%0/4
A or B || C
A= $ENV{"https_proxy"}B= $ENV{"HTTPS_PROXY"}C= $self->{'proxy'}
ABCresult
1XX1
01X1
0011
0000
211 }
212  
213 0 ▶ if ( $self->{https_proxy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'https_proxy'}) { } 0 0 0
214 0 $self->_split_proxy( https_proxy => $self->{https_proxy} ); # validate
215 0 $self->{_has_proxy}{https} = 1;
216 }
217 else {
218 0 delete $self->{https_proxy};
219 }
220  
221 # Split no_proxy to array reference if not provided as such
222 0 ▶ unless ( ref $self->{no_proxy} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"no_proxy"} eq "ARRAY") 0 0 0
223 $self->{no_proxy} =
224 0 ▶ (defined $self->{no_proxy}) ? [ split /\s*,\s*/, $self->{no_proxy} ] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'no_proxy'} ? : 0 0 0
225 }
226  
227 0 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 5 for my $sub_name ( qw/get head put post patch delete/ ) {
245 30 my $req_method = uc $sub_name;
246 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
247 0 ▶ eval <<"HERE"; ## no critic
Subroutine delete: not called CC=5 SCAR=33.7
Subroutine get: not called CC=5 SCAR=33.7
Subroutine head: not called CC=5 SCAR=33.7
Subroutine patch: not called CC=5 SCAR=33.7
Subroutine post: not called CC=5 SCAR=33.7
Subroutine put: not called CC=5 SCAR=33.7
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 6 branches
BranchTrueFalseTotal
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->delete(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->get(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->head(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->patch(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->post(URL, [HASHREF])\n") 0 0 0
@_ == 2 or @_ == 3 and ref $args eq "HASH" or _croak("Usage: \$http->put(URL, [HASHREF])\n") 0 0 0
Condition0%0/48
@_ == 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
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
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
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
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
000
@_ == 3 and ref $args eq "HASH"
!ll&&!rl&&r
000
$args || {}
l!l
00
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
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 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 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 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 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 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 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
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 0 ▶sub post_form {
Subroutine post_form: not called CC=6 SCAR=37.4
Pod documented
278 0 my ($self, $url, $data, $args) = @_;
279 0 ▶ (@_ == 3 || @_ == 4 && ref $args eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or @_ == 4 and ref $args eq "HASH" or _croak("Usage: \$http->post_form(URL, DATAREF, [HASHREF])\n") 0 0 0
Condition0%0/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
000
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table0%0/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 0 my $headers = {};
283 0 ▶ while ( my ($key, $value) = each %{$args->{headers} || {}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args->{"headers"} or {};}) 0 0 0
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
284 0 $headers->{lc $key} = $value;
285 }
286  
287 0 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 0 ▶sub mirror {
Subroutine mirror: not called CC=17 SCAR=57.2
Pod documented
324 0 my ($self, $url, $file, $args) = @_;
325 0 ▶ @_ == 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") 0 0 0
Condition0%0/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
000
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table0%0/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 0 ▶ if ( exists $args->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"headers"}) 0 0 0
329 0 my $headers = {};
330 0 ▶ while ( my ($key, $value) = each %{$args->{headers} || {}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args->{"headers"} or {};}) 0 0 0
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
331 0 $headers->{lc $key} = $value;
332 }
333 0 $args->{headers} = $headers;
334 }
335  
336 0 ▶ if ( -e $file and my $mtime = (stat($file))[9] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and my $mtime = (stat $file)[9]) 0 0 0
Condition0%0/3
-e $file and my $mtime = (stat $file)[9]
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and my $mtime = (stat $file)[9]
-e $file my $mtime = (stat $file)[9]
Truth table0%0/3
A and B
A= -e $fileB= my $mtime = (stat $file)[9]
ABresult
0X0
100
111
337 0 ▶ $args->{headers}{'if-modified-since'} ||= $self->_http_date($mtime);
Condition0%0/3
$args->{"headers"}{"if-modified-since"} ||= $self->_http_date($mtime)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"headers"}{"if-modified-since"} ||= $self->_http_date($mtime)
$args->{"headers"}{"if-modified-since"} $self->_http_date($mtime)
Truth table0%0/3
A ||= B
A= $args->{"headers"}{"if-modified-since"}B= $self->_http_date($mtime)
ABresult
1X1
011
000
338 }
339 0 my $tempfile = $file . int(rand(2**31));
340  
341 0 require Fcntl;
342 0 ▶ sysopen my $fh, $tempfile, Fcntl::O_CREAT()|Fcntl::O_EXCL()|Fcntl::O_WRONLY()
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $tempfile, 193 or _croak("Error: Could not create temporary file $tempfile for downloading: $!\n") 0 0 0
343 or _croak(qq/Error: Could not create temporary file $tempfile for downloading: $!\n/);
344 0 binmode $fh;
345 0 ▶ $args->{data_callback} = sub { print {$fh} $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 0 my $response = $self->request('GET', $url, $args);
347 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or _croak("Error: Caught error closing temporary file ${tempfile}: $!\n") 0 0 0
348 or _croak(qq/Error: Caught error closing temporary file $tempfile: $!\n/);
349  
350 0 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 0 0 0
351 0 ▶ rename $tempfile, $file
Branch: 1 branch
BranchTrueFalseTotal
rename $tempfile, $file or _croak("Error replacing $file with ${tempfile}: $!\n") 0 0 0
352 or _croak(qq/Error replacing $file with $tempfile: $!\n/);
353 0 my $lm = $response->{headers}{'last-modified'};
354 0 ▶ if ( $lm and my $mtime = $self->_parse_http_date($lm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lm and my $mtime = $self->_parse_http_date($lm)) 0 0 0
Condition0%0/3
$lm and my $mtime = $self->_parse_http_date($lm)
!ll&&!rl&&r
000
MC/DC0%0/2
$lm and my $mtime = $self->_parse_http_date($lm)
$lm my $mtime = $self->_parse_http_date($lm)
Truth table0%0/3
A and B
A= $lmB= my $mtime = $self->_parse_http_date($lm)
ABresult
0X0
100
111
355 0 utime $mtime, $mtime, $file;
356 }
357 }
358 0 ▶ $response->{success} ||= $response->{status} eq '304';
Condition0%0/3
$response->{"success"} ||= $response->{"status"} eq 304
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->{"success"} ||= $response->{"status"} eq 304
$response->{"success"} $response->{"status"} eq 304
Truth table0%0/3
A ||= B
A= $response->{"success"}B= $response->{"status"} eq 304
ABresult
1X1
011
000
359 0 unlink $tempfile;
360 0 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 5 my %idempotent = map { $_ => 1 } qw/GET HEAD PUT DELETE OPTIONS TRACE/;
474 5 my %sensitive_headers = map { $_ => 1 } qw/authorization cookie proxy-authorization/;
475  
476 0 ▶sub request {
Subroutine request: not called CC=16 SCAR=56.1
Pod documented
477 0 my ($self, $method, $url, $args) = @_;
478 0 ▶ @_ == 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") 0 0 0
Condition0%0/6
@_ == 3 or @_ == 4 and ref $args eq "HASH"
l!l&&r!l&&!r
000
@_ == 4 and ref $args eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 or @_ == 4 and ref $args eq "HASH"
@_ == 3 @_ == 4 ref $args eq "HASH"
Truth table0%0/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 0 ▶ $args ||= {}; # we keep some state in this during _request
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
481  
482 # RFC 2616 Section 8.1.4 mandates a single retry on broken socket
483 0 my $response;
484 0 for ( 0 .. 1 ) {
485 0 $response = eval { $self->_request($method, $url, $args) };
486 0 ▶ last unless $@ && $idempotent{$method}
Branch: 1 branch
BranchTrueFalseTotal
unless $@ and $idempotent{$method} and $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/ 0 0 0
Condition0%0/6
$@ and $idempotent{$method} and $@ =~ /^(?:Socket closed|Unexpected end|SSL read error)/
!ll&&!rl&&r
000
$@ and $idempotent{$method}
!ll&&!rl&&r
000
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 table0%0/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 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
491 # maybe we got a response hash thrown from somewhere deep
492 0 ▶ if ( ref $e eq 'HASH' && exists $e->{status} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq "HASH" and exists $e->{"status"}) 0 0 0
Condition0%0/3
ref $e eq "HASH" and exists $e->{"status"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $e eq "HASH" and exists $e->{"status"}
ref $e eq "HASH" exists $e->{"status"}
Truth table0%0/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 0 $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 0 ▶ ( @{$args->{_redirects} || []} ? (redirects => delete $args->{_redirects}) : () ),
Branch: 1 branch
BranchTrueFalseTotal
@{$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
510 };
511 }
512 0 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 0 ▶sub www_form_urlencode {
Subroutine www_form_urlencode: not called CC=10 SCAR=47.0
Pod documented
530 0 my ($self, $data) = @_;
531 0 ▶ (@_ == 2 && ref $data)
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 and ref $data or _croak("Usage: \$http->www_form_urlencode(DATAREF)\n") 0 0 0
Condition0%0/3
@_ == 2 and ref $data
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 2 and ref $data
@_ == 2 ref $data
Truth table0%0/3
A and B
A= @_ == 2B= ref $data
ABresult
0X0
100
111
532 or _croak(q/Usage: $http->www_form_urlencode(DATAREF)/ . "\n");
533 0 ▶ (ref $data eq 'HASH' || ref $data eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or ref $data eq "ARRAY" or _croak("form data must be a hash or array reference\n") 0 0 0
Condition0%0/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table0%0/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 0 ▶ ? map { ($_ => $data->{$_}) } sort keys %$data
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 0 0 0
539 : @$data;
540 0 ▶ @params % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
@params % 2 == 0 or _croak("form data reference must have an even number of terms\n") 0 0 0
541 or _croak("form data reference must have an even number of terms\n");
542  
543 0 my @terms;
544 0 ▶ while( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 0 0 0
545 0 my ($key, $value) = splice(@params, 0, 2);
546 0 ▶ _croak("form data keys must not be undef")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
547 if !defined($key);
548 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
549 0 unshift @params, map { $key => $_ } @$value;
550 }
551 else {
552 0 push @terms, join("=", map { $self->_uri_escape($_) } $key, $value);
553 }
554 }
555  
556 0 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 0 ▶sub can_ssl {
Subroutine can_ssl: not called CC=9 SCAR=45.0
Pod documented
577 0 my ($self) = @_;
578  
579 0 my($ok, $reason) = (1, '');
580  
581 # Need IO::Socket::SSL 1.968 for default_ca()
582 0 local @INC = @INC;
583 0 ▶ pop @INC if $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 0 0
584 0 ▶ 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 0 0
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 0 ▶ 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 0 0
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 0 ▶ 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"}) 0 0 0
Condition0%0/6
ref $self and $self->{"verify_SSL"} || $self->{"SSL_options"}{"SSL_verify_mode"}
!ll&&!rl&&r
000
$self->{"verify_SSL"} or $self->{"SSL_options"}{"SSL_verify_mode"}
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $self and $self->{"verify_SSL"} || $self->{"SSL_options"}{"SSL_verify_mode"}
ref $self $self->{"verify_SSL"} $self->{"SSL_options"}{"SSL_verify_mode"}
Truth table0%0/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 0 );
601 0 ▶ unless ( eval { $handle->_find_CA; 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $handle->_find_CA; 1 } }) 0 0 0
602 0 $ok = 0;
603 0 $reason .= "$@";
604 }
605 }
606  
607 0 ▶ wantarray ? ($ok, $reason) : $ok;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
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 5 my %DefaultPort = (
642 http => 80,
643 https => 443,
644 );
645  
646 0 ▶sub _agent {
Subroutine _agent: not called CC=3 SCAR=24.8
647 0 ▶ my $class = ref($_[0]) || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
648 0 (my $default_agent = $class) =~ s{::}{-}g;
649 0 my $version = $class->VERSION;
650 0 ▶ $default_agent .= "/$version" if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 0 0
651 0 return $default_agent;
652 }
653  
654 0 ▶sub _request {
Subroutine _request: not called CC=22 SCAR=62.3
655 0 my ($self, $method, $url, $args) = @_;
656  
657 0 my ($scheme, $host, $port, $path_query, $auth) = $self->_split_url($url);
658  
659 0 ▶ if ($scheme ne 'http' && $scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "http" and $scheme ne "https") 0 0 0
Condition0%0/3
$scheme ne "http" and $scheme ne "https"
!ll&&!rl&&r
000
MC/DC0%0/2
$scheme ne "http" and $scheme ne "https"
$scheme ne "http" $scheme ne "https"
Truth table0%0/3
A and B
A= $scheme ne "http"B= $scheme ne "https"
ABresult
0X0
100
111
660 0 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 0 ▶ host_port => ($port == $DefaultPort{$scheme} ? $host : "$host:$port"),
Branch: 1 branch
BranchTrueFalseTotal
$port == $DefaultPort{$scheme} ? : 0 0 0
669 uri => $path_query,
670 headers => {},
671 };
672  
673 0 ▶ my $peer = $args->{peer} || $host;
Condition0%0/3
$args->{'peer'} || $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'peer'} || $host
$args->{'peer'} $host
Truth table0%0/3
A || B
A= $args->{'peer'}B= $host
ABresult
1X1
011
000
674  
675 # Allow 'peer' to be a coderef.
676 0 ▶ if ('CODE' eq ref $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $peer) 0 0 0
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 0 my $handle = delete $self->{handle};
684 0 ▶ if ( $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) 0 0 0
685 0 ▶ unless ( $handle->can_reuse( $scheme, $host, $port, $peer ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle->can_reuse($scheme, $host, $port, $peer)) 0 0 0
686 0 $handle->close;
687 0 undef $handle;
688 }
689 }
690 0 ▶ $handle ||= $self->_open_handle( $request, $scheme, $host, $port, $peer );
Condition0%0/3
$handle ||= $self->_open_handle($request, $scheme, $host, $port, $peer)
l!l&&r!l&&!r
000
MC/DC0%0/2
$handle ||= $self->_open_handle($request, $scheme, $host, $port, $peer)
$handle $self->_open_handle($request, $scheme, $host, $port, $peer)
Truth table0%0/3
A ||= B
A= $handleB= $self->_open_handle($request, $scheme, $host, $port, $peer)
ABresult
1X1
011
000
691  
692 0 $self->_prepare_headers_and_cb($request, $args, $url, $auth);
693 0 $handle->write_request($request);
694  
695 0 my $response;
696 0 do { $response = $handle->read_response_header }
697 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
698  
699 0 ▶ $self->_update_cookie_jar( $url, $response ) if $self->{cookie_jar};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_jar"} 0 0 0
700 0 my @redir_args = $self->_maybe_redirect($request, $response, $args);
701  
702 0 my $known_message_length;
703 0 ▶ if ($method eq 'HEAD' || $response->{status} =~ /^[23]04/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'HEAD' or $response->{'status'} =~ /^[23]04/) { } 0 0 0
Condition0%0/3
$method eq "HEAD" or $response->{"status"} =~ /^[23]04/
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "HEAD" or $response->{"status"} =~ /^[23]04/
$method eq "HEAD" $response->{"status"} =~ /^[23]04/
Truth table0%0/3
A or B
A= $method eq "HEAD"B= $response->{"status"} =~ /^[23]04/
ABresult
1X1
011
000
704 # response has no message body
705 0 $known_message_length = 1;
706 }
707 else {
708 # Ignore any data callbacks during redirection.
709 0 ▶ my $cb_args = @redir_args ? +{} : $args;
Branch: 1 branch
BranchTrueFalseTotal
@redir_args ? : 0 0 0
710 0 my $data_cb = $self->_prepare_data_cb($response, $cb_args);
711 0 $known_message_length = $handle->read_body($data_cb, $response);
712 }
713  
714 0 ▶ 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') { } 0 0 0
Condition0%0/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
000
$self->{"keep_alive"} and $handle->connected and $known_message_length and $response->{"protocol"} eq "HTTP/1.1"
!ll&&!rl&&r
000
$self->{"keep_alive"} and $handle->connected and $known_message_length
!ll&&!rl&&r
000
$self->{"keep_alive"} and $handle->connected
!ll&&!rl&&r
000
$response->{'headers'}{'connection'} || ''
l!l
00
MC/DC0%0/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 table0%0/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 0 $handle->_update_last_used();
721 0 $self->{handle} = $handle;
722 }
723 else {
724 0 $handle->close;
725 }
726  
727 0 $response->{success} = substr( $response->{status}, 0, 1 ) eq '2';
728 0 $response->{url} = $url;
729  
730 # Push the current response onto the stack of redirects if redirecting.
731 0 ▶ if (@redir_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@redir_args) 0 0 0
732 0 push @{$args->{_redirects}}, $response;
733 0 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 0 ▶ if @{$args->{_redirects}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$args->{"_redirects"};} 0 0 0
739 0 return $response;
740 }
741  
742 0 ▶sub _open_handle {
Subroutine _open_handle: not called CC=4 SCAR=30.0
743 0 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 0 );
753  
754 0 ▶ require Time::HiRes if $self->{keep_alive_timeout} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"keep_alive_timeout"} > 0 0 0 0
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'};})) { } 0 0 0
Condition0%0/3
$self->{"_has_proxy"}{$scheme} and not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/3
A and not(B)
A= $self->{"_has_proxy"}{$scheme}B= not grep({$host =~ /\Q$_\E$/;} @{$$self{"no_proxy"};})
ABresult
0X0
100
111
757 0 return $self->_proxy_connect( $request, $handle );
758 }
759 else {
760 0 return $handle->connect($scheme, $host, $port, $peer);
761 }
762 }
763  
764 0 ▶sub _proxy_connect {
Subroutine _proxy_connect: not called CC=8 SCAR=42.8
765 0 my ($self, $request, $handle) = @_;
766  
767 0 my @proxy_vars;
768 0 ▶ if ( $request->{scheme} eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'scheme'} eq 'https') { } 0 0 0
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 0 ▶ _croak(qq{No http_proxy defined}) unless $self->{http_proxy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"http_proxy"} 0 0 0
777 0 @proxy_vars = $self->_split_proxy( http_proxy => $self->{http_proxy} );
778 }
779  
780 0 my ($p_scheme, $p_host, $p_port, $p_auth) = @proxy_vars;
781  
782 0 ▶ if ( length $p_auth && ! defined $request->{headers}{'proxy-authorization'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}) 0 0 0
Condition0%0/3
length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $p_auth and not defined $request->{"headers"}{"proxy-authorization"}
length $p_auth not defined $request->{"headers"}{"proxy-authorization"}
Truth table0%0/3
A and not(B)
A= length $p_authB= not defined $request->{"headers"}{"proxy-authorization"}
ABresult
0X0
100
111
783 0 $self->_add_basic_auth_header( $request, 'proxy-authorization' => $p_auth );
784 }
785  
786 0 $handle->connect($p_scheme, $p_host, $p_port, $p_host);
787  
788 0 ▶ if ($request->{scheme} eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'scheme'} eq 'https') { } 0 0 0
789 0 $self->_create_proxy_tunnel( $request, $handle );
790 }
791 else {
792 # non-tunneled proxy requires absolute URI
793 0 $request->{uri} = "$request->{scheme}://$request->{host_port}$request->{uri}";
794 }
795  
796 0 return $handle;
797 }
798  
799 0 ▶sub _split_proxy {
Subroutine _split_proxy: not called CC=6 SCAR=37.4
800 0 my ($self, $type, $proxy) = @_;
801  
802 0 my ($scheme, $host, $port, $path_query, $auth) = eval { $self->_split_url($proxy) };
803  
804 0 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scheme and length $scheme and length $host and length $port and $path_query eq "/") 0 0 0
Condition0%0/12
defined $scheme and length $scheme and length $host and length $port and $path_query eq "/"
!ll&&!rl&&r
000
defined $scheme and length $scheme and length $host and length $port
!ll&&!rl&&r
000
defined $scheme and length $scheme and length $host
!ll&&!rl&&r
000
defined $scheme and length $scheme
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 _croak(qq{$type URL must be in format http[s]://[auth@]<host>:<port>/\n});
809 }
810  
811 0 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 0 ▶sub _prepare_headers_and_cb {
Subroutine _prepare_headers_and_cb: not called CC=31 SCAR=69.0
854 0 my ($self, $request, $args, $url, $auth) = @_;
855  
856 0 for ($self->{default_headers}, $args->{headers}) {
857 0 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
858 0 ▶ while (my ($k, $v) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 0 0 0
859 0 ▶ next if $args->{_strip_credentials} && exists $sensitive_headers{lc $k};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"_strip_credentials"} and exists $sensitive_headers{lc $k} 0 0 0
Condition0%0/3
$args->{"_strip_credentials"} and exists $sensitive_headers{lc $k}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"_strip_credentials"} and exists $sensitive_headers{lc $k}
$args->{"_strip_credentials"} exists $sensitive_headers{lc $k}
Truth table0%0/3
A and B
A= $args->{"_strip_credentials"}B= exists $sensitive_headers{lc $k}
ABresult
0X0
100
111
860 0 $request->{headers}{lc $k} = $v;
861 0 $request->{header_case}{lc $k} = $k;
862 }
863 }
864  
865 0 ▶ if (exists $request->{headers}{'host'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"headers"}{"host"}) 0 0 0
866 0 die(qq/The 'Host' header must not be provided as header option\n/);
867 }
868  
869 0 $request->{headers}{'host'} = $request->{host_port};
870 0 ▶ $request->{headers}{'user-agent'} ||= $self->{agent};
Condition0%0/3
$request->{"headers"}{"user-agent"} ||= $self->{"agent"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->{"headers"}{"user-agent"} ||= $self->{"agent"}
$request->{"headers"}{"user-agent"} $self->{"agent"}
Truth table0%0/3
A ||= B
A= $request->{"headers"}{"user-agent"}B= $self->{"agent"}
ABresult
1X1
011
000
871 $request->{headers}{'connection'} = "close"
872 0 ▶ unless $self->{keep_alive};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_alive"} 0 0 0
873  
874 # Some servers error on an empty-body PUT/POST without a content-length
875 0 ▶ if ( $request->{method} eq 'PUT' || $request->{method} eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"method"} eq "PUT" or $request->{"method"} eq "POST") 0 0 0
Condition0%0/3
$request->{"method"} eq "PUT" or $request->{"method"} eq "POST"
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->{"method"} eq "PUT" or $request->{"method"} eq "POST"
$request->{"method"} eq "PUT" $request->{"method"} eq "POST"
Truth table0%0/3
A or B
A= $request->{"method"} eq "PUT"B= $request->{"method"} eq "POST"
ABresult
1X1
011
000
876 0 ▶ if (!defined($args->{content}) || !length($args->{content}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"content"} and length $args->{"content"}) 0 0 0
Condition0%0/3
defined $args->{"content"} and length $args->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"content"} and length $args->{"content"}
defined $args->{"content"} length $args->{"content"}
Truth table0%0/3
A and B
A= defined $args->{"content"}B= length $args->{"content"}
ABresult
0X0
100
111
877 0 $request->{headers}{'content-length'} = 0;
878 }
879 }
880  
881 0 ▶ if ( defined $args->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"content"}) 0 0 0
882 0 ▶ if ( ref $args->{content} eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{'content'} eq 'CODE') { } 0 0 0
elsif (length $args->{'content'}) { } 0 0 0
883 0 ▶ if ( exists $request->{'content-length'} && $request->{'content-length'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{'content-length'} and $request->{'content-length'} == 0) { } 0 0 0
Condition0%0/3
exists $request->{"content-length"} and $request->{"content-length"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $request->{"content-length"} and $request->{"content-length"} == 0
exists $request->{"content-length"} $request->{"content-length"} == 0
Truth table0%0/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 0 ▶ $request->{headers}{'content-type'} ||= "application/octet-stream";
Condition0%0/2
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
l!l
00
MC/DC0%0/1
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
$request->{"headers"}{"content-type"}
Truth table0%0/2
A
A= $request->{"headers"}{"content-type"}
Aresult
11
00
888 $request->{headers}{'transfer-encoding'} = 'chunked'
889 unless exists $request->{headers}{'content-length'}
890 0 ▶ || $request->{headers}{'transfer-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"} 0 0 0
Condition0%0/3
exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
exists $request->{"headers"}{"content-length"} $request->{"headers"}{"transfer-encoding"}
Truth table0%0/3
A or B
A= exists $request->{"headers"}{"content-length"}B= $request->{"headers"}{"transfer-encoding"}
ABresult
1X1
011
000
891 0 $request->{cb} = $args->{content};
892 }
893 }
894 elsif ( length $args->{content} ) {
895 0 my $content = $args->{content};
896 0 ▶ if ( "$]" >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= "5.008") 0 0 0
897 0 ▶ utf8::downgrade($content, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($content, 1) or die "Wide character in request message body\n" 0 0 0
898 or die(qq/Wide character in request message body\n/);
899 }
900 0 ▶ $request->{headers}{'content-type'} ||= "application/octet-stream";
Condition0%0/2
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
l!l
00
MC/DC0%0/1
$request->{"headers"}{"content-type"} ||= "application/octet-stream"
$request->{"headers"}{"content-type"}
Truth table0%0/2
A
A= $request->{"headers"}{"content-type"}
Aresult
11
00
901 $request->{headers}{'content-length'} = length $content
902 unless $request->{headers}{'content-length'}
903 0 ▶ || $request->{headers}{'transfer-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"} 0 0 0
Condition0%0/3
$request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->{"headers"}{"content-length"} or $request->{"headers"}{"transfer-encoding"}
$request->{"headers"}{"content-length"} $request->{"headers"}{"transfer-encoding"}
Truth table0%0/3
A or B
A= $request->{"headers"}{"content-length"}B= $request->{"headers"}{"transfer-encoding"}
ABresult
1X1
011
000
904 0 ▶ $request->{cb} = sub { substr $content, 0, length $content, '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
905 }
906 $request->{trailer_cb} = $args->{trailer_callback}
907 0 ▶ if ref $args->{trailer_callback} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"trailer_callback"} eq "CODE" 0 0 0
908 }
909  
910 ### If we have a cookie jar, then maybe add relevant cookies
911 0 ▶ if ( $self->{cookie_jar} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cookie_jar"}) 0 0 0
912 0 my $cookies = $self->cookie_jar->cookie_header( $url );
913 0 ▶ $request->{headers}{cookie} = $cookies if length $cookies;
Branch: 1 branch
BranchTrueFalseTotal
if length $cookies 0 0 0
914 }
915  
916 # if we have Basic auth parameters, add them
917 0 ▶ if ( length $auth && ! defined $request->{headers}{authorization} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $auth and not defined $request->{"headers"}{"authorization"}) 0 0 0
Condition0%0/3
length $auth and not defined $request->{"headers"}{"authorization"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $auth and not defined $request->{"headers"}{"authorization"}
length $auth not defined $request->{"headers"}{"authorization"}
Truth table0%0/3
A and not(B)
A= length $authB= not defined $request->{"headers"}{"authorization"}
ABresult
0X0
100
111
918 0 $self->_add_basic_auth_header( $request, 'authorization' => $auth );
919 }
920  
921 0 return;
922 }
923  
924 0 ▶sub _add_basic_auth_header {
Subroutine _add_basic_auth_header: not called CC=1 SCAR=6.9
925 0 my ($self, $request, $header, $auth) = @_;
926 0 require MIME::Base64;
927 0 $request->{headers}{$header} =
928 "Basic " . MIME::Base64::encode_base64($auth, "");
929 0 return;
930 }
931  
932 0 ▶sub _prepare_data_cb {
Subroutine _prepare_data_cb: not called CC=4 SCAR=30.0
933 0 my ($self, $response, $args) = @_;
934 0 my $data_cb = $args->{data_callback};
935 0 $response->{content} = '';
936  
937 0 ▶ if (!$data_cb || $response->{status} !~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_cb and $response->{"status"} =~ /^2/) 0 0 0
Condition0%0/3
$data_cb and $response->{"status"} =~ /^2/
!ll&&!rl&&r
000
MC/DC0%0/2
$data_cb and $response->{"status"} =~ /^2/
$data_cb $response->{"status"} =~ /^2/
Truth table0%0/3
A and B
A= $data_cbB= $response->{"status"} =~ /^2/
ABresult
0X0
100
111
938 0 ▶ if (defined $self->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'max_size'}) { } 0 0 0
939 0 ▶ $data_cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
940 0 $_[1]->{content} .= $_[0];
941 die(qq/Size of response body exceeds the maximum allowed of $self->{max_size}\n/)
942 0 ▶ if length $_[1]->{content} > $self->{max_size};
Branch: 1 branch
BranchTrueFalseTotal
if length $_[1]{"content"} > $self->{"max_size"} 0 0 0
943 0 };
944 }
945 else {
946 0 ▶ $data_cb = sub { $_[1]->{content} .= $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
947 }
948 }
949 0 return $data_cb;
950 }
951  
952 0 ▶sub _update_cookie_jar {
Subroutine _update_cookie_jar: not called CC=4 SCAR=30.0
953 0 my ($self, $url, $response) = @_;
954  
955 0 my $cookies = $response->{headers}->{'set-cookie'};
956 0 ▶ return unless defined $cookies;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookies 0 0 0
957  
958 0 ▶ my @cookies = ref $cookies ? @$cookies : $cookies;
Branch: 1 branch
BranchTrueFalseTotal
ref $cookies ? : 0 0 0
959  
960 0 $self->cookie_jar->add( $url, $_ ) for @cookies;
961  
962 0 return;
963 }
964  
965 0 ▶sub _validate_cookie_jar {
Subroutine _validate_cookie_jar: not called CC=4 SCAR=30.0
966 0 my ($class, $jar) = @_;
967  
968 # duck typing
969 0 for my $method ( qw/add cookie_header/ ) {
970 0 ▶ _croak(qq/Cookie jar must provide the '$method' method\n/)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $jar and ref($jar)->can($method) 0 0 0
Condition0%0/3
ref $jar and ref($jar)->can($method)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $jar and ref($jar)->can($method)
ref $jar ref($jar)->can($method)
Truth table0%0/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 0 return;
975 }
976  
977 0 ▶sub _maybe_redirect {
Subroutine _maybe_redirect: not called CC=17 SCAR=57.2
978 0 my ($self, $request, $response, $args) = @_;
979 0 my $headers = $response->{headers};
980 0 my ($status, $method) = ($response->{status}, $request->{method});
981 0 ▶ $args->{_redirects} ||= [];
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
982  
983 0 ▶ 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"}) 0 0 0
Condition0%0/12
$status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"} and @{$args->{"_redirects"};} < $self->{"max_redirect"}
!ll&&!rl&&r
000
$status eq 303 || $status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/ and $headers->{"location"}
!ll&&!rl&&r
000
$status eq 303 or $status =~ /^30[1278]/ and $method =~ /^GET|HEAD$/
l!l&&r!l&&!r
000
$status =~ /^30[1278]/ and $method =~ /^GET|HEAD$/
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 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 0 ▶ : $headers->{location};
Branch: 2 branches
BranchTrueFalseTotal
$headers->{'location'} =~ m[^//] ? : 0 0 0
$headers->{'location'} =~ m[^/] ? : 0 0 0
992 0 my ($to_scheme, $to_host, $to_port) = $self->_split_url($location);
993 0 ▶ 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") 0 0 0
Condition0%0/6
not $self->{"allow_downgrade"} and $request->{"scheme"} eq "https" and $to_scheme eq "http"
!ll&&!rl&&r
000
not $self->{"allow_downgrade"} and $request->{"scheme"} eq "https"
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 return;
995 }
996 0 ▶ 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) 0 0 0
Condition0%0/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
000
$request->{"scheme"} ne $to_scheme or $request->{"host"} ne $to_host or $request->{"port"} ne $to_port
l!l&&r!l&&!r
000
$request->{"scheme"} ne $to_scheme or $request->{"host"} ne $to_host
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/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 0 $args->{_strip_credentials} = 1;
1003 }
1004  
1005 0 ▶ return (($status eq '303' ? 'GET' : $method), $location);
Branch: 1 branch
BranchTrueFalseTotal
$status eq '303' ? : 0 0 0
1006 }
1007 0 return;
1008 }
1009  
1010 0 ▶sub _split_url {
Subroutine _split_url: not called CC=9 SCAR=45.0
1011 0 my $url = pop;
1012  
1013 # URI regex adapted from the URI module
1014 0 ▶ my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)>
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#]*)] or die "Cannot parse URL: '${url}'\n" 0 0 0
1015 or die(qq/Cannot parse URL: '$url'\n/);
1016  
1017 0 $scheme = lc $scheme;
1018 0 ▶ $path_query = "/$path_query" unless $path_query =~ m<\A/>;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 0 0 0
1019  
1020 0 my $auth = '';
1021 0 ▶ if ( (my $i = index $host, '@') != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($host, "\@")) != -1) 0 0 0
1022 # user:pass@host
1023 0 $auth = substr $host, 0, $i, ''; # take up to the @ for auth
1024 0 substr $host, 0, 1, ''; # knock the @ off the host
1025  
1026 # userinfo might be percent escaped, so recover real auth info
1027 0 $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
1028 }
1029 0 ▶ my $port = $host =~ s/:(\d*)\z// && length $1 ? $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:(\d*)\z// && length $1 ? : 0 0 0
$scheme eq 'http' ? : 0 0 0
$scheme eq 'https' ? : 0 0 0
Condition0%0/3
$host =~ s/:(\d*)\z// and length $1
!ll&&!rl&&r
000
MC/DC0%0/2
$host =~ s/:(\d*)\z// and length $1
$host =~ s/:(\d*)\z// length $1
Truth table0%0/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 0 ▶ return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth);
Branch: 1 branch
BranchTrueFalseTotal
length $host ? : 0 0 0
1035 }
1036  
1037 # Date conversions adapted from HTTP::Date
1038 5 my $DoW = "Sun|Mon|Tue|Wed|Thu|Fri|Sat";
1039 5 my $MoY = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
1040 0 ▶sub _http_date {
Subroutine _http_date: not called CC=1 SCAR=6.9
1041 0 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($_[1]);
1042 0 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 0 ▶sub _parse_http_date {
Subroutine _parse_http_date: not called CC=6 SCAR=37.4
1050 0 my ($self, $str) = @_;
1051 0 require Time::Local;
1052 0 my @tl_parts;
1053 0 ▶ 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$/) { } 0 0 0
elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/) { } 0 0 0
elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/) { } 0 0 0
1054 0 @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 0 @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 0 @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6);
1061 }
1062 0 return eval {
1063 0 ▶ my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1;
Branch: 1 branch
BranchTrueFalseTotal
@tl_parts ? : 0 0 0
1064 0 ▶ $t < 0 ? undef : $t;
Branch: 1 branch
BranchTrueFalseTotal
$t < 0 ? : 0 0 0
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 5 my %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255;
1072 5 $escapes{' '}="+";
1073 5 my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
1074  
1075 0 ▶sub _uri_escape {
Subroutine _uri_escape: not called CC=4 SCAR=25.8
1076 0 my ($self, $str) = @_;
1077 0 ▶ return "" if !defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
1078 0 ▶ if ( "$]" >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= 5.008) { } 0 0 0
1079 0 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 0 $str =~ s/($unsafe_char)/$escapes{$1}/g;
1087 0 return $str;
1088 }
1089  
1090 package
1091 HTTP::Tiny::Handle; # hide from PAUSE/indexers
1092 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1093 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1094  
1095 5 ▶use Errno qw[EINTR EPIPE];
Subroutine BEGIN: called CC=1 SCAR=0.0
1096 5 ▶use IO::Socket qw[SOCK_STREAM];
Subroutine BEGIN: called CC=1 SCAR=0.0
1097 5 ▶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 5 ▶ 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 5 5
eval { do { require IO::Socket::IP; 'IO::Socket::IP'->VERSION(0.32) } } ? : 5 0 5
1105 'IO::Socket::INET';
1106  
1107 sub BUFSIZE () { 32768 } ## no critic
1108  
1109 0 ▶my $Printable = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1110 0 local $_ = shift;
1111 0 s/\r/\\r/g;
1112 0 s/\n/\\n/g;
1113 0 s/\t/\\t/g;
1114 0 s/([^\x20-\x7E])/sprintf('\\x%.2X', ord($1))/ge;
1115 0 $_;
1116 5 };
1117  
1118 5 my $Token = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;
1119 5 my $Field_Content = qr/[[:print:]]+ (?: [\x20\x09]+ [[:print:]]+ )*/x;
1120  
1121 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
1122 0 my ($class, %args) = @_;
1123 0 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 0 ▶sub timeout {
Subroutine timeout: not called CC=4 SCAR=30.0
1135 0 my ($self, $timeout) = @_;
1136 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
1137 0 $self->{timeout} = $timeout;
1138 0 ▶ if ( $self->{fh} && $self->{fh}->can('timeout') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"fh"}->can("timeout")) 0 0 0
Condition0%0/3
$self->{"fh"} and $self->{"fh"}->can("timeout")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"fh"} and $self->{"fh"}->can("timeout")
$self->{"fh"} $self->{"fh"}->can("timeout")
Truth table0%0/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 0 return $self->{timeout};
1143 }
1144  
1145 0 ▶sub connect {
Subroutine connect: not called CC=9 SCAR=45.0
1146 0 ▶ @_ == 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" 0 0 0
1147 0 my ($self, $scheme, $host, $port, $peer) = @_;
1148  
1149 0 ▶ if ( $scheme eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "https") 0 0 0
1150 0 $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 0 ▶ ) 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" 0 0 0
$self->{'local_address'} ? : 0 0 0
1162  
1163 0 $self->{fh}->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1);
1164  
1165 binmode($self->{fh})
1166 0 ▶ or die(qq/Could not binmode() socket: '$!'\n/);
Branch: 1 branch
BranchTrueFalseTotal
binmode $self->{"fh"} or die "Could not binmode() socket: '$!'\n" 0 0 0
1167  
1168 0 ▶ if ( $self->{keep_alive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keep_alive"}) 0 0 0
1169 0 ▶ unless ( defined( $self->{fh}->setsockopt( SOL_SOCKET, SO_KEEPALIVE, 1 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}->setsockopt(1, 9, 1)) 0 0 0
1170 0 CORE::close($self->{fh});
1171 0 die(qq/Could not set SO_KEEPALIVE on socket: '$!'\n/);
1172 }
1173 }
1174  
1175 0 ▶ $self->start_ssl($host) if $scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "https" 0 0 0
1176  
1177 0 $self->{scheme} = $scheme;
1178 0 $self->{host} = $host;
1179 0 $self->{peer} = $peer;
1180 0 $self->{port} = $port;
1181 0 $self->{pid} = $$;
1182 0 $self->{tid} = _get_tid();
1183  
1184 0 return $self;
1185 }
1186  
1187 0 ▶sub connected {
Subroutine connected: not called CC=4 SCAR=30.0
1188 0 my ($self) = @_;
1189 0 ▶ if ( $self->{fh} && $self->{fh}->connected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"fh"}->connected) 0 0 0
Condition0%0/3
$self->{"fh"} and $self->{"fh"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"fh"} and $self->{"fh"}->connected
$self->{"fh"} $self->{"fh"}->connected
Truth table0%0/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 0 ▶ : join( ':', $self->{fh}->peerhost, $self->{fh}->peerport );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1193 }
1194 0 return;
1195 }
1196  
1197 0 ▶sub start_ssl {
Subroutine start_ssl: not called CC=4 SCAR=30.0
1198 0 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 0 ▶ if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"fh"} eq "IO::Socket::SSL") 0 0 0
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 0 my $ssl_args = $self->_ssl_args($host);
1211 IO::Socket::SSL->start_SSL(
1212 $self->{fh},
1213 %$ssl_args,
1214 0 ▶ SSL_create_ctx_callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1215 0 my $ctx = shift;
1216 0 Net::SSLeay::CTX_set_mode($ctx, Net::SSLeay::MODE_AUTO_RETRY());
1217 },
1218 0 );
1219  
1220 0 ▶ unless ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"fh"} eq "IO::Socket::SSL") 0 0 0
1221 0 my $ssl_err = IO::Socket::SSL->errstr;
1222 0 die(qq/SSL connection failed for $host: $ssl_err\n/);
1223 }
1224 }
1225  
1226 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
1227 0 ▶ @_ == 1 || die(q/Usage: $handle->close()/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->close()\n" 0 0 0
1228 0 my ($self) = @_;
1229 CORE::close($self->{fh})
1230 0 ▶ or die(qq/Could not close socket: '$!'\n/);
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $self->{"fh"} or die "Could not close socket: '$!'\n" 0 0 0
1231 }
1232  
1233 0 ▶sub write {
Subroutine write: not called CC=10 SCAR=47.0
1234 0 ▶ @_ == 2 || die(q/Usage: $handle->write(buf)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write(buf)\n" 0 0 0
1235 0 my ($self, $buf) = @_;
1236  
1237 0 ▶ if ( "$]" >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= "5.008") 0 0 0
1238 0 ▶ utf8::downgrade($buf, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($buf, 1) or die "Wide character in write()\n" 0 0 0
1239 or die(qq/Wide character in write()\n/);
1240 }
1241  
1242 0 my $len = length $buf;
1243 0 my $off = 0;
1244  
1245 0 local $SIG{PIPE} = 'IGNORE';
1246  
1247 0 while () {
1248 0 ▶ $self->can_write
Branch: 1 branch
BranchTrueFalseTotal
$self->can_write or die "Timed out while waiting for socket to become ready for writing\n" 0 0 0
1249 or die(qq/Timed out while waiting for socket to become ready for writing\n/);
1250 0 my $r = syswrite($self->{fh}, $buf, $len, $off);
1251 0 ▶ if (defined $r) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! == 32) { } 0 0 0
elsif ($! != 4) { } 0 0 0
1252 0 $len -= $r;
1253 0 $off += $r;
1254 0 ▶ last unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 0 0 0
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 0 return $off;
1271 }
1272  
1273 0 ▶sub read {
Subroutine read: not called CC=13 SCAR=52.0
1274 0 ▶ @_ == 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" 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
1275 0 my ($self, $len, $allow_partial) = @_;
1276  
1277 0 my $buf = '';
1278 0 my $got = length $self->{rbuf};
1279  
1280 0 ▶ if ($got) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got) 0 0 0
1281 0 ▶ my $take = ($got < $len) ? $got : $len;
Branch: 1 branch
BranchTrueFalseTotal
$got < $len ? : 0 0 0
1282 0 $buf = substr($self->{rbuf}, 0, $take, '');
1283 0 $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 0 local $SIG{PIPE} = 'IGNORE';
1290  
1291 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
1292 0 ▶ $self->can_read
Branch: 1 branch
BranchTrueFalseTotal
$self->can_read or die "Timed out while waiting for socket to become ready for reading\n" 0 0 0
1293 or die(q/Timed out while waiting for socket to become ready for reading/ . "\n");
1294 0 my $r = sysread($self->{fh}, $buf, $len, length $buf);
1295 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
1296 0 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
1297 0 $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 0 ▶ if ($len && !$allow_partial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len and not $allow_partial) 0 0 0
Condition0%0/3
$len and not $allow_partial
!ll&&!rl&&r
000
MC/DC0%0/2
$len and not $allow_partial
$len not $allow_partial
Truth table0%0/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 0 return $buf;
1313 }
1314  
1315 0 ▶sub readline {
Subroutine readline: not called CC=9 SCAR=45.0
1316 0 ▶ @_ == 1 || die(q/Usage: $handle->readline()/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->readline()\n" 0 0 0
1317 0 my ($self) = @_;
1318  
1319 0 while () {
1320 0 ▶ if ($self->{rbuf} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rbuf"} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) 0 0 0
1321 0 return $1;
1322 }
1323 0 ▶ if (length $self->{rbuf} >= $self->{max_line_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"rbuf"} >= $self->{"max_line_size"}) 0 0 0
1324 0 die(qq/Line size exceeds the maximum allowed size of $self->{max_line_size}\n/);
1325 }
1326 $self->can_read
1327 0 ▶ or die(qq/Timed out while waiting for socket to become ready for reading\n/);
Branch: 1 branch
BranchTrueFalseTotal
$self->can_read or die "Timed out while waiting for socket to become ready for reading\n" 0 0 0
1328 0 my $r = sysread($self->{fh}, $self->{rbuf}, BUFSIZE, length $self->{rbuf});
1329 0 ▶ if (defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
elsif ($! != 4) { } 0 0 0
1330 0 ▶ last unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
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 0 ▶sub read_header_lines {
Subroutine read_header_lines: not called CC=14 SCAR=53.5
1346 0 ▶ @_ == 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" 0 0 0
Condition0%0/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1347 0 my ($self, $headers) = @_;
1348 0 ▶ $headers ||= {};
Condition0%0/2
$headers ||= {}
l!l
00
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
1349 0 my $lines = 0;
1350 0 my $val;
1351  
1352 0 while () {
1353 0 my $line = $self->readline;
1354  
1355 0 ▶ if (++$lines >= $self->{max_header_lines}) {
Branch: 4 branches
BranchTrueFalseTotal
if (++$lines >= $self->{'max_header_lines'}) { } 0 0 0
elsif ($line =~ /\A ([^\x00-\x1F\x7F:]+) : [\x09\x20]* ([^\x0D\x0A]*)/x) { } 0 0 0
elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) { } 0 0 0
elsif ($line =~ /\A \x0D?\x0A \z/x) { } 0 0 0
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 0 my ($field_name) = lc $1;
1360 0 ▶ if (exists $headers->{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $headers->{$field_name}) { } 0 0 0
1361 0 for ($headers->{$field_name}) {
1362 0 ▶ $_ = [$_] unless ref $_ eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 0 0 0
1363 0 push @$_, $2;
1364 0 $val = \$_->[-1];
1365 }
1366 }
1367 else {
1368 0 $val = \($headers->{$field_name} = $2);
1369 }
1370 }
1371 elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) {
1372 0 ▶ $val
Branch: 1 branch
BranchTrueFalseTotal
$val or die "Unexpected header continuation line\n" 0 0 0
1373 or die(qq/Unexpected header continuation line\n/);
1374 0 ▶ next unless length $1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 0 0 0
1375 0 ▶ $$val .= ' ' if length $$val;
Branch: 1 branch
BranchTrueFalseTotal
if length $$val 0 0 0
1376 0 $$val .= $1;
1377 }
1378 elsif ($line =~ /\A \x0D?\x0A \z/x) {
1379 0 last;
1380 }
1381 else {
1382 0 die(q/Malformed header line: / . $Printable->($line) . "\n");
1383 }
1384 }
1385 0 return $headers;
1386 }
1387  
1388 0 ▶sub write_request {
Subroutine write_request: not called CC=3 SCAR=24.8
1389 0 ▶ @_ == 2 || die(q/Usage: $handle->write_request(request)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_request(request)\n" 0 0 0
1390 0 my($self, $request) = @_;
1391 0 $self->write_request_header(@{$request}{qw/method uri headers header_case/});
1392 0 ▶ $self->write_body($request) if $request->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $request->{"cb"} 0 0 0
1393 0 return;
1394 }
1395  
1396 # Standard request header names/case from HTTP/1.1 RFCs
1397 5 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 5 my @other_request_headers = qw(
1406 Content-Encoding Content-MD5 Content-Type Cookie DNT Date Origin
1407 X-XSS-Protection
1408 );
1409  
1410 5 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 0 ▶sub write_header_lines {
Subroutine write_header_lines: not called CC=23 SCAR=63.1
1415 0 ▶ (@_ >= 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" 0 0 0
Condition0%0/6
@_ >= 2 and @_ <= 4 and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
@_ >= 2 and @_ <= 4
!ll&&!rl&&r
000
MC/DC0%0/3
@_ >= 2 and @_ <= 4 and ref $_[1] eq "HASH"
@_ >= 2 @_ <= 4 ref $_[1] eq "HASH"
Truth table0%0/4
A and B and C
A= @_ >= 2B= @_ <= 4C= ref $_[1] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
1416 0 my($self, $headers, $header_case, $prefix_data) = @_;
1417 0 ▶ $header_case ||= {};
Condition0%0/2
$header_case ||= {}
l!l
00
MC/DC0%0/1
$header_case ||= {}
$header_case
Truth table0%0/2
A
A= $header_case
Aresult
11
00
1418  
1419 0 ▶ my $buf = (defined $prefix_data ? $prefix_data : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix_data ? : 0 0 0
1420  
1421 # Per RFC, control fields should be listed first
1422 0 my %seen;
1423 0 for my $k ( qw/host cache-control expect max-forwards pragma range te/ ) {
1424 0 ▶ next unless exists $headers->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $headers->{$k} 0 0 0
1425 0 $seen{$k}++;
1426 0 my $field_name = $HeaderCase{$k};
1427 0 my $v = $headers->{$k};
1428 0 ▶ for (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
1429 0 ▶ die(qq/Invalid HTTP header field value ($field_name): / . $Printable->($_). "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "" or /\A $Field_Content \z/ox 0 0 0
Condition0%0/3
$_ eq "" or /\A $Field_Content \z/ox
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq "" or /\A $Field_Content \z/ox
$_ eq "" /\A $Field_Content \z/ox
Truth table0%0/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 0 ▶ $_ = '' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
1432 0 $buf .= "$field_name: $_\x0D\x0A";
1433 }
1434 }
1435  
1436 # Other headers sent in arbitrary order
1437 0 ▶ while (my ($k, $v) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 0 0 0
1438 0 my $field_name = lc $k;
1439 0 ▶ next if $seen{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$field_name} 0 0 0
1440 0 ▶ if (exists $HeaderCase{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HeaderCase{$field_name}) { } 0 0 0
1441 0 $field_name = $HeaderCase{$field_name};
1442 }
1443 else {
1444 0 ▶ if (exists $header_case->{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $header_case->{$field_name}) { } 0 0 0
1445 0 $field_name = $header_case->{$field_name};
1446 }
1447 else {
1448 0 $field_name =~ s/\b(\w)/\u$1/g;
1449 }
1450 0 ▶ $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" 0 0 0
1451 or die(q/Invalid HTTP header field name: / . $Printable->($field_name) . "\n");
1452 0 $HeaderCase{lc $field_name} = $field_name;
1453 }
1454 0 ▶ for (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
1455 # unwrap a field value if pre-wrapped by user
1456 0 s/\x0D?\x0A\s+/ /g;
1457 0 ▶ die(qq/Invalid HTTP header field value ($field_name): / . $Printable->($_). "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq "" or /\A $Field_Content \z/ox 0 0 0
Condition0%0/3
$_ eq "" or /\A $Field_Content \z/ox
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq "" or /\A $Field_Content \z/ox
$_ eq "" /\A $Field_Content \z/ox
Truth table0%0/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 0 ▶ $_ = '' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
1460 0 $buf .= "$field_name: $_\x0D\x0A";
1461 }
1462 }
1463 0 $buf .= "\x0D\x0A";
1464 0 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 0 ▶sub read_body {
Subroutine read_body: not called CC=5 SCAR=34.0
1471 0 ▶ @_ == 3 || die(q/Usage: $handle->read_body(callback, response)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or die "Usage: \$handle->read_body(callback, response)\n" 0 0 0
1472 0 my ($self, $cb, $response) = @_;
1473 0 ▶ my $te = $response->{headers}{'transfer-encoding'} || '';
Condition0%0/2
$response->{'headers'}{'transfer-encoding'} || ''
l!l
00
MC/DC0%0/1
$response->{'headers'}{'transfer-encoding'} || ''
$response->{'headers'}{'transfer-encoding'}
Truth table0%0/2
A
A= $response->{'headers'}{'transfer-encoding'}
Aresult
11
00
1474 0 ▶ my $chunked = grep { /chunked/i } ( ref $te eq 'ARRAY' ? @$te : $te ) ;
Branch: 1 branch
BranchTrueFalseTotal
ref $te eq 'ARRAY' ? : 0 0 0
1475 0 ▶ return $chunked
Branch: 1 branch
BranchTrueFalseTotal
$chunked ? : 0 0 0
1476 ? $self->read_chunked_body($cb, $response)
1477 : $self->read_content_body($cb, $response);
1478 }
1479  
1480 0 ▶sub write_body {
Subroutine write_body: not called CC=4 SCAR=30.0
1481 0 ▶ @_ == 2 || die(q/Usage: $handle->write_body(request)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_body(request)\n" 0 0 0
1482 0 my ($self, $request) = @_;
1483 0 ▶ if (exists $request->{headers}{'content-length'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{'headers'}{'content-length'}) { } 0 0 0
1484 0 ▶ return unless $request->{headers}{'content-length'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"headers"}{"content-length"} 0 0 0
1485 0 return $self->write_content_body($request);
1486 }
1487 else {
1488 0 return $self->write_chunked_body($request);
1489 }
1490 }
1491  
1492 0 ▶sub read_content_body {
Subroutine read_content_body: not called CC=8 SCAR=42.8
1493 0 ▶ @_ == 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" 0 0 0
Condition0%0/3
@_ == 3 or @_ == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 3 or @_ == 4
@_ == 3 @_ == 4
Truth table0%0/3
A or B
A= @_ == 3B= @_ == 4
ABresult
1X1
011
000
1494 0 my ($self, $cb, $response, $content_length) = @_;
1495 0 ▶ $content_length ||= $response->{headers}{'content-length'};
Condition0%0/3
$content_length ||= $response->{"headers"}{"content-length"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$content_length ||= $response->{"headers"}{"content-length"}
$content_length $response->{"headers"}{"content-length"}
Truth table0%0/3
A ||= B
A= $content_lengthB= $response->{"headers"}{"content-length"}
ABresult
1X1
011
000
1496  
1497 0 ▶ if ( defined $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_length) 0 0 0
1498 0 my $len = $content_length;
1499 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
1500 0 ▶ my $read = ($len > BUFSIZE) ? BUFSIZE : $len;
Branch: 1 branch
BranchTrueFalseTotal
$len > 32768 ? : 0 0 0
1501 0 $cb->($self->read($read, 0), $response);
1502 0 $len -= $read;
1503 }
1504 0 return length($self->{rbuf}) == 0;
1505 }
1506  
1507 0 my $chunk;
1508 0 ▶ $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) } 0 0 0
1509  
1510 0 return;
1511 }
1512  
1513 0 ▶sub write_content_body {
Subroutine write_content_body: not called CC=7 SCAR=40.3
1514 0 ▶ @_ == 2 || die(q/Usage: $handle->write_content_body(request)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_content_body(request)\n" 0 0 0
1515 0 my ($self, $request) = @_;
1516  
1517 0 my ($len, $content_length) = (0, $request->{headers}{'content-length'});
1518 0 while () {
1519 0 my $data = $request->{cb}->();
1520  
1521 0 ▶ defined $data && length $data
Branch: 1 branch
BranchTrueFalseTotal
defined $data and length $data or last 0 0 0
Condition0%0/3
defined $data and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table0%0/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
1522 or last;
1523  
1524 0 ▶ if ( "$]" >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= "5.008") 0 0 0
1525 0 ▶ utf8::downgrade($data, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($data, 1) or die "Wide character in write_content()\n" 0 0 0
1526 or die(qq/Wide character in write_content()\n/);
1527 }
1528  
1529 0 $len += $self->write($data);
1530 }
1531  
1532 0 ▶ $len == $content_length
Branch: 1 branch
BranchTrueFalseTotal
$len == $content_length or die "Content-Length mismatch (got: $len expected: $content_length)\n" 0 0 0
1533 or die(qq/Content-Length mismatch (got: $len expected: $content_length)\n/);
1534  
1535 0 return $len;
1536 }
1537  
1538 0 ▶sub read_chunked_body {
Subroutine read_chunked_body: not called CC=5 SCAR=34.0
1539 0 ▶ @_ == 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" 0 0 0
1540 0 my ($self, $cb, $response) = @_;
1541  
1542 0 while () {
1543 0 my $head = $self->readline;
1544  
1545 0 ▶ $head =~ /\A ([A-Fa-f0-9]+)/x
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /\A ([A-Fa-f0-9]+)/x or die 'Malformed chunk head: ' . &$Printable($head) . "\n" 0 0 0
1546 or die(q/Malformed chunk head: / . $Printable->($head) . "\n");
1547  
1548 0 ▶ my $len = hex($1)
Branch: 1 branch
BranchTrueFalseTotal
my $len = hex $1 or last 0 0 0
1549 or last;
1550  
1551 0 $self->read_content_body($cb, $response, $len);
1552  
1553 0 ▶ $self->read(2) eq "\x0D\x0A"
Branch: 1 branch
BranchTrueFalseTotal
$self->read(2) eq "\r\n" or die "Malformed chunk: missing CRLF after chunk data\n" 0 0 0
1554 or die(qq/Malformed chunk: missing CRLF after chunk data\n/);
1555 }
1556 0 $self->read_header_lines($response->{headers});
1557 0 return 1;
1558 }
1559  
1560 0 ▶sub write_chunked_body {
Subroutine write_chunked_body: not called CC=7 SCAR=40.3
1561 0 ▶ @_ == 2 || die(q/Usage: $handle->write_chunked_body(request)/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "Usage: \$handle->write_chunked_body(request)\n" 0 0 0
1562 0 my ($self, $request) = @_;
1563  
1564 0 my $len = 0;
1565 0 while () {
1566 0 my $data = $request->{cb}->();
1567  
1568 0 ▶ defined $data && length $data
Branch: 1 branch
BranchTrueFalseTotal
defined $data and length $data or last 0 0 0
Condition0%0/3
defined $data and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table0%0/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
1569 or last;
1570  
1571 0 ▶ if ( "$]" >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= "5.008") 0 0 0
1572 0 ▶ utf8::downgrade($data, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($data, 1) or die "Wide character in write_chunked_body()\n" 0 0 0
1573 or die(qq/Wide character in write_chunked_body()\n/);
1574 }
1575  
1576 0 $len += length $data;
1577  
1578 0 my $chunk = sprintf '%X', length $data;
1579 0 $chunk .= "\x0D\x0A";
1580 0 $chunk .= $data;
1581 0 $chunk .= "\x0D\x0A";
1582  
1583 0 $self->write($chunk);
1584 }
1585 0 $self->write("0\x0D\x0A");
1586 0 ▶ if ( ref $request->{trailer_cb} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request->{'trailer_cb'} eq 'CODE') { } 0 0 0
1587 0 $self->write_header_lines($request->{trailer_cb}->())
1588 }
1589 else {
1590 0 $self->write("\x0D\x0A");
1591 }
1592 0 return $len;
1593 }
1594  
1595 0 ▶sub read_response_header {
Subroutine read_response_header: not called CC=5 SCAR=34.0
1596 0 ▶ @_ == 1 || die(q/Usage: $handle->read_response_header()/ . "\n");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Usage: \$handle->read_response_header()\n" 0 0 0
1597 0 my ($self) = @_;
1598  
1599 0 my $line = $self->readline;
1600  
1601 0 ▶ $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" 0 0 0
1602 or die(q/Malformed Status-Line: / . $Printable->($line). "\n");
1603  
1604 0 my ($protocol, $version, $status, $reason) = ($1, $2, $3, $4);
1605 0 ▶ $reason = "" unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 0 0 0
1606  
1607 0 ▶ die (qq/Unsupported HTTP protocol: $protocol\n/)
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /0*1\.0*[01]/ 0 0 0
1608 unless $version =~ /0*1\.0*[01]/;
1609  
1610 return {
1611 0 status => $status,
1612 reason => $reason,
1613 headers => $self->read_header_lines,
1614 protocol => $protocol,
1615 };
1616 }
1617  
1618 0 ▶sub write_request_header {
Subroutine write_request_header: not called CC=4 SCAR=30.0
1619 0 ▶ @_ == 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" 0 0 0
1620 0 my ($self, $method, $request_uri, $headers, $header_case) = @_;
1621  
1622 0 ▶ die (q/Invalid characters in Request-URI /. $Printable->($request_uri). "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $request_uri =~ /[\x00-\x20\x7F]/ 0 0 0
1623 if $request_uri =~ /[\x00-\x20\x7F]/;
1624  
1625 0 ▶ die (q/Invalid characters in Method /. $Printable->($method). "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /[\x00-\x20\x7F]/ 0 0 0
1626 if $method =~ /[\x00-\x20\x7F]/;
1627  
1628 0 return $self->write_header_lines($headers, $header_case, "$method $request_uri HTTP/1.1\x0D\x0A");
1629 }
1630  
1631 0 ▶sub _do_timeout {
Subroutine _do_timeout: not called CC=10 SCAR=47.0
1632 0 my ($self, $type, $timeout) = @_;
1633 $timeout = $self->{timeout}
1634 0 ▶ unless defined $timeout && $timeout >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout and $timeout >= 0 0 0 0
Condition0%0/3
defined $timeout and $timeout >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and $timeout >= 0
defined $timeout $timeout >= 0
Truth table0%0/3
A and B
A= defined $timeoutB= $timeout >= 0
ABresult
0X0
100
111
1635  
1636 0 my $fd = fileno $self->{fh};
1637 0 ▶ defined $fd && $fd >= 0
Branch: 1 branch
BranchTrueFalseTotal
defined $fd and $fd >= 0 or die "select(2): 'Bad file descriptor'\n" 0 0 0
Condition0%0/3
defined $fd and $fd >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fd and $fd >= 0
defined $fd $fd >= 0
Truth table0%0/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 0 my $initial = time;
1641 0 my $pending = $timeout;
1642 0 my $nfound;
1643  
1644 0 vec(my $fdset = '', $fd, 1) = 1;
1645  
1646 0 while () {
1647 0 ▶ $nfound = ($type eq 'read')
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'read' ? : 0 0 0
1648 ? select($fdset, undef, undef, $pending)
1649 : select(undef, $fdset, undef, $pending) ;
1650 0 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) 0 0 0
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 0 last;
1657 }
1658 0 $! = 0;
1659 0 return $nfound;
1660 }
1661  
1662 0 ▶sub can_read {
Subroutine can_read: not called CC=5 SCAR=34.0
1663 0 ▶ @_ == 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" 0 0 0
Condition0%0/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1664 0 my $self = shift;
1665 0 ▶ if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"fh"} eq "IO::Socket::SSL") 0 0 0
1666 0 ▶ return 1 if $self->{fh}->pending;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"}->pending 0 0 0
1667 }
1668 0 return $self->_do_timeout('read', @_)
1669 }
1670  
1671 0 ▶sub can_write {
Subroutine can_write: not called CC=3 SCAR=24.8
1672 0 ▶ @_ == 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" 0 0 0
Condition0%0/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table0%0/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
1673 0 my $self = shift;
1674 0 return $self->_do_timeout('write', @_)
1675 }
1676  
1677 0 ▶sub _has_keep_alive_expired {
Subroutine _has_keep_alive_expired: not called CC=3 SCAR=24.8
1678 0 my $self = shift;
1679 0 ▶ return unless $self->{keep_alive_timeout} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_alive_timeout"} > 0 0 0 0
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 0 ▶sub _update_last_used {
Subroutine _update_last_used: not called CC=2 SCAR=17.9
1685 0 my $self = shift;
1686 0 ▶ return unless $self->{keep_alive_timeout} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"keep_alive_timeout"} > 0 0 0 0
1687 0 $self->{last_used} = Time::HiRes::time();
1688 }
1689  
1690 0 ▶sub _assert_ssl {
Subroutine _assert_ssl: not called CC=2 SCAR=17.9
1691 0 my($ok, $reason) = HTTP::Tiny->can_ssl();
1692 0 ▶ die $reason unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
1693 }
1694  
1695 0 ▶sub can_reuse {
Subroutine can_reuse: not called CC=11 SCAR=48.8
1696 0 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 0 ▶ || 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 $@ 0 0 0
Condition0%0/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
000
$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
000
$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
000
$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
000
$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
000
$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
000
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"} or $scheme ne $self->{"scheme"}
l!l&&r!l&&!r
000
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid() or length $self->{"rbuf"}
l!l&&r!l&&!r
000
$self->{"pid"} != $$ or $self->{"tid"} != _get_tid()
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/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 0 return 1;
1709 }
1710  
1711 0 ▶sub _find_CA {
Subroutine _find_CA: not called CC=6 SCAR=37.4
1712 0 my $self = shift;
1713  
1714 0 my $ca_file = $self->{SSL_options}->{SSL_ca_file};
1715  
1716 0 ▶ if ( defined $ca_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ca_file) 0 0 0
1717 0 ▶ unless ( -r $ca_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $ca_file) 0 0 0
1718 0 die qq/SSL_ca_file '$ca_file' not found or not readable\n/;
1719 }
1720 0 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 0 my %default_ca = IO::Socket::SSL::default_ca();
1728 0 ▶ return %default_ca if %default_ca;
Branch: 1 branch
BranchTrueFalseTotal
if %default_ca 0 0 0
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 0 ▶sub _find_CA_file {
Subroutine _find_CA_file: not called CC=1 SCAR=6.9
1753 0 my $self = shift;
1754 0 my %res = $self->_find_CA();
1755 0 return $res{SSL_ca_file};
1756 }
1757  
1758 # for thread safety, we need to know thread id if threads are loaded
1759 0 ▶sub _get_tid {
Subroutine _get_tid: not called CC=2 SCAR=9.2
1760 5 ▶ no warnings 'reserved'; # for 'threads'
Subroutine BEGIN: called CC=1 SCAR=0.0
1761 0 ▶ return threads->can("tid") ? threads->tid : 0;
Branch: 1 branch
BranchTrueFalseTotal
'threads'->can('tid') ? : 0 0 0
1762 }
1763  
1764 0 ▶sub _ssl_args {
Subroutine _ssl_args: not called CC=5 SCAR=34.0
1765 0 my ($self, $host) = @_;
1766  
1767 0 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 0 ▶ if ( Net::SSLeay::OPENSSL_VERSION_NUMBER() >= 0x01000000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Net::SSLeay::OPENSSL_VERSION_NUMBER() >= 16777216) 0 0 0
1772 0 $ssl_args{SSL_hostname} = $host, # Sane SNI support
1773 }
1774  
1775 0 ▶ if ($self->{verify_SSL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'verify_SSL'}) { } 0 0 0
1776 0 $ssl_args{SSL_verifycn_scheme} = 'http'; # enable CN validation
1777 0 $ssl_args{SSL_verifycn_name} = $host; # set validation hostname
1778 0 $ssl_args{SSL_verify_mode} = 0x01; # enable cert validation
1779  
1780 0 %ssl_args = ( %ssl_args, $self->_find_CA );
1781 }
1782 else {
1783 0 $ssl_args{SSL_verifycn_scheme} = 'none'; # disable CN validation
1784 0 $ssl_args{SSL_verify_mode} = 0x00; # disable cert validation
1785 }
1786  
1787 # user options override settings from verify_SSL
1788 0 for my $k ( keys %{$self->{SSL_options}} ) {
1789 0 ▶ $ssl_args{$k} = $self->{SSL_options}{$k} if $k =~ m/^SSL_/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^SSL_/ 0 0 0
1790 }
1791  
1792 0 return \%ssl_args;
1793 }
1794  
1795 5 1;
1796  
1797 __END__
« /usr/local/lib/perl5/5.44.0/base.pm ↑ Summary /usr/local/lib/perl5/5.44.0/x86_64-linux/Compress/Raw/Zlib.pm »