[cgiapp] need a fresh WebApp instance with every request ?

Matthias Ferdinand mf+cgiapp at mfedv.net
Sun Nov 27 11:24:56 EST 2011


Hello all,

I am writing a CGI application and I am facing issues with variable
persistence between requests:

 a) using redirect() (from CGI::Application::Plugin::Redirect) leads to
    a subsequent Redirect loop
    ( $self->{__HEADER_PROPS} )

 b) using prerun_mode() will lock subsequent requests into the given
    runmode
    ( $self->{__PRERUN_MODE} )

These issues only show with my CGI::Application::Server(*) dev
environment, not with plain CGI under apache.
(*) a wonderfully simple, lightweight, single-process web application
server featuring persistence and session-stickyness :-)

CGI::Application->run() does not remove or clean up these variables when 
the request is finished.

At the moment, I work around this in teardown():

    sub teardown { 
        my $self = shift;
        
        $self->header_props({});
        $self->header_type('header');
        delete $self->{__PRERUN_MODE_LOCKED};
        delete $self->{__PRERUN_MODE};
    }
   

My server script looks like this:

    use lib qw(./lib);
    use CGI::Application::Server;
    use WebApp;

    my $object = WebApp->new();

    my $server = CGI::Application::Server->new(8888);
    $server->document_root('./htdocs');
    $server->entry_points({ '/index.cgi' => $object, });
    $server->run();

new() and cgiapp_init() will be called only once, and the single WebApp
instance $object->run() is called for each and every request.

Like this:

               $object = WebApp->new();
Request_1 =>                               $object->run()
...
Request_n =>                               $object->run()


I could not find in CGI::Application's docs if a single instance object
is capable of handling multiple requests (i.e. has run() invoked several
times) or not.

If a fresh WebApp instance should be used for each request, it might be 
worth mentioning (or even carp'ing about it if run gets called more
than once), and the synopsis of CGI::Application::Server should be
changed accordingly.

If handling multiple requests with a single instance object should be
ok, then CGI::Application should cleanup its per-request variables from
the object hash. There might be others besides __PRERUN_MODE and
__HEADER_PROPS (session variables / CGI query objects that should not
spill between different sessions?)

Regards,
Matthias


More information about the cgiapp mailing list