From: Christian Hansen Date: Sat, 23 Apr 2005 15:44:35 +0000 (+0000) Subject: Added body_ref and body_length and minor optimization, use refs where it's possible X-Git-Tag: 5.7099_04~1445 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=8fbcd90cdb30aed53d22d1cdbad95880f1c11693 Added body_ref and body_length and minor optimization, use refs where it's possible --- diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 1a2d3ee..cbc5896 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -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 || ''; diff --git a/lib/Catalyst/Engine/Test.pm b/lib/Catalyst/Engine/Test.pm index a093ccb..f7afec3 100644 --- a/lib/Catalyst/Engine/Test.pm +++ b/lib/Catalyst/Engine/Test.pm @@ -55,7 +55,7 @@ This class overloads some methods from C. 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 diff --git a/lib/Catalyst/Manual/Internals.pod b/lib/Catalyst/Manual/Internals.pod index dcebdc4..0c2c33d 100644 --- a/lib/Catalyst/Manual/Internals.pod +++ b/lib/Catalyst/Manual/Internals.pod @@ -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 diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index db1ed87..09741ab 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -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 or C. 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 diff --git a/lib/Catalyst/Response.pm b/lib/Catalyst/Response.pm index 2d83a48..a433f02 100644 --- a/lib/Catalyst/Response.pm +++ b/lib/Catalyst/Response.pm @@ -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