Fix ->finalize_headers getting called twice. RT#78090
Tomas Doran [Sat, 30 Jun 2012 15:40:57 +0000 (16:40 +0100)]
Changes
lib/Catalyst.pm
lib/Catalyst/Engine.pm
lib/Catalyst/Response.pm
t/aggregate/live_component_controller_action_streaming.t
t/lib/TestApp.pm

diff --git a/Changes b/Changes
index 68a4626..78fb387 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,7 @@
 # This file documents the revision history for Perl extension Catalyst.
 
+  - Fix $c->finalize_headers getting called twice. RT#78090
+
 5.90014 - 2012-06-26 10:00:00
 
   - Fix calling finalize_headers before writing body when using $c->write /
index 77bd1c5..0e15f5b 100644 (file)
@@ -1788,7 +1788,7 @@ sub finalize {
             $c->finalize_error;
         }
 
-        $c->finalize_headers;
+        $c->finalize_headers unless $c->response->finalized_headers;
 
         # HEAD request
         if ( $c->request->method eq 'HEAD' ) {
@@ -1898,7 +1898,7 @@ EOF
 
     $c->finalize_cookies;
 
-    $c->engine->finalize_headers( $c, @_ );
+    $c->response->finalize_headers();
 
     # Done
     $response->finalized_headers(1);
index 4781e45..c414896 100644 (file)
@@ -331,7 +331,7 @@ Allows engines to write headers to response
 sub finalize_headers {
     my ($self, $ctx) = @_;
 
-    $ctx->response->finalize_headers;
+    $ctx->finalize_headers unless $ctx->response->finalized_headers;
     return;
 }
 
index eebe22c..6dc661e 100644 (file)
@@ -57,7 +57,7 @@ sub write {
     my ( $self, $buffer ) = @_;
 
     # Finalize headers if someone manually writes output
-    $self->_context->finalize_headers;
+    $self->_context->finalize_headers unless $self->finalized_headers;
 
     $buffer = q[] unless defined $buffer;
 
index f6d93ee..ba18bd7 100644 (file)
@@ -30,6 +30,7 @@ sub run_tests {
         ok( $response->is_success, 'Response Successful 2xx' );
         is( $response->content_type, 'text/plain', 'Response Content-Type' );
         is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
+        is( $response->header('X-Test-Header-Call-Count'), 1);
 
         SKIP:
         {
@@ -69,6 +70,7 @@ EOF
         is( $response->content_type, 'text/plain', 'Response Content-Type' );
         is( $response->content_length, -s $file, 'Response Content-Length' );
         is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
+        is( $response->header('X-Test-Header-Call-Count'), 1);
         is( $response->content, $buffer, 'Content is read from filehandle' );
 
         ok( $response = request('http://localhost/action/streaming/body_glob'),
@@ -77,6 +79,7 @@ EOF
         is( $response->content_type, 'text/plain', 'Response Content-Type' );
         is( $response->content_length, -s $file, 'Response Content-Length' );
         is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
+        is( $response->header('X-Test-Header-Call-Count'), 1);
         is( $response->content, $buffer, 'Content is read from filehandle' );
     }
 
@@ -87,6 +90,7 @@ EOF
         ok( $response->is_success, 'Response Successful 2xx' );
         is( $response->content_type, 'text/plain', 'Response Content-Type' );
         is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
+        is( $response->header('X-Test-Header-Call-Count'), 1);
         is( $response->content_length, $size, 'Response Content-Length' );
         is( $response->content, "\0" x $size, 'Content is read from filehandle' );
     }
index d1d8797..a99301c 100644 (file)
@@ -131,6 +131,11 @@ sub finalize_headers {
 
     $c->res->header('X-Test-Header', 'valid');
 
+    my $call_count = $c->stash->{finalize_headers_call_count} || 0;
+    $call_count++;
+    $c->stash(finalize_headers_call_count => $call_count);
+    $c->res->header('X-Test-Header-Call-Count' => $call_count);
+
     return $c->maybe::next::method(@_);
 }