X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=22309080dd0066a22271ec02307a59dc491256a4;hb=4427ee06faecbb7a080ca98d2f5ed191ad6dfccf;hp=de7e082ba29e9c7ca98fd583fae07f3256992f6c;hpb=62d9b03003fb7bc4be52d27b67cf0edcaaf18162;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index de7e082..2230908 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -1,36 +1,22 @@ package Catalyst::Engine; use strict; -use base qw/Class::Data::Inheritable Class::Accessor::Fast/; -use attributes (); -use UNIVERSAL::require; +use base 'Class::Accessor::Fast'; use CGI::Cookie; use Data::Dumper; use HTML::Entities; +use HTTP::Body; use HTTP::Headers; -use Time::HiRes qw/gettimeofday tv_interval/; -use Text::ASCIITable; -use Catalyst::Exception; -use Catalyst::Request; -use Catalyst::Request::Upload; -use Catalyst::Response; -use Catalyst::Utils; - -__PACKAGE__->mk_classdata('components'); -__PACKAGE__->mk_accessors(qw/counter depth request response state/); - -*comp = \&component; -*req = \&request; -*res = \&response; - -# For backwards compatibility -*finalize_output = \&finalize_body; - -# For statistics -our $COUNT = 1; -our $START = time; -our $RECURSION = 1000; -our $DETACH = "catalyst_detach\n"; +use URI::QueryParam; + +# input position and length +__PACKAGE__->mk_accessors(qw/read_position read_length/); + +# Stringify to class +use overload '""' => sub { return ref shift }, fallback => 1; + +# Amount of data to read from input on each pass +our $CHUNKSIZE = 4096; =head1 NAME @@ -46,216 +32,40 @@ See L. =over 4 -=item $c->benchmark($coderef) - -Takes a coderef with arguments and returns elapsed time as float. - - my ( $elapsed, $status ) = $c->benchmark( sub { return 1 } ); - $c->log->info( sprintf "Processing took %f seconds", $elapsed ); - -=cut - -sub benchmark { - my $c = shift; - my $code = shift; - my $time = [gettimeofday]; - my @return = &$code(@_); - my $elapsed = tv_interval $time; - return wantarray ? ( $elapsed, @return ) : $elapsed; -} - -=item $c->comp($name) - -=item $c->component($name) - -Get a component object by name. - - $c->comp('MyApp::Model::MyModel')->do_stuff; - -Regex search for a component. - - $c->comp('mymodel')->do_stuff; - -=cut - -sub component { - my $c = shift; - - if (@_) { - - my $name = shift; - - if ( my $component = $c->components->{$name} ) { - return $component; - } - - else { - for my $component ( keys %{ $c->components } ) { - return $c->components->{$component} if $component =~ /$name/i; - } - } - } - - return sort keys %{ $c->components }; -} - -=item $c->counter - -Returns a hashref containing coderefs and execution counts. -(Needed for deep recursion detection) - -=item $c->depth - -Returns the actual forward depth. - -=item $c->error - -=item $c->error($error, ...) - -=item $c->error($arrayref) +=item $self->finalize_output -Returns an arrayref containing error messages. - - my @error = @{ $c->error }; - -Add a new error. - - $c->error('Something bad happened'); - -=cut - -sub error { - my $c = shift; - my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; - push @{ $c->{error} }, @$error; - return $c->{error}; -} +, see finalize_body -=item $c->execute($class, $coderef) +=item $self->finalize_body($c) -Execute a coderef in given class and catch exceptions. -Errors are available via $c->error. +Finalize body. Prints the response output. =cut -sub execute { - my ( $c, $class, $code ) = @_; - $class = $c->components->{$class} || $class; - $c->state(0); - my $callsub = ( caller(1) )[3]; - - my $action = ''; - if ( $c->debug ) { - $action = $c->actions->{reverse}->{"$code"}; - $action = "/$action" unless $action =~ /\-\>/; - $c->counter->{"$code"}++; - - if ( $c->counter->{"$code"} > $RECURSION ) { - my $error = qq/Deep recursion detected in "$action"/; - $c->log->error($error); - $c->error($error); - $c->state(0); - return $c->state; +sub finalize_body { + my ( $self, $c ) = @_; + if ( ref $c->response->body && $c->response->body->can('read') ) { + while ( !$c->response->body->eof() ) { + $c->response->body->read( my $buffer, $CHUNKSIZE ); + $self->write( $c, $buffer ); } - - $action = "-> $action" if $callsub =~ /forward$/; + $c->response->body->close(); } - - $c->{depth}++; - eval { - if ( $c->debug ) - { - my ( $elapsed, @state ) = - $c->benchmark( $code, $class, $c, @{ $c->req->args } ); - push @{ $c->{stats} }, [ $action, sprintf( '%fs', $elapsed ) ]; - $c->state(@state); - } - else { $c->state( &$code( $class, $c, @{ $c->req->args } ) || 0 ) } - }; - $c->{depth}--; - - if ( my $error = $@ ) { - - if ( $error eq $DETACH ) { die $DETACH if $c->{depth} > 1 } - else { - unless ( ref $error ) { - chomp $error; - $error = qq/Caught exception "$error"/; - } - - $c->log->error($error); - $c->error($error); - $c->state(0); - } - } - return $c->state; -} - -=item $c->finalize - -Finalize request. - -=cut - -sub finalize { - my $c = shift; - - $c->finalize_cookies; - - if ( my $location = $c->response->redirect ) { - $c->log->debug(qq/Redirecting to "$location"/) if $c->debug; - $c->response->header( Location => $location ); - $c->response->status(302) if $c->response->status !~ /^3\d\d$/; - } - - if ( $#{ $c->error } >= 0 ) { - $c->finalize_error; - } - - if ( !$c->response->body && $c->response->status == 200 ) { - $c->finalize_error; - } - - if ( $c->response->body && !$c->response->content_length ) { - $c->response->content_length( bytes::length( $c->response->body ) ); - } - - if ( $c->response->status =~ /^(1\d\d|[23]04)$/ ) { - $c->response->headers->remove_header("Content-Length"); - $c->response->body(''); - } - - if ( $c->request->method eq 'HEAD' ) { - $c->response->body(''); + else { + $self->write( $c, $c->response->body ); } - - my $status = $c->finalize_headers; - $c->finalize_body; - return $status; } -=item $c->finalize_output - -, see finalize_body - -=item $c->finalize_body - -Finalize body. - -=cut - -sub finalize_body { } - -=item $c->finalize_cookies - -Finalize cookies. +=item $self->finalize_cookies($c) =cut sub finalize_cookies { - my $c = shift; + my ( $self, $c ) = @_; + my @cookies; while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { + my $cookie = CGI::Cookie->new( -name => $name, -value => $cookie->{value}, @@ -265,18 +75,20 @@ sub finalize_cookies { -secure => $cookie->{secure} || 0 ); - $c->res->headers->push_header( 'Set-Cookie' => $cookie->as_string ); + push @cookies, $cookie->as_string; } -} -=item $c->finalize_error + if (@cookies) { + $c->res->headers->push_header( 'Set-Cookie' => join ',', @cookies ); + } +} -Finalize error. +=item $self->finalize_error($c) =cut sub finalize_error { - my $c = shift; + my ( $self, $c ) = @_; $c->res->headers->content_type('text/html'); my $name = $c->config->{name} || 'Catalyst Application'; @@ -291,18 +103,40 @@ sub finalize_error { @{ $c->error }; $error ||= 'No output'; $title = $name = "$name on Catalyst $Catalyst::VERSION"; + + # Don't show context in the dump + delete $c->req->{_context}; + delete $c->res->{_context}; + + # Don't show body parser in the dump + delete $c->req->{_body}; + + # Don't show response header state in dump + delete $c->res->{_finalized_headers}; + my $req = encode_entities Dumper $c->req; my $res = encode_entities Dumper $c->res; my $stash = encode_entities Dumper $c->stash; - $infos = <<""; -
-Request
-
$req
-Response
-
$res
-Stash
-
$stash
+ my @infos; + my $i = 0; + for my $dump ( $c->dump_these ) { + my $name = $dump->[0]; + my $value = encode_entities( Dumper $dump->[1] ); + push @infos, sprintf <<"EOF", $name, $value; +
+ + %s + +
+
+
+
%s
+
+EOF + $i++; + } + $infos = join "\n", @infos; } else { $title = $name; @@ -316,7 +150,7 @@ sub finalize_error { (fr) Veuillez revenir plus tard (es) Vuelto por favor mas adelante (pt) Voltado por favor mais tarde -(it) Ritornato prego più successivamente +(it) Ritornato prego più successivamente $name = ''; @@ -325,6 +159,18 @@ sub finalize_error { $title +