blib/lib/Starlet/Server.pm

filter: Statementstmt 71.8% 273 / 380 Branchbran 43.1% 94 / 218 Conditioncond 43.6% 48 / 110 MC/DCmcdc 18.0% 11 / 61 Subroutinesub 80.4% 37 / 46 Podpod 0.0% 0 / 9 totaltotal 56.1% 463 / 824 CC 152 SCAR 51.9
CC
152
Cov
59%
handle_connection
490.1
_handle_response
370.1
__ANON__
42.0
CRAP 179.4
↑ Summary blib/lib/Plack/Handler/Starlet.pm »
1 package Starlet::Server;
2 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 55 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Plack;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Plack::HTTPParser qw( parse_http_request );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use List::Util qw(max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 55 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use Plack::TempBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use POSIX qw(EINTR EAGAIN EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 55 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 55 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 55 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 55 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 55 ▶use constant MAX_REQUEST_SIZE => 131072;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 55 ▶use constant CHUNKSIZE => 64 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 55 ▶use constant MAX_CHUNK_LINE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 55 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 55 my $null_io = do { open my $io, "<", \""; $io };
28  
29 55 ▶sub new {
Subroutine new: called CC=18 SCAR=32.5
Pod undocumented
30 55 my($class, %args) = @_;
31  
32 my $self = bless {
33 listens => $args{listens} || [],
34 host => $args{host} || 0,
35 port => $args{port} || $args{socket} || 8080,
36 timeout => $args{timeout} || 300,
37 keepalive_timeout => $args{keepalive_timeout} || 2,
38 max_keepalive_reqs => $args{max_keepalive_reqs} || 1,
39 server_software => $args{server_software} || $class,
40 39 ▶ server_ready => $args{server_ready} || sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
41 min_reqs_per_child => (
42 defined $args{min_reqs_per_child}
43 ? $args{min_reqs_per_child} : undef,
44 ),
45 max_reqs_per_child => (
46 $args{max_reqs_per_child} || $args{max_requests} || 100,
47 ),
48 spawn_interval => $args{spawn_interval} || 0,
49 err_respawn_interval => (
50 defined $args{err_respawn_interval}
51 ? $args{err_respawn_interval} : undef,
52 ),
53 is_multiprocess => Plack::Util::FALSE,
54 15 ▶ child_exit => $args{child_exit} || sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
55 55 ▶ _using_defer_accept => undef,
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'min_reqs_per_child'} ? : 1 54 55
defined $args{'err_respawn_interval'} ? : 0 55 55
Condition74%23/31
$args{'listens'} || []
l!l
1639
$args{'host'} || 0
l!l
2728
$args{'port'} || $args{'socket'} || 8080
l!l
541
$args{"port"} or $args{"socket"}
l!l&&r!l&&!r
5311
$args{'timeout'} || 300
l!l
055
$args{'keepalive_timeout'} || 2
l!l
055
$args{'max_keepalive_reqs'} || 1
l!l
055
$args{'server_software'} || $class
l!l&&r!l&&!r
0550
$args{'server_ready'} || sub { }
l!l&&r!l&&!r
15400
$args{'max_reqs_per_child'} || $args{'max_requests'} || 100
l!l
154
$args{"max_reqs_per_child"} or $args{"max_requests"}
l!l&&r!l&&!r
1054
$args{'spawn_interval'} || 0
l!l
1144
$args{'child_exit'} || sub { }
l!l&&r!l&&!r
2530
MC/DC100%1/1
$args{'listens'} || []
$args{'listens'}
MC/DC100%1/1
$args{'host'} || 0
$args{'host'}
MC/DC100%2/2
$args{'port'} || $args{'socket'} || 8080
$args{"port"} $args{"socket"}
MC/DC0%0/1
$args{'timeout'} || 300
$args{'timeout'}
MC/DC0%0/1
$args{'keepalive_timeout'} || 2
$args{'keepalive_timeout'}
MC/DC0%0/1
$args{'max_keepalive_reqs'} || 1
$args{'max_keepalive_reqs'}
MC/DC0%0/2
$args{'server_software'} || $class
$args{'server_software'} $class
MC/DC0%0/2
$args{'server_ready'} || sub { }
$args{'server_ready'} sub { }
MC/DC50%1/2
$args{'max_reqs_per_child'} || $args{'max_requests'} || 100
$args{"max_reqs_per_child"} $args{"max_requests"}
MC/DC100%1/1
$args{'spawn_interval'} || 0
$args{'spawn_interval'}
MC/DC0%0/2
$args{'child_exit'} || sub { }
$args{'child_exit'} sub { }
Truth table100%2/2
A
A= $args{'listens'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'host'}
Aresult
11
00
Truth table100%3/3
A or B
A= $args{"port"}B= $args{"socket"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'keepalive_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'max_keepalive_reqs'}
Aresult
11
00
Truth table33%1/3
A || B
A= $args{'server_software'}B= $class
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $args{'server_ready'}B= sub { }
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $args{"max_reqs_per_child"}B= $args{"max_requests"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{'spawn_interval'}
Aresult
11
00
Truth table66%2/3
A || B
A= $args{'child_exit'}B= sub { }
ABresult
1X1
011
000
56 }, $class;
57  
58 55 ▶ if ($args{max_workers} && $args{max_workers} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"max_workers"} and $args{"max_workers"} > 1) 0 55 55
Condition33%1/3
$args{"max_workers"} and $args{"max_workers"} > 1
!ll&&!rl&&r
5500
MC/DC0%0/2
$args{"max_workers"} and $args{"max_workers"} > 1
$args{"max_workers"} $args{"max_workers"} > 1
Truth table33%1/3
A and B
A= $args{"max_workers"}B= $args{"max_workers"} > 1
ABresult
0X0
100
111
59 0 Carp::carp(
60 "Preforking in $class is deprecated. Falling back to the non-forking mode. ",
61 "If you need preforking, use Starman or Starlet instead and run like `plackup -s Starlet`",
62 );
63 }
64  
65 55 $self;
66 }
67  
68 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my($self, $app) = @_;
70 0 $self->setup_listener();
71 0 $self->accept_loop($app);
72 }
73  
74 54 ▶sub setup_listener {
Subroutine setup_listener: called CC=11 SCAR=24.3
Pod undocumented
75 54 my $self = shift;
76 54 ▶ if (scalar(grep {defined $_} @{$self->{listens}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({defined $_;} @{$$self{"listens"};}) == 0) 38 16 54
77 38 my $sock;
78 38 ▶ if ($self->{port} =~ /^[0-9]+$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'port'} =~ /^[0-9]+$/s) { } 37 1 38
79 $sock = IO::Socket::INET->new(
80 Listen => SOMAXCONN,
81 LocalPort => $self->{port},
82 LocalAddr => $self->{host},
83 37 ▶ Proto => 'tcp',
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::INET"->new("Listen", 4096, "LocalPort", $self->{"port"}, "LocalAddr", $self->{"host"}, "Proto", "tcp", "ReuseAddr", 1) or die qq[failed to listen to port $self->{"port"}:$!] 37 0 37
84 ReuseAddr => 1,
85 ) or die "failed to listen to port $self->{port}:$!";
86 } else {
87 $sock = IO::Socket::UNIX->new(
88 Listen => SOMAXCONN,
89 Local => $self->{port},
90 1 ▶ ) or die "failed to listen to socket $self->{port}:$!";
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::UNIX"->new("Listen", 4096, "Local", $self->{"port"}) or die qq[failed to listen to socket $self->{"port"}:$!] 1 0 1
91 }
92 $self->{listens}[fileno($sock)] = {
93 host => $self->{host},
94 port => $self->{port},
95 38 sock => $sock,
96 };
97 }
98  
99 54 my @listens = grep {defined $_} @{$self->{listens}};
100 54 for my $listen (@listens) {
101 72 my $family = Socket::sockaddr_family(getsockname($listen->{sock}));
102 72 $listen->{_is_tcp} = $family != AF_UNIX;
103  
104 # set defer accept
105 72 ▶ if ($^O eq 'linux' && $listen->{_is_tcp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux" and $listen->{"_is_tcp"}) 58 14 72
Condition66%2/3
$^O eq "linux" and $listen->{"_is_tcp"}
!ll&&!rl&&r
01458
MC/DC50%1/2
$^O eq "linux" and $listen->{"_is_tcp"}
$^O eq "linux" $listen->{"_is_tcp"}
Truth table66%2/3
A and B
A= $^O eq "linux"B= $listen->{"_is_tcp"}
ABresult
0X0
100
111
106 setsockopt($listen->{sock}, IPPROTO_TCP, 9, 1)
107 58 ▶ and $listen->{_using_defer_accept} = 1;
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $listen->{"sock"}, 6, 9, 1 and $listen->{"_using_defer_accept"} = 1 58 0 58
108 }
109 }
110  
111 54 ▶ if (scalar(@listens) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @listens > 1) 3 51 54
112 3 ▶ $self->{lock_path} ||= do {
Condition33%1/3
$self->{"lock_path"} ||= do { () = tempfile("UNLINK", 1); close $fh; $lock_path }
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{"lock_path"} ||= do { () = tempfile("UNLINK", 1); close $fh; $lock_path }
$self->{"lock_path"} do { () = tempfile("UNLINK", 1); close $fh; $lock_path }
Truth table33%1/3
A ||= B
A= $self->{"lock_path"}B= do { () = tempfile("UNLINK", 1); close $fh; $lock_path }
ABresult
1X1
011
000
113 3 my ($fh, $lock_path) = tempfile(UNLINK => 1);
114 # closing the file handle explicitly for two reasons
115 # 1) tempfile retains the handle when UNLINK is set
116 # 2) tempfile implicitely locks the file on OS X
117 3 close $fh;
118 3 $lock_path;
119 };
120 }
121  
122 54 $self->{server_ready}->($self);
123 }
124  
125 31 ▶sub accept_loop {
Subroutine accept_loop: called CC=16 SCAR=29.1
Pod undocumented
126 # TODO handle $max_reqs_per_child
127 31 my($self, $app, $max_reqs_per_child) = @_;
128 31 my $proc_req_count = 0;
129 31 my $is_keepalive = 0;
130  
131 16 ▶ local $SIG{TERM} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 16 $self->{term_received} = 1;
133 31 };
134 31 local $SIG{PIPE} = 'IGNORE';
135  
136 31 my $acceptor = $self->_get_acceptor;
137  
138 31 ▶ while (! defined $max_reqs_per_child || $proc_req_count < $max_reqs_per_child) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max_reqs_per_child or $proc_req_count < $max_reqs_per_child) 62 0 62
Condition33%1/3
not defined $max_reqs_per_child or $proc_req_count < $max_reqs_per_child
l!l&&r!l&&!r
0620
MC/DC0%0/2
not defined $max_reqs_per_child or $proc_req_count < $max_reqs_per_child
not defined $max_reqs_per_child $proc_req_count < $max_reqs_per_child
Truth table33%1/3
not(A) or B
A= not defined $max_reqs_per_childB= $proc_req_count < $max_reqs_per_child
ABresult
1X1
011
000
139 # accept (or exit on SIGTERM)
140 62 ▶ if ($self->{term_received}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"term_received"}) 16 46 62
141 16 $self->{child_exit}->($self, $app);
142 16 exit 0;
143 }
144 46 my ($conn, $peer, $listen) = $acceptor->();
145 46 ▶ next unless $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn 16 30 46
146  
147 30 $self->{_is_deferred_accept} = $listen->{_using_defer_accept};
148 30 ▶ defined($conn->blocking(0))
Branch: 1 branch
BranchTrueFalseTotal
defined $conn->blocking(0) or die "failed to set socket to nonblocking mode:$!" 30 0 30
149 or die "failed to set socket to nonblocking mode:$!";
150 30 my ($peerport, $peerhost, $peeraddr) = (0, undef, undef);
151 30 ▶ if ($listen->{_is_tcp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listen->{"_is_tcp"}) 26 4 30
152 26 ▶ $conn->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
Branch: 1 branch
BranchTrueFalseTotal
$conn->setsockopt(6, 1, 1) or die "setsockopt(TCP_NODELAY) failed:$!" 26 0 26
153 or die "setsockopt(TCP_NODELAY) failed:$!";
154 26 ($peerport, $peerhost) = unpack_sockaddr_in $peer;
155 26 $peeraddr = inet_ntoa($peerhost);
156 }
157 30 my $req_count = 0;
158 30 my $pipelined_buf = '';
159  
160 30 while (1) {
161 30 ++$req_count;
162 30 ++$proc_req_count;
163 my $env = {
164 SERVER_PORT => $listen->{port} || 0,
165 SERVER_NAME => $listen->{host} || 0,
166 SCRIPT_NAME => '',
167 REMOTE_ADDR => $peeraddr,
168 REMOTE_PORT => $peerport,
169 'psgi.version' => [ 1, 1 ],
170 'psgi.errors' => *STDERR,
171 'psgi.url_scheme' => 'http',
172 'psgi.run_once' => Plack::Util::FALSE,
173 'psgi.multithread' => Plack::Util::FALSE,
174 'psgi.multiprocess' => $self->{is_multiprocess},
175 'psgi.streaming' => Plack::Util::TRUE,
176 'psgi.nonblocking' => Plack::Util::FALSE,
177 'psgix.input.buffered' => Plack::Util::TRUE,
178 'psgix.io' => $conn,
179 'psgix.harakiri' => 1,
180 0 ▶ 'psgix.informational' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 $self->_informational($conn, @_);
182 },
183 30 ▶ };
Condition75%3/4
$listen->{'port'} || 0
l!l
300
$listen->{'host'} || 0
l!l
1911
MC/DC0%0/1
$listen->{'port'} || 0
$listen->{'port'}
MC/DC100%1/1
$listen->{'host'} || 0
$listen->{'host'}
Truth table50%1/2
A
A= $listen->{'port'}
Aresult
11
00
Truth table100%2/2
A
A= $listen->{'host'}
Aresult
11
00
184  
185 30 my $may_keepalive = $req_count < $self->{max_keepalive_reqs};
186 30 ▶ if ($may_keepalive && $max_reqs_per_child && $proc_req_count >= $max_reqs_per_child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($may_keepalive and $max_reqs_per_child and $proc_req_count >= $max_reqs_per_child) 0 30 30
Condition33%2/6
$may_keepalive and $max_reqs_per_child and $proc_req_count >= $max_reqs_per_child
!ll&&!rl&&r
3000
$may_keepalive and $max_reqs_per_child
!ll&&!rl&&r
3000
MC/DC0%0/3
$may_keepalive and $max_reqs_per_child and $proc_req_count >= $max_reqs_per_child
$may_keepalive $max_reqs_per_child $proc_req_count >= $max_reqs_per_child
Truth table25%1/4
A and B and C
A= $may_keepaliveB= $max_reqs_per_childC= $proc_req_count >= $max_reqs_per_child
ABCresult
0XX0
10X0
1100
1111
187 0 $may_keepalive = undef;
188 }
189 30 ▶ $may_keepalive = 1 if length $pipelined_buf;
Branch: 1 branch
BranchTrueFalseTotal
if length $pipelined_buf 0 30 30
190 30 my $keepalive;
191 30 ($keepalive, $pipelined_buf) = $self->handle_connection($env, $conn, $app,
192 $may_keepalive, $req_count != 1, $pipelined_buf);
193  
194 29 ▶ if ($env->{'psgix.harakiri.commit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.harakiri.commit"}) 14 15 29
195 14 $conn->close;
196 14 return;
197 }
198 15 ▶ last unless $keepalive;
Branch: 1 branch
BranchTrueFalseTotal
unless $keepalive 15 0 15
199 # TODO add special cases for clients with broken keep-alive support, as well as disabling keep-alive for HTTP/1.0 proxies
200 }
201 15 $conn->close;
202 }
203 }
204  
205 31 ▶sub _get_acceptor {
Subroutine _get_acceptor: called CC=5 SCAR=16.2
206 31 my $self = shift;
207 31 my @listens = grep {defined $_} @{$self->{listens}};
208  
209 31 ▶ if (scalar(@listens) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @listens == 1) { } 29 2 31
210 29 my $listen = $listens[0];
211 37 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
212 37 ▶ if (my ($conn, $peer) = $listen->{sock}->accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $listen->{"sock"}->accept) 23 14 37
213 23 return ($conn, $peer, $listen);
214 }
215 14 return +();
216 29 };
217 }
218 else {
219 # wait for multiple sockets with select(2)
220 2 my @fds;
221 2 my $rin = '';
222 2 for my $listen (@listens) {
223 14 ▶ defined($listen->{sock}->blocking(0))
Branch: 1 branch
BranchTrueFalseTotal
defined $listen->{"sock"}->blocking(0) or die "failed to set listening socket to non-blocking mode:$!" 14 0 14
224 or die "failed to set listening socket to non-blocking mode:$!";
225 14 my $fd = fileno($listen->{sock});
226 14 push @fds, $fd;
227 14 vec($rin, $fd, 1) = 1;
228 }
229  
230 open(my $lock_fh, '>', $self->{lock_path})
231 0 ▶ or die "failed to open lock file:@{[$self->{lock_path}]}:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $lock_fh, ">", $self->{"lock_path"} or die qq[failed to open lock file:@{[$self->{"lock_path"}];}:$!] 2 0 2
232  
233 9 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
234 9 ▶ if (! flock($lock_fh, LOCK_EX)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $lock_fh, 2) 1 8 9
235 0 ▶ die "failed to lock file:@{[$self->{lock_path}]}:$!"
Branch: 1 branch
BranchTrueFalseTotal
if $! != 4 0 1 1
236 if $! != EINTR;
237 1 return +();
238 }
239 8 my $nfound = select(my $rout = $rin, undef, undef, undef);
240 8 ▶ for (my $i = 0; $nfound > 0; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound > 0) 28 1 29
241 28 my $fd = $fds[$i];
242 28 ▶ next unless vec($rout, $fd, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $rout, $fd, 1 21 7 28
243 7 --$nfound;
244 7 my $listen = $self->{listens}[$fd];
245 7 ▶ if (my ($conn, $peer) = $listen->{sock}->accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $listen->{"sock"}->accept) 7 0 7
246 7 flock($lock_fh, LOCK_UN);
247 7 return ($conn, $peer, $listen);
248 }
249 }
250 1 flock($lock_fh, LOCK_UN);
251 1 return +();
252 2 };
253 }
254 }
255  
256 55 my $bad_response = [ 400, [ 'Content-Type' => 'text/plain', 'Connection' => 'close' ], [ 'Bad Request' ] ];
257 30 ▶sub handle_connection {
Subroutine handle_connection: called CC=41 SCAR=61.9
Pod undocumented
258 30 my($self, $env, $conn, $app, $use_keepalive, $is_keepalive, $prebuf) = @_;
259
260 30 my $buf = '';
261 30 my $method;
262 30 my $pipelined_buf='';
263 30 my $res = $bad_response;
264
265 30 REQUEST: while (1) {
266 30 my $rlen;
267 30 ▶ if ( $rlen = length $prebuf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rlen = length $prebuf) { } 0 30 30
268 0 $buf = $prebuf;
269 0 undef $prebuf;
270 }
271 else {
272 $rlen = $self->read_timeout(
273 $conn, \$buf, MAX_REQUEST_SIZE - length($buf), length($buf),
274 $is_keepalive ? $self->{keepalive_timeout} : $self->{timeout},
275 30 ▶ ) or return;
Branch: 2 branches
BranchTrueFalseTotal
$rlen = $self->read_timeout($conn, \$buf, 131072 - length($buf), length $buf, $is_keepalive ? $self->{"keepalive_timeout"} : $self->{"timeout"}) or return 26 4 30
$is_keepalive ? : 0 30 30
276 }
277 26 my $reqlen = parse_http_request($buf, $env);
278 26 ▶ if ($reqlen >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqlen >= 0) 26 0 26
279 # handle request
280 26 $method = $env->{REQUEST_METHOD};
281 26 my $protocol = $env->{SERVER_PROTOCOL};
282 26 ▶ if ($use_keepalive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_keepalive) 0 26 26
283 0 ▶ if ($self->{term_received}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'term_received'}) { } 0 0 0
elsif ($protocol eq 'HTTP/1.1') { } 0 0 0
284 0 $use_keepalive = undef;
285 } elsif ( $protocol eq 'HTTP/1.1' ) {
286 0 ▶ if (my $c = $env->{HTTP_CONNECTION}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $env->{"HTTP_CONNECTION"}) 0 0 0
287 0 ▶ $use_keepalive = undef
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^\s*close\s*/i 0 0 0
288 if $c =~ /^\s*close\s*/i;
289 }
290 } else {
291 0 ▶ if (my $c = $env->{HTTP_CONNECTION}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $env->{'HTTP_CONNECTION'}) { } 0 0 0
292 0 ▶ $use_keepalive = undef
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /^\s*keep-alive\s*/i 0 0 0
293 unless $c =~ /^\s*keep-alive\s*/i;
294 } else {
295 0 $use_keepalive = undef;
296 }
297 }
298 }
299 26 $buf = substr $buf, $reqlen;
300 55 ▶ my $chunked = do { no warnings; lc delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' };
Subroutine BEGIN: called CC=1 SCAR=0.0
301  
302 # If a message is received with both a Transfer-Encoding and a
303 # Content-Length header field, the Transfer-Encoding overrides the
304 # Content-Length. Such a message might indicate an attempt to
305 # perform request smuggling (Section 9.5) or response splitting
306 # (Section 9.4) and ought to be handled as an error. A sender MUST
307 # remove the received Content-Length field prior to forwarding such
308 # a message downstream.
309 26 ▶ if ($chunked && $env->{CONTENT_LENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked and $env->{"CONTENT_LENGTH"}) 0 26 26
Condition33%1/3
$chunked and $env->{"CONTENT_LENGTH"}
!ll&&!rl&&r
2600
MC/DC0%0/2
$chunked and $env->{"CONTENT_LENGTH"}
$chunked $env->{"CONTENT_LENGTH"}
Truth table33%1/3
A and B
A= $chunkedB= $env->{"CONTENT_LENGTH"}
ABresult
0X0
100
111
310 0 delete $env->{CONTENT_LENGTH};
311 }
312  
313 26 ▶ if ( $env->{HTTP_EXPECT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_EXPECT"}) 0 26 26
314 0 ▶ if ( lc $env->{HTTP_EXPECT} eq '100-continue' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $env->{'HTTP_EXPECT'} eq '100-continue') { } 0 0 0
315 0 ▶ $self->write_all($conn, "HTTP/1.1 100 Continue\015\012\015\012")
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($conn, "HTTP/1.1 100 Continue\r\n\r\n") or return 0 0 0
316 or return;
317 } else {
318 0 $res = [417,[ 'Content-Type' => 'text/plain', 'Connection' => 'close' ], [ 'Expectation Failed' ] ];
319 0 last;
320 }
321 }
322  
323 26 ▶ if (my $cl = $env->{CONTENT_LENGTH}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cl = $env->{'CONTENT_LENGTH'}) { } 3 23 26
elsif ($chunked) { } 0 23 23
324 3 ▶ if ($cl !~ /^[0-9]+$/) { # content-length header must be digits.
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl =~ /^[0-9]+$/) 0 3 3
325 0 last; # Return bad response
326 }
327  
328 3 my $buffer = Plack::TempBuffer->new($cl);
329 3 ▶ while ($cl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl > 0) 4 1 5
330 4 my $chunk;
331 4 ▶ if (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 2 2 4
332 2 $chunk = $buf;
333 2 $buf = '';
334 } else {
335 $self->read_timeout(
336 $conn, \$chunk, $cl < CHUNKSIZE ? $cl : CHUNKSIZE, 0, $self->{timeout})
337 2 ▶ or return;
Branch: 2 branches
BranchTrueFalseTotal
$self->read_timeout($conn, \$chunk, $cl < 65536 ? $cl : 65536, 0, $self->{"timeout"}) or return 0 2 2
$cl < 65536 ? : 1 1 2
338 }
339 2 $buffer->print($chunk);
340 2 $cl -= length $chunk;
341 }
342 1 $env->{'psgi.input'} = $buffer->rewind;
343 }
344 elsif ($chunked) {
345 0 my $buffer = Plack::TempBuffer->new;
346 0 my $chunk_buffer = '';
347 0 my $length;
348 0 my $chunk_left = 0; # bytes of the current chunk still to come, CRLF included
349 0 DECHUNK: while(1) {
350 0 my $chunk;
351 0 ▶ if ( length $buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 0 0 0
352 0 $chunk = $buf;
353 0 $buf = '';
354 }
355 else {
356 $self->read_timeout($conn, \$chunk, CHUNKSIZE, 0, $self->{timeout})
357 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->read_timeout($conn, \$chunk, 65536, 0, $self->{"timeout"}) or return 0 0 0
358 }
359  
360 0 $chunk_buffer .= $chunk;
361 0 while (1) {
362 0 ▶ if ($chunk_left > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_left > 2) 0 0 0
363 0 my $n = $chunk_left - 2;
364 0 ▶ $n = length $chunk_buffer if length $chunk_buffer < $n;
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk_buffer < $n 0 0 0
365 0 ▶ last unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
366 0 $buffer->print(substr $chunk_buffer, 0, $n, '');
367 0 $length += $n;
368 0 $chunk_left -= $n;
369 0 next;
370 }
371 0 ▶ if ($chunk_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_left) 0 0 0
372 0 ▶ last if length $chunk_buffer < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk_buffer < 2 0 0 0
373 0 $chunk_buffer =~ s/^\015\012//;
374 0 $chunk_left = 0;
375 }
376 # atomic hex run: a size line that cannot match fails in linear time
377 0 ▶ $chunk_buffer =~ s/^((?>([0-9a-fA-F]+))[^\012]*\015\012)//
Branch: 1 branch
BranchTrueFalseTotal
$chunk_buffer =~ s/^((?>([0-9a-fA-F]+))[^\012]*\015\012)// or last 0 0 0
378 or last;
379 0 ▶ last REQUEST if length $1 > MAX_CHUNK_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 > 4096 0 0 0
380 0 my $chunk_len = hex $2;
381 0 ▶ last DECHUNK if $chunk_len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_len == 0 0 0 0
382 0 $chunk_left = $chunk_len + 2;
383 }
384 0 ▶ last REQUEST if !$chunk_left && length $chunk_buffer > MAX_CHUNK_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if not $chunk_left and length $chunk_buffer > 4096 0 0 0
Condition0%0/3
not $chunk_left and length $chunk_buffer > 4096
!ll&&!rl&&r
000
MC/DC0%0/2
not $chunk_left and length $chunk_buffer > 4096
not $chunk_left length $chunk_buffer > 4096
Truth table0%0/3
not(A) and B
A= not $chunk_leftB= length $chunk_buffer > 4096
ABresult
0X0
100
111
385 }
386 0 $env->{CONTENT_LENGTH} = $length;
387 0 $env->{'psgi.input'} = $buffer->rewind;
388 } else {
389 23 ▶ if ( $buf =~ m!^(?:GET|HEAD)! ) { #pipeline
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^(?:GET|HEAD)/) 0 23 23
390 0 $pipelined_buf = $buf;
391 0 $use_keepalive = 1; #force keepalive
392 } # else clear buffer
393 23 $env->{'psgi.input'} = $null_io;
394 }
395  
396 24 $res = Plack::Util::run_app $app, $env;
397 23 last;
398 }
399 0 ▶ if ($reqlen == -2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reqlen == -2) { } 0 0 0
elsif ($reqlen == -1) { } 0 0 0
400 # request is incomplete, do nothing
401 } elsif ($reqlen == -1) {
402 # error, close conn
403 0 last;
404 }
405 }
406  
407 23 ▶ if (ref $res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 23 0 23
elsif (ref $res eq 'CODE') { } 0 0 0
408 23 $self->_handle_response($env->{SERVER_PROTOCOL}, $res, $conn, \$use_keepalive, $method);
409 } elsif (ref $res eq 'CODE') {
410 0 ▶ $res->(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
411 0 $self->_handle_response($env->{SERVER_PROTOCOL}, $_[0], $conn, \$use_keepalive, $method);
412 0 });
413 } else {
414 0 die "Bad response $res";
415 }
416
417 23 return ($use_keepalive, $pipelined_buf);
418 }
419  
420 0 ▶sub _informational {
Subroutine _informational: not called CC=2 SCAR=17.9
421 0 my ($self, $conn, $status_code, $headers) = @_;
422  
423 0 my @lines = "HTTP/1.1 $status_code @{[ HTTP::Status::status_message($status_code) ]}\015\012";
424 0 ▶ for (my $i = 0; $i < @$headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 0 0 0
425 0 my $k = $headers->[$i];
426 0 my $v = $headers->[$i + 1];
427 0 push @lines, "$k: $v\015\012";
428 }
429 0 push @lines, "\015\012";
430  
431 0 $self->write_all($conn, join("", @lines), $self->{timeout});
432 }
433  
434 23 ▶sub _handle_response {
Subroutine _handle_response: called CC=37 SCAR=59.1
435 23 my($self, $protocol, $res, $conn, $use_keepalive_r, $method) = @_;
436 23 my $status_code = $res->[0];
437 23 my $headers = $res->[1];
438 23 my $body = $res->[2];
439 23 ▶ my $no_body = (defined $method && $method eq 'HEAD')
Condition33%3/9
defined $method && $method eq 'HEAD' || $status_code == 204 || $status_code == 304
l!l&&r!l&&!r
0023
defined $method and $method eq "HEAD" or $status_code == 204
l!l&&r!l&&!r
0023
defined $method and $method eq "HEAD"
!ll&&!rl&&r
0230
MC/DC0%0/4
defined $method && $method eq 'HEAD' || $status_code == 204 || $status_code == 304
defined $method $method eq "HEAD" $status_code == 204 $status_code == 304
Truth table14%1/7
A and B or C || D
A= defined $methodB= $method eq "HEAD"C= $status_code == 204D= $status_code == 304
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
440 || $status_code == 204 || $status_code == 304;
441
442 23 my @lines;
443 my %send_headers;
444 23 ▶ for (my $i = 0; $i < @$headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 23 23 46
445 23 my $k = $headers->[$i];
446 23 my $v = $headers->[$i + 1];
447 23 my $lck = lc $k;
448 23 ▶ if ($lck eq 'connection') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lck eq 'connection') { } 0 23 23
449 0 ▶ $$use_keepalive_r = undef
Branch: 1 branch
BranchTrueFalseTotal
if $$use_keepalive_r and lc $v ne "keep-alive" 0 0 0
Condition0%0/3
$$use_keepalive_r and lc $v ne "keep-alive"
!ll&&!rl&&r
000
MC/DC0%0/2
$$use_keepalive_r and lc $v ne "keep-alive"
$$use_keepalive_r lc $v ne "keep-alive"
Truth table0%0/3
A and B
A= $$use_keepalive_rB= lc $v ne "keep-alive"
ABresult
0X0
100
111
450 if $$use_keepalive_r && lc $v ne 'keep-alive';
451 } else {
452 23 push @lines, "$k: $v\015\012";
453 23 $send_headers{$lck} = $v;
454 }
455 }
456 23 ▶ if ( ! exists $send_headers{server} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $send_headers{"server"}) 23 0 23
457 23 unshift @lines, "Server: $self->{server_software}\015\012";
458 }
459 23 ▶ if ( ! exists $send_headers{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $send_headers{"date"}) 23 0 23
460 23 unshift @lines, "Date: @{[HTTP::Date::time2str()]}\015\012";
461 }
462  
463 # try to set content-length when keepalive can be used, or disable it
464 23 my $use_chunked;
465 23 ▶ if (defined($protocol) && $protocol eq 'HTTP/1.1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $protocol and $protocol eq 'HTTP/1.1') { } 15 8 23
Condition66%2/3
defined $protocol and $protocol eq "HTTP/1.1"
!ll&&!rl&&r
0815
MC/DC50%1/2
defined $protocol and $protocol eq "HTTP/1.1"
defined $protocol $protocol eq "HTTP/1.1"
Truth table66%2/3
A and B
A= defined $protocolB= $protocol eq "HTTP/1.1"
ABresult
0X0
100
111
466 15 ▶ if (defined $send_headers{'content-length'}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $send_headers{'content-length'} or defined $send_headers{'transfer-encoding'}) { } 0 15 15
elsif (not $no_body || Plack::Util::status_with_no_entity_body($status_code)) { } 15 0 15
Condition33%1/3
defined $send_headers{"content-length"} or defined $send_headers{"transfer-encoding"}
l!l&&r!l&&!r
0015
MC/DC0%0/2
defined $send_headers{"content-length"} or defined $send_headers{"transfer-encoding"}
defined $send_headers{"content-length"} defined $send_headers{"transfer-encoding"}
Truth table33%1/3
A or B
A= defined $send_headers{"content-length"}B= defined $send_headers{"transfer-encoding"}
ABresult
1X1
011
000
467 || defined $send_headers{'transfer-encoding'}) {
468 # ok
469 ▶ } elsif (!$no_body && !Plack::Util::status_with_no_entity_body($status_code)) {
Condition33%1/3
$no_body || Plack::Util::status_with_no_entity_body($status_code)
l!l&&r!l&&!r
0015
MC/DC0%0/2
$no_body || Plack::Util::status_with_no_entity_body($status_code)
$no_body Plack::Util::status_with_no_entity_body($status_code)
Truth table33%1/3
A || B
A= $no_bodyB= Plack::Util::status_with_no_entity_body($status_code)
ABresult
1X1
011
000
470 15 push @lines, "Transfer-Encoding: chunked\015\012";
471 15 $use_chunked = 1;
472 }
473 15 ▶ push @lines, "Connection: close\015\012" unless $$use_keepalive_r;
Branch: 1 branch
BranchTrueFalseTotal
unless $$use_keepalive_r 15 0 15
474 } else {
475 # HTTP/1.0
476 8 ▶ if ($$use_keepalive_r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$use_keepalive_r) 0 8 8
477 0 ▶ if (defined $send_headers{'content-length'}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $send_headers{'content-length'} or defined $send_headers{'transfer-encoding'}) { } 0 0 0
elsif (not Plack::Util::status_with_no_entity_body($status_code) and defined(my $cl = Plack::Util::content_length($body))) { } 0 0 0
Condition0%0/3
defined $send_headers{"content-length"} or defined $send_headers{"transfer-encoding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $send_headers{"content-length"} or defined $send_headers{"transfer-encoding"}
defined $send_headers{"content-length"} defined $send_headers{"transfer-encoding"}
Truth table0%0/3
A or B
A= defined $send_headers{"content-length"}B= defined $send_headers{"transfer-encoding"}
ABresult
1X1
011
000
478 || defined $send_headers{'transfer-encoding'}) {
479 # ok
480 ▶ } elsif (!Plack::Util::status_with_no_entity_body($status_code)
Condition0%0/3
not Plack::Util::status_with_no_entity_body($status_code) and defined(my $cl = Plack::Util::content_length($body))
!ll&&!rl&&r
000
MC/DC0%0/2
not Plack::Util::status_with_no_entity_body($status_code) and defined(my $cl = Plack::Util::content_length($body))
not Plack::Util::status_with_no_entity_body($status_code) defined(my $cl = Plack::Util::content_length($body))
Truth table0%0/3
not(A) and B
A= not Plack::Util::status_with_no_entity_body($status_code)B= defined(my $cl = Plack::Util::content_length($body))
ABresult
0X0
100
111
481 && defined(my $cl = Plack::Util::content_length($body))) {
482 0 push @lines, "Content-Length: $cl\015\012";
483 } else {
484 0 $$use_keepalive_r = undef;
485 }
486 }
487 8 ▶ push @lines, "Connection: keep-alive\015\012" if $$use_keepalive_r;
Branch: 1 branch
BranchTrueFalseTotal
if $$use_keepalive_r 0 8 8
488 8 ▶ push @lines, "Connection: close\015\012" if !$$use_keepalive_r; #fmm..
Branch: 1 branch
BranchTrueFalseTotal
unless $$use_keepalive_r 8 0 8
489 }
490  
491 23 unshift @lines, "HTTP/1.1 $status_code @{[ HTTP::Status::status_message($status_code) ]}\015\012";
492 23 push @lines, "\015\012";
493
494 23 ▶ if ($no_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_body) 0 23 23
495 0 $self->write_all($conn, join('', @lines), $self->{timeout});
496 0 ▶ return Plack::Util::inline_object write => sub {}, close => sub {}
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
497 0 ▶ unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
498 0 ▶ $body->close if ref $body ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $body ne "ARRAY" 0 0 0
499 0 return;
500 }
501 23 ▶ if (defined $body && ref $body eq 'ARRAY' && @$body == 1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and ref $body eq "ARRAY" and @$body == 1 and length $body->[0] < 8192) 23 0 23
Condition33%3/9
defined $body and ref $body eq "ARRAY" and @$body == 1 and length $body->[0] < 8192
!ll&&!rl&&r
0023
defined $body and ref $body eq "ARRAY" and @$body == 1
!ll&&!rl&&r
0023
defined $body and ref $body eq "ARRAY"
!ll&&!rl&&r
0023
MC/DC0%0/4
defined $body and ref $body eq "ARRAY" and @$body == 1 and length $body->[0] < 8192
defined $body ref $body eq "ARRAY" @$body == 1 length $body->[0] < 8192
Truth table20%1/5
A and B and C and D
A= defined $bodyB= ref $body eq "ARRAY"C= @$body == 1D= length $body->[0] < 8192
ABCDresult
0XXX0
10XX0
110X0
11100
11111
502 && length $body->[0] < 8192) {
503 # combine response header and small request body
504 23 my $buf = $body->[0];
505 23 ▶ if ($use_chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked) 15 8 23
506 15 my $len = length $buf;
507 15 $buf = sprintf("%x",$len) . "\015\012" . $buf . "\015\012" . '0' . "\015\012\015\012";
508 }
509 $self->write_all(
510 $conn, join('', @lines, $buf), $self->{timeout},
511 23 );
512 23 return;
513 }
514 $self->write_all($conn, join('', @lines), $self->{timeout})
515 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($conn, join("", @lines), $self->{"timeout"}) or return 0 0 0
516  
517 0 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 0 0 0
518 0 my $failed;
519 my $completed;
520 0 ▶ my $body_count = (ref $body eq 'ARRAY') ? $#{$body} + 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'ARRAY' ? : 0 0 0
521 Plack::Util::foreach(
522 $body,
523 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
524 0 ▶ unless ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($failed) 0 0 0
525 0 my $buf = $_[0];
526 0 --$body_count;
527 0 ▶ if ( $use_chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked) 0 0 0
528 0 my $len = length $buf;
529 0 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
530 0 $buf = sprintf("%x",$len) . "\015\012" . $buf . "\015\012";
531 0 ▶ if ( $body_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_count == 0) 0 0 0
532 0 $buf .= '0' . "\015\012\015\012";
533 0 $completed = 1;
534 }
535 }
536 $self->write_all($conn, $buf, $self->{timeout})
537 0 ▶ or $failed = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->write_all($conn, $buf, $self->{"timeout"}) or $failed = 1 0 0 0
538 }
539 },
540 0 );
541 0 ▶ $self->write_all($conn, '0' . "\015\012\015\012", $self->{timeout}) if $use_chunked && !$completed;
Branch: 1 branch
BranchTrueFalseTotal
if $use_chunked and not $completed 0 0 0
Condition0%0/3
$use_chunked and not $completed
!ll&&!rl&&r
000
MC/DC0%0/2
$use_chunked and not $completed
$use_chunked not $completed
Truth table0%0/3
A and not(B)
A= $use_chunkedB= not $completed
ABresult
0X0
100
111
542 } else {
543 return Plack::Util::inline_object
544 0 ▶ write => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
545 0 my $buf = $_[0];
546 0 ▶ if ( $use_chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked) 0 0 0
547 0 my $len = length $buf;
548 0 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
549 0 $buf = sprintf("%x",$len) . "\015\012" . $buf . "\015\012"
550 }
551 $self->write_all($conn, $buf, $self->{timeout})
552 0 },
553 0 ▶ close => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
554 0 ▶ $self->write_all($conn, '0' . "\015\012\015\012", $self->{timeout}) if $use_chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $use_chunked 0 0 0
555 0 };
556 }
557 }
558  
559 # returns value returned by $cb, or undef on timeout or network error
560 55 ▶sub do_io {
Subroutine do_io: called CC=13 SCAR=29.2
Pod undocumented
561 55 my ($self, $is_write, $sock, $buf, $len, $off, $timeout) = @_;
562 55 my $ret;
563 55 ▶ unless ($is_write || delete $self->{_is_deferred_accept}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_write or delete $self->{"_is_deferred_accept"}) 6 49 55
Condition100%3/3
$is_write or delete $self->{"_is_deferred_accept"}
l!l&&r!l&&!r
23266
MC/DC100%2/2
$is_write or delete $self->{"_is_deferred_accept"}
$is_write delete $self->{"_is_deferred_accept"}
Truth table100%3/3
A or B
A= $is_writeB= delete $self->{"_is_deferred_accept"}
ABresult
1X1
011
000
564 6 goto DO_SELECT;
565 }
566 DO_READWRITE:
567 # try to do the IO
568 55 ▶ if ($is_write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_write) { } 23 32 55
569 23 ▶ $ret = syswrite $sock, $buf, $len, $off
Branch: 1 branch
BranchTrueFalseTotal
$ret = syswrite($sock, $buf, $len, $off) and return $ret 23 0 23
570 and return $ret;
571 } else {
572 32 ▶ $ret = sysread $sock, $$buf, $len, $off
Branch: 1 branch
BranchTrueFalseTotal
$ret = sysread($sock, $$buf, $len, $off) and return $ret 26 6 32
573 and return $ret;
574 }
575 6 ▶ unless ((! defined($ret)
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $ret and $! == 4 || $! == 11 || $! == 11) 6 0 6
Condition11%1/9
not defined $ret and $! == 4 || $! == 11 || $! == 11
!ll&&!rl&&r
600
$! == 4 or $! == 11 or $! == 11
l!l&&r!l&&!r
000
$! == 4 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $ret and $! == 4 || $! == 11 || $! == 11
not defined $ret $! == 4 $! == 11 $! == 11
Truth table20%1/5
not(A) and B or C or D
A= not defined $retB= $! == 4C= $! == 11D= $! == 11
ABCDresult
0XXX0
10000
11XX1
101X1
10011
576 && ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK))) {
577 6 return;
578 }
579 # wait for data
580 DO_SELECT:
581 6 while (1) {
582 6 my ($rfd, $wfd);
583 6 my $efd = '';
584 6 vec($efd, fileno($sock), 1) = 1;
585 6 ▶ if ($is_write) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_write) { } 0 6 6
586 0 ($rfd, $wfd) = ('', $efd);
587 } else {
588 6 ($rfd, $wfd) = ($efd, '');
589 }
590 6 my $start_at = time;
591 6 my $nfound = select($rfd, $wfd, $efd, $timeout);
592 6 $timeout -= (time - $start_at);
593 6 ▶ last if $nfound;
Branch: 1 branch
BranchTrueFalseTotal
if $nfound 6 0 6
594 0 ▶ return if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
595 }
596 6 goto DO_READWRITE;
597 }
598  
599 # returns (positive) number of bytes read, or undef if the socket is to be closed
600 32 ▶sub read_timeout {
Subroutine read_timeout: called CC=1 SCAR=0.0
Pod undocumented
601 32 my ($self, $sock, $buf, $len, $off, $timeout) = @_;
602 32 $self->do_io(undef, $sock, $buf, $len, $off, $timeout);
603 }
604  
605 # returns (positive) number of bytes written, or undef if the socket is to be closed
606 23 ▶sub write_timeout {
Subroutine write_timeout: called CC=1 SCAR=0.0
Pod undocumented
607 23 my ($self, $sock, $buf, $len, $off, $timeout) = @_;
608 23 $self->do_io(1, $sock, $buf, $len, $off, $timeout);
609 }
610  
611 # writes all data in buf and returns number of bytes written or undef if failed
612 23 ▶sub write_all {
Subroutine write_all: called CC=3 SCAR=11.0
Pod undocumented
613 23 my ($self, $sock, $buf, $timeout) = @_;
614 23 my $off = 0;
615 23 ▶ while (my $len = length($buf) - $off) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length($buf) - $off) 23 23 46
616 23 ▶ my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout)
Branch: 1 branch
BranchTrueFalseTotal
my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout) or return 23 0 23
617 or return;
618 23 $off += $ret;
619 }
620 23 return length $buf;
621 }
622  
623 55 1;
↑ Summary blib/lib/Plack/Handler/Starlet.pm »