blib/lib/Hijk.pm

filter: Statementstmt 60.5% 115 / 190 Branchbran 43.3% 65 / 150 Conditioncond 37.6% 38 / 101 MC/DCmcdc 27.2% 15 / 55 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 1 totaltotal 47.8% 244 / 510 SCAR 74.6
CC
112
Cov
49%
request
930.0
_construct_socket
156.0
_read_http_message
63.5
CRAP 1733.9
↑ Summary
1 package Hijk;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Socket qw(PF_INET SOCK_STREAM pack_sockaddr_in inet_ntoa $CRLF SOL_SOCKET SO_ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $VERSION = "0.26";
10  
11 sub Hijk::Error::CONNECT_TIMEOUT () { 1 << 0 } # 1
12 sub Hijk::Error::READ_TIMEOUT () { 1 << 1 } # 2
13 sub Hijk::Error::TIMEOUT () { Hijk::Error::READ_TIMEOUT | Hijk::Error::CONNECT_TIMEOUT } # 3
14 sub Hijk::Error::CANNOT_RESOLVE () { 1 << 2 } # 4
15 sub Hijk::Error::REQUEST_SELECT_ERROR () { 1 << 3 } # 8
16 sub Hijk::Error::REQUEST_WRITE_ERROR () { 1 << 4 } # 16
17 sub Hijk::Error::REQUEST_ERROR () { Hijk::Error::REQUEST_SELECT_ERROR | Hijk::Error::REQUEST_WRITE_ERROR } # 24
18 sub Hijk::Error::RESPONSE_READ_ERROR () { 1 << 5 } # 32
19 sub Hijk::Error::RESPONSE_BAD_READ_VALUE () { 1 << 6 } # 64
20 sub Hijk::Error::RESPONSE_ERROR () { Hijk::Error::RESPONSE_READ_ERROR | Hijk::Error::RESPONSE_BAD_READ_VALUE } # 96
21  
22 sub _read_http_message {
23 12 ▶ my ($fd, $read_length, $read_timeout, $parse_chunked, $head_as_array, $method) = @_;
Subroutine _read_http_message: called CC=43 SCAR=41.5
24 12 ▶ $read_timeout = undef if defined($read_timeout) && $read_timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $read_timeout and $read_timeout <= 0 4 8 12
Condition66%2/3
defined $read_timeout and $read_timeout <= 0
!ll&&!rl&&r
804
MC/DC50%1/2
defined $read_timeout and $read_timeout <= 0
defined $read_timeout $read_timeout <= 0
Truth table66%2/3
A and B
A= defined $read_timeoutB= $read_timeout <= 0
ABresult
0X0
100
111
25  
26 12 my ($body,$buf,$decapitated,$nbytes,$proto);
27 12 my $status_code = 0;
28 12 ▶ my $header = $head_as_array ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
$head_as_array ? : 2 10 12
29 12 my $no_content_len = 0;
30 12 my $head = "";
31 18 ▶ my $method_has_no_content = do { no warnings qw(uninitialized); $method eq "HEAD" };
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 my $close_connection;
33 12 vec(my $rin = '', $fd, 1) = 1;
34 12 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
!$decapitated || !$method_has_no_content && ($read_length > 0 || $no_content_len) and do { do { return $close_connection, undef, 0, undef, undef, 2 if _select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0; my $nbytes = POSIX::read($fd, $buf, $read_length); return $close_connection, $proto, $status_code, $header, $body if $no_content_len and $decapitated and !defined($nbytes) || $nbytes == 0; unless (defined $nbytes) { next if $! == 11 or $! == 11 or $! == 4; return $close_connection, undef, 0, undef, undef, 32, 'Failed to read http ' . ($decapitated ? "body" : "head") . ' from socket', $! + 0, "$!"; } ; if ($nbytes == 0) { return $close_connection, undef, 0, undef, undef, 64, q[Wasn't expecting a 0 byte response for http ] . ($decapitated ? "body" : "head") . q[. This shouldn't happen]; } ; if ($decapitated) { $body .= $buf; unless ($no_content_len) { $read_length -= $nbytes; } ; } else { $head .= $buf; my $neck_pos = index($head, "$Hijk::CRLF$Hijk::CRLF"); if ($neck_pos > 0) { $decapitated = 1; $body = substr($head, $neck_pos + 4); $head = substr($head, 0, $neck_pos); $proto = substr($head, 0, 8); $status_code = substr($head, 9, 3); $method_has_no_content = 1 if $status_code == 204; substr($head, 0, index($head, $Hijk::CRLF) + 2, ''); my($doing_chunked, $content_length, $trailer_mode, $trailer_value_is_true); foreach $_ (split(/$Hijk::CRLF/o, $head, 0)) { () = split(/: /, $_, 2); if ($key eq "Transfer-Encoding" and $value eq "chunked") { $doing_chunked = 1; } elsif ($key eq "Content-Length") { $content_length = $value; } elsif ($key eq "Connection" and $value eq "close") { $close_connection = 1; } elsif ($key eq "Trailer" and $value) { $trailer_value_is_true = 1; } ; if ($head_as_array) { push @$header, $key, $value; } else { $header->{$key} = $value; } ; } ; if ($doing_chunked and $trailer_value_is_true) { $trailer_mode = 1; } ; if ($doing_chunked) { die "PANIC: The experimental Hijk support for chunked transfer encoding needs to be explicitly enabled with parse_chunked => 1" unless $parse_chunked; return $close_connection, $proto, $status_code, $header, _read_chunked_body($body, $fd, $read_length, $read_timeout, $head_as_array ? $trailer_mode : ($header->{"Trailer"} ? 1 : 0)); } ; if (defined $content_length) { if ($content_length == 0) { $read_length = 0; } else { $read_length = $content_length - length($body); } ; } else { $read_length = 10204; $no_content_len = 1; } ; } ; } } } 14 2 16
Condition55%5/9
not $decapitated or not $method_has_no_content and $read_length > 0 || $no_content_len
l!l&&r!l&&!r
3013
not $method_has_no_content and $read_length > 0 || $no_content_len
!ll&&!rl&&r
0130
$read_length > 0 or $no_content_len
l!l&&r!l&&!r
1102
MC/DC25%1/4
not $decapitated or not $method_has_no_content and $read_length > 0 || $no_content_len
not $decapitated not $method_has_no_content $read_length > 0 $no_content_len
Truth table40%2/5
not(A) or not(B) and C or D
A= not $decapitatedB= not $method_has_no_contentC= $read_length > 0D= $no_content_len
ABCDresult
1XXX1
011X1
01011
00XX0
01000
35 26 ▶ return ($close_connection,undef,0,undef,undef, Hijk::Error::READ_TIMEOUT)
Branch: 1 branch
BranchTrueFalseTotal
if _select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0 0 26 26
Condition33%2/6
_select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0
l!l&&r!l&&!r
0026
defined $read_timeout and $read_timeout <= 0
!ll&&!rl&&r
2600
MC/DC0%0/3
_select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0
_select($rin, undef, undef, $read_timeout) != 1 defined $read_timeout $read_timeout <= 0
Truth table0%0/4
A or B and C
A= _select($rin, undef, undef, $read_timeout) != 1B= defined $read_timeoutC= $read_timeout <= 0
ABCresult
1XX1
0111
00X0
0100
36 if ((_select($rin, undef, undef, $read_timeout) != 1) || (defined($read_timeout) && $read_timeout <= 0));
37  
38 26 my $nbytes = POSIX::read($fd, $buf, $read_length);
39 26 ▶ return ($close_connection, $proto, $status_code, $header, $body)
Branch: 1 branch
BranchTrueFalseTotal
if $no_content_len and $decapitated and !defined($nbytes) || $nbytes == 0 2 24 26
Condition77%7/9
$no_content_len and $decapitated and !defined($nbytes) || $nbytes == 0
!ll&&!rl&&r
1592
$no_content_len and $decapitated
!ll&&!rl&&r
15011
not defined $nbytes or $nbytes == 0
l!l&&r!l&&!r
029
MC/DC50%2/4
$no_content_len and $decapitated and !defined($nbytes) || $nbytes == 0
$no_content_len $decapitated not defined $nbytes $nbytes == 0
Truth table60%3/5
A and B and not(C) or D
A= $no_content_lenB= $decapitatedC= not defined $nbytesD= $nbytes == 0
ABCDresult
0XXX0
10XX0
11000
111X1
11011
40 if $no_content_len && $decapitated && (!defined($nbytes) || $nbytes == 0);
41 24 ▶ if (!defined($nbytes)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 24 24
42 0 ▶ next if ($! == EWOULDBLOCK || $! == EAGAIN || $! == EINTR);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 or $! == 4 0 0 0
Condition0%0/6
$! == 11 or $! == 11 or $! == 4
l!l&&r!l&&!r
000
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/3
$! == 11 or $! == 11 or $! == 4
$! == 11 $! == 11 $! == 4
Truth table0%0/4
A or B or C
A= $! == 11B= $! == 11C= $! == 4
ABCresult
1XX1
01X1
0011
0000
43 return (
44 0 ▶ $close_connection, undef, 0, undef, undef,
Branch: 1 branch
BranchTrueFalseTotal
$decapitated ? : 0 0 0
45 Hijk::Error::RESPONSE_READ_ERROR,
46 "Failed to read http " . ($decapitated ? "body": "head") . " from socket",
47 $!+0,
48 "$!",
49 );
50 }
51  
52 24 ▶ if ($nbytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes == 0) 5 19 24
53 return (
54 5 ▶ $close_connection, undef, 0, undef, undef,
Branch: 1 branch
BranchTrueFalseTotal
$decapitated ? : 0 5 5
55 Hijk::Error::RESPONSE_BAD_READ_VALUE,
56 "Wasn't expecting a 0 byte response for http " . ($decapitated ? "body": "head" ) . ". This shouldn't happen",
57 );
58 }
59  
60 19 ▶ if ($decapitated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decapitated) { } 9 10 19
61 9 $body .= $buf;
62 9 ▶ if (!$no_content_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_content_len) 0 9 9
63 0 $read_length -= $nbytes;
64 }
65 }
66 else {
67 10 $head .= $buf;
68 10 my $neck_pos = index($head, "${CRLF}${CRLF}");
69 10 ▶ if ($neck_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neck_pos > 0) 7 3 10
70 7 $decapitated = 1;
71 7 $body = substr($head, $neck_pos+4);
72 7 $head = substr($head, 0, $neck_pos);
73 7 $proto = substr($head, 0, 8);
74 7 $status_code = substr($head, 9, 3);
75 7 ▶ $method_has_no_content = 1 if $status_code == 204; # 204 NO CONTENT, see http://tools.ietf.org/html/rfc2616#page-60
Branch: 1 branch
BranchTrueFalseTotal
if $status_code == 204 0 7 7
76 7 substr($head, 0, index($head, $CRLF) + 2, ""); # 2 = length($CRLF)
77  
78 7 my ($doing_chunked, $content_length, $trailer_mode, $trailer_value_is_true);
79 7 for (split /${CRLF}/o, $head) {
80 45 my ($key, $value) = split /: /, $_, 2;
81  
82 # Figure this out now so we don't need to scan the
83 # list later under $head_as_array, and just for
84 # simplicity and to avoid duplicating code later
85 # when !$head_as_array.
86 45 ▶ if ($key eq 'Transfer-Encoding' and $value eq 'chunked') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'Transfer-Encoding' and $value eq 'chunked') { } 3 42 45
elsif ($key eq 'Content-Length') { } 2 40 42
elsif ($key eq 'Connection' and $value eq 'close') { } 2 38 40
elsif ($key eq 'Trailer' and $value) { } 2 72 74
Condition66%2/3
$key eq "Transfer-Encoding" and $value eq "chunked"
!ll&&!rl&&r
4203
MC/DC50%1/2
$key eq "Transfer-Encoding" and $value eq "chunked"
$key eq "Transfer-Encoding" $value eq "chunked"
Truth table66%2/3
A and B
A= $key eq "Transfer-Encoding"B= $value eq "chunked"
ABresult
0X0
100
111
87 3 $doing_chunked = 1;
88 } elsif ($key eq 'Content-Length') {
89 2 $content_length = $value;
90 ▶ } elsif ($key eq 'Connection' and $value eq 'close') {
Condition66%2/3
$key eq "Connection" and $value eq "close"
!ll&&!rl&&r
3802
MC/DC50%1/2
$key eq "Connection" and $value eq "close"
$key eq "Connection" $value eq "close"
Truth table66%2/3
A and B
A= $key eq "Connection"B= $value eq "close"
ABresult
0X0
100
111
91 2 $close_connection = 1;
92 ▶ } elsif ($key eq 'Trailer' and $value) {
Condition66%2/3
$key eq "Trailer" and $value
!ll&&!rl&&r
3602
MC/DC50%1/2
$key eq "Trailer" and $value
$key eq "Trailer" $value
Truth table66%2/3
A and B
A= $key eq "Trailer"B= $value
ABresult
0X0
100
111
93 2 $trailer_value_is_true = 1;
94 }
95  
96 45 ▶ if ($head_as_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_as_array) { } 13 32 45
97 13 push @$header => $key, $value;
98 } else {
99 32 $header->{$key} = $value;
100 }
101 }
102  
103 # We're processing the headers as a stream, and we
104 # only want to turn on $trailer_mode if
105 # Transfer-Encoding=chunked && Trailer=TRUE. However I
106 # don't think there's any guarantee that
107 # Transfer-Encoding comes before Trailer, so we're
108 # effectively doing a second-pass here.
109 7 ▶ if ($doing_chunked and $trailer_value_is_true) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doing_chunked and $trailer_value_is_true) 2 5 7
Condition100%3/3
$doing_chunked and $trailer_value_is_true
!ll&&!rl&&r
412
MC/DC100%2/2
$doing_chunked and $trailer_value_is_true
$doing_chunked $trailer_value_is_true
Truth table100%3/3
A and B
A= $doing_chunkedB= $trailer_value_is_true
ABresult
0X0
100
111
110 2 $trailer_mode = 1;
111 }
112  
113 7 ▶ if ($doing_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doing_chunked) 3 4 7
114 3 ▶ die "PANIC: The experimental Hijk support for chunked transfer encoding needs to be explicitly enabled with parse_chunked => 1"
Branch: 1 branch
BranchTrueFalseTotal
unless $parse_chunked 0 3 3
115 unless $parse_chunked;
116  
117 # if there is chunked encoding we have to ignore content length even if we have it
118 return (
119 $close_connection, $proto, $status_code, $header,
120 _read_chunked_body(
121 $body, $fd, $read_length, $read_timeout,
122 $head_as_array
123 ? $trailer_mode
124 3 ▶ : ($header->{Trailer} ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
$head_as_array ? : 1 2 3
$header->{'Trailer'} ? : 1 1 2
125 ),
126 );
127 }
128  
129 4 ▶ if (defined $content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_length) { } 2 2 4
130 2 ▶ if ($content_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length == 0) { } 0 2 2
131 0 $read_length = 0;
132 } else {
133 2 $read_length = $content_length - length($body);
134 }
135 } else {
136 2 $read_length = 10204;
137 2 $no_content_len = 1;
138 }
139 }
140 }
141 } while( !$decapitated || (!$method_has_no_content && ($read_length > 0 || $no_content_len)) );
142 2 return ($close_connection, $proto, $status_code, $header, $body);
143 }
144  
145 sub _read_chunked_body {
146 3 ▶ my ($buf,$fd,$read_length,$read_timeout,$true_trailer_header) = @_;
Subroutine _read_chunked_body: called CC=18 SCAR=39.8
147 3 my $chunk_size = 0;
148 3 my $body = "";
149 3 my $trailer_mode = 0;
150  
151 3 vec(my $rin = '', $fd, 1) = 1;
152 3 while(1) {
153 # just read a 10k block and process it until it is consumed
154 23 ▶ if (length($buf) == 0 || length($buf) < $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf == 0 or length $buf < $chunk_size) 0 23 23
Condition33%1/3
length $buf == 0 or length $buf < $chunk_size
l!l&&r!l&&!r
0023
MC/DC0%0/2
length $buf == 0 or length $buf < $chunk_size
length $buf == 0 length $buf < $chunk_size
Truth table33%1/3
A or B
A= length $buf == 0B= length $buf < $chunk_size
ABresult
1X1
011
000
155 0 ▶ return (undef, Hijk::Error::READ_TIMEOUT)
Branch: 1 branch
BranchTrueFalseTotal
if _select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0 0 0 0
Condition0%0/6
_select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0
l!l&&r!l&&!r
000
defined $read_timeout and $read_timeout <= 0
!ll&&!rl&&r
000
MC/DC0%0/3
_select($rin, undef, undef, $read_timeout) != 1 or defined $read_timeout and $read_timeout <= 0
_select($rin, undef, undef, $read_timeout) != 1 defined $read_timeout $read_timeout <= 0
Truth table0%0/4
A or B and C
A= _select($rin, undef, undef, $read_timeout) != 1B= defined $read_timeoutC= $read_timeout <= 0
ABCresult
1XX1
0111
00X0
0100
156 if ((_select($rin, undef, undef, $read_timeout) != 1) || (defined($read_timeout) && $read_timeout <= 0));
157 0 my $current_buf = "";
158 0 my $nbytes = POSIX::read($fd, $current_buf, $read_length);
159 0 ▶ if (!defined($nbytes)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 0 0
160 0 ▶ next if ($! == EWOULDBLOCK || $! == EAGAIN || $! == EINTR);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 or $! == 4 0 0 0
Condition0%0/6
$! == 11 or $! == 11 or $! == 4
l!l&&r!l&&!r
000
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/3
$! == 11 or $! == 11 or $! == 4
$! == 11 $! == 11 $! == 4
Truth table0%0/4
A or B or C
A= $! == 11B= $! == 11C= $! == 4
ABCresult
1XX1
01X1
0011
0000
161 return (
162 undef,
163 0 Hijk::Error::RESPONSE_READ_ERROR,
164 "Failed to chunked http body from socket",
165 $!+0,
166 "$!",
167 );
168 }
169  
170 0 ▶ if ($nbytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes == 0) 0 0 0
171 return (
172 undef,
173 0 Hijk::Error::RESPONSE_BAD_READ_VALUE,
174 "Wasn't expecting a 0 byte response for chunked http body. This shouldn't happen, buf:<$buf>, current_buf:<$current_buf>",
175 );
176 }
177  
178 0 $buf .= $current_buf;
179 }
180 23 ▶ if ($trailer_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer_mode) { } 2 21 23
181 # http://tools.ietf.org/html/rfc2616#section-14.40
182 # http://tools.ietf.org/html/rfc2616#section-3.6.1
183 # A server using chunked transfer-coding in a response MUST NOT use the
184 # trailer for any header fields unless at least one of the following is
185 # true:
186  
187 # a)the request included a TE header field that indicates "trailers" is
188 # acceptable in the transfer-coding of the response, as described in
189 # section 14.39; or,
190  
191 # b)the server is the origin server for the response, the trailer
192 # fields consist entirely of optional metadata, and the recipient
193 # could use the message (in a manner acceptable to the origin server)
194 # without receiving this metadata. In other words, the origin server
195 # is willing to accept the possibility that the trailer fields might
196 # be silently discarded along the path to the client.
197  
198 # in case of trailer mode, we just read everything until the next CRLFCRLF
199 2 my $neck_pos = index($buf, "${CRLF}${CRLF}");
200 2 ▶ if ($neck_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neck_pos > 0) 2 0 2
201 2 return $body;
202 }
203 } else {
204 21 ▶ if ($chunk_size > 0 && length($buf) >= $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_size > 0 and length $buf >= $chunk_size) { } 9 12 21
Condition66%2/3
$chunk_size > 0 and length $buf >= $chunk_size
!ll&&!rl&&r
1209
MC/DC50%1/2
$chunk_size > 0 and length $buf >= $chunk_size
$chunk_size > 0 length $buf >= $chunk_size
Truth table66%2/3
A and B
A= $chunk_size > 0B= length $buf >= $chunk_size
ABresult
0X0
100
111
205 9 $body .= substr($buf, 0, $chunk_size - 2); # our chunk size includes the CRLF
206 9 $buf = substr($buf, $chunk_size);
207 9 $chunk_size = 0;
208 } else {
209 12 my $neck_pos = index($buf, ${CRLF});
210 12 ▶ if ($neck_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neck_pos > 0) 12 0 12
211 12 $chunk_size = hex(substr($buf, 0, $neck_pos));
212 12 ▶ if ($chunk_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_size == 0) { } 3 9 12
213 3 ▶ if ($true_trailer_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($true_trailer_header) { } 2 1 3
214 2 $trailer_mode = 1;
215 } else {
216 1 return $body;
217 }
218 } else {
219 9 $chunk_size += 2; # include the final CRLF
220 9 $buf = substr($buf, $neck_pos + 2);
221 }
222 }
223 }
224 }
225 }
226 }
227  
228 sub _construct_socket {
229 0 ▶ my ($host, $port, $connect_timeout) = @_;
Subroutine _construct_socket: not called CC=12 SCAR=50.5
230  
231 # If we can't find the IP address there'll be no point in even
232 # setting up a socket.
233 0 my $addr;
234 {
235 0 my $inet_aton = gethostbyname($host);
236 0 ▶ return (undef, {error => Hijk::Error::CANNOT_RESOLVE}) unless defined $inet_aton;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inet_aton 0 0 0
237 0 $addr = pack_sockaddr_in($port, $inet_aton);
238 }
239  
240 0 my $tcp_proto = getprotobyname("tcp");
241 0 my $soc;
242 0 ▶ socket($soc, PF_INET, SOCK_STREAM, $tcp_proto) || die "Failed to construct TCP socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket $soc, 2, 1, $tcp_proto or die "Failed to construct TCP socket: $!" 0 0 0
243 0 ▶ my $flags = fcntl($soc, F_GETFL, 0) or die "Failed to set fcntl F_GETFL flag: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($soc, 3, 0) or die "Failed to set fcntl F_GETFL flag: $!" 0 0 0
244 0 ▶ fcntl($soc, F_SETFL, $flags | O_NONBLOCK) or die "Failed to set fcntl O_NONBLOCK flag: $!";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $soc, 4, $flags | 2048 or die "Failed to set fcntl O_NONBLOCK flag: $!" 0 0 0
245  
246 0 ▶ if (!connect($soc, $addr) && $! != EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (not connect($soc, $addr) and $! != 115) 0 0 0
Condition0%0/3
not connect($soc, $addr) and $! != 115
!ll&&!rl&&r
000
MC/DC0%0/2
not connect($soc, $addr) and $! != 115
not connect($soc, $addr) $! != 115
Truth table0%0/3
not(A) and B
A= not connect($soc, $addr)B= $! != 115
ABresult
0X0
100
111
247 0 die "Failed to connect $!";
248 }
249  
250 0 ▶ $connect_timeout = undef if defined($connect_timeout) && $connect_timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $connect_timeout and $connect_timeout <= 0 0 0 0
Condition0%0/3
defined $connect_timeout and $connect_timeout <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $connect_timeout and $connect_timeout <= 0
defined $connect_timeout $connect_timeout <= 0
Truth table0%0/3
A and B
A= defined $connect_timeoutB= $connect_timeout <= 0
ABresult
0X0
100
111
251 0 vec(my $rout = '', fileno($soc), 1) = 1;
252 0 ▶ if (_select(undef, $rout, undef, $connect_timeout) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_select(undef, $rout, undef, $connect_timeout) != 1) 0 0 0
253 0 ▶ if (defined($connect_timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connect_timeout) { } 0 0 0
254 0 return (undef, {error => Hijk::Error::CONNECT_TIMEOUT});
255 } else {
256 return (
257 undef,
258 {
259 0 error => Hijk::Error::REQUEST_SELECT_ERROR,
260 error_message => "select() error on constructing the socket",
261 errno_number => $!+0,
262 errno_string => "$!",
263 },
264 );
265 }
266 }
267  
268 0 ▶ if ($! = unpack("L", getsockopt($soc, SOL_SOCKET, SO_ERROR))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! = unpack("L", getsockopt($soc, 1, 4))) 0 0 0
269 0 die $!;
270 }
271  
272 0 return $soc;
273 }
274  
275 sub _build_http_message {
276 36 ▶ my $args = $_[0];
Subroutine _build_http_message: called CC=10 SCAR=23.0
277 36 ▶ my $path_and_qs = ($args->{path} || "/") . ( defined($args->{query_string}) ? ("?".$args->{query_string}) : "" );
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'query_string'} ? : 4 32 36
Condition100%2/2
$args->{'path'} || '/'
l!l
432
MC/DC100%1/1
$args->{'path'} || '/'
$args->{'path'}
Truth table100%2/2
A
A= $args->{'path'}
Aresult
11
00
278 return join(
279 $CRLF,
280 ($args->{method} || "GET")." $path_and_qs " . ($args->{protocol} || "HTTP/1.1"),
281 ($args->{no_default_host_header}
282 ? ()
283 : ("Host: $args->{host}")),
284 defined($args->{body}) ? ("Content-Length: " . length($args->{body})) : (),
285 12 ($args->{head} and @{$args->{head}}) ? (
286 map {
287 14 $args->{head}[2*$_] . ": " . $args->{head}[2*$_+1]
288 10 ▶ } 0..$#{$args->{head}}/2
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'body'} ? : 8 28 36
289 ) : (),
290 ""
291 36 ▶ ) . $CRLF . (defined($args->{body}) ? $args->{body} : "");
Branch: 3 branches
BranchTrueFalseTotal
$args->{'no_default_host_header'} ? : 8 28 36
defined $args->{'body'} ? : 8 28 36
$args->{'head'} && @{$args->{'head'};} ? : 10 26 36
Condition85%6/7
$args->{'method'} || 'GET'
l!l
234
$args->{'protocol'} || 'HTTP/1.1'
l!l
360
$args->{"head"} and @{$args->{"head"};}
!ll&&!rl&&r
24210
MC/DC100%1/1
$args->{'method'} || 'GET'
$args->{'method'}
MC/DC0%0/1
$args->{'protocol'} || 'HTTP/1.1'
$args->{'protocol'}
MC/DC100%2/2
$args->{"head"} and @{$args->{"head"};}
$args->{"head"} @{$args->{"head"};}
Truth table100%2/2
A
A= $args->{'method'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'protocol'}
Aresult
11
00
Truth table100%3/3
A and B
A= $args->{"head"}B= @{$args->{"head"};}
ABresult
0X0
100
111
292 }
293  
294 18 our $SOCKET_CACHE = {};
295  
296 sub request {
297 0 ▶ my $args = $_[0];
Subroutine request: not called CC=30 SCAR=68.4
Pod undocumented
298  
299 # Backwards compatibility for code that provided the old timeout
300 # argument.
301 0 ▶ $args->{connect_timeout} = $args->{read_timeout} = $args->{timeout} if exists $args->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"timeout"} 0 0 0
302  
303 # Ditto for providing a default socket cache, allow for setting it
304 # to "socket_cache => undef" to disable the cache.
305 0 ▶ $args->{socket_cache} = $SOCKET_CACHE unless exists $args->{socket_cache};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"socket_cache"} 0 0 0
306  
307 # Provide a default for the read_length option
308 0 ▶ $args->{read_length} = 10 * 2 ** 10 unless exists $args->{read_length};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"read_length"} 0 0 0
309  
310 # Use $; so we can use the $socket_cache->{$$, $host, $port}
311 # idiom to access the cache.
312 0 ▶ my $cache_key; $cache_key = join($;, $$, @$args{qw(host port)}) if defined $args->{socket_cache};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"socket_cache"} 0 0 0
313  
314 0 my $soc;
315 0 ▶ if (defined $cache_key and exists $args->{socket_cache}->{$cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_key and exists $args->{'socket_cache'}{$cache_key}) { } 0 0 0
Condition0%0/3
defined $cache_key and exists $args->{"socket_cache"}{$cache_key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cache_key and exists $args->{"socket_cache"}{$cache_key}
defined $cache_key exists $args->{"socket_cache"}{$cache_key}
Truth table0%0/3
A and B
A= defined $cache_keyB= exists $args->{"socket_cache"}{$cache_key}
ABresult
0X0
100
111
316 0 $soc = $args->{socket_cache}->{$cache_key};
317 } else {
318 0 ($soc, my $error) = _construct_socket(@$args{qw(host port connect_timeout)});
319 0 ▶ return $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
320 0 ▶ $args->{socket_cache}->{$cache_key} = $soc if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
321 0 ▶ $args->{on_connect}->() if exists $args->{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"on_connect"} 0 0 0
322 }
323  
324 0 my $r = _build_http_message($args);
325 0 my $total = length($r);
326 0 my $left = $total;
327  
328 0 vec(my $rout = '', fileno($soc), 1) = 1;
329 0 ▶ while ($left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 0 0 0
330 0 ▶ if (_select(undef, $rout, undef, undef) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_select(undef, $rout, undef, undef) != 1) 0 0 0
331 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
332 return {
333 0 error => Hijk::Error::REQUEST_SELECT_ERROR,
334 error_message => "Got error on select() before the write() when while writing the HTTP request the socket",
335 errno_number => $!+0,
336 errno_string => "$!",
337 };
338 }
339  
340 0 my $rc = syswrite($soc,$r,$left, $total - $left);
341 0 ▶ if (!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
342 0 ▶ next if ($! == EWOULDBLOCK || $! == EAGAIN || $! == EINTR);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 or $! == 4 0 0 0
Condition0%0/6
$! == 11 or $! == 11 or $! == 4
l!l&&r!l&&!r
000
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/3
$! == 11 or $! == 11 or $! == 4
$! == 11 $! == 11 $! == 4
Truth table0%0/4
A or B or C
A= $! == 11B= $! == 11C= $! == 4
ABCresult
1XX1
01X1
0011
0000
343 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
344 0 shutdown($soc, 2);
345 return {
346 0 error => Hijk::Error::REQUEST_WRITE_ERROR,
347 error_message => "Got error trying to write the HTTP request with write() to the socket",
348 errno_number => $!+0,
349 errno_string => "$!",
350 };
351 }
352 0 $left -= $rc;
353 }
354  
355 0 my ($close_connection,$proto,$status,$head,$body,$error,$error_message,$errno_number,$errno_string);
356 eval {
357 ($close_connection,$proto,$status,$head,$body,$error,$error_message,$errno_number,$errno_string) =
358 0 _read_http_message(fileno($soc), @$args{qw(read_length read_timeout parse_chunked head_as_array method)});
359 0 1;
360 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { () = _read_http_message(fileno $soc, @$args{"read_length", "read_timeout", "parse_chunked", "head_as_array", "method"}); 1 } } or do { my $err = $@ || "zombie error"; delete $args->{"socket_cache"}{$cache_key} if defined $cache_key; shutdown $soc, 2; die $err } 0 0 0
361 0 ▶ my $err = $@ || "zombie error";
Condition0%0/2
$@ || 'zombie error'
l!l
00
MC/DC0%0/1
$@ || 'zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
362 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
363 0 shutdown($soc, 2);
364 0 die $err;
365 };
366  
367 0 ▶ if ($status == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0 or $close_connection or defined $proto and $proto eq "HTTP/1.0") 0 0 0
Condition0%0/9
$status == 0 or $close_connection or defined $proto and $proto eq "HTTP/1.0"
l!l&&r!l&&!r
000
$status == 0 or $close_connection
l!l&&r!l&&!r
000
defined $proto and $proto eq "HTTP/1.0"
!ll&&!rl&&r
000
MC/DC0%0/4
$status == 0 or $close_connection or defined $proto and $proto eq "HTTP/1.0"
$status == 0 $close_connection defined $proto $proto eq "HTTP/1.0"
Truth table0%0/5
A or B or C and D
A= $status == 0B= $close_connectionC= defined $protoD= $proto eq "HTTP/1.0"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
368 # We always close connections for 1.0 because some servers LIE
369 # and say that they're 1.0 but don't close the connection on
370 # us! An example of this. Test::HTTP::Server (used by the
371 # ShardedKV::Storage::Rest tests) is an example of such a
372 # server. In either case we can't cache a connection for a 1.0
373 # server anyway, so BEGONE!
374 or $close_connection
375 or (defined $proto and $proto eq 'HTTP/1.0')) {
376 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
377 0 shutdown($soc, 2);
378 }
379 return {
380 0 ▶ proto => $proto,
Branch: 4 branches
BranchTrueFalseTotal
defined $error ? : 0 0 0
defined $error_message ? : 0 0 0
defined $errno_number ? : 0 0 0
defined $errno_string ? : 0 0 0
381 status => $status,
382 head => $head,
383 body => $body,
384 defined($error) ? ( error => $error ) : (),
385 defined($error_message) ? ( error_message => $error_message ) : (),
386 defined($errno_number) ? ( errno_number => $errno_number ) : (),
387 defined($errno_string) ? ( errno_string => $errno_string ) : (),
388 };
389 }
390  
391 sub _select {
392 27 ▶ my ($rbits, $wbits, $ebits, $timeout) = @_;
Subroutine _select: called CC=4 SCAR=14.0
393 27 while (1) {
394 37 my $start = Time::HiRes::time();
395 37 my $nfound = select($rbits, $wbits, $ebits, $timeout);
396 37 ▶ if ($nfound == -1 && $! == EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1 and $! == 4) 10 27 37
Condition66%2/3
$nfound == -1 and $! == 4
!ll&&!rl&&r
27010
MC/DC50%1/2
$nfound == -1 and $! == 4
$nfound == -1 $! == 4
Truth table66%2/3
A and B
A= $nfound == -1B= $! == 4
ABresult
0X0
100
111
397 10 ▶ $timeout -= Time::HiRes::time() - $start if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 10 0 10
398 10 next;
399 }
400 27 return $nfound;
401 }
402 }
403  
404 18 1;
405  
406 __END__
↑ Summary