blib/lib/Plack/Handler/Net/FastCGI.pm

filter: Statementstmt 29.5% 47 / 159 Branchbran 1.5% 1 / 64 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 46.4% 13 / 28 Podpod 0.0% 0 / 3 totaltotal 21.9% 62 / 283 CC 40 SCAR 46.1
CC
40
Cov
20%
_handle_connection
306.0
run
110.0
_handle_request
42.0
CRAP 100.4
↑ Summary
1 package Plack::Handler::Net::FastCGI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use IO::Socket qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::FastCGI 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::FastCGI::Constant qw[:common :type :flag :role :protocol_status];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::FastCGI::IO qw[:all];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::FastCGI::Protocol qw[:all];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::TempBuffer qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::Util qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape qw[uri_unescape];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
14 1 our $VERSION = '0.01';
15  
16 1 eval {
17 1 require PerlIO::code;
18 };
19 1 ▶ my $mode = $@ ? ">:via(@{[__PACKAGE__]})" : '>:Code';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 0 1
20 0 ▶ *PERLIO_MODE = sub () { $mode };
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 }
22  
23 sub DEBUG () { 0 }
24  
25 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
26 0 my $class = shift;
27 0 my $self = bless { @_ }, $class;
28 0 ▶ $self->{listen} ||= [ ":$self->{port}" ] if $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"port"} 0 0 0
Condition0%0/2
$self->{"listen"} ||= [qq[:$self->{"port"}]]
l!l
00
MC/DC0%0/1
$self->{"listen"} ||= [qq[:$self->{"port"}]]
$self->{"listen"}
Truth table0%0/2
A
A= $self->{"listen"}
Aresult
11
00
29 $self->{values} ||= {
30 0 ▶ FCGI_MAX_CONNS => 1, # maximum number of concurrent transport connections this application will accept
Condition0%0/2
$self->{"values"} ||= {"FCGI_MAX_CONNS", 1, "FCGI_MAX_REQS", 1, "FCGI_MPXS_CONNS", 0}
l!l
00
MC/DC0%0/1
$self->{"values"} ||= {"FCGI_MAX_CONNS", 1, "FCGI_MAX_REQS", 1, "FCGI_MPXS_CONNS", 0}
$self->{"values"}
Truth table0%0/2
A
A= $self->{"values"}
Aresult
11
00
31 FCGI_MAX_REQS => 1, # maximum number of concurrent requests this application will accept
32 FCGI_MPXS_CONNS => 0, # this implementation can't multiplex
33 };
34 0 $self;
35 }
36  
37 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
38 1 require Socket;
39 1 ▶ my $HAS_AF_UNIX = eval { Socket->import('AF_UNIX'); defined(my $v = &AF_UNIX) } && !$@;
Condition33%1/3
eval { do { 'Socket'->import('AF_UNIX'); defined(my $v = &AF_UNIX) } } && !$@
!ll&&!rl&&r
001
MC/DC0%0/2
eval { do { 'Socket'->import('AF_UNIX'); defined(my $v = &AF_UNIX) } } && !$@
eval { do { 'Socket'->import('AF_UNIX'); defined(my $v = &AF_UNIX) } } !$@
Truth table33%1/3
A && not(B)
A= eval { do { 'Socket'->import('AF_UNIX'); defined(my $v = &AF_UNIX) } }B= !$@
ABresult
0X0
100
111
40 0 ▶ *HAS_AF_UNIX = sub () { $HAS_AF_UNIX };
Subroutine __ANON__: not called CC=1 SCAR=1.2
41 }
42  
43 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
44 0 my ($self, $app) = @_;
45 0 $self->{app} = $app;
46  
47 0 my $socket;
48 my $proto;
49 0 my $port;
50  
51 0 ▶ if ($self->{listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'listen'}) { } 0 0 0
52 0 $port = $self->{listen}->[0];
53 0 ▶ if ($port =~ s/^://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ s/^://) { } 0 0 0
54 0 $proto = 'tcp';
55 0 ▶ $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::INET"->new("Listen", 5, "LocalPort", $port, "Reuse", 1) or die "Couldn't create listener socket: $!" 0 0 0
56 Listen => 5,
57 LocalPort => $port,
58 Reuse => 1
59 ) or die "Couldn't create listener socket: $!";
60 } else {
61 0 $proto = 'unix';
62 0 ▶ $socket = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::UNIX"->new("Listen", 5, "Local", $port) or die "Couldn't create UNIX listener socket: $!" 0 0 0
63 Listen => 5,
64 Local => $port,
65 ) or die "Couldn't create UNIX listener socket: $!";
66 }
67 }
68 else {
69 0 ▶ (-S STDIN)
Branch: 1 branch
BranchTrueFalseTotal
-S STDIN or die "Standard input is not a socket: specify a listen location" 0 0 0
70 || die "Standard input is not a socket: specify a listen location";
71  
72 0 $socket = \*STDIN;
73 0 $proto = 'tcp';
74  
75 0 if (HAS_AF_UNIX) {
76 0 my $sockaddr = getsockname(*STDIN);
77 0 ▶ if (unpack('S', $sockaddr) == &Socket::AF_UNIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (unpack("S", $sockaddr) == &Socket::AF_UNIX) 0 0 0
78 0 $proto = 'unix';
79 }
80 }
81 }
82  
83 $self->{server_ready}->({
84 host => 'localhost',
85 port => $port,
86 proto => $proto,
87 server_software => 'Plack::Handler::Net::FastCGI',
88 0 ▶ }) if $self->{server_ready} && $proto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"server_ready"} and $proto 0 0 0
Condition0%0/3
$self->{"server_ready"} and $proto
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"server_ready"} and $proto
$self->{"server_ready"} $proto
Truth table0%0/3
A and B
A= $self->{"server_ready"}B= $proto
ABresult
0X0
100
111
89  
90 0 ▶ while (accept(my $connection, $socket)) {
Branch: 1 branch
BranchTrueFalseTotal
if (accept my $connection, $socket) 0 0 0
91 0 $self->_handle_connection($connection);
92 }
93 }
94  
95 0 ▶sub _handle_request {
Subroutine _handle_request: not called CC=6 SCAR=37.4
96 0 my($self, $env, $stdin, $stdout, $stderr) = @_;
97  
98 $env = {
99 %$env,
100 'psgi.version' => [1,1],
101 0 ▶ 'psgi.url_scheme' => ($env->{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
Branch: 1 branch
BranchTrueFalseTotal
($env->{'HTTPS'} || 'off') =~ /^(?:on|1)$/i ? : 0 0 0
Condition0%0/2
$env->{'HTTPS'} || 'off'
l!l
00
MC/DC0%0/1
$env->{'HTTPS'} || 'off'
$env->{'HTTPS'}
Truth table0%0/2
A
A= $env->{'HTTPS'}
Aresult
11
00
102 'psgi.input' => $stdin,
103 'psgi.errors' => $stderr,
104 'psgi.multithread' => Plack::Util::FALSE,
105 'psgi.multiprocess' => Plack::Util::FALSE,
106 'psgi.run_once' => Plack::Util::FALSE,
107 'psgi.streaming' => Plack::Util::TRUE,
108 'psgi.nonblocking' => Plack::Util::FALSE,
109 };
110  
111 0 delete $env->{HTTP_CONTENT_TYPE};
112 0 delete $env->{HTTP_CONTENT_LENGTH};
113  
114 # lighttpd munges multiple slashes in PATH_INFO into one. Try recovering it
115 0 my $uri = URI->new("http://localhost" . $env->{REQUEST_URI});
116 0 $env->{PATH_INFO} = uri_unescape($uri->path);
117 0 $env->{PATH_INFO} =~ s/^\Q$env->{SCRIPT_NAME}\E//;
118  
119 # root access for mod_fastcgi
120 0 ▶ if (!exists $env->{PATH_INFO}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $env->{"PATH_INFO"}) 0 0 0
121 0 $env->{PATH_INFO} = '';
122 }
123  
124 0 my $res = Plack::Util::run_app $self->{app}, $env;
125  
126 0 ▶ if (ref $res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 0 0 0
elsif (ref $res eq 'CODE') { } 0 0 0
127 0 $self->_handle_response($res, $stdout);
128 } elsif (ref $res eq 'CODE') {
129 0 ▶ $res->(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 $self->_handle_response($_[0], $stdout);
131 0 });
132 } else {
133 0 die "Bad response $res";
134 }
135 }
136  
137 0 ▶sub _handle_response {
Subroutine _handle_response: not called CC=3 SCAR=24.8
138 0 my ($self, $res, $stdout) = @_;
139  
140 0 my $hdrs;
141 0 $hdrs = "Status: $res->[0]\015\012";
142  
143 0 my $headers = $res->[1];
144 0 ▶ while (my ($k, $v) = splice @$headers, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$headers, 0, 2)) 0 0 0
145 0 $hdrs .= "$k: $v\015\012";
146 }
147 0 $hdrs .= "\015\012";
148  
149 0 print {$stdout} $hdrs;
150  
151 0 ▶ my $cb = sub { print {$stdout} $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 my $body = $res->[2];
153 0 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 0 0 0
154 0 Plack::Util::foreach($body, $cb);
155 }
156 else {
157 return Plack::Util::inline_object
158 write => $cb,
159 0 ▶ close => sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 }
161 }
162  
163 1 our $STDOUT_BUFFER_SIZE = 8192;
164 1 our $STDERR_BUFFER_SIZE = 0;
165  
166 1 ▶use warnings FATAL => 'Net::FastCGI::IO';
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 0 ▶sub _handle_connection {
Subroutine _handle_connection: not called CC=17 SCAR=57.2
169 0 my($self, $socket) = @_;
170  
171 0 my ( $current_id, # id of the request we are currently processing
172 $stdin, # buffer for stdin
173 $stdout, # buffer for stdout
174 $stderr, # buffer for stderr
175 $params, # buffer for params (environ)
176 $done, # done with connection?
177 $keep_conn ); # more requests on this connection?
178  
179 0 ($current_id, $stdin, $stdout, $stderr) = (0, undef, '', '');
180  
181 0 ▶ while (!$done) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 0 0 0
182 0 ▶ my ($type, $request_id, $content) = read_record($socket)
Branch: 1 branch
BranchTrueFalseTotal
() = read_record($socket) or last 0 0 0
183 or last;
184  
185 0 if (DEBUG) {
186 warn '< ', dump_record($type, $request_id, $content), "\n";
187 }
188  
189 0 ▶ if ($request_id == FCGI_NULL_REQUEST_ID) {
Branch: 6 branches
BranchTrueFalseTotal
if ($request_id == 0) { } 0 0 0
elsif ($request_id != $current_id and $type != 1) { } 0 0 0
elsif ($type == 2) { } 0 0 0
elsif ($type == 1) { } 0 0 0
elsif ($type == 4) { } 0 0 0
elsif ($type == 5) { } 0 0 0
190 0 ▶ if ($type == FCGI_GET_VALUES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 9) { } 0 0 0
191 0 my $query = parse_params($content);
192 0 my %reply = map { $_ => $self->{values}->{$_} }
193 0 grep { exists $self->{values}->{$_} }
194 keys %$query;
195 0 write_record($socket, FCGI_GET_VALUES_RESULT,
196 FCGI_NULL_REQUEST_ID, build_params(\%reply));
197 }
198 else {
199 0 write_record($socket, FCGI_UNKNOWN_TYPE,
200 FCGI_NULL_REQUEST_ID, build_unknown_type($type));
201 }
202 }
203 ▶ elsif ($request_id != $current_id && $type != FCGI_BEGIN_REQUEST) {
Condition0%0/3
$request_id != $current_id and $type != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$request_id != $current_id and $type != 1
$request_id != $current_id $type != 1
Truth table0%0/3
A and B
A= $request_id != $current_idB= $type != 1
ABresult
0X0
100
111
204 # ignore inactive requests (FastCGI Specification 3.3)
205 }
206 elsif ($type == FCGI_ABORT_REQUEST) {
207 0 $current_id = 0;
208 0 ($stdin, $stdout, $stderr, $params) = (undef, '', '', '');
209 }
210 elsif ($type == FCGI_BEGIN_REQUEST) {
211 0 my ($role, $flags) = parse_begin_request_body($content);
212 0 ▶ if ($current_id || $role != FCGI_RESPONDER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_id or $role != 1) { } 0 0 0
Condition0%0/3
$current_id or $role != 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$current_id or $role != 1
$current_id $role != 1
Truth table0%0/3
A or B
A= $current_idB= $role != 1
ABresult
1X1
011
000
213 0 ▶ my $status = $current_id ? FCGI_CANT_MPX_CONN : FCGI_UNKNOWN_ROLE;
Branch: 1 branch
BranchTrueFalseTotal
$current_id ? : 0 0 0
214 0 write_record($socket, FCGI_END_REQUEST, $request_id,
215 build_end_request_body(0, $status));
216 }
217 else {
218 0 $current_id = $request_id;
219 0 $stdin = Plack::TempBuffer->new;
220 0 $keep_conn = ($flags & FCGI_KEEP_CONN);
221 }
222 }
223 elsif ($type == FCGI_PARAMS) {
224 0 $params .= $content;
225 }
226 elsif ($type == FCGI_STDIN) {
227 0 $stdin->print($content);
228  
229 0 ▶ unless (length $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $content) 0 0 0
230 0 my $in = $stdin->rewind;
231  
232 0 ▶ my $stdout_cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
233 0 $stdout .= $_[0];
234 0 ▶ if (length $stdout >= $STDOUT_BUFFER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stdout >= $Plack::Handler::Net::FastCGI::STDOUT_BUFFER_SIZE) 0 0 0
235 0 write_stream($socket, FCGI_STDOUT, $current_id, $stdout, 0);
236 0 $stdout = '';
237 }
238 0 };
239  
240 0 ▶ open(my $out, PERLIO_MODE, $stdout_cb)
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">:via(Plack::Handler::Net::FastCGI)", $stdout_cb or die "Couldn't open sub as fh: $!" 0 0 0
241 || die(qq/Couldn't open sub as fh: $!/);
242  
243 0 ▶ my $stderr_cb = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
244 0 $stderr .= $_[0];
245 0 ▶ if (length $stderr >= $STDERR_BUFFER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stderr >= $Plack::Handler::Net::FastCGI::STDERR_BUFFER_SIZE) 0 0 0
246 0 write_stream($socket, FCGI_STDERR, $current_id, $stderr, 0);
247 0 $stderr = '';
248 }
249 0 };
250  
251 0 ▶ open(my $err, PERLIO_MODE, $stderr_cb)
Branch: 1 branch
BranchTrueFalseTotal
open my $err, ">:via(Plack::Handler::Net::FastCGI)", $stderr_cb or die "Couldn't open sub as fh: $!" 0 0 0
252 || die(qq/Couldn't open sub as fh: $!/);
253  
254 0 $self->_handle_request(parse_params($params), $in, $out, $err);
255  
256 0 write_stream($socket, FCGI_STDOUT, $current_id, $stdout, 1);
257 0 write_stream($socket, FCGI_STDERR, $current_id, $stderr, 1);
258 0 write_record($socket, FCGI_END_REQUEST, $current_id,
259 build_end_request_body(0, FCGI_REQUEST_COMPLETE));
260  
261 # prepare for next request
262 0 $current_id = 0;
263 0 ($stdin, $stdout, $stderr, $params) = (undef, '', '', '');
264 }
265 }
266 else {
267 0 warn(qq/Received an unknown record type '$type'/);
268 }
269 }
270 }
271  
272 0 ▶sub PUSHED {
Subroutine PUSHED: not called CC=1 SCAR=6.9
Pod undocumented
273 0 my ($class) = @_;
274 0 return bless \(my $self), $class;
275 }
276  
277 0 ▶sub OPEN {
Subroutine OPEN: not called CC=1 SCAR=6.9
278 0 my ($self, $sub) = @_;
279 0 $$self = $sub;
280 }
281  
282 0 ▶sub WRITE {
Subroutine WRITE: not called CC=1 SCAR=6.9
283 0 my ($self) = @_;
284 0 $$self->($_[1]);
285 0 return length $_[1];
286 }
287  
288 1 1;
289  
290 __END__
↑ Summary