FCGI/Engine/PSGI.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 62 CC 4 SCAR 19.1
CC
4
Cov
0%
handle_request
12.0
_handle_response
6.0
BEGIN
2.0
CRAP 6.8
1 package FCGI::Engine::PSGI;
2 use Moose;
3  
4 use Plack::Util;
5  
6 our $VERSION = '0.22';
7 our $AUTHORITY = 'cpan:STEVAN';
8  
9 extends 'FCGI::Engine::Core';
10  
11 has 'app' => (
12 is => 'ro',
13 isa => 'CodeRef',
14 required => 1,
15 );
16  
17 # NOTE:
18 # Most of this is taken from
19 # Plack::Handler::FCGI or at
20 # least heavily based on it.
21 # - SL
22  
23 augment 'prepare_environment' => sub {
24 my ($self, $env) = @_;
25 return +{
26 %$env,
27 'psgi.version' => [1,0],
28 'psgi.url_scheme' => ($env->{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
29 'psgi.input' => *STDIN,
30 'psgi.errors' => *STDERR, # FCGI.pm redirects STDERR in Accept() loop, so just print STDERR
31 # print to the correct error handle based on keep_stderr
32 'psgi.multithread' => Plack::Util::FALSE,
33 'psgi.multiprocess' => Plack::Util::TRUE,
34 'psgi.run_once' => Plack::Util::FALSE,
35 'psgi.streaming' => Plack::Util::TRUE,
36 'psgi.nonblocking' => Plack::Util::FALSE,
37 };
38 };
39  
40 sub handle_request {
41 my ( $self, $env ) = @_;
42  
43 my $res = Plack::Util::run_app( $self->app, $env );
44  
45 if (ref $res eq 'ARRAY') {
46 $self->_handle_response($res);
47 }
48 elsif (ref $res eq 'CODE') {
49 $res->(sub {
50 $self->_handle_response($_[0]);
51 });
52 }
53 else {
54 die "Bad response $res";
55 }
56 }
57  
58 sub _handle_response {
59 my ($self, $res) = @_;
60  
61 *STDOUT->autoflush(1);
62  
63 my $hdrs;
64 $hdrs = "Status: $res->[0]\015\012";
65  
66 my $headers = $res->[1];
67 while (my ($k, $v) = splice @$headers, 0, 2) {
68 $hdrs .= "$k: $v\015\012";
69 }
70 $hdrs .= "\015\012";
71  
72 print STDOUT $hdrs;
73  
74 my $cb = sub { print STDOUT $_[0] };
75 my $body = $res->[2];
76 if (defined $body) {
77 Plack::Util::foreach($body, $cb);
78 }
79 else {
80 return Plack::Util::inline_object
81 write => $cb,
82 close => sub { };
83 }
84 }
85  
86 __PACKAGE__->meta->make_immutable;
87  
88 no Moose; 1;
89  
90 __END__