NAME

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

VERSION

This document describes FCGI::EV::Std::Nonblock version v2.0.1

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

$this->send( $data );

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

Return nothing.

done

$this->done();

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

Return nothing.

wrap_cb

$this->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 $@).

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at https://github.com/powerman/perl-FCGI-EV-Std/issues. You will be notified automatically of any progress on your issue.

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.

https://github.com/powerman/perl-FCGI-EV-Std

git clone https://github.com/powerman/perl-FCGI-EV-Std.git

Resources

AUTHOR

Alex Efros <powerman@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2009- by Alex Efros <powerman@cpan.org>.

This is free software, licensed under:

The MIT (X11) License