blib/lib/Hijk.pm

filter: Statementstmt 60.3% 120 / 199 Branchbran 42.4% 67 / 158 Conditioncond 38.3% 41 / 107 MC/DCmcdc 27.5% 16 / 58 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 1 totaltotal 47.5% 255 / 536 CC 118 SCAR 55.9
CC
118
Cov
49%
request
930.0
_construct_socket
156.0
_read_chunked_body
93.5
CRAP 268.7
↑ 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.27";
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=44 SCAR=41.8
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); my $key_lc = lc $key; if ($key_lc eq "transfer-encoding" and $value eq "chunked") { $doing_chunked = 1; } elsif ($key_lc eq "content-length" or lc $key eq "content-length") { $content_length = $value; } elsif ($key_lc eq "connection" and $value eq "close") { $close_connection = 1; } elsif ($key_lc 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 45 my $key_lc = lc($key);
82  
83 # Figure this out now so we don't need to scan the
84 # list later under $head_as_array, and just for
85 # simplicity and to avoid duplicating code later
86 # when !$head_as_array.
87 45 ▶ if ($key_lc eq 'transfer-encoding' and $value eq 'chunked') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key_lc eq 'transfer-encoding' and $value eq 'chunked') { } 3 42 45
elsif ($key_lc eq 'content-length' or lc $key eq 'content-length') { } 2 40 42
elsif ($key_lc eq 'connection' and $value eq 'close') { } 2 38 40
elsif ($key_lc eq 'trailer' and $value) { } 2 72 74
Condition66%2/3
$key_lc eq "transfer-encoding" and $value eq "chunked"
!ll&&!rl&&r
4203
MC/DC50%1/2
$key_lc eq "transfer-encoding" and $value eq "chunked"
$key_lc eq "transfer-encoding" $value eq "chunked"
Truth table66%2/3
A and B
A= $key_lc eq "transfer-encoding"B= $value eq "chunked"
ABresult
0X0
100
111
88 3 $doing_chunked = 1;
89 ▶ } elsif ( ($key_lc eq 'content-length') || (lc($key) eq 'content-length') ) {
Condition66%2/3
$key_lc eq "content-length" or lc $key eq "content-length"
l!l&&r!l&&!r
2040
MC/DC50%1/2
$key_lc eq "content-length" or lc $key eq "content-length"
$key_lc eq "content-length" lc $key eq "content-length"
Truth table66%2/3
A or B
A= $key_lc eq "content-length"B= lc $key eq "content-length"
ABresult
1X1
011
000
90 2 $content_length = $value;
91 ▶ } elsif ($key_lc eq 'connection' and $value eq 'close') {
Condition66%2/3
$key_lc eq "connection" and $value eq "close"
!ll&&!rl&&r
3802
MC/DC50%1/2
$key_lc eq "connection" and $value eq "close"
$key_lc eq "connection" $value eq "close"
Truth table66%2/3
A and B
A= $key_lc eq "connection"B= $value eq "close"
ABresult
0X0
100
111
92 2 $close_connection = 1;
93 ▶ } elsif ($key_lc eq 'trailer' and $value) {
Condition66%2/3
$key_lc eq "trailer" and $value
!ll&&!rl&&r
3602
MC/DC50%1/2
$key_lc eq "trailer" and $value
$key_lc eq "trailer" $value
Truth table66%2/3
A and B
A= $key_lc eq "trailer"B= $value
ABresult
0X0
100
111
94 2 $trailer_value_is_true = 1;
95 }
96  
97 45 ▶ if ($head_as_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_as_array) { } 13 32 45
98 13 push @$header => $key, $value;
99 } else {
100 32 $header->{$key} = $value;
101 }
102 }
103  
104 # We're processing the headers as a stream, and we
105 # only want to turn on $trailer_mode if
106 # Transfer-Encoding=chunked && Trailer=TRUE. However I
107 # don't think there's any guarantee that
108 # Transfer-Encoding comes before Trailer, so we're
109 # effectively doing a second-pass here.
110 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
111 2 $trailer_mode = 1;
112 }
113  
114 7 ▶ if ($doing_chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doing_chunked) 3 4 7
115 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
116 unless $parse_chunked;
117  
118 # if there is chunked encoding we have to ignore content length even if we have it
119 return (
120 $close_connection, $proto, $status_code, $header,
121 _read_chunked_body(
122 $body, $fd, $read_length, $read_timeout,
123 $head_as_array
124 ? $trailer_mode
125 3 ▶ : ($header->{Trailer} ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
$head_as_array ? : 1 2 3
$header->{'Trailer'} ? : 1 1 2
126 ),
127 );
128 }
129  
130 4 ▶ if (defined $content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_length) { } 2 2 4
131 2 ▶ if ($content_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length == 0) { } 0 2 2
132 0 $read_length = 0;
133 } else {
134 2 $read_length = $content_length - length($body);
135 }
136 } else {
137 2 $read_length = 10204;
138 2 $no_content_len = 1;
139 }
140 }
141 }
142 } while( !$decapitated || (!$method_has_no_content && ($read_length > 0 || $no_content_len)) );
143 2 return ($close_connection, $proto, $status_code, $header, $body);
144 }
145  
146 sub _read_chunked_body {
147 3 ▶ my ($buf,$fd,$read_length,$read_timeout,$true_trailer_header) = @_;
Subroutine _read_chunked_body: called CC=23 SCAR=45.4
148 3 my $chunk_size = 0;
149 3 my $body = "";
150 3 my $trailer_mode = 0;
151 3 my $wait_for_last_clrf = 0;
152 3 vec(my $rin = '', $fd, 1) = 1;
153 3 while(1) {
154 # just read a 10k block and process it until it is consumed
155 23 ▶ if (length($buf) < 3 || length($buf) < $chunk_size || $wait_for_last_clrf > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < 3 or length $buf < $chunk_size or $wait_for_last_clrf > 0) 0 23 23
Condition33%2/6
length $buf < 3 or length $buf < $chunk_size or $wait_for_last_clrf > 0
l!l&&r!l&&!r
0023
length $buf < 3 or length $buf < $chunk_size
l!l&&r!l&&!r
0023
MC/DC0%0/3
length $buf < 3 or length $buf < $chunk_size or $wait_for_last_clrf > 0
length $buf < 3 length $buf < $chunk_size $wait_for_last_clrf > 0
Truth table25%1/4
A or B or C
A= length $buf < 3B= length $buf < $chunk_sizeC= $wait_for_last_clrf > 0
ABCresult
1XX1
01X1
0011
0000
156 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
157 if ((_select($rin, undef, undef, $read_timeout) != 1) || (defined($read_timeout) && $read_timeout <= 0));
158 0 my $current_buf = "";
159 0 my $nbytes = POSIX::read($fd, $current_buf, $read_length);
160 0 ▶ if (!defined($nbytes)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 0 0
161 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
162 return (
163 undef,
164 0 Hijk::Error::RESPONSE_READ_ERROR,
165 "Failed to chunked http body from socket",
166 $!+0,
167 "$!",
168 );
169 }
170  
171 0 ▶ if ($nbytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nbytes == 0) 0 0 0
172 return (
173 undef,
174 0 Hijk::Error::RESPONSE_BAD_READ_VALUE,
175 "Wasn't expecting a 0 byte response for chunked http body. This shouldn't happen, buf:<$buf>, current_buf:<$current_buf>",
176 );
177 }
178  
179 0 $buf .= $current_buf;
180 }
181  
182 23 ▶ if ($wait_for_last_clrf > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait_for_last_clrf > 0) 0 23 23
183 0 $wait_for_last_clrf -= length($buf);
184 0 ▶ return $body if ($wait_for_last_clrf <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $wait_for_last_clrf <= 0 0 0 0
185 }
186  
187 23 ▶ if ($trailer_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer_mode) { } 2 21 23
188 # http://tools.ietf.org/html/rfc2616#section-14.40
189 # http://tools.ietf.org/html/rfc2616#section-3.6.1
190 # A server using chunked transfer-coding in a response MUST NOT use the
191 # trailer for any header fields unless at least one of the following is
192 # true:
193  
194 # a)the request included a TE header field that indicates "trailers" is
195 # acceptable in the transfer-coding of the response, as described in
196 # section 14.39; or,
197  
198 # b)the server is the origin server for the response, the trailer
199 # fields consist entirely of optional metadata, and the recipient
200 # could use the message (in a manner acceptable to the origin server)
201 # without receiving this metadata. In other words, the origin server
202 # is willing to accept the possibility that the trailer fields might
203 # be silently discarded along the path to the client.
204  
205 # in case of trailer mode, we just read everything until the next CRLFCRLF
206 2 my $neck_pos = index($buf, "${CRLF}${CRLF}");
207 2 ▶ if ($neck_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neck_pos > 0) 2 0 2
208 2 return $body;
209 }
210 } else {
211 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
212 9 $body .= substr($buf, 0, $chunk_size - 2); # our chunk size includes the following CRLF
213 9 $buf = substr($buf, $chunk_size);
214 9 $chunk_size = 0;
215 } else {
216 12 my $neck_pos = index($buf, ${CRLF});
217 12 ▶ if ($neck_pos > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($neck_pos > 0) { } 12 0 12
elsif ($neck_pos == 0) { } 0 0 0
218 12 $chunk_size = hex(substr($buf, 0, $neck_pos));
219 12 ▶ if ($chunk_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_size == 0) { } 3 9 12
220 3 ▶ if ($true_trailer_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($true_trailer_header) { } 2 1 3
221 2 $trailer_mode = 1;
222 } else {
223 1 $buf = substr($buf, $neck_pos + 2);
224 # in case we are missing the ending CLRF, we have to wait for it
225 # otherwise it is left int he socket
226 1 ▶ if (length($buf) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < 2) { } 0 1 1
227 0 $wait_for_last_clrf = 2 - length($buf);
228 } else {
229 1 return $body;
230 }
231 }
232 } else {
233 9 $chunk_size += 2; # include the following CRLF
234 9 $buf = substr($buf, $neck_pos + 2);
235 }
236 } elsif($neck_pos == 0) {
237 return (
238 undef,
239 0 Hijk::Error::RESPONSE_BAD_READ_VALUE,
240 "Wasn't expecting CLRF without chunk size. This shouldn't happen, buf:<$buf>",
241 );
242 }
243 }
244 }
245 }
246 }
247  
248 sub _construct_socket {
249 0 ▶ my ($host, $port, $connect_timeout) = @_;
Subroutine _construct_socket: not called CC=12 SCAR=50.5
250  
251 # If we can't find the IP address there'll be no point in even
252 # setting up a socket.
253 0 my $addr;
254 {
255 0 my $inet_aton = gethostbyname($host);
256 0 ▶ return (undef, {error => Hijk::Error::CANNOT_RESOLVE}) unless defined $inet_aton;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inet_aton 0 0 0
257 0 $addr = pack_sockaddr_in($port, $inet_aton);
258 }
259  
260 0 my $tcp_proto = getprotobyname("tcp");
261 0 my $soc;
262 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
263 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
264 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
265  
266 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
267 0 die "Failed to connect $!";
268 }
269  
270 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
271 0 vec(my $rout = '', fileno($soc), 1) = 1;
272 0 ▶ if (_select(undef, $rout, undef, $connect_timeout) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_select(undef, $rout, undef, $connect_timeout) != 1) 0 0 0
273 0 ▶ if (defined($connect_timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connect_timeout) { } 0 0 0
274 0 return (undef, {error => Hijk::Error::CONNECT_TIMEOUT});
275 } else {
276 return (
277 undef,
278 {
279 0 error => Hijk::Error::REQUEST_SELECT_ERROR,
280 error_message => "select() error on constructing the socket",
281 errno_number => $!+0,
282 errno_string => "$!",
283 },
284 );
285 }
286 }
287  
288 0 ▶ if ($! = unpack("L", getsockopt($soc, SOL_SOCKET, SO_ERROR))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! = unpack("L", getsockopt($soc, 1, 4))) 0 0 0
289 0 die $!;
290 }
291  
292 0 return $soc;
293 }
294  
295 sub _build_http_message {
296 36 ▶ my $args = $_[0];
Subroutine _build_http_message: called CC=10 SCAR=23.0
297 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
298 return join(
299 $CRLF,
300 ($args->{method} || "GET")." $path_and_qs " . ($args->{protocol} || "HTTP/1.1"),
301 ($args->{no_default_host_header}
302 ? ()
303 : ("Host: $args->{host}")),
304 defined($args->{body}) ? ("Content-Length: " . length($args->{body})) : (),
305 12 ($args->{head} and @{$args->{head}}) ? (
306 map {
307 14 $args->{head}[2*$_] . ": " . $args->{head}[2*$_+1]
308 10 ▶ } 0..$#{$args->{head}}/2
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'body'} ? : 8 28 36
309 ) : (),
310 ""
311 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
312 }
313  
314 18 our $SOCKET_CACHE = {};
315  
316 sub request {
317 0 ▶ my $args = $_[0];
Subroutine request: not called CC=30 SCAR=68.4
Pod undocumented
318  
319 # Backwards compatibility for code that provided the old timeout
320 # argument.
321 0 ▶ $args->{connect_timeout} = $args->{read_timeout} = $args->{timeout} if exists $args->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"timeout"} 0 0 0
322  
323 # Ditto for providing a default socket cache, allow for setting it
324 # to "socket_cache => undef" to disable the cache.
325 0 ▶ $args->{socket_cache} = $SOCKET_CACHE unless exists $args->{socket_cache};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"socket_cache"} 0 0 0
326  
327 # Provide a default for the read_length option
328 0 ▶ $args->{read_length} = 10 * 2 ** 10 unless exists $args->{read_length};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"read_length"} 0 0 0
329  
330 # Use $; so we can use the $socket_cache->{$$, $host, $port}
331 # idiom to access the cache.
332 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
333  
334 0 my $soc;
335 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
336 0 $soc = $args->{socket_cache}->{$cache_key};
337 } else {
338 0 ($soc, my $error) = _construct_socket(@$args{qw(host port connect_timeout)});
339 0 ▶ return $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
340 0 ▶ $args->{socket_cache}->{$cache_key} = $soc if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
341 0 ▶ $args->{on_connect}->() if exists $args->{on_connect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"on_connect"} 0 0 0
342 }
343  
344 0 my $r = _build_http_message($args);
345 0 my $total = length($r);
346 0 my $left = $total;
347  
348 0 vec(my $rout = '', fileno($soc), 1) = 1;
349 0 ▶ while ($left > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > 0) 0 0 0
350 0 ▶ if (_select(undef, $rout, undef, undef) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (_select(undef, $rout, undef, undef) != 1) 0 0 0
351 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
352 return {
353 0 error => Hijk::Error::REQUEST_SELECT_ERROR,
354 error_message => "Got error on select() before the write() when while writing the HTTP request the socket",
355 errno_number => $!+0,
356 errno_string => "$!",
357 };
358 }
359  
360 0 my $rc = syswrite($soc,$r,$left, $total - $left);
361 0 ▶ if (!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
362 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
363 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
364 0 shutdown($soc, 2);
365 return {
366 0 error => Hijk::Error::REQUEST_WRITE_ERROR,
367 error_message => "Got error trying to write the HTTP request with write() to the socket",
368 errno_number => $!+0,
369 errno_string => "$!",
370 };
371 }
372 0 $left -= $rc;
373 }
374  
375 0 my ($close_connection,$proto,$status,$head,$body,$error,$error_message,$errno_number,$errno_string);
376 eval {
377 ($close_connection,$proto,$status,$head,$body,$error,$error_message,$errno_number,$errno_string) =
378 0 _read_http_message(fileno($soc), @$args{qw(read_length read_timeout parse_chunked head_as_array method)});
379 0 1;
380 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
381 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
382 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
383 0 shutdown($soc, 2);
384 0 die $err;
385 };
386  
387 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
388 # We always close connections for 1.0 because some servers LIE
389 # and say that they're 1.0 but don't close the connection on
390 # us! An example of this. Test::HTTP::Server (used by the
391 # ShardedKV::Storage::Rest tests) is an example of such a
392 # server. In either case we can't cache a connection for a 1.0
393 # server anyway, so BEGONE!
394 or $close_connection
395 or (defined $proto and $proto eq 'HTTP/1.0')) {
396 0 ▶ delete $args->{socket_cache}->{$cache_key} if defined $cache_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache_key 0 0 0
397 0 shutdown($soc, 2);
398 }
399 return {
400 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
401 status => $status,
402 head => $head,
403 body => $body,
404 defined($error) ? ( error => $error ) : (),
405 defined($error_message) ? ( error_message => $error_message ) : (),
406 defined($errno_number) ? ( errno_number => $errno_number ) : (),
407 defined($errno_string) ? ( errno_string => $errno_string ) : (),
408 };
409 }
410  
411 sub _select {
412 27 ▶ my ($rbits, $wbits, $ebits, $timeout) = @_;
Subroutine _select: called CC=4 SCAR=14.0
413 27 while (1) {
414 37 my $start = Time::HiRes::time();
415 37 my $nfound = select($rbits, $wbits, $ebits, $timeout);
416 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
417 10 ▶ $timeout -= Time::HiRes::time() - $start if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 10 0 10
418 10 next;
419 }
420 27 return $nfound;
421 }
422 }
423  
424 18 1;
425  
426 __END__
↑ Summary