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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
package FCGI::EV;
use 5.010001;
use warnings;
use strict;
use utf8;
use Carp;
our $VERSION = 'v2.0.1';
use Scalar::Util qw( weaken );
use IO::Stream;
use constant FCGI_HEADER_LEN => 8;
use constant FCGI_VERSION_1 => 1;
use constant FCGI_BEGIN_REQUEST => 1;
use constant FCGI_END_REQUEST => 3;
use constant FCGI_PARAMS => 4;
use constant FCGI_STDIN => 5;
use constant FCGI_STDOUT => 6;
use constant FCGI_RESPONDER => 1;
use constant FCGI_REQUEST_COMPLETE => 0;
use constant END_REQUEST_COMPLETE =>
pack 'N C CCC', 0, FCGI_REQUEST_COMPLETE, 0, 0, 0;
use constant MAX_CONTENT_LEN => 0xFFFF;
sub new {
my ($class, $sock, $handler_class) = @_;
my $self = bless {
io => undef,
req_id => undef,
params => q{},
stdin_eof => undef,
handler => undef,
handler_class=>$handler_class,
}, $class;
$self->{io} = IO::Stream->new({
fh => $sock,
wait_for => IN|EOF,
cb => $self,
Wait_header => 1,
Need_in => FCGI_HEADER_LEN,
});
weaken($self->{io});
# It MAY have sense to add timeout between read() calls and timeout for
# overall time until EOF on STDIN will be received. First timeout
# can be about 3 minutes for slow clients, second can be about 4 hours
# for uploading huge files.
return;
}
sub DESTROY {
my ($self) = @_;
$self->{handler} = undef; # call handler's DESTROY while $self is alive
return;
}
sub stdout {
my ($self, $stdout, $is_eof) = @_;
my $io = $self->{io};
if (length $stdout) {
$io->{out_buf} .= _pack_pkt(FCGI_STDOUT, $self->{req_id}, $stdout);
}
if ($is_eof) {
$io->{out_buf} .= _pack_pkt(FCGI_STDOUT, $self->{req_id}, q{});
$io->{out_buf} .= _pack_pkt(FCGI_END_REQUEST, $self->{req_id}, END_REQUEST_COMPLETE);
$io->{wait_for} |= SENT;
}
$io->write();
return;
}
sub IO {
my ($self, $io, $e, $err) = @_;
if ($err) {
warn "FCGI::EV: IO: $err\n";
return $io->close();
}
if ($e & EOF) {
return $io->close();
}
if ($e & SENT) {
return $io->close();
}
while (length $io->{in_buf} >= $io->{Need_in}) {
if ($io->{Wait_header}) {
$io->{Wait_header} = 0;
my ($content_len, $padding_len) = unpack 'x4 n C', $io->{in_buf};
$io->{Need_in} += $content_len + $padding_len;
}
else {
my $pkt = substr $io->{in_buf}, 0, $io->{Need_in}, q{};
$io->{Wait_header} = 1;
$io->{Need_in} = FCGI_HEADER_LEN;
my $error = $self->_process($pkt);
if ($error) {
warn "FCGI::EV: $error\n";
return $io->close();
}
}
}
return;
}
sub _process {
my ($self, $pkt) = @_;
my ($ver, $type, $req_id, $content_len) = unpack 'C C n n', $pkt;
my $content = substr $pkt, FCGI_HEADER_LEN, $content_len;
if ($ver != FCGI_VERSION_1) {
return "unsupported version: $ver";
}
if (defined $self->{req_id} && $self->{req_id} != $req_id) {
return "unknown request id: $req_id";
}
if ($type == FCGI_BEGIN_REQUEST) {
my ($role) = unpack 'n', $content;
if ($role != FCGI_RESPONDER) {
return "role not supported: $role";
}
if (defined $self->{req_id}) {
return 'duplicated BEGIN_REQUEST';
}
$self->{req_id} = $req_id;
}
elsif ($type == FCGI_PARAMS) {
if ($self->{handler}) {
return 'got PARAMS for existing handler';
}
if (length $content) {
$self->{params} .= $content;
}
else {
my ($env, $err) = _unpack_nv($self->{params});
return $err if $err;
$self->{handler} = $self->{handler_class}->new($self, $env);
}
}
elsif ($type == FCGI_STDIN) {
if (!$self->{handler}) {
return 'got STDIN for non-existing handler';
}
if ($self->{stdin_eof}) {
return 'got STDIN after STDIN EOF';
}
if (length $content) {
$self->{handler}->stdin($content, 0);
}
else {
$self->{handler}->stdin(q{}, 1);
$self->{stdin_eof} = 1;
}
}
else {
return 'unknown type';
}
return;
}
sub _unpack_nv {
my ($s) = @_;
my %nv;
while (length $s) {
my ($nlen, $vlen);
for my $len ($nlen, $vlen) {
## no critic (ProhibitMagicNumbers)
return (undef, 'unpack_nv: not enough data') if length $s < 1;
($len) = unpack 'C', $s;
if ($len & 0x80) {
return (undef, 'unpack_nv: not enough data') if length $s < 4;
($len) = unpack 'N', $s;
$len &= 0x7FFFFFFF;
substr $s, 0, 4, q{};
}
else {
substr $s, 0, 1, q{};
}
## use critic
}
return (undef, 'unpack_nv: not enough data') if length $s < $nlen + $vlen;
my $n = substr $s, 0, $nlen, q{};
my $v = substr $s, 0, $vlen, q{};
$nv{$n} = $v;
}
return (\%nv, undef);
}
sub _pack_pkt {
my ($type, $req_id, $content) = @_;
$content = pack 'a*', $content; # convert from Unicode to UTF-8, if any
my $pkt = q{};
while (1) {
my $c = substr $content, 0, MAX_CONTENT_LEN, q{};
my $padding = q{};
$pkt .= pack 'CCnnCCa*a*',
FCGI_VERSION_1,
$type,
$req_id,
length $c,
length $padding,
0, # reserved
$c,
$padding,
;
last if !length $content;
}
return $pkt;
}
1; # Magic true value required at end of module
__END__
=encoding utf8
=head1 NAME
FCGI::EV - Implement FastCGI protocol for use in EV-based applications
=head1 VERSION
This document describes FCGI::EV version v2.0.1
=head1 SYNOPSIS
use FCGI::EV;
use Some::FCGI::EV::Handler;
# while in EV::loop, accept incoming connection from web server into
# $sock, then start handling FastCGI protocol on that connection,
# using Some::FCGI::EV::Handler for processing CGI requests:
FCGI::EV->new($sock, 'Some::FCGI::EV::Handler');
#
# EXAMPLE: complete FastCGI server (without error handling code)
# use FCGI::EV::Std handler (download separately from CPAN)
#
use Socket;
use Fcntl;
use EV;
use FCGI::EV;
use FCGI::EV::Std;
my $path = '/tmp/fastcgi.sock';
socket my $srvsock, AF_UNIX, SOCK_STREAM, 0;
unlink $path;
my $umask = umask 0; # ensure 0777 perms for unix socket
bind $srvsock, sockaddr_un($path);
umask $umask;
listen $srvsock, SOMAXCONN;
fcntl $srvsock, F_SETFL, O_NONBLOCK;
my $w = EV::io $srvsock, EV::READ, sub {
accept my($sock), $srvsock;
fcntl $sock, F_SETFL, O_NONBLOCK;
FCGI::EV->new($sock, 'FCGI::EV::Std');
};
EV::loop;
=head1 DESCRIPTION
This module implement FastCGI protocol for use in EV-based applications.
(That mean you have to run EV::loop in your application or this module
will not work.)
It receive and parse data from web server, pack and send data to web
server, but it doesn't process CGI requests received from web server -
instead it delegate this work to another module called 'handler'. For
one example of such handler, see L<FCGI::EV::Std>.
FCGI::EV work using non-blocking sockets and initially was designed to use
in event-based CGI applications (which able to handle multiple parallel
CGI requests in single process without threads/fork). This require from
CGI to avoid any operations which may block, like using SQL database -
instead CGI should delegate all such tasks to remote services and talk to
these services in non-blocking mode.
It also possible to use it to run usual CGI.pm-based applications. If you
will do this using FCGI::EV::Std handler, then only one CGI request will
be executed at a time (which is probably not what you expect from
FastCGI!), because FCGI::EV::Std doesn't implement any process-manager.
But it's possible to develop another handlers for FCGI::EV, which will
support process-management and so will handle multiple CGI request in
parallel.
This module doesn't require from user to use CGI.pm - any module for
parsing CGI params can be used in general (details depends on used
FCGI::EV handler module).
=head1 INTERFACE
=head2 new
FCGI::EV->new( $sock, $class );
Start talking FastCGI protocol on $sock (which should be socket open to
just-connected web server), and use $class to handle received CGI requests.
Module $class should implement "FCGI::EV handler" interface. You can use
either L<FCGI::EV::Std> from CPAN or develop your own.
Return nothing. (Created FCGI::EV object will work in background and will
be automatically destroyed after finishing I/O with web server.)
=head1 HANDLER CLASS INTERFACE
Handler class (which name provided in $class parameter to FCGI::EV->new())
must implement this interface:
=over
=item new( $server, \%env )
When FCGI::EV object receive initial part of CGI request (environment
variables) it will call $handler_class->new() to create handler object
which should process that CGI request.
Parameter $server is FCGI::EV object itself. It's required to send CGI
reply. WARNING! Handler may keep only weaken() reference to $server!
After calling new() FCGI::EV object ($server) will continue receiving
STDIN content from web server and will call $handler->stdin() each time it
get next part of STDIN.
=item stdin( $data, $is_eof )
The $data is next chunk of STDIN received from web server. Flag $is_eof will
be true if $data was last part of STDIN.
Usually handler shouldn't begin processing CGI request until all content
of STDIN will be received.
=item DESTROY
This method is optional. It will be called when connection to web server is
closed and FCGI::EV object going to die (but it's still exists when DESTROY
is called - except if DESTROY was called while global destruction stage).
Handler object may use DESTROY to interrupt current CGI request if web server
close connection before CGI send it reply.
=back
=head2 SENDING CGI REPLY
After handler got %env (in new()) and complete STDIN (in one or more calls
of stdin()) it may start handling this CGI request and prepare reply to send
to web server. To send this data it should use method $server->stdout(),
where $server is object given to new() while creating handler object
(it should keep weak reference to $server inside to be able to reply).
=over
=item stdout( $data, $is_eof )
CGI may send reply in one or more parts. Last part should have $is_eof set
to true. DESTROY method of handler object will be called shortly after
handler object will do $server->stdout( $data, 1 ).
=back
=head2 HANDLER EXAMPLE
This handler will process CGI requests one-by-one (i.e. in blocking mode).
On request function main::main() will be executed. That function may use
standard CGI.pm module to get request parameters and send it reply using
usual print to STDOUT.
There no error-handling code in this example, see L<FCGI::EV::Std> for
more details.
package FCGI::EV::ExampleHandler;
use Scalar::Util qw( weaken );
use CGI::Stateless; # needed to re-init CGI.pm state between requests
sub new {
my ($class, $server, $env) = @_;
my $self = bless {
server => $server,
env => $env,
stdin => q{},
}, $class;
weaken($self->{server});
return $self;
}
sub stdin {
my ($self, $stdin, $is_eof) = @_;
$self->{stdin} .= $stdin;
if ($is_eof) {
local *STDIN;
open STDIN, '<', \$self->{stdin};
local %ENV = %{ $self->{env} };
local $CGI::Q = CGI::Stateless->new();
local *STDOUT;
my $reply = q{};
open STDOUT, '>', \$reply;
main::main();
$self->{server}->stdout($reply, 1);
}
return;
}
=head1 DIAGNOSTICS
There no errors returned in any way by this module, but there few warning
messages may be printed:
=over
=item C<< FCGI::EV: IO: %s >>
While doing I/O with web server error %s happened and connection was closed.
=item C<< FCGI::EV: %s >>
While parsing data from web server error %s happened and connection was closed.
(That error probably mean bug either in web server or this module.)
=back
=head1 SUPPORT
=head2 Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/perl-FCGI-EV/issues>.
You will be notified automatically of any progress on your issue.
=head2 Source Code
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
Feel free to fork the repository and submit pull requests.
L<https://github.com/powerman/perl-FCGI-EV>
git clone https://github.com/powerman/perl-FCGI-EV.git
=head2 Resources
=over
=item * MetaCPAN Search
L<https://metacpan.org/search?q=FCGI-EV>
=item * CPAN Ratings
L<http://cpanratings.perl.org/dist/FCGI-EV>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/FCGI-EV>
=item * CPAN Testers Matrix
L<http://matrix.cpantesters.org/?dist=FCGI-EV>
=item * CPANTS: A CPAN Testing Service (Kwalitee)
L<http://cpants.cpanauthors.org/dist/FCGI-EV>
=back
=head1 AUTHOR
Alex Efros E<lt>powerman@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2009- by Alex Efros E<lt>powerman@cpan.orgE<gt>.
This is free software, licensed under:
The MIT (X11) License
=cut
|