From 742db182e8f15a90dc2481d420eaaf9ba800d006 Mon Sep 17 00:00:00 2001 From: Scott Walters Date: Wed, 4 May 2011 23:11:56 -0400 Subject: [PATCH] don't force an extra layer of streaming as doing so bypasses plack middlewares. --- lib/WebGUI.pm | 52 ++++++++++++++++++-------------- lib/WebGUI/Middleware/Session.pm | 2 +- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index 3580bbc62..e6fcf2bc1 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -68,23 +68,37 @@ sub call { my $self = shift; my $env = shift; - # Use the PSGI callback style response, which allows for nice things like - # delayed response/streaming body (server push). For now we just use this for - # unbuffered response writing - return sub { - my $responder = shift; - my $session = $env->{'webgui.session'} - or die 'Missing WebGUI Session - check WebGUI::Middleware::Session'; + my $session = $env->{'webgui.session'} + or die 'Missing WebGUI Session - check WebGUI::Middleware::Session'; - # Handle the request - $self->handle($session); + # Handle the request - # Construct the PSGI response - my $response = $session->response; - my $psgi_response = $response->finalize; + $self->handle($session); + + my $response = $session->response; + my $psgi_response = $response->finalize; + + if ( ! $response->streaming ) { + + # Not streaming, so immediately tell the callback to return + # the response. In the future we could use an Event framework here + # to make this a non-blocking delayed response. + + return $psgi_response; + + } + else { + + # Use the PSGI callback style response, which allows for nice things like + # delayed response/streaming body (server push). + # Delayed response prevents any nice MiddleWare::StackTrace-like modules from + # engaging so minimal error handling is done here. + + return sub { + my $responder = shift; + + # Construct the PSGI response - # See if the content handler is doing unbuffered response writing - if ( $response->streaming ) { try { # Ask PSGI server for a streaming writer object by returning only the first # two elements of the array reference @@ -114,18 +128,12 @@ sub call { } }; } - else { - # Not streaming, so immediately tell the callback to return - # the response. In the future we could use an Event framework here - # to make this a non-blocking delayed response. - $responder->($psgi_response); - } - }; + } } sub handle { my ( $self, $session ) = @_; - + # uncomment the following to short-circuit contentHandlers (for benchmarking PSGI scaffolding vs. modperl) # $session->output->print("WebGUI PSGI with contentHandlers short-circuited for benchmarking\n"); # return; diff --git a/lib/WebGUI/Middleware/Session.pm b/lib/WebGUI/Middleware/Session.pm index 9ff4d7ca7..1f66360b8 100644 --- a/lib/WebGUI/Middleware/Session.pm +++ b/lib/WebGUI/Middleware/Session.pm @@ -74,7 +74,7 @@ sub call { my $res = shift; # Close the Session if we aren't streaming - if ( !$env->{'webgui.session'}->response->streaming ) { + if ( $env->{'webgui.session'} and $env->{'webgui.session'}->response and ! $env->{'webgui.session'}->response->streaming ) { $env->{'webgui.session'}->close(); delete $env->{'webgui.session'}; }