From cbebf8c9d2b9457ae212a61ec308edf138ee1dfb Mon Sep 17 00:00:00 2001 From: Ian Burrell Date: Tue, 10 Nov 2015 13:15:34 -0800 Subject: [PATCH 1/3] Call FCGI::Request Finish before post_dispatch --- lib/FCGI/Engine/Core.pm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/FCGI/Engine/Core.pm b/lib/FCGI/Engine/Core.pm index ffbd2ab..256cdf7 100644 --- a/lib/FCGI/Engine/Core.pm +++ b/lib/FCGI/Engine/Core.pm @@ -200,6 +200,8 @@ sub run { $request ); + $request->Finish(); + $proc_manager && $proc_manager->post_dispatch; } } From e5dc739a4e83730fcf3c40480e323f903a8262d3 Mon Sep 17 00:00:00 2001 From: Ian Burrell Date: Mon, 11 Jan 2016 14:27:27 -0800 Subject: [PATCH 2/3] Add Plack::Test::Suite tests for FCGI::Engine::PSGI --- t/201_fcgi_engine_psgi.t | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 t/201_fcgi_engine_psgi.t diff --git a/t/201_fcgi_engine_psgi.t b/t/201_fcgi_engine_psgi.t new file mode 100644 index 0000000..ca1f4f1 --- /dev/null +++ b/t/201_fcgi_engine_psgi.t @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use FindBin; + +use Test::More; + +BEGIN { + { + local $@; + eval "use Plack 0.9910; use FCGI::Client 0.06; use MooseX::NonMoose 0.07; use IO::String; use Plack::App::FCGIDispatcher;"; + plan skip_all => "Plack 0.9910, FCGI::Client 0.06, MooseX::NonMoose 0.07 and Plack::App::FCGIDispatcher are required for this test" if $@; + } +} + +use FCGI::Engine::PSGI; +use Test::TCP; +use Plack::Test::Suite; +use t::lib::FCGIUtils; + +my $http_port; +my $fcgi_port; + +test_fcgi_standalone( + sub { + ($http_port, $fcgi_port) = @_; + Plack::Test::Suite->run_server_tests(\&run_server, $fcgi_port, $http_port); + done_testing(); + } +); + +sub run_server { + my($port, $app) = @_; + + $| = 0; # Test::Builder autoflushes this. reset! + + my $server = FCGI::Engine::PSGI->new( + listen => "127.0.0.1:$port", + pidfile => '/tmp/101_plack_server_fcgi_engine_client.pid', + app => $app, + ); + $server->run(); +} + From faf864cf89b0c1f12aecf386b62631055ef61f96 Mon Sep 17 00:00:00 2001 From: Ian Burrell Date: Mon, 11 Jan 2016 13:20:56 -0800 Subject: [PATCH 3/3] Add psgix.harakiri support to FCGI::Engine::PSGI Refactor process_request to support running code after finishing request Pass proc_manager to prepare_environment --- lib/FCGI/Engine/Core.pm | 22 ++++++++++++++-------- lib/FCGI/Engine/PSGI.pm | 14 +++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/FCGI/Engine/Core.pm b/lib/FCGI/Engine/Core.pm index 256cdf7..a0ac8ec 100644 --- a/lib/FCGI/Engine/Core.pm +++ b/lib/FCGI/Engine/Core.pm @@ -192,18 +192,24 @@ sub run { } while ($request->Accept() >= 0) { + $self->process_request( + $self->prepare_environment($env, $proc_manager), + $request, + $proc_manager + ); + } +} - $proc_manager && $proc_manager->pre_dispatch; +sub process_request { + my ($self, $env, $request, $proc_manager) = @_; - $self->handle_request( - $self->prepare_environment( $env ), - $request - ); + $proc_manager && $proc_manager->pre_dispatch; - $request->Finish(); + $self->handle_request($env, $request); - $proc_manager && $proc_manager->post_dispatch; - } + $request->Finish(); + + $proc_manager && $proc_manager->post_dispatch; } 1; diff --git a/lib/FCGI/Engine/PSGI.pm b/lib/FCGI/Engine/PSGI.pm index 2db5157..18cf49f 100644 --- a/lib/FCGI/Engine/PSGI.pm +++ b/lib/FCGI/Engine/PSGI.pm @@ -21,7 +21,7 @@ has 'app' => ( # - SL augment 'prepare_environment' => sub { - my ($self, $env) = @_; + my ($self, $env, $proc_manager) = @_; return +{ %$env, 'psgi.version' => [1,0], @@ -34,6 +34,8 @@ augment 'prepare_environment' => sub { 'psgi.run_once' => Plack::Util::FALSE, 'psgi.streaming' => Plack::Util::TRUE, 'psgi.nonblocking' => Plack::Util::FALSE, + + 'psgix.harakiri' => defined $proc_manager, }; }; @@ -83,6 +85,16 @@ sub _handle_response { } } +around 'process_request' => sub { + my ($orig, $self, $env, $request, $proc_manager) = @_; + + $self->$orig($env, $request); + + if ($proc_manager && $env->{'psgix.harakiri.commit'}) { + $proc_manager->exit("safe exit with harakiri"); + } +}; + __PACKAGE__->meta->make_immutable; no Moose; 1;