1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
package Plack::Handler::AnyEvent::FCGI;
use strict;
use 5.008_001;
our $VERSION = '0.01';
use AnyEvent;
use AnyEvent::FCGI;
use Plack::Util;
use IO::Handle::Util qw(io_from_write_cb);
use URI;
use URI::Escape;
sub new {
my($class, %args) = @_;
bless { %args }, $class;
}
sub run {
my $self = shift;
$self->register_service(@_);
$self->{_cv}->recv;
}
sub register_service {
my($self, $app) = @_;
my $listen = $self->{listen}[0]
or die "listen address/socket not specified";
my %args;
if ($listen =~ /:\d+$/) {
@args{qw(host port)} = split /:/, $listen, 2;
$args{host} = undef if $args{host} eq '';
} else {
$args{socket} = $listen;
}
warn "Listening on $listen for FCGI connections\n";
$self->{_server} = AnyEvent::FCGI->new(
%args,
on_request => sub { $self->_on_request($app, @_) },
);
$self->{_cv} = AE::cv;
}
sub _on_request {
my($self, $app, $request) = @_;
my $env = $request->params;
# deal with ligttpd/nginx path normalization
my $uri = URI->new("http://localhost" . $env->{REQUEST_URI});
$env->{PATH_INFO} = URI::Escape::uri_unescape($uri->path);
$env->{PATH_INFO} =~ s/^\Q$env->{SCRIPT_NAME}\E//;
$env = {
%$env,
'psgi.version' => [1,1],
'psgi.url_scheme' => ($env->{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
'psgi.input' => do { open my $io, "<", \$request->read_stdin || \''; $io },
'psgi.errors' => io_from_write_cb sub { $request->print_stderr(@_) },
'psgi.multithread' => 0,
'psgi.multiprocess' => 0,
'psgi.run_once' => 0,
'psgi.streaming' => 1,
'psgi.nonblocking' => 1,
'psgix.input.buffered' => 1,
};
my $res = Plack::Util::run_app($app, $env);
if (ref $res eq 'ARRAY') {
$self->handle_response($res, $request);
} elsif (ref $res eq 'CODE') {
$res->(sub { $self->handle_response($_[0], $request) });
} else {
die "Bad response: $res";
}
}
sub handle_response {
my($self, $res, $request) = @_;
my $hdrs;
$hdrs = "Status: $res->[0]\015\012";
my $headers = $res->[1];
while (my ($k, $v) = splice @$headers, 0, 2) {
$hdrs .= "$k: $v\015\012";
}
$hdrs .= "\015\012";
$request->print_stdout($hdrs);
my $cb = sub { $request->print_stdout($_[0]) };
my $body = $res->[2];
if (defined $body) {
Plack::Util::foreach($body, $cb);
$request->finish;
} else {
return Plack::Util::inline_object
write => $cb,
close => sub { $request->finish };
}
}
1;
__END__
=encoding utf-8
=for stopwords
=head1 NAME
Plack::Handler::AnyEvent::FCGI - Asynchronous FCGI handler for PSGI using AnyEvent::FCGI
=head1 SYNOPSIS
> plackup -s AnyEvent::FCGI myapp.psgi
=head1 DESCRIPTION
Plack::Handler::AnyEvent::FCGI is a PSGI adapter for L<AnyEvent::FCGI>
allowing AnyEvent based non-blocking applications running behind a web
server using FastCGI as a protocol.
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
=head1 COPYRIGHT
Copyright 2011- Tatsuhiko Miyagawa
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<AnyEvent::FCGI> L<Plack::Handler::AnyEvent::SCGI> L<Plack::Handler::FCGI>
=cut
|