Starman/Server.pm

filter: Statementstmt 89.8% 407 / 453 Branchbran 79.6% 204 / 256 Conditioncond 59.7% 52 / 87 MC/DCmcdc 37.0% 20 / 54 Subroutinesub 90.7% 49 / 54 Podpod 70.0% 7 / 10 totaltotal 80.8% 739 / 914 CC 168 SCAR 31.0
CC
168
Cov
83%
run
54.2
process_request
49.6
_prepare_env
27.0
CRAP 22.2
↑ Summary Plack/Handler/Starman.pm »
1 package Starman::Server;
2 137 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 137 ▶use base 'Net::Server::PreFork';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 137 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 137 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 137 ▶use IO::Socket qw(:crlf);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 137 ▶use HTTP::Parser::XS qw(parse_http_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 137 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 137 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 137 ▶use POSIX qw(EINTR EPIPE ECONNRESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 137 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 137 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 137 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 137 ▶use Plack::TempBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 137 ▶use constant DEBUG => $ENV{STARMAN_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'STARMAN_DEBUG'} || 0
l!l
0137
MC/DC0%0/1
$ENV{'STARMAN_DEBUG'} || 0
$ENV{'STARMAN_DEBUG'}
Truth table50%1/2
A
A= $ENV{'STARMAN_DEBUG'}
Aresult
11
00
19 137 ▶use constant CHUNKSIZE => 64 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 137 ▶use constant MAX_CHUNK_LINE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 137 ▶use constant MAX_TRAILER_SIZE => 16 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 137 ▶use constant MAX_HEADER_SIZE => 128 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 137 my $null_io = do { open my $io, "<", \""; $io };
25  
26 137 ▶use Net::Server::SIG qw(register_sig);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Override Net::Server's HUP handling - just restart all the workers and that's about it
29 0 ▶sub sig_hup {
Subroutine sig_hup: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my $self = shift;
31 0 $self->hup_children;
32 }
33  
34 127 ▶sub run {
Subroutine run: called CC=29 SCAR=39.9
Pod documented
35 127 my($self, $app, $options) = @_;
36  
37 127 $self->{app} = $app;
38 127 $self->{options} = $options;
39  
40 127 my %extra = ();
41  
42 127 ▶ if ($options->{net_server_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"net_server_args"}) 0 127 127
43 0 %extra = %{ $options->{net_server_args} };
44 }
45  
46 127 ▶ if ( $options->{pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"pid"}) 0 127 127
47 0 $extra{pid_file} = $options->{pid};
48 }
49 127 ▶ if ( $options->{daemonize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"daemonize"}) 0 127 127
50 0 $extra{setsid} = $extra{background} = 1;
51 }
52 127 ▶ if ( $options->{error_log} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"error_log"}) 0 127 127
53 0 $extra{log_file} = $options->{error_log};
54 }
55 127 if ( DEBUG ) {
56 $extra{log_level} = 4;
57 }
58 127 ▶ if ( $options->{ssl_cert} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"ssl_cert"}) 14 113 127
59 14 $extra{SSL_cert_file} = $options->{ssl_cert};
60 }
61 127 ▶ if ( $options->{ssl_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"ssl_key"}) 14 113 127
62 14 $extra{SSL_key_file} = $options->{ssl_key};
63 }
64 127 ▶ if (! exists $options->{keepalive}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"keepalive"}) 127 0 127
65 127 $options->{keepalive} = 1;
66 }
67 127 ▶ if (! exists $options->{keepalive_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"keepalive_timeout"}) 127 0 127
68 127 $options->{keepalive_timeout} = 1;
69 }
70 127 ▶ if (! exists $options->{read_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"read_timeout"}) 123 4 127
71 123 $options->{read_timeout} = 5;
72 }
73 127 ▶ if (! exists $options->{body_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"body_timeout"}) 125 2 127
74 125 $options->{body_timeout} = 300;
75 }
76 127 ▶ if (! exists $options->{proctitle}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options->{"proctitle"}) 127 0 127
77 127 $options->{proctitle} = 1;
78 }
79  
80 127 my @port;
81 127 ▶ for my $listen (@{$options->{listen} || [ "$options->{host}:$options->{port}" ]}) {
Condition50%1/2
$options->{'listen'} || ["$options->{'host'}:$options->{'port'}"]
l!l
0127
MC/DC0%0/1
$options->{'listen'} || ["$options->{'host'}:$options->{'port'}"]
$options->{'listen'}
Truth table50%1/2
A
A= $options->{'listen'}
Aresult
11
00
82 127 my %listen;
83 127 ▶ if ($listen =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listen =~ /:/) { } 127 0 127
84 127 my($h, $p, $opt) = split /:/, $listen, 3;
85 127 ▶ $listen{host} = $h if $h;
Branch: 1 branch
BranchTrueFalseTotal
if $h 127 0 127
86 127 $listen{port} = $p;
87 127 ▶ $listen{proto} = 'ssl' if defined $opt && lc $opt eq 'ssl';
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt and lc $opt eq "ssl" 0 127 127
Condition33%1/3
defined $opt and lc $opt eq "ssl"
!ll&&!rl&&r
12700
MC/DC0%0/2
defined $opt and lc $opt eq "ssl"
defined $opt lc $opt eq "ssl"
Truth table33%1/3
A and B
A= defined $optB= lc $opt eq "ssl"
ABresult
0X0
100
111
88 } else {
89 0 %listen = (
90 host => 'localhost',
91 port => $listen,
92 proto => 'unix',
93 );
94 }
95 127 push @port, \%listen;
96 }
97  
98 127 ▶ my $workers = $options->{workers} || 5;
Condition100%2/2
$options->{'workers'} || 5
l!l
26101
MC/DC100%1/1
$options->{'workers'} || 5
$options->{'workers'}
Truth table100%2/2
A
A= $options->{'workers'}
Aresult
11
00
99 127 local @ARGV = ();
100  
101 $self->SUPER::run(
102 port => \@port,
103 host => '*', # default host
104 proto => $options->{ssl} ? 'ssl' : 'tcp', # default proto
105 serialize => ( $^O =~ m!(linux|darwin|bsd|cygwin)$! ) ? 'none' : 'flock',
106 min_servers => $options->{min_servers} || $workers,
107 min_spare_servers => $options->{min_spare_servers} || $workers - 1,
108 max_spare_servers => $options->{max_spare_servers} || $workers - 1,
109 max_servers => $options->{max_servers} || $workers,
110 max_requests => $options->{max_requests} || 1000,
111 user => $options->{user} || $>,
112 group => $options->{group} || $),
113 127 ▶ listen => $options->{backlog} || 1024,
Branch: 2 branches
BranchTrueFalseTotal
$options->{'ssl'} ? : 14 113 127
$^O =~ /(linux|darwin|bsd|cygwin)$/ ? : 127 0 127
Condition45%10/22
$options->{'min_servers'} || $workers
l!l&&r!l&&!r
01270
$options->{'min_spare_servers'} || $workers - 1
l!l&&r!l&&!r
010126
$options->{'max_spare_servers'} || $workers - 1
l!l&&r!l&&!r
010126
$options->{'max_servers'} || $workers
l!l&&r!l&&!r
01270
$options->{'max_requests'} || 1000
l!l
0127
$options->{'user'} || $>
l!l&&r!l&&!r
01270
$options->{'group'} || $)
l!l&&r!l&&!r
01270
$options->{'backlog'} || 1024
l!l
0127
MC/DC0%0/2
$options->{'min_servers'} || $workers
$options->{'min_servers'} $workers
MC/DC50%1/2
$options->{'min_spare_servers'} || $workers - 1
$options->{'min_spare_servers'} $workers - 1
MC/DC50%1/2
$options->{'max_spare_servers'} || $workers - 1
$options->{'max_spare_servers'} $workers - 1
MC/DC0%0/2
$options->{'max_servers'} || $workers
$options->{'max_servers'} $workers
MC/DC0%0/1
$options->{'max_requests'} || 1000
$options->{'max_requests'}
MC/DC0%0/2
$options->{'user'} || $>
$options->{'user'} $>
MC/DC0%0/2
$options->{'group'} || $)
$options->{'group'} $)
MC/DC0%0/1
$options->{'backlog'} || 1024
$options->{'backlog'}
Truth table33%1/3
A || B
A= $options->{'min_servers'}B= $workers
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $options->{'min_spare_servers'}B= $workers - 1
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $options->{'max_spare_servers'}B= $workers - 1
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $options->{'max_servers'}B= $workers
ABresult
1X1
011
000
Truth table50%1/2
A
A= $options->{'max_requests'}
Aresult
11
00
Truth table33%1/3
A || B
A= $options->{'user'}B= $>
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $options->{'group'}B= $)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $options->{'backlog'}
Aresult
11
00
114 check_for_waiting => 1,
115 no_client_stdout => 1,
116 %extra
117 );
118 }
119  
120 127 ▶sub pre_loop_hook {
Subroutine pre_loop_hook: called CC=4 SCAR=15.5
Pod documented
121 127 my $self = shift;
122  
123 127 my $port = $self->{server}->{port}->[0];
124 my $proto = $port->{proto} eq 'ssl' ? 'https' :
125 127 ▶ $port->{proto} eq 'unix' ? 'unix' :
Branch: 2 branches
BranchTrueFalseTotal
$port->{'proto'} eq 'ssl' ? : 14 113 127
$port->{'proto'} eq 'unix' ? : 0 113 113
126 'http';
127  
128 $self->{options}{server_ready}->({
129 host => $port->{host},
130 port => $port->{port},
131 proto => $proto,
132 server_software => 'Starman',
133 127 ▶ }) if $self->{options}{server_ready};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"server_ready"} 0 127 127
134  
135 register_sig(
136 0 ▶ TTIN => sub { $self->{server}->{$_}++ for qw( min_servers max_servers ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
137 0 ▶ TTOU => sub { $self->{server}->{$_}-- for qw( min_servers max_servers ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
138 0 ▶ QUIT => sub { $self->server_close(1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 127 );
140 }
141  
142 27 ▶sub server_close {
Subroutine server_close: called CC=4 SCAR=24.1
Pod documented
143 27 my($self, $quit) = @_;
144  
145 27 ▶ if ($quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quit) 0 27 27
146 0 $self->log(2, $self->log_time . " Received QUIT. Running a graceful shutdown\n");
147 0 $self->{server}->{$_} = 0 for qw( min_servers max_servers );
148 0 $self->hup_children;
149 0 while (1) {
150 0 Net::Server::SIG::check_sigs();
151 0 $self->coordinate_children;
152 0 ▶ last if !keys %{$self->{server}{children}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"server"}{"children"};} 0 0 0
153 0 sleep 1;
154 }
155 0 $self->log(2, $self->log_time . " Worker processes cleaned up\n");
156 }
157  
158 27 $self->SUPER::server_close();
159 }
160  
161 45 ▶sub run_parent {
Subroutine run_parent: called CC=3 SCAR=11.1
Pod undocumented
162 45 my $self = shift;
163 45 ▶ $0 = "starman master " . join(" ", @{$self->{options}{argv} || []})
Condition50%1/2
$self->{'options'}{'argv'} || []
l!l
045
MC/DC0%0/1
$self->{'options'}{'argv'} || []
$self->{'options'}{'argv'}
Truth table50%1/2
A
A= $self->{'options'}{'argv'}
Aresult
11
00
164 45 ▶ if $self->{options}{proctitle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"proctitle"} 45 0 45
165 137 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
166 45 ▶ local *Net::Server::PreFork::register_sig = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 45 my %args = @_;
168 45 delete $args{QUIT};
169 45 Net::Server::SIG::register_sig(%args);
170 45 };
171 45 $self->SUPER::run_parent(@_);
172 }
173  
174 # The below methods run in the child process
175  
176 100 ▶sub child_init_hook {
Subroutine child_init_hook: called CC=4 SCAR=15.9
Pod documented
177 100 my $self = shift;
178 100 srand();
179 100 ▶ if ($self->{options}->{psgi_app_builder}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"psgi_app_builder"}) 0 100 100
180 0 DEBUG && warn "[$$] Initializing the PSGI app\n";
181 0 $self->{app} = $self->{options}->{psgi_app_builder}->();
182 }
183 100 ▶ $0 = "starman worker " . join(" ", @{$self->{options}{argv} || []})
Condition50%1/2
$self->{'options'}{'argv'} || []
l!l
0100
MC/DC0%0/1
$self->{'options'}{'argv'} || []
$self->{'options'}{'argv'}
Truth table50%1/2
A
A= $self->{'options'}{'argv'}
Aresult
11
00
184 100 ▶ if $self->{options}{proctitle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"proctitle"} 100 0 100
185  
186 }
187  
188 197 ▶sub post_accept_hook {
Subroutine post_accept_hook: called CC=1 SCAR=0.0
Pod documented
189 197 my $self = shift;
190  
191 $self->{client} = {
192 197 headerbuf => '',
193 inputbuf => '',
194 keepalive => 1,
195 };
196 }
197  
198 160 ▶sub dispatch_request {
Subroutine dispatch_request: called CC=2 SCAR=6.9
Pod undocumented
199 160 my ($self, $env) = @_;
200  
201 # Run PSGI apps
202 160 my $res = Plack::Util::run_app($self->{app}, $env);
203  
204 160 ▶ if (ref $res eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq 'CODE') { } 9 151 160
205 9 ▶ $res->(sub { $self->_finalize_response($env, $_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
206 } else {
207 151 $self->_finalize_response($env, $res);
208 }
209 }
210  
211 197 ▶sub process_request {
Subroutine process_request: called CC=33 SCAR=39.0
Pod documented
212 197 my $self = shift;
213 197 my $conn = $self->{server}->{client};
214  
215 197 ▶ if ($conn->NS_proto eq 'TCP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->NS_proto eq "TCP") 195 2 197
216 195 ▶ setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1)
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $conn, 6, 1, 1 or die $! 195 0 195
217 or die $!;
218 }
219  
220 197 ▶ while ( $self->{client}->{keepalive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{"keepalive"}) 318 38 356
221 318 ▶ last if !$conn->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->connected 0 318 318
222  
223 # Read until we see all headers
224 318 ▶ last if !$self->_read_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_read_headers 92 226 318
225  
226 my $env = {
227 REMOTE_ADDR => $self->{server}->{peeraddr},
228 REMOTE_HOST => $self->{server}->{peerhost} || $self->{server}->{peeraddr},
229 REMOTE_PORT => $self->{server}->{peerport} || 0,
230 SERVER_NAME => $self->{server}->{sockaddr} || 0, # XXX: needs to be resolved?
231 SERVER_PORT => $self->{server}->{sockport} || 0,
232 SCRIPT_NAME => '',
233 'psgi.version' => [ 1, 1 ],
234 'psgi.errors' => *STDERR,
235 'psgi.url_scheme' => ($conn->NS_proto eq 'SSL' ? 'https' : 'http'),
236 'psgi.nonblocking' => Plack::Util::FALSE,
237 'psgi.streaming' => Plack::Util::TRUE,
238 'psgi.run_once' => Plack::Util::FALSE,
239 'psgi.multithread' => Plack::Util::FALSE,
240 'psgi.multiprocess' => Plack::Util::TRUE,
241 'psgix.io' => $conn,
242 'psgix.input.buffered' => Plack::Util::TRUE,
243 'psgix.harakiri' => Plack::Util::TRUE,
244 1 ▶ 'psgix.informational' => sub { _write_informational($conn, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
245 226 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$conn->NS_proto eq 'SSL' ? : 2 224 226
Condition44%4/9
$self->{'server'}{'peerhost'} || $self->{'server'}{'peeraddr'}
l!l&&r!l&&!r
02260
$self->{'server'}{'peerport'} || 0
l!l
2260
$self->{'server'}{'sockaddr'} || 0
l!l
2260
$self->{'server'}{'sockport'} || 0
l!l
2260
MC/DC0%0/2
$self->{'server'}{'peerhost'} || $self->{'server'}{'peeraddr'}
$self->{'server'}{'peerhost'} $self->{'server'}{'peeraddr'}
MC/DC0%0/1
$self->{'server'}{'peerport'} || 0
$self->{'server'}{'peerport'}
MC/DC0%0/1
$self->{'server'}{'sockaddr'} || 0
$self->{'server'}{'sockaddr'}
MC/DC0%0/1
$self->{'server'}{'sockport'} || 0
$self->{'server'}{'sockport'}
Truth table33%1/3
A || B
A= $self->{'server'}{'peerhost'}B= $self->{'server'}{'peeraddr'}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'server'}{'peerport'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'server'}{'sockaddr'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'server'}{'sockport'}
Aresult
11
00
246  
247 # Parse headers
248 226 my $headerbuf = delete $self->{client}->{headerbuf};
249 226 my $reqlen = parse_http_request($headerbuf, $env);
250 226 ▶ if ( $reqlen != length $headerbuf || !_valid_header_names($headerbuf) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqlen != length $headerbuf or not _valid_header_names($headerbuf)) 20 206 226
Condition100%3/3
$reqlen != length $headerbuf or not _valid_header_names($headerbuf)
l!l&&r!l&&!r
812206
MC/DC100%2/2
$reqlen != length $headerbuf or not _valid_header_names($headerbuf)
$reqlen != length $headerbuf not _valid_header_names($headerbuf)
Truth table100%3/3
A or not(B)
A= $reqlen != length $headerbufB= not _valid_header_names($headerbuf)
ABresult
1X1
011
000
251 # Bad request
252 20 DEBUG && warn "[$$] Bad request\n";
253 20 $self->_http_error(400, { SERVER_PROTOCOL => "HTTP/1.0" });
254 20 last;
255 }
256  
257 # Reject ambiguous message framing before we touch the body
258 206 my($framing_error, $close_after) = $self->_check_framing($env);
259 206 ▶ if ( $framing_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($framing_error) 19 187 206
260 19 DEBUG && warn "[$$] Invalid message framing, returning $framing_error\n";
261 19 $self->_http_error( $framing_error, $env );
262 19 last;
263 }
264  
265 # Initialize PSGI environment
266 # Determine whether we will keep the connection open after the request
267 187 my $connection = delete $env->{HTTP_CONNECTION};
268 187 my $proto = $env->{SERVER_PROTOCOL};
269 187 ▶ if ( $proto && $proto eq 'HTTP/1.0' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($proto and $proto eq 'HTTP/1.0') { } 0 187 187
elsif ($proto and $proto eq 'HTTP/1.1') { } 187 0 187
Condition33%1/3
$proto and $proto eq "HTTP/1.0"
!ll&&!rl&&r
01870
MC/DC0%0/2
$proto and $proto eq "HTTP/1.0"
$proto $proto eq "HTTP/1.0"
Truth table33%1/3
A and B
A= $protoB= $proto eq "HTTP/1.0"
ABresult
0X0
100
111
270 0 ▶ if ( $connection && $connection =~ /^keep-alive$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection and $connection =~ /^keep-alive$/i) { } 0 0 0
Condition0%0/3
$connection and $connection =~ /^keep-alive$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$connection and $connection =~ /^keep-alive$/i
$connection $connection =~ /^keep-alive$/i
Truth table0%0/3
A and B
A= $connectionB= $connection =~ /^keep-alive$/i
ABresult
0X0
100
111
271 # Keep-alive only with explicit header in HTTP/1.0
272 0 $self->{client}->{keepalive} = 1;
273 }
274 else {
275 0 $self->{client}->{keepalive} = 0;
276 }
277 }
278 ▶ elsif ( $proto && $proto eq 'HTTP/1.1' ) {
Condition33%1/3
$proto and $proto eq "HTTP/1.1"
!ll&&!rl&&r
00187
MC/DC0%0/2
$proto and $proto eq "HTTP/1.1"
$proto $proto eq "HTTP/1.1"
Truth table33%1/3
A and B
A= $protoB= $proto eq "HTTP/1.1"
ABresult
0X0
100
111
279 187 ▶ if ( $connection && $connection =~ /^close$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection and $connection =~ /^close$/i) { } 17 170 187
Condition100%3/3
$connection and $connection =~ /^close$/i
!ll&&!rl&&r
168217
MC/DC100%2/2
$connection and $connection =~ /^close$/i
$connection $connection =~ /^close$/i
Truth table100%3/3
A and B
A= $connectionB= $connection =~ /^close$/i
ABresult
0X0
100
111
280 17 $self->{client}->{keepalive} = 0;
281 }
282 else {
283 # Keep-alive assumed in HTTP/1.1
284 170 $self->{client}->{keepalive} = 1;
285 }
286  
287 # Do we need to send 100 Continue?
288 187 ▶ if ( $env->{HTTP_EXPECT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_EXPECT"}) 1 186 187
289 1 ▶ if ( lc $env->{HTTP_EXPECT} eq '100-continue' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $env->{'HTTP_EXPECT'} eq '100-continue') { } 1 0 1
290 1 _syswrite($conn, \('HTTP/1.1 100 Continue' . $CRLF . $CRLF));
291 1 DEBUG && warn "[$$] Sent 100 Continue response\n";
292 }
293 else {
294 0 DEBUG && warn "[$$] Invalid Expect header, returning 417\n";
295 0 $self->_http_error( 417, $env );
296 0 last;
297 }
298 }
299  
300 187 ▶ unless ($env->{HTTP_HOST}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"HTTP_HOST"}) 0 187 187
301 # No host, bad request
302 0 DEBUG && warn "[$$] Bad request, HTTP/1.1 without Host header\n";
303 0 $self->_http_error( 400, $env );
304 0 last;
305 }
306 }
307  
308 187 ▶ unless ($self->{options}->{keepalive}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}{"keepalive"}) 0 187 187
309 0 DEBUG && warn "[$$] keep-alive is disabled. Closing the connection after this request\n";
310 0 $self->{client}->{keepalive} = 0;
311 }
312  
313 187 ▶ if ($close_after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_after) 2 185 187
314 2 DEBUG && warn "[$$] Both Transfer-Encoding and Content-Length present. Closing the connection after this request\n";
315 2 $self->{client}->{keepalive} = 0;
316 }
317  
318 # Read the request body. _prepare_env dies with "Timed out" if the
319 # client is too slow; the app is run after that, outside any alarm.
320 187 my $status = eval { $self->_prepare_env($env) };
321 187 alarm(0);
322  
323 187 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 8 179 187
324 8 ▶ die $@ unless $@ eq "Timed out\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "Timed out\n" 0 8 8
325 8 DEBUG && warn "[$$] Timed out reading the request body\n";
326 8 $status = 408;
327 }
328  
329 187 ▶ if ( $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 27 160 187
330 27 DEBUG && warn "[$$] Bad request body, returning $status\n";
331 27 $self->_http_error( $status, $env );
332 27 last;
333 }
334  
335 160 $self->dispatch_request($env);
336  
337 160 DEBUG && warn "[$$] Request done\n";
338  
339 160 ▶ if ( $self->{client}->{keepalive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{"keepalive"}) 122 38 160
340 # If we still have data in the input buffer it may be a pipelined request
341 # The request body has been read exactly, so this is the next request
342 122 ▶ if ( $self->{client}->{inputbuf} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{"inputbuf"} ne "") 22 100 122
343 22 if ( DEBUG ) {
344 warn "Pipelined request in input buffer: "
345 . dump( $self->{client}->{inputbuf} ) . "\n";
346 }
347  
348 # Continue processing the input buffer
349 22 next;
350 }
351  
352 100 DEBUG && warn "[$$] Waiting on previous connection for keep-alive request...\n";
353  
354 100 my $sel = IO::Select->new($conn);
355 100 ▶ last unless $sel->can_read($self->{options}->{keepalive_timeout});
Branch: 1 branch
BranchTrueFalseTotal
unless $sel->can_read($self->{"options"}{"keepalive_timeout"}) 0 99 99
356 }
357 }
358  
359 196 DEBUG && warn "[$$] Closing connection\n";
360 }
361  
362 318 ▶sub _read_headers {
Subroutine _read_headers: called CC=11 SCAR=24.2
363 318 my $self = shift;
364  
365 318 eval {
366 0 ▶ local $SIG{ALRM} = sub { die "Timed out\n"; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
367  
368 318 alarm( $self->{options}->{read_timeout} );
369  
370 318 while (1) {
371 # Ignore empty lines before the request line (RFC 9112 section 2.2)
372 530 $self->{client}->{inputbuf} =~ s/\A(?:$CR?$LF)+//;
373  
374 # Do we have a full header in the buffer?
375 # This is before sysread so we don't read if we have a pipelined request
376 # waiting in the buffer
377 530 ▶ if ( $self->{client}->{inputbuf} ne '' && $self->{client}->{inputbuf} =~ /$CR?$LF$CR?$LF/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{"inputbuf"} ne "" and $self->{"client"}{"inputbuf"} =~ /$Starman::Server::CR?$Starman::Server::LF$Starman::Server::CR?$Starman::Server::LF/s) 226 304 530
Condition100%3/3
$self->{"client"}{"inputbuf"} ne "" and $self->{"client"}{"inputbuf"} =~ /$Starman::Server::CR?$Starman::Server::LF$Starman::Server::CR?$Starman::Server::LF/s
!ll&&!rl&&r
2986226
MC/DC100%2/2
$self->{"client"}{"inputbuf"} ne "" and $self->{"client"}{"inputbuf"} =~ /$Starman::Server::CR?$Starman::Server::LF$Starman::Server::CR?$Starman::Server::LF/s
$self->{"client"}{"inputbuf"} ne "" $self->{"client"}{"inputbuf"} =~ /$Starman::Server::CR?$Starman::Server::LF$Starman::Server::CR?$Starman::Server::LF/s
Truth table100%3/3
A and B
A= $self->{"client"}{"inputbuf"} ne ""B= $self->{"client"}{"inputbuf"} =~ /$Starman::Server::CR?$Starman::Server::LF$Starman::Server::CR?$Starman::Server::LF/s
ABresult
0X0
100
111
378 226 ▶ die "Header too large\n" if $-[0] > MAX_HEADER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $-[0] > 131072 0 226 226
379 226 last;
380 }
381 304 ▶ die "Header too large\n" if length $self->{client}->{inputbuf} > MAX_HEADER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"client"}{"inputbuf"} > 131072 2 302 304
382  
383 # If not, read some data
384 302 my $read = _sysread($self->{server}->{client}, my $buf, CHUNKSIZE);
385  
386 302 ▶ if ( !defined $read || $read == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $read or $read == 0) 90 212 302
Condition66%2/3
not defined $read or $read == 0
l!l&&r!l&&!r
090212
MC/DC50%1/2
not defined $read or $read == 0
not defined $read $read == 0
Truth table66%2/3
not(A) or B
A= not defined $readB= $read == 0
ABresult
1X1
011
000
387 90 die "Read error: $!\n";
388 }
389  
390 212 if ( DEBUG ) {
391 warn "[$$] Read $read bytes: " . dump($buf) . "\n";
392 }
393  
394 212 $self->{client}->{inputbuf} .= $buf;
395 }
396 };
397  
398 318 alarm(0);
399  
400 318 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 92 226 318
401 92 ▶ if ( $@ =~ /Timed out/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Timed out/) 0 92 92
402 0 DEBUG && warn "[$$] Client connection timed out\n";
403 0 return;
404 }
405  
406 92 ▶ if ( $@ =~ /Read error/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Read error/) 90 2 92
407 90 DEBUG && warn "[$$] Read error: $!\n";
408 90 return;
409 }
410  
411 2 ▶ if ( $@ =~ /Header too large/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Header too large/) 2 0 2
412 2 DEBUG && warn "[$$] Request header too large, returning 431\n";
413 2 $self->_http_error(431, { SERVER_PROTOCOL => "HTTP/1.0" });
414 2 return;
415 }
416 }
417  
418 # Pull out the complete header into a new buffer, leaving any body data
419 # or pipelined requests in the input buffer
420 226 $self->{client}->{inputbuf} =~ /$CR?$LF$CR?$LF/;
421 226 $self->{client}->{headerbuf} = substr $self->{client}->{inputbuf}, 0, $+[0], '';
422  
423 226 return 1;
424 }
425  
426 # Field names must be tokens directly followed by ":" and must not use
427 # obs-fold (RFC 9112 section 5). The parser maps "_" to "-", so
428 # Content-Length and Transfer-Encoding spelled with "_" are rejected too.
429 218 ▶sub _valid_header_names {
Subroutine _valid_header_names: called CC=5 SCAR=16.1
430 218 my $header = shift;
431  
432 218 my @lines = split /$CR?$LF/, $header;
433 218 shift @lines; # request line
434 218 for my $line (@lines) {
435 545 ▶ my($name) = $line =~ /\A([!#\$%&'*+\-.^_`|~0-9A-Za-z]+):/ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $line =~ /\A([!#\$%&'*+\-.^_`|~0-9A-Za-z]+):/ or return 539 6 545
436 539 ▶ return if $name =~ /_/ && $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /_/ and $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i 6 533 539
Condition100%3/3
$name =~ /_/ and $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i
!ll&&!rl&&r
53126
MC/DC100%2/2
$name =~ /_/ and $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i
$name =~ /_/ $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i
Truth table100%3/3
A and B
A= $name =~ /_/B= $name =~ /\A(?:content[-_]length|transfer[-_]encoding)\z/i
ABresult
0X0
100
111
437 }
438  
439 206 return 1;
440 }
441  
442 # Returns an error status if the request framing is invalid, and
443 # whether the connection must be closed after this request.
444 206 ▶sub _check_framing {
Subroutine _check_framing: called CC=11 SCAR=24.0
445 206 my($self, $env) = @_;
446  
447 206 ▶ if ( defined $env->{HTTP_TRANSFER_ENCODING} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"HTTP_TRANSFER_ENCODING"}) 45 161 206
448 # Only "chunked" is supported, and it must be the only coding
449 # (RFC 9112 section 6.1). HTTP/1.0 has no Transfer-Encoding.
450 45 ▶ return 400 if $env->{SERVER_PROTOCOL} ne 'HTTP/1.1';
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"SERVER_PROTOCOL"} ne "HTTP/1.1" 1 44 45
451  
452 44 my @codings = map { s/^[ \t]+|[ \t]+$//g; lc } split /,/, $env->{HTTP_TRANSFER_ENCODING}, -1;
453 44 ▶ return 400 unless @codings && $codings[-1] eq 'chunked';
Branch: 1 branch
BranchTrueFalseTotal
unless @codings and $codings[-1] eq "chunked" 5 39 44
Condition100%3/3
@codings and $codings[-1] eq "chunked"
!ll&&!rl&&r
1439
MC/DC100%2/2
@codings and $codings[-1] eq "chunked"
@codings $codings[-1] eq "chunked"
Truth table100%3/3
A and B
A= @codingsB= $codings[-1] eq "chunked"
ABresult
0X0
100
111
454 39 ▶ return 400 if grep { $_ eq 'chunked' } @codings[0..$#codings-1];
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "chunked";} @codings[0 .. $#codings - 1] 2 37 39
455 37 ▶ return 501 if @codings > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @codings > 1 1 36 37
456  
457 36 $env->{HTTP_TRANSFER_ENCODING} = 'chunked';
458  
459 # Transfer-Encoding overrides Content-Length, but a message with
460 # both may be an attempt at request smuggling (RFC 9112 6.3).
461 36 my $close_after = defined delete $env->{CONTENT_LENGTH};
462 36 return (undef, $close_after);
463 }
464  
465 161 ▶ if ( defined $env->{CONTENT_LENGTH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"CONTENT_LENGTH"}) 35 126 161
466 # Duplicate headers arrive joined with ",". They are only acceptable
467 # if every value is the same valid length (RFC 9112 section 6.3).
468 35 my @values = split /\s*,\s*/, $env->{CONTENT_LENGTH}, -1;
469 35 my %seen;
470 35 for my $v (@values) {
471 38 $v =~ s/^[ \t]+|[ \t]+$//g;
472 38 ▶ return 400 unless $v =~ /\A[0-9]{1,18}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\A[0-9]{1,18}\z/ 6 32 38
473 32 $seen{0 + $v}++;
474 }
475 29 ▶ return 400 unless keys %seen == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %seen == 1 4 25 29
476 25 ($env->{CONTENT_LENGTH}) = keys %seen;
477 }
478  
479 151 return;
480 }
481  
482 68 ▶sub _http_error {
Subroutine _http_error: called CC=2 SCAR=7.0
483 68 my ( $self, $code, $env ) = @_;
484  
485 68 ▶ my $status = $code || 500;
Condition50%1/2
$code || 500
l!l
680
MC/DC0%0/1
$code || 500
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
486 68 my $msg = status_message($status);
487  
488 68 my $res = [
489 $status,
490 [ 'Content-Type' => 'text/plain', 'Content-Length' => length($msg) ],
491 [ $msg ],
492 ];
493  
494 68 $self->{client}->{keepalive} = 0;
495 68 $self->_finalize_response($env, $res);
496 }
497  
498 254 ▶sub _prepare_env {
Subroutine _prepare_env: called CC=27 SCAR=33.0
499 254 my($self, $env) = @_;
500  
501 # Body bytes are taken from the input buffer, leaving anything after
502 # the body there for the next request on this connection.
503 254 my $input = \$self->{client}->{inputbuf};
504  
505 # Wait at most read_timeout seconds for each read, and body_timeout
506 # seconds in total, so a slow client cannot hold on to a worker.
507 254 ▶ my $idle_timeout = $self->{options}->{read_timeout} || 0;
Condition100%2/2
$self->{'options'}{'read_timeout'} || 0
l!l
18866
MC/DC100%1/1
$self->{'options'}{'read_timeout'} || 0
$self->{'options'}{'read_timeout'}
Truth table100%2/2
A
A= $self->{'options'}{'read_timeout'}
Aresult
11
00
508 my $deadline = $self->{options}->{body_timeout}
509 254 ▶ ? Time::HiRes::time() + $self->{options}->{body_timeout} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'body_timeout'} ? : 188 66 254
510  
511 310 ▶ my $read_more = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
512 310 my $timeout = $idle_timeout;
513 310 ▶ if ($deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deadline) 36 274 310
514 36 my $left = $deadline - Time::HiRes::time();
515 36 ▶ die "Timed out\n" if $left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left <= 0 0 36 36
516 36 ▶ $timeout = $left if !$timeout || $left < $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if not $timeout or $left < $timeout 15 21 36
Condition66%2/3
not $timeout or $left < $timeout
l!l&&r!l&&!r
01521
MC/DC50%1/2
not $timeout or $left < $timeout
not $timeout $left < $timeout
Truth table66%2/3
not(A) or B
A= not $timeoutB= $left < $timeout
ABresult
1X1
011
000
517 }
518  
519 # Time::HiRes::alarm, because alarm() would truncate a fraction of a
520 # second left to 0, which disables the alarm
521 310 ▶ local $SIG{ALRM} = sub { die "Timed out\n"; };
Subroutine __ANON__: called CC=1 SCAR=0.0
522 310 Time::HiRes::alarm($timeout);
523 310 my $read = _sysread($self->{server}->{client}, my($chunk), CHUNKSIZE);
524 302 alarm(0);
525  
526 302 ▶ $$input .= $chunk if $read;
Branch: 1 branch
BranchTrueFalseTotal
if $read 294 8 302
527 302 return $read;
528 254 };
529  
530 137 ▶ my $chunked = do { no warnings; delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' };
Subroutine BEGIN: called CC=1 SCAR=0.0
531  
532 254 ▶ if ($chunked) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunked) { } 100 154 254
elsif (my $cl = $env->{'CONTENT_LENGTH'}) { } 24 130 154
533 100 my $buf = Plack::TempBuffer->new;
534 100 my $length = 0;
535 100 my $remaining; # bytes left in the current chunk, undef while reading a size line
536 my $trailer_size; # defined once the last chunk has been read
537  
538 DECHUNK:
539 100 while (1) {
540 378 ▶ while ($$input ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$input ne "") 1049 243 1292
541 1049 ▶ if (defined $trailer_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trailer_size) 300 749 1049
542 300 my $eol = index $$input, "\015\012";
543 300 ▶ if ($eol < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol < 0) 16 284 300
544 16 ▶ return 400 if $trailer_size + length $$input > MAX_TRAILER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer_size + length($$input) > 16384 0 16 16
545 16 last;
546 }
547 284 $trailer_size += $eol + 2;
548 284 ▶ return 400 if $trailer_size > MAX_TRAILER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer_size > 16384 1 283 284
549  
550 283 my $line = substr $$input, 0, $eol + 2, '';
551 283 ▶ last DECHUNK if $eol == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $eol == 0 76 207 283
552 207 ▶ return 400 if $line =~ /[\015\012].*\015\012/s;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /[\015\012].*\015\012/s 0 207 207
553 207 next;
554 }
555  
556 749 ▶ if (defined $remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $remaining) 471 278 749
557 471 ▶ if ($remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 308 163 471
558 # Write chunk data out as it arrives
559 308 my $data = substr $$input, 0, _min($remaining, length $$input), '';
560 308 $buf->print($data);
561 308 $remaining -= length $data;
562 308 $length += length $data;
563 308 next;
564 }
565  
566 163 ▶ last if length $$input < 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $$input < 2 5 158 163
567 158 ▶ return 400 unless substr($$input, 0, 2, '') eq "\015\012";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($$input, 0, 2, "") eq "\r\n" 1 157 158
568 157 undef $remaining;
569 157 next;
570 }
571  
572 278 my $eol = index $$input, "\015\012";
573 278 ▶ if ($eol < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol < 0) 26 252 278
574 26 ▶ return 400 if length $$input > MAX_CHUNK_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if length $$input > 4096 2 24 26
575 24 last;
576 }
577 252 ▶ return 400 if $eol > MAX_CHUNK_LINE;
Branch: 1 branch
BranchTrueFalseTotal
if $eol > 4096 1 251 252
578  
579 251 my $chunk_len = _parse_chunk_size(substr $$input, 0, $eol + 2, '');
580 251 ▶ return 400 unless defined $chunk_len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk_len 9 242 251
581  
582 242 ▶ if ($chunk_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_len == 0) { } 80 162 242
583 80 $trailer_size = 0;
584 } else {
585 162 $remaining = $chunk_len;
586 }
587 }
588  
589 # The connection ended before the body did
590 288 ▶ return 400 unless $read_more->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$read_more() 6 278 284
591 }
592  
593 # The buffer could not store the whole body, e.g. the disk is full
594 76 ▶ return 500 unless $buf->size == $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf->size == $length 1 75 76
595  
596 75 $env->{CONTENT_LENGTH} = $length;
597 75 $env->{'psgi.input'} = $buf->rewind;
598 } elsif (my $cl = $env->{CONTENT_LENGTH}) {
599 24 my $buf = Plack::TempBuffer->new($cl);
600 24 ▶ while ($cl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl > 0) 39 18 57
601 39 ▶ if ($$input eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$input eq "") 22 17 39
602 # The connection ended before the body did
603 22 ▶ return 400 unless $read_more->();
Branch: 1 branch
BranchTrueFalseTotal
unless &$read_more() 2 16 18
604 }
605  
606 33 my $data = substr $$input, 0, _min($cl, length $$input), '';
607 33 $cl -= length $data;
608 33 $buf->print($data);
609 }
610  
611 # The buffer could not store the whole body, e.g. the disk is full
612 18 ▶ return 500 unless $buf->size == $env->{CONTENT_LENGTH};
Branch: 1 branch
BranchTrueFalseTotal
unless $buf->size == $env->{"CONTENT_LENGTH"} 1 17 18
613  
614 17 $env->{'psgi.input'} = $buf->rewind;
615 } else {
616 130 $env->{'psgi.input'} = $null_io;
617 }
618  
619 222 return;
620 }
621  
622 # Older perls truncate substr() lengths to 32 bits, so never pass more
623 # than what is in the buffer.
624 341 ▶sub _min { $_[0] < $_[1] ? $_[0] : $_[1] }
Subroutine _min: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 154 187 341
625  
626 # chunk-size [ chunk-ext ] CRLF (RFC 9112 section 7.1). Returns undef if
627 # the line is malformed or the size is too large to count exactly.
628 251 ▶sub _parse_chunk_size {
Subroutine _parse_chunk_size: called CC=3 SCAR=11.0
629 251 my $line = shift;
630  
631 251 ▶ $line =~ /\A([0-9A-Fa-f]+)(?:[ \t]*;[^\015\012]*)?\015\012\z/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A([0-9A-Fa-f]+)(?:[ \t]*;[^\015\012]*)?\015\012\z/ or return 243 8 251
632 or return;
633 243 (my $size = $1) =~ s/\A0+(?=.)//;
634 243 ▶ return if length $size > 13;
Branch: 1 branch
BranchTrueFalseTotal
if length $size > 13 1 242 243
635  
636 242 return hex $size;
637 }
638  
639 228 ▶sub _finalize_response {
Subroutine _finalize_response: called CC=19 SCAR=29.8
640 228 my($self, $env, $res) = @_;
641  
642 228 ▶ if ($env->{'psgix.harakiri.commit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.harakiri.commit"}) 19 209 228
643 19 $self->{client}->{keepalive} = 0;
644 19 $self->{client}->{harakiri} = 1;
645 }
646  
647 228 my $protocol = $env->{SERVER_PROTOCOL};
648 228 my $status = $res->[0];
649 228 my $message = status_message($status);
650  
651 # These responses never have a body (RFC 9110 sections 6.4.1 and 9.3.2)
652 228 ▶ my $no_body = $env->{REQUEST_METHOD} eq 'HEAD' || $status =~ /^(?:1\d\d|204|304)$/;
Condition100%3/3
$env->{'REQUEST_METHOD'} eq 'HEAD' || $status =~ /^(?:1\d\d|204|304)$/
l!l&&r!l&&!r
912207
MC/DC100%2/2
$env->{'REQUEST_METHOD'} eq 'HEAD' || $status =~ /^(?:1\d\d|204|304)$/
$env->{'REQUEST_METHOD'} eq 'HEAD' $status =~ /^(?:1\d\d|204|304)$/
Truth table100%3/3
A || B
A= $env->{'REQUEST_METHOD'} eq 'HEAD'B= $status =~ /^(?:1\d\d|204|304)$/
ABresult
1X1
011
000
653  
654 228 my(@headers, %headers);
655 228 push @headers, "$protocol $status $message";
656  
657 # Switch on Transfer-Encoding: chunked if we don't know Content-Length.
658 228 my $chunked;
659 228 my $headers = $res->[1];
660 228 ▶ for (my $i = 0; $i < @$headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 384 228 612
661 384 my $k = $headers->[$i];
662 384 my $v = $headers->[$i + 1];
663 384 ▶ next if $k eq 'Connection';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "Connection" 0 384 384
664 # 1xx and 204 responses must not have these (RFC 9110 8.6, RFC 9112 6.1)
665 384 ▶ next if $status =~ /^(?:1\d\d|204)$/ && $k =~ /^(?:Content-Length|Transfer-Encoding)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $status =~ /^(?:1\d\d|204)$/ and $k =~ /^(?:Content-Length|Transfer-Encoding)$/i 7 377 384
Condition100%3/3
$status =~ /^(?:1\d\d|204)$/ and $k =~ /^(?:Content-Length|Transfer-Encoding)$/i
!ll&&!rl&&r
37077
MC/DC100%2/2
$status =~ /^(?:1\d\d|204)$/ and $k =~ /^(?:Content-Length|Transfer-Encoding)$/i
$status =~ /^(?:1\d\d|204)$/ $k =~ /^(?:Content-Length|Transfer-Encoding)$/i
Truth table100%3/3
A and B
A= $status =~ /^(?:1\d\d|204)$/B= $k =~ /^(?:Content-Length|Transfer-Encoding)$/i
ABresult
0X0
100
111
666 377 push @headers, "$k: $v";
667 377 $headers{lc $k} = $v;
668 }
669  
670 228 ▶ if ( $no_body ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($no_body) { } 21 207 228
elsif ($protocol eq 'HTTP/1.1') { } 184 23 207
671 # Do not frame (or send) a body, whatever the headers say
672 }
673 elsif ( $protocol eq 'HTTP/1.1' ) {
674 184 ▶ if ( !exists $headers{'content-length'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $headers{'content-length'}) { } 82 102 184
elsif (my $te = $headers{'transfer-encoding'}) { } 0 102 102
675 82 DEBUG && warn "[$$] Using chunked transfer-encoding to send unknown length body\n";
676 82 push @headers, 'Transfer-Encoding: chunked';
677 82 $chunked = 1;
678 }
679 elsif ( my $te = $headers{'transfer-encoding'} ) {
680 0 ▶ if ( $te eq 'chunked' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te eq "chunked") 0 0 0
681 0 DEBUG && warn "[$$] Chunked transfer-encoding set for response\n";
682 0 $chunked = 1;
683 }
684 }
685 } else {
686 23 ▶ if ( !exists $headers{'content-length'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $headers{"content-length"}) 0 23 23
687 0 DEBUG && warn "[$$] Disabling keep-alive after sending unknown length body on $protocol\n";
688 0 $self->{client}->{keepalive} = 0;
689 }
690 }
691  
692 228 ▶ if ( ! $headers{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers{"date"}) 228 0 228
693 228 push @headers, "Date: " . time2str( time() );
694 }
695  
696 # Should we keep the connection open?
697 228 ▶ if ( $self->{client}->{keepalive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'client'}{'keepalive'}) { } 122 106 228
698 122 push @headers, 'Connection: keep-alive';
699 } else {
700 106 push @headers, 'Connection: close';
701 }
702  
703 228 my $conn = $self->{server}->{client};
704  
705 # Buffer the headers so they are sent with the first write() call
706 # This reduces the number of TCP packets we are sending
707 228 _syswrite($conn, \(join( $CRLF, @headers, '' ) . $CRLF));
708  
709 228 ▶ if (defined $res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->[2]) { } 220 8 228
710 220 ▶ if ($no_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_body) 17 203 220
711 # Release the body without reading it
712 17 ▶ $res->[2]->close if ref $res->[2] ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res->[2] ne "ARRAY" 2 15 17
713 17 return;
714 }
715 206 ▶ Plack::Util::foreach($res->[2], sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
716 206 my $buffer = $_[0];
717 206 ▶ if ($chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) 81 125 206
718 81 my $len = length $buffer;
719 81 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 81 81
720 81 $buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF;
721 }
722 206 _syswrite($conn, \$buffer);
723 203 });
724 203 ▶ _syswrite($conn, \"0$CRLF$CRLF") if $chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 78 125 203
725 } else {
726 return Plack::Util::inline_object
727 14 ▶ write => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
728 14 ▶ return if $no_body;
Branch: 1 branch
BranchTrueFalseTotal
if $no_body 8 6 14
729 6 my $buffer = $_[0];
730 6 ▶ if ($chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) 6 0 6
731 6 my $len = length $buffer;
732 6 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 1 5 6
733 5 $buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF;
734 }
735 5 _syswrite($conn, \$buffer);
736 },
737 8 ▶ close => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
738 8 ▶ _syswrite($conn, \"0$CRLF$CRLF") if $chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 4 4 8
739 8 };
740 }
741 }
742  
743 523 ▶sub _syswrite {
Subroutine _syswrite: called CC=6 SCAR=23.0
744 523 my ($conn, $buffer_ref) = @_;
745  
746 523 my $amount = length $$buffer_ref;
747 523 my $offset = 0;
748  
749 523 ▶ while ($amount > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount > 0) 523 523 1046
750 523 my $len = syswrite($conn, $$buffer_ref, $amount, $offset);
751  
752 523 ▶ if (not defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 0 523 523
753 0 ▶ return if $! == EPIPE;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 32 0 0 0
754 0 ▶ return if $! == ECONNRESET;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 104 0 0 0
755 0 ▶ redo if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
756 0 die "write error: $!";
757 }
758  
759 523 $amount -= $len;
760 523 $offset += $len;
761  
762 523 DEBUG && warn "[$$] Wrote $len byte", ($len == 1 ? '' : 's'), "\n";
763 }
764 }
765  
766 612 ▶sub _sysread {
Subroutine _sysread: called CC=3 SCAR=12.5
767 612 while (1) {
768 612 my $len = sysread $_[0], $_[1], $_[2];
769 604 ▶ return $len if defined $len or $! != EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len or $! != 4 604 0 604
Condition33%1/3
defined $len or $! != 4
l!l&&r!l&&!r
60400
MC/DC0%0/2
defined $len or $! != 4
defined $len $! != 4
Truth table33%1/3
A or B
A= defined $lenB= $! != 4
ABresult
1X1
011
000
770 }
771 }
772  
773 1 ▶sub _write_informational {
Subroutine _write_informational: called CC=2 SCAR=6.9
774 1 my ($conn, $code, $headers) = @_;
775 1 my $message = HTTP::Status::status_message($code);
776 1 my @lines = "HTTP/1.1 $code $message";
777 1 ▶ for (my $i = 0; $i < @$headers; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$headers) 1 1 2
778 1 my $k = $headers->[$i];
779 1 my $v = $headers->[$i + 1];
780 1 push @lines, "$k: $v" ;
781 }
782 1 _syswrite($conn, \join($CRLF, @lines, $CRLF));
783  
784 1 DEBUG && warn "[$$] Sent $code $message response\n";
785 }
786  
787 196 ▶sub post_client_connection_hook {
Subroutine post_client_connection_hook: called CC=2 SCAR=6.9
Pod documented
788 196 my $self = shift;
789 196 ▶ if ($self->{client}->{harakiri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{"harakiri"}) 19 177 196
790 19 exit;
791 }
792 }
793  
794 137 1;
↑ Summary Plack/Handler/Starman.pm »