Net-Async-FastCGI
view release on metacpan or search on metacpan
lib/Net/Async/FastCGI/PSGI.pm view on Meta::CPAN
Reference to the actual C<PSGI> application to use for responding to requests
=back
=cut
sub configure
{
my $self = shift;
my %args = @_;
if( exists $args{app} ) {
$self->{app} = delete $args{app};
}
$self->SUPER::configure( %args );
}
=head1 PSGI ENVIRONMENT
The following extra keys are supplied to the environment of the C<PSGI> app:
=over 8
=item C<net.async.fastcgi>
The C<Net::Async::FastCGI::PSGI> object serving the request
=item C<net.async.fastcgi.req>
The L<Net::Async::FastCGI::Request> object representing this particular
request
=item C<io.async.loop>
The L<IO::Async::Loop> object that the C<Net::Async::FastCGI::PSGI> object is
a member of.
=back
=cut
sub on_request
{
my $self = shift;
my ( $req ) = @_;
# Much of this code stolen fro^W^Winspired by Plack::Handler::Net::FastCGI
my %env = (
%{ $req->params },
'psgi.version' => [1,0],
'psgi.url_scheme' => ($req->param("HTTPS")||"off") =~ m/^(?:on|1)/i ? "https" : "http",
'psgi.input' => $req->stdin,
'psgi.errors' => $req->stderr,
'psgi.multithread' => 0,
'psgi.multiprocess' => 0,
'psgi.run_once' => 0,
'psgi.nonblocking' => 1,
'psgi.streaming' => 1,
# Extensions
'net.async.fastcgi' => $self,
'net.async.fastcgi.req' => $req,
'io.async.loop' => $self->get_loop,
);
my $resp = $self->{app}->( \%env );
my $responder = sub {
my ( $status, $headers, $body ) = @{ +shift };
$req->print_stdout( "Status: $status$CRLF" );
while( my ( $header, $value ) = splice @$headers, 0, 2 ) {
$req->print_stdout( "$header: $value$CRLF" );
}
$req->print_stdout( $CRLF );
if( !defined $body ) {
croak "Responder given no body in void context" unless defined wantarray;
return $req->stdout_with_close;
}
if( ref $body eq "ARRAY" ) {
$req->print_stdout( $_ ) for @$body;
$req->finish( 0 );
}
else {
$req->stream_stdout_then_finish(
sub {
local $/ = \8192;
my $buffer = $body->getline;
defined $buffer and return $buffer;
$body->close;
return undef;
},
0
);
}
};
if( ref $resp eq "ARRAY" ) {
$responder->( $resp );
}
elsif( ref $resp eq "CODE" ) {
$resp->( $responder );
}
}
=head1 SEE ALSO
=over 4
=item *
L<PSGI> - Perl Web Server Gateway Interface Specification
=item *
( run in 0.791 second using v1.01-cache-2.11-cpan-140bd7fdf52 )