blib/lib/Plack/Handler/Gazelle.pm

filter: Statementstmt 70.0% 124 / 177 Branchbran 36.2% 29 / 80 Conditioncond 43.7% 14 / 32 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 86.2% 25 / 29 Podpod 0.0% 0 / 3 totaltotal 56.9% 193 / 339 CC 55 SCAR 35.2
CC
55
Cov
58%
__ANON__
67.1
new
65.4
_handle_response
24.1
CRAP 33.6
↑ Summary builder/MyBuilder.pm »
1 package Plack::Handler::Gazelle;
2  
3 19 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Stream::Buffered;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use POSIX qw(EINTR EAGAIN EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use Parallel::Prefork;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Server::Starter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 19 our $VERSION = "0.42";
17  
18 19 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 XSLoader::load(__PACKAGE__, $VERSION);
20  
21 19 ▶use constant MAX_REQUEST_SIZE => 131072;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 ▶use constant CHUNKSIZE => 64 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 19 my $null_io = do { open my $io, "<", \""; $io };
25 19 my $bad_response = [ 400, [ 'Content-Type' => 'text/plain', 'Connection' => 'close' ], [ 'Bad Request' ] ];
26  
27 sub new {
28 18 ▶ my($class, %args) = @_;
Subroutine new: called CC=17 SCAR=41.8
Pod undocumented
29  
30 # setup before instantiation
31 18 my $listen_sock;
32 18 ▶ if (defined $ENV{SERVER_STARTER_PORT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"SERVER_STARTER_PORT"}) 0 18 18
33 0 my ($hostport, $fd) = %{Server::Starter::server_ports()};
34 0 ▶ if ($hostport =~ /(.*):(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostport =~ /(.*):(\d+)/) { } 0 0 0
35 0 $args{host} = $1;
36 0 $args{port} = $2;
37 } else {
38 0 $args{port} = $hostport;
39 }
40 0 ▶ $listen_sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
$listen_sock = "IO::Socket::INET"->new("Proto", "tcp") or die "failed to create socket:$!" 0 0 0
41 Proto => 'tcp',
42 ) or die "failed to create socket:$!";
43 0 ▶ $listen_sock->fdopen($fd, 'w')
Branch: 1 branch
BranchTrueFalseTotal
$listen_sock->fdopen($fd, "w") or die "failed to bind to listening socket:$!" 0 0 0
44 or die "failed to bind to listening socket:$!";
45 }
46  
47 18 my $max_workers = 10;
48 18 for (qw(max_workers workers)) {
49 $max_workers = delete $args{$_}
50 36 ▶ if defined $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$_} 3 33 36
51 }
52  
53 my $self = bless {
54 server_software => $args{server_software} || $class,
55 ▶ server_ready => $args{server_ready} || sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
56 listen_sock => $listen_sock,
57 host => $args{host} || 0,
58 port => $args{port} || 8080,
59 timeout => $args{timeout} || 300,
60 max_workers => $max_workers,
61 min_reqs_per_child => (
62 defined $args{min_reqs_per_child}
63 ? $args{min_reqs_per_child} : undef,
64 ),
65 max_reqs_per_child => (
66 $args{max_reqs_per_child} || $args{max_requests} || 1000,
67 ),
68 spawn_interval => $args{spawn_interval} || 0,
69 err_respawn_interval => (
70 defined $args{err_respawn_interval}
71 ? $args{err_respawn_interval} : undef,
72 18 ▶ ),
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'min_reqs_per_child'} ? : 0 18 18
defined $args{'err_respawn_interval'} ? : 0 18 18
Condition47%9/19
$args{'server_software'} || $class
l!l&&r!l&&!r
0180
$args{'server_ready'} || sub { }
l!l&&r!l&&!r
0180
$args{'host'} || 0
l!l
153
$args{'port'} || 8080
l!l
180
$args{'timeout'} || 300
l!l
018
$args{'max_reqs_per_child'} || $args{'max_requests'} || 1000
l!l
018
$args{"max_reqs_per_child"} or $args{"max_requests"}
l!l&&r!l&&!r
0018
$args{'spawn_interval'} || 0
l!l
018
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/DC100%1/1
$args{'host'} || 0
$args{'host'}
MC/DC0%0/1
$args{'port'} || 8080
$args{'port'}
MC/DC0%0/1
$args{'timeout'} || 300
$args{'timeout'}
MC/DC0%0/2
$args{'max_reqs_per_child'} || $args{'max_requests'} || 1000
$args{"max_reqs_per_child"} $args{"max_requests"}
MC/DC0%0/1
$args{'spawn_interval'} || 0
$args{'spawn_interval'}
Truth table33%1/3
A || B
A= $args{'server_software'}B= $class
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $args{'server_ready'}B= sub { }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{'host'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'port'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
Truth table33%1/3
A or B
A= $args{"max_reqs_per_child"}B= $args{"max_requests"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'spawn_interval'}
Aresult
11
00
73 }, $class;
74  
75 18 $self;
76 }
77  
78 sub setup_listener {
79 18 ▶ my $self = shift;
Subroutine setup_listener: called CC=5 SCAR=18.1
Pod undocumented
80 $self->{listen_sock} ||= IO::Socket::INET->new(
81 Listen => SOMAXCONN,
82 LocalPort => $self->{port},
83 LocalAddr => $self->{host},
84 18 ▶ Proto => 'tcp',
Branch: 1 branch
BranchTrueFalseTotal
$self->{"listen_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"}:$!] 0 18 18
Condition33%1/3
$self->{"listen_sock"} ||= "IO::Socket::INET"->new("Listen", 4096, "LocalPort", $self->{"port"}, "LocalAddr", $self->{"host"}, "Proto", "tcp", "ReuseAddr", 1)
l!l&&r!l&&!r
0180
MC/DC0%0/2
$self->{"listen_sock"} ||= "IO::Socket::INET"->new("Listen", 4096, "LocalPort", $self->{"port"}, "LocalAddr", $self->{"host"}, "Proto", "tcp", "ReuseAddr", 1)
$self->{"listen_sock"} "IO::Socket::INET"->new("Listen", 4096, "LocalPort", $self->{"port"}, "LocalAddr", $self->{"host"}, "Proto", "tcp", "ReuseAddr", 1)
Truth table33%1/3
A ||= B
A= $self->{"listen_sock"}B= "IO::Socket::INET"->new("Listen", 4096, "LocalPort", $self->{"port"}, "LocalAddr", $self->{"host"}, "Proto", "tcp", "ReuseAddr", 1)
ABresult
1X1
011
000
85 ReuseAddr => 1,
86 ) or die "failed to listen to port $self->{port}:$!";
87  
88 18 my $family = Socket::sockaddr_family(getsockname($self->{listen_sock}));
89 18 $self->{_listen_sock_is_tcp} = $family != AF_UNIX;
90  
91 # set defer accept
92 18 ▶ if ($^O eq 'linux' && $self->{_listen_sock_is_tcp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux" and $self->{"_listen_sock_is_tcp"}) 18 0 18
Condition33%1/3
$^O eq "linux" and $self->{"_listen_sock_is_tcp"}
!ll&&!rl&&r
0018
MC/DC0%0/2
$^O eq "linux" and $self->{"_listen_sock_is_tcp"}
$^O eq "linux" $self->{"_listen_sock_is_tcp"}
Truth table33%1/3
A and B
A= $^O eq "linux"B= $self->{"_listen_sock_is_tcp"}
ABresult
0X0
100
111
93 18 setsockopt($self->{listen_sock}, IPPROTO_TCP, 9, 1);
94 }
95 18 $self->{server_ready}->($self);
96 }
97  
98  
99 sub run {
100 18 ▶ my($self, $app) = @_;
Subroutine run: called CC=5 SCAR=19.4
Pod undocumented
101 18 $self->setup_listener();
102 # use Parallel::Prefork
103 my %pm_args = (
104 max_workers => $self->{max_workers},
105 18 trap_signals => {
106 TERM => 'TERM',
107 HUP => 'TERM',
108 },
109 );
110 18 ▶ if (defined $self->{spawn_interval}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"spawn_interval"}) 18 0 18
111 18 $pm_args{trap_signals}{USR1} = [ 'TERM', $self->{spawn_interval} ];
112 18 $pm_args{spawn_interval} = $self->{spawn_interval};
113 }
114 18 ▶ if (defined $self->{err_respawn_interval}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"err_respawn_interval"}) 0 18 18
115 0 $pm_args{err_respawn_interval} = $self->{err_respawn_interval};
116 }
117 18 my $pm = Parallel::Prefork->new(\%pm_args);
118 18 ▶ while ($pm->signal_received !~ /^(TERM|USR1)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pm->signal_received =~ /^(TERM|USR1)$/) 18 17 35
119 $pm->start(sub{
120 1 ▶ srand((rand() * 2 ** 30) ^ $$ ^ time);
Subroutine __ANON__: called CC=20 SCAR=42.1
121  
122 my $max_reqs_per_child = $self->_calc_minmax_per_child(
123 $self->{max_reqs_per_child},
124 $self->{min_reqs_per_child}
125 1 );
126  
127 1 my $proc_req_count = 0;
128 1 $self->{term_received} = 0;
129 local $SIG{TERM} = sub {
130 1 ▶ $self->{term_received}++;
Subroutine __ANON__: called CC=1 SCAR=0.0
131 1 };
132 1 local $SIG{PIPE} = 'IGNORE';
133 PROC_LOOP:
134 1 ▶ while ( $proc_req_count < $max_reqs_per_child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_req_count < $max_reqs_per_child) 15 0 15
135 15 ▶ exit 0 if $self->{term_received};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"term_received"} 1 14 15
136 14 ▶ if ( my ($conn, $buf, $env) = accept_psgi(
Branch: 1 branch
BranchTrueFalseTotal
if (() = accept_psgi(fileno $self->{"listen_sock"}, $self->{"timeout"}, $self->{"_listen_sock_is_tcp"}, $self->{"host"} || 0, $self->{"port"} || 0)) 8 6 14
Condition50%2/4
$self->{'host'} || 0
l!l
014
$self->{'port'} || 0
l!l
140
MC/DC0%0/1
$self->{'host'} || 0
$self->{'host'}
MC/DC0%0/1
$self->{'port'} || 0
$self->{'port'}
Truth table50%1/2
A
A= $self->{'host'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'port'}
Aresult
11
00
137 fileno($self->{listen_sock}), $self->{timeout}, $self->{_listen_sock_is_tcp},
138 $self->{host} || 0, $self->{port} || 0
139 ) ) {
140 8 ▶ my $guard = guard { close_client($conn) };
Subroutine __ANON__: called CC=1 SCAR=0.0
141 8 ++$proc_req_count;
142  
143 8 my $res = $bad_response;
144 19 ▶ my $chunked = do { no warnings; lc delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' };
Subroutine BEGIN: called CC=1 SCAR=0.0
145 8 ▶ if (my $cl = $env->{CONTENT_LENGTH}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cl = $env->{'CONTENT_LENGTH'}) { } 1 7 8
elsif ($chunked) { } 0 7 7
146 1 my $buffer = Stream::Buffered->new($cl);
147 1 ▶ while ($cl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl > 0) 1 1 2
148 1 my $chunk = "";
149 1 ▶ if (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 1 0 1
150 1 $chunk = $buf;
151 1 $buf = '';
152 } else {
153 read_timeout(
154 $conn, \$chunk, $cl, 0, $self->{timeout})
155 0 ▶ or next PROC_LOOP;
Branch: 1 branch
BranchTrueFalseTotal
read_timeout($conn, \$chunk, $cl, 0, $self->{"timeout"}) or next PROC_LOOP 0 0 0
156 }
157 1 $buffer->print($chunk);
158 1 $cl -= length $chunk;
159 }
160 1 $env->{'psgi.input'} = $buffer->rewind;
161 } elsif ( $chunked ) {
162 0 my $buffer = Stream::Buffered->new($cl);
163 0 my $chunk_buffer = '';
164 0 my $length;
165 0 DECHUNK: while(1) {
166 0 my $chunk = "";
167 0 ▶ if ( length $buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) { } 0 0 0
168 0 $chunk = $buf;
169 0 $buf = '';
170 }
171 else {
172 read_timeout(
173 $conn, \$chunk, 16384, 0, $self->{timeout})
174 0 ▶ or next PROC_LOOP;
Branch: 1 branch
BranchTrueFalseTotal
read_timeout($conn, \$chunk, 16384, 0, $self->{"timeout"}) or next PROC_LOOP 0 0 0
175 }
176  
177 0 $chunk_buffer .= $chunk;
178 0 ▶ while ( $chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)//) 0 0 0
179 0 my $trailer = $1;
180 0 my $chunk_len = hex $2;
181 0 ▶ if ($chunk_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk_len == 0) { } 0 0 0
elsif (length $chunk_buffer < $chunk_len + 2) { } 0 0 0
182 0 last DECHUNK;
183 } elsif (length $chunk_buffer < $chunk_len + 2) {
184 0 $chunk_buffer = $trailer . $chunk_buffer;
185 0 last;
186 }
187 0 $buffer->print(substr $chunk_buffer, 0, $chunk_len, '');
188 0 $chunk_buffer =~ s/^\015\012//;
189 0 $length += $chunk_len;
190 }
191 }
192 0 $env->{CONTENT_LENGTH} = $length;
193 0 $env->{'psgi.input'} = $buffer->rewind;
194 } else {
195 7 $env->{'psgi.input'} = $null_io;
196 }
197 8 $res = Plack::Util::run_app $app, $env;
198 8 ▶ my $use_chunked = $env->{"SERVER_PROTOCOL"} eq 'HTTP/1.1' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$env->{'SERVER_PROTOCOL'} eq 'HTTP/1.1' ? : 0 8 8
199 8 ▶ if (ref $res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 8 0 8
elsif (ref $res eq 'CODE') { } 0 0 0
200 8 $self->_handle_response($res, $conn, $use_chunked);
201 } elsif (ref $res eq 'CODE') {
202 $res->(sub {
203 0 ▶ $self->_handle_response($_[0], $conn, $use_chunked);
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 });
205 } else {
206 0 die "Bad response $res";
207 }
208 8 ▶ if ($env->{'psgix.harakiri.commit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.harakiri.commit"}) 0 8 8
209 0 exit 0;
210 }
211 }
212 }
213 18 });
214 }
215 17 ▶ while ($pm->wait_all_children(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pm->wait_all_children(1)) 15 17 32
216 15 $pm->signal_all_children('TERM');
217 }
218 }
219  
220 sub _calc_minmax_per_child {
221 1 ▶ my $self = shift;
Subroutine _calc_minmax_per_child: called CC=2 SCAR=7.4
222 1 my ($max,$min) = @_;
223 1 ▶ if (defined $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min) { } 0 1 1
224 0 return $max - int(($max - $min + 1) * rand);
225 } else {
226 1 return $max;
227 }
228 }
229  
230 sub _handle_response {
231 8 ▶ my($self, $res, $conn, $use_chunked) = @_;
Subroutine _handle_response: called CC=6 SCAR=31.8
232 8 my $status_code = $res->[0];
233 8 my $headers = $res->[1];
234 8 my $body = $res->[2];
235  
236 8 ▶ if (defined $body && ref $body eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and ref $body eq "ARRAY") 8 0 8
Condition33%1/3
defined $body and ref $body eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
defined $body and ref $body eq "ARRAY"
defined $body ref $body eq "ARRAY"
Truth table33%1/3
A and B
A= defined $bodyB= ref $body eq "ARRAY"
ABresult
0X0
100
111
237 8 write_psgi_response($conn, $self->{timeout}, $status_code, $headers , $body, $use_chunked);
238 8 return;
239 }
240  
241 0 ▶ write_psgi_response_header($conn, $self->{timeout}, $status_code, $headers, [], $use_chunked) or return;
Branch: 1 branch
BranchTrueFalseTotal
write_psgi_response_header($conn, $self->{"timeout"}, $status_code, $headers, scalar [], $use_chunked) or return 0 0 0
242  
243 0 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 0 0 0
244 0 my $failed;
245 Plack::Util::foreach(
246 $body,
247 sub {
248 0 ▶ return if $failed;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 0 0
249 0 my $ret;
250 0 ▶ if ( $use_chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked) { } 0 0 0
251 0 $ret = write_chunk($conn, $_[0], 0, $self->{timeout});
252 }
253 else {
254 0 $ret = write_all($conn, $_[0], 0, $self->{timeout});
255 }
256 0 ▶ $failed = 1 if ! defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ret 0 0 0
257 },
258 0 );
259 0 ▶ write_all($conn, "0\015\012\015\012", 0, $self->{timeout}) if $use_chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $use_chunked 0 0 0
260 } else {
261 return Plack::Util::inline_object
262 write => sub {
263 0 ▶ if ( $use_chunked ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked) { } 0 0 0
264 0 write_chunk($conn, $_[0], 0, $self->{timeout});
265 }
266 else {
267 0 write_all($conn, $_[0], 0, $self->{timeout});
268 }
269 },
270 close => sub {
271 0 ▶ write_all($conn, "0\015\012\015\012", 0, $self->{timeout}) if $use_chunked;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $use_chunked 0 0 0
272 0 };
273 }
274 }
275  
276 19 1;
277  
278  
↑ Summary builder/MyBuilder.pm »