The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

FCGI::EV::Std::Nonblock - Ease non-blocking CGI using FCGI::EV::Std

VERSION

This document describes FCGI::EV::Std::Nonblock version 1.3.2

SYNOPSIS

 use FCGI::EV;
 use FCGI::EV::Std;
 use FCGI::EV::Std::Nonblock;   # just loading module will activate it!

 FCGI::EV->new($sock, 'FCGI::EV::Std');

 #
 # Example CGI with FCGI::EV::Std::Nonblock interface
 #
 
 sub PRE {}
 sub POST {}
 sub ERROR {}
 sub START {
    my ($this) = @_;
    $this->{timer} = EV::timer 1, 0, $this->wrap_cb(\&reply);
 }

 sub reply {
    my ($this) = @_;
    $this->send("Status: 200 OK\r\n");
    $this->send("Content-Type: text/plain\r\n\r\n");
    $this->send("Reply after 1 sec!");
    $this->done();
    return;
 }

DESCRIPTION

This module will made use of FCGI::EV::Std in non-blocking mode ease for user. To activate it it's enough to load that module - it will automatically reconfigure FCGI::EV::Std and that result in calling user code on incoming CGI requests in completely different way than explained in FCGI::EV::Std documentation.

INTERFACE

This module will configure $BLOCKING, $MAIN and $HUP variables in FCGI::EV::Std, so only user-configurable variable left is $MAX_STDIN (see FCGI::EV::Std documentation for details).

On incoming CGI request this module will call user function main::START($this). The $this parameter is object related to ... this :) CGI request. This object has several methods listed below, but no fields - user can use $this as usual HASHREF to store ANY data related to this request.

To keep access to $this when user need to delay processing of this CGI request until some event happens, user should generate callback for that event in special way - using $this->wrap_cb($callback, @params) method. This way when event happens $callback->($this, @params, @event_params) will be called, and user will have $this.

User should send reply to web server using $this->send($data) and $this->done() methods.

There also 3 another predefined functions which user must define: main::PRE, main::POST and main::ERROR. The PRE($this) and POST($this) will be called before and after user's main::START and $callback prepared using $this->wrap_cb() - you can use these hooks to setup some environment which all your callbacks need and make some cleanup after them. The ERROR($this, $@) will be called if main::START or $callback will throw exception. Exceptions within PRE, POST and ERROR will not be intercepted and will kill your process.

send( $data )

Will send $data as (part of) CGI reply. Can be called any amount of times before done() was called.

Return nothing.

done()

Will finish processing current request. WARNING! User shouldn't keep references to $this after calling done()!

Return nothing.

wrap_cb( $callback, @params )

Will generate special CODEREF which, when called, will result in calling $callback->($this, @params, @callback_params). User must ALWAYS use this way of generating callbacks for event watchers to not lose access to $this in event handlers, automatically execute main::PRE and main::POST hooks before and after $callback, and intercept exceptions in $callback (which will be automatically delivered to main::ERROR hook after executing POST hook.

The PRE and POST hooks will have only parameter: $this. The ERROR hook will two parameters: $this and $exception (stored copy of $@).

DIAGNOSTICS

None.

CONFIGURATION AND ENVIRONMENT

FCGI::EV::Std::Nonblock requires no configuration files or environment variables.

DEPENDENCIES

 version

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-fcgi-ev-std-nonblock@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Alex Efros <powerman-asdf@ya.ru>

LICENSE AND COPYRIGHT

Copyright (c) 2009, Alex Efros <powerman-asdf@ya.ru>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.