| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Net::Async::FastCGI::PSGI 0.26; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use base qw( Net::Async::FastCGI ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | my $CRLF = "\x0d\x0a"; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | C<Net::Async::FastCGI::PSGI> - use C<PSGI> applications with C<Net::Async::FastCGI> | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | use Net::Async::FastCGI::PSGI; | ||||||||||||||||||||
| 24 | use IO::Async::Loop; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | my $fcgi = Net::Async::FastCGI::PSGI->new( | ||||||||||||||||||||
| 29 | port => 12345, | ||||||||||||||||||||
| 30 | app => sub { | ||||||||||||||||||||
| 31 | my $env = shift; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | return [ | ||||||||||||||||||||
| 34 | 200, | ||||||||||||||||||||
| 35 | [ "Content-Type" => "text/plain" ], | ||||||||||||||||||||
| 36 | [ "Hello, world!" ], | ||||||||||||||||||||
| 37 | ]; | ||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | $loop->add( $fcgi ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | $loop->run; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | This subclass of L<Net::Async::FastCGI> allows a FastCGI responder to use a | ||||||||||||||||||||
| 48 | L<PSGI> application to respond to requests. It acts as a gateway between the | ||||||||||||||||||||
| 49 | FastCGI connection from the webserver, and the C<PSGI> application. Aside from | ||||||||||||||||||||
| 50 | the use of C<PSGI> instead of the C<on_request> event, this class behaves | ||||||||||||||||||||
| 51 | similarly to C<Net::Async::FastCGI>. | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 PARAMETERS | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =over 8 | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =item app => CODE | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Reference to the actual C<PSGI> application to use for responding to requests | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =back | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | sub configure | ||||||||||||||||||||
| 70 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
configure: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 71 | 5 | my $self = shift; | |||||||||||||||||||
| 72 | 5 | my %args = @_; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 5 | ▶ | if( exists $args{app} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 5 | $self->{app} = delete $args{app}; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 5 | $self->SUPER::configure( %args ); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 PSGI ENVIRONMENT | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | The following extra keys are supplied to the environment of the C<PSGI> app: | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =over 8 | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =item C<net.async.fastcgi> | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | The C<Net::Async::FastCGI::PSGI> object serving the request | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =item C<net.async.fastcgi.req> | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | The L<Net::Async::FastCGI::Request> object representing this particular | ||||||||||||||||||||
| 94 | request | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item C<io.async.loop> | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | The L<IO::Async::Loop> object that the C<Net::Async::FastCGI::PSGI> object is | ||||||||||||||||||||
| 99 | a member of. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub on_request | ||||||||||||||||||||
| 106 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
on_request: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 107 | 6 | my $self = shift; | |||||||||||||||||||
| 108 | 6 | my ( $req ) = @_; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # Much of this code stolen fro^W^Winspired by Plack::Handler::Net::FastCGI | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | my %env = ( | ||||||||||||||||||||
| 113 | 6 | ▶ | %{ $req->params }, | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$req->param('HTTPS') || 'off'
MC/DC0%0/1
$req->param('HTTPS') || 'off'
$req->param('HTTPS')
Truth table50%1/2
A
A= $req->param('HTTPS')
| |||||||||||||||||||||
| 114 | 'psgi.version' => [1,0], | ||||||||||||||||||||
| 115 | 'psgi.url_scheme' => ($req->param("HTTPS")||"off") =~ m/^(?:on|1)/i ? "https" : "http", | ||||||||||||||||||||
| 116 | 'psgi.input' => $req->stdin, | ||||||||||||||||||||
| 117 | 'psgi.errors' => $req->stderr, | ||||||||||||||||||||
| 118 | 'psgi.multithread' => 0, | ||||||||||||||||||||
| 119 | 'psgi.multiprocess' => 0, | ||||||||||||||||||||
| 120 | 'psgi.run_once' => 0, | ||||||||||||||||||||
| 121 | 'psgi.nonblocking' => 1, | ||||||||||||||||||||
| 122 | 'psgi.streaming' => 1, | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # Extensions | ||||||||||||||||||||
| 125 | 'net.async.fastcgi' => $self, | ||||||||||||||||||||
| 126 | 'net.async.fastcgi.req' => $req, | ||||||||||||||||||||
| 127 | 'io.async.loop' => $self->get_loop, | ||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 6 | my $resp = $self->{app}->( \%env ); | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 6 | ▶ | my $responder = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 133 | 6 | my ( $status, $headers, $body ) = @{ +shift }; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 6 | $req->print_stdout( "Status: $status$CRLF" ); | |||||||||||||||||||
| 136 | 6 | ▶ | while( my ( $header, $value ) = splice @$headers, 0, 2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 6 | $req->print_stdout( "$header: $value$CRLF" ); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 6 | $req->print_stdout( $CRLF ); | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 6 | ▶ | if( !defined $body ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 1 | ▶ | croak "Responder given no body in void context" unless defined wantarray; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 1 | return $req->stdout_with_close; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 5 | ▶ | if( ref $body eq "ARRAY" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 4 | $req->print_stdout( $_ ) for @$body; | |||||||||||||||||||
| 149 | 4 | $req->finish( 0 ); | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||
| 152 | $req->stream_stdout_then_finish( | ||||||||||||||||||||
| 153 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 154 | 2 | local $/ = \8192; | |||||||||||||||||||
| 155 | 2 | my $buffer = $body->getline; | |||||||||||||||||||
| 156 | 2 | ▶ | defined $buffer and return $buffer; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | $body->close; | |||||||||||||||||||
| 159 | 1 | return undef; | |||||||||||||||||||
| 160 | }, | ||||||||||||||||||||
| 161 | 1 | 0 | |||||||||||||||||||
| 162 | ); | ||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | 6 | }; | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 6 | ▶ | if( ref $resp eq "ARRAY" ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 167 | 4 | $responder->( $resp ); | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | elsif( ref $resp eq "CODE" ) { | ||||||||||||||||||||
| 170 | 2 | $resp->( $responder ); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head1 SEE ALSO | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =over 4 | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =item * | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | L<PSGI> - Perl Web Server Gateway Interface Specification | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =item * | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | L<Plack::Handler::Net::Async::FastCGI> - FastCGI handler for Plack using L<Net::Async::FastCGI> | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 2 | 0x55AA; | |||||||||||||||||||