Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Plack::Handler::Apache2: implement support for cleanup handlers

Change Plack::Handler::Apache2 to Implement support for the PSGI
environment cleanup handlers proposal specified at
https://github.com/plack/psgi-specs/wiki/Proposal:-PSGI-environment-cleanup-handlers. Being
able to emulate Apache-like behavior was the original reason for the
proposal, so it's fitting that we should have support for this in our
Apache compatibility layer.

The same patch should work pretty much as-is for Apache1 as well, but
I don't have access to an Apache1 server anymore so I've just
implemented it for Apache2.

Note that we check psgix.harakiri.commit after the cleanup handlers
have run in case one of the cleanup handlers sets that flag, if we
don't have any cleanup handlers we ask the server to child_terminate()
without setting up a PerlCleanupHandler.

This is perhaps a needles premature optimization (we could just get
rid of that if() entirely and always do "push_handlers"), but it
seemed to be superfluous to always set up a PerlCleanupHandler even
when we don't need any asynchronous cleanup, but only child
termination.

This patch is best viewed with "git show -w", I re-indented the
key/values in the $env assignment to fit the current coding style of
that file, but I didn't change any of the existing keys there. I only
added psgix.cleanup and psgix.cleanup.handlers.
  • Loading branch information...
commit 598c18cd31e60c3b2c8b88af4d9ebb63532b1d3a 1 parent b8eadec
@avar avar authored
Showing with 28 additions and 12 deletions.
  1. +28 −12 lib/Plack/Handler/Apache2.pm
View
40 lib/Plack/Handler/Apache2.pm
@@ -50,16 +50,18 @@ sub call_app {
my $env = {
%ENV,
- 'psgi.version' => [ 1, 1 ],
- 'psgi.url_scheme' => ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
- 'psgi.input' => $r,
- 'psgi.errors' => *STDERR,
- 'psgi.multithread' => Plack::Util::FALSE,
- 'psgi.multiprocess' => Plack::Util::TRUE,
- 'psgi.run_once' => Plack::Util::FALSE,
- 'psgi.streaming' => Plack::Util::TRUE,
- 'psgi.nonblocking' => Plack::Util::FALSE,
- 'psgix.harakiri' => Plack::Util::TRUE,
+ 'psgi.version' => [ 1, 1 ],
+ 'psgi.url_scheme' => ($ENV{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
+ 'psgi.input' => $r,
+ 'psgi.errors' => *STDERR,
+ 'psgi.multithread' => Plack::Util::FALSE,
+ 'psgi.multiprocess' => Plack::Util::TRUE,
+ 'psgi.run_once' => Plack::Util::FALSE,
+ 'psgi.streaming' => Plack::Util::TRUE,
+ 'psgi.nonblocking' => Plack::Util::FALSE,
+ 'psgix.harakiri' => Plack::Util::TRUE,
+ 'psgix.cleanup' => Plack::Util::TRUE,
+ 'psgix.cleanup.handlers' => [],
};
if (defined(my $HTTP_AUTHORIZATION = $r->headers_in->{Authorization})) {
@@ -87,8 +89,22 @@ sub call_app {
die "Bad response $res";
}
- if ($env->{'psgix.harakiri.commit'}) {
- $r->child_terminate;
+ if (@{ $env->{'psgix.cleanup.handlers'} }) {
+ $r->push_handlers(
+ PerlCleanupHandler => sub {
+ for my $cleanup_handler (@{ $env->{'psgix.cleanup.handlers'} }) {
+ $cleanup_handler->($env);
+ }
+
+ if ($env->{'psgix.harakiri.commit'}) {
+ $r->child_terminate;
+ }
+ },
+ );
+ } else {
+ if ($env->{'psgix.harakiri.commit'}) {
+ $r->child_terminate;
+ }
}
return Apache2::Const::OK;
Please sign in to comment.
Something went wrong with that request. Please try again.