Added body_ref and body_length and minor optimization, use refs where it's possible
Christian Hansen [Sat, 23 Apr 2005 15:44:35 +0000 (15:44 +0000)]
lib/Catalyst/Engine.pm
lib/Catalyst/Engine/Test.pm
lib/Catalyst/Manual/Internals.pod
lib/Catalyst/Request.pm
lib/Catalyst/Response.pm

index 1a2d3ee..cbc5896 100644 (file)
@@ -179,13 +179,12 @@ sub finalize {
         $c->finalize_error;
     }
 
-    if ( !$c->response->body && $c->response->status !~ /^(1|3)\d\d$/ ) {
+    if ( !$c->response->body_length && $c->response->status !~ /^(1|3)\d\d$/ ) {
         $c->finalize_error;
     }
 
-    if ( $c->response->body && !$c->response->content_length ) {
-        use bytes;    # play safe with a utf8 aware perl
-        $c->response->content_length( length $c->response->body );
+    if ( $c->response->body_length && !$c->response->content_length ) {
+        $c->response->content_length( $c->response->body_length );
     }
 
     my $status = $c->finalize_headers;
@@ -401,6 +400,7 @@ sub prepare {
         request => Catalyst::Request->new(
             {
                 arguments  => [],
+                body       => undef,
                 cookies    => {},
                 headers    => HTTP::Headers->new,
                 parameters => {},
@@ -409,7 +409,12 @@ sub prepare {
             }
         ),
         response => Catalyst::Response->new(
-            { cookies => {}, headers => HTTP::Headers->new, status => 200 }
+            { 
+                body       => undef,
+                cookies    => {},
+                headers    => HTTP::Headers->new,
+                status     => 200
+            }
         ),
         stash => {},
         state => 0
@@ -425,10 +430,10 @@ sub prepare {
     }
 
     $c->prepare_request($engine);
-    $c->prepare_path;
+    $c->prepare_connection;
     $c->prepare_headers;
     $c->prepare_cookies;
-    $c->prepare_connection;
+    $c->prepare_path;
     $c->prepare_action;
 
     my $method   = $c->req->method   || '';
index a093ccb..f7afec3 100644 (file)
@@ -55,7 +55,7 @@ This class overloads some methods from C<Catalyst::Engine>.
 
 sub finalize_body {
     my $c = shift;
-    $c->http->response->content( $c->response->body );
+    $c->http->response->content_ref( $c->response->body_ref );
 }
 
 =item $c->finalize_headers
index dcebdc4..0c2c33d 100644 (file)
@@ -53,10 +53,10 @@ extend Catalyst.
     handler
       prepare
         prepare_request
-        prepare_path
+        prepare_connection
         prepare_headers
         prepare_cookies
-        prepare_connection
+        prepare_path
         prepare_action
         prepare_body
         prepare_parameters
index db1ed87..09741ab 100644 (file)
@@ -33,6 +33,8 @@ Catalyst::Request - Catalyst Request Class
     $req->arguments;
     $req->base;
     $req->body;
+    $req->body_length;
+    $req->body_ref;
     $req->content_encoding;
     $req->content_length;
     $req->content_type;
@@ -99,6 +101,34 @@ C<application/x-www-form-urlencoded> or C<multipart/form-data>.
 
     print $c->request->body
 
+=item $req->body_length
+
+Returns the length of body in bytes.
+
+    print $c->request->body_length
+
+=cut
+
+sub body_length {
+    my $self = shift;
+    
+    use bytes;
+    
+    return 0 unless $self->body;
+    return length($self->body);
+}
+
+=item $req->body_ref
+
+Returns a reference to body.
+
+=cut
+
+sub body_ref {
+    my $self = shift;    
+    return \$self->{body};
+}
+
 =item $req->content_encoding
 
 Shortcut to $req->headers->content_encoding
index 2d83a48..a433f02 100644 (file)
@@ -20,6 +20,8 @@ Catalyst::Response - Catalyst Response Class
 
     $resp = $c->response;
     $resp->body;
+    $resp->body_length;
+    $resp->body_ref;
     $resp->content_encoding;
     $resp->content_length;
     $resp->content_type;
@@ -47,6 +49,34 @@ to response data.
 
 Contains the final output.
 
+=item $resp->body_length
+
+Returns the length of body in bytes.
+
+    print $c->response->body_length
+
+=cut
+
+sub body_length {
+    my $self = shift;
+    
+    use bytes;
+    
+    return 0 unless $self->body;
+    return length($self->body);
+}
+
+=item $resp->body_ref
+
+Returns a reference to body.
+
+=cut
+
+sub body_ref {
+    my $self = shift;    
+    return \$self->{body};
+}
+
 =item $resp->content_encoding
 
 Shortcut to $resp->headers->content_encoding