add $c->finalize_error and improve $c->finalize
Christian Hansen [Sat, 2 Apr 2005 05:59:33 +0000 (05:59 +0000)]
lib/Catalyst/Engine.pm
lib/Catalyst/Engine/Apache.pm
lib/Catalyst/Engine/Test.pm

index 4b7fad7..b469d70 100644 (file)
@@ -167,105 +167,19 @@ sub finalize {
         $c->response->status(302);
     }
 
-    if ( $c->res->status =~ /^(1\d\d|[23]04)$/ ) {
-        $c->response->headers->remove_content_headers;
-        return $c->finalize_headers;
-    }
-
-    if ( !$c->res->output || $#{ $c->error } >= 0 ) {
-        $c->res->headers->content_type('text/html');
-        my $name = $c->config->{name} || 'Catalyst Application';
-        my ( $title, $error, $infos );
-        if ( $c->debug ) {
-            $error = join '<br/>', @{ $c->error };
-            $error ||= 'No output';
-            $title = $name = "$name on Catalyst $Catalyst::VERSION";
-            my $req   = encode_entities Dumper $c->req;
-            my $res   = encode_entities Dumper $c->res;
-            my $stash = encode_entities Dumper $c->stash;
-            $infos = <<"";
-<br/>
-<b><u>Request</u></b><br/>
-<pre>$req</pre>
-<b><u>Response</u></b><br/>
-<pre>$res</pre>
-<b><u>Stash</u></b><br/>
-<pre>$stash</pre>
-
-        }
-        else {
-            $title = $name;
-            $error = '';
-            $infos = <<"";
-<pre>
-(en) Please come back later
-(de) Bitte versuchen sie es spaeter nocheinmal
-(nl) Gelieve te komen later terug
-(no) Vennligst prov igjen senere
-(fr) Veuillez revenir plus tard
-(es) Vuelto por favor mas adelante
-(pt) Voltado por favor mais tarde
-(it) Ritornato prego più successivamente
-</pre>
+    if ( $#{ $c->error } >= 0 ) {
+        $c->finalize_error;
+    }
 
-            $name = '';
-        }
-        $c->res->{output} = <<"";
-<html>
-    <head>
-        <title>$title</title>
-        <style type="text/css">
-            body {
-                font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
-                             Tahoma, Arial, helvetica, sans-serif;
-                color: #ddd;
-                background-color: #eee;
-                margin: 0px;
-                padding: 0px;
-            }
-            div.box {
-                background-color: #ccc;
-                border: 1px solid #aaa;
-                padding: 4px;
-                margin: 10px;
-                -moz-border-radius: 10px;
-            }
-            div.error {
-                background-color: #977;
-                border: 1px solid #755;
-                padding: 8px;
-                margin: 4px;
-                margin-bottom: 10px;
-                -moz-border-radius: 10px;
-            }
-            div.infos {
-                background-color: #797;
-                border: 1px solid #575;
-                padding: 8px;
-                margin: 4px;
-                margin-bottom: 10px;
-                -moz-border-radius: 10px;
-            }
-            div.name {
-                background-color: #779;
-                border: 1px solid #557;
-                padding: 8px;
-                margin: 4px;
-                -moz-border-radius: 10px;
-            }
-        </style>
-    </head>
-    <body>
-        <div class="box">
-            <div class="error">$error</div>
-            <div class="infos">$infos</div>
-            <div class="name">$name</div>
-        </div>
-    </body>
-</html>
+    if ( !$c->res->output && $c->res->status !~ /^(1\d\d|[23]04)$/ ) {
+        $c->finalize_error;
+    }
 
+    if ( $c->res->output ) {
+        use bytes; # play safe with a utf8 aware perl
+        $c->response->content_length( length $c->res->output );
     }
-    $c->res->headers->content_length( length $c->res->output );
+
     my $status = $c->finalize_headers;
     $c->finalize_output;
     return $status;
@@ -294,6 +208,109 @@ sub finalize_cookies {
     }
 }
 
+=item $c->finalize_error
+
+Finalize error.
+
+=cut
+
+sub finalize_error {
+    my $c = shift;
+
+    $c->res->headers->content_type('text/html');
+    my $name = $c->config->{name} || 'Catalyst Application';
+
+    my ( $title, $error, $infos );
+    if ( $c->debug ) {
+        $error = join '<br/>', @{ $c->error };
+        $error ||= 'No output';
+        $title = $name = "$name on Catalyst $Catalyst::VERSION";
+        my $req   = encode_entities Dumper $c->req;
+        my $res   = encode_entities Dumper $c->res;
+        my $stash = encode_entities Dumper $c->stash;
+        $infos = <<"";
+<br/>
+<b><u>Request</u></b><br/>
+<pre>$req</pre>
+<b><u>Response</u></b><br/>
+<pre>$res</pre>
+<b><u>Stash</u></b><br/>
+<pre>$stash</pre>
+
+    }
+    else {
+        $title = $name;
+        $error = '';
+        $infos = <<"";
+<pre>
+(en) Please come back later
+(de) Bitte versuchen sie es spaeter nocheinmal
+(nl) Gelieve te komen later terug
+(no) Vennligst prov igjen senere
+(fr) Veuillez revenir plus tard
+(es) Vuelto por favor mas adelante
+(pt) Voltado por favor mais tarde
+(it) Ritornato prego più successivamente
+</pre>
+
+        $name = '';
+    }
+    $c->res->output( <<"" );
+<html>
+<head>
+    <title>$title</title>
+    <style type="text/css">
+        body {
+            font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
+                         Tahoma, Arial, helvetica, sans-serif;
+            color: #ddd;
+            background-color: #eee;
+            margin: 0px;
+            padding: 0px;
+        }
+        div.box {
+            background-color: #ccc;
+            border: 1px solid #aaa;
+            padding: 4px;
+            margin: 10px;
+            -moz-border-radius: 10px;
+        }
+        div.error {
+            background-color: #977;
+            border: 1px solid #755;
+            padding: 8px;
+            margin: 4px;
+            margin-bottom: 10px;
+            -moz-border-radius: 10px;
+        }
+        div.infos {
+            background-color: #797;
+            border: 1px solid #575;
+            padding: 8px;
+            margin: 4px;
+            margin-bottom: 10px;
+            -moz-border-radius: 10px;
+        }
+        div.name {
+            background-color: #779;
+            border: 1px solid #557;
+            padding: 8px;
+            margin: 4px;
+            -moz-border-radius: 10px;
+        }
+    </style>
+</head>
+<body>
+    <div class="box">
+        <div class="error">$error</div>
+        <div class="infos">$infos</div>
+        <div class="name">$name</div>
+    </div>
+</body>
+</html>
+
+}
+
 =item $c->finalize_headers
 
 Finalize headers.
index f715456..ac7fe90 100644 (file)
@@ -70,7 +70,7 @@ sub finalize_headers {
 
 sub finalize_output {
     my $c = shift;
-    $c->apache->print( $c->response->{output} );
+    $c->apache->print( $c->response->output );
 }
 
 =item $c->prepare_connection
index fb6f26e..429bdad 100644 (file)
@@ -69,7 +69,7 @@ sub finalize_headers {
 
 sub finalize_output {
     my $c = shift;
-    $c->http->response->content_ref( \$c->response->{output} );
+    $c->http->response->content( $c->response->output );
 }
 
 =item $c->prepare_connection