X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=0389367c3092713fb1e284965051452d3618ead4;hp=40d4281bc9c8761f0cae3d0e541f23ca5574edc1;hb=f3414019f472b55682ef3af53f761b6db7955887;hpb=c6ef5e69c5192516590db9aa162c29f3424cc033 diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 40d4281..0389367 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -1,22 +1,26 @@ package Catalyst::Engine; -use strict; -use base 'Class::Accessor::Fast'; -use CGI::Cookie; -use Data::Dumper; +use Class::C3; +use Moose; +with 'MooseX::Emulate::Class::Accessor::Fast'; + +use CGI::Simple::Cookie; +use Data::Dump qw/dump/; +use Errno 'EWOULDBLOCK'; use HTML::Entities; use HTTP::Body; use HTTP::Headers; use URI::QueryParam; +use Scalar::Util (); # input position and length -__PACKAGE__->mk_accessors(qw/read_position read_length/); +has read_length => (is => 'rw'); +has read_position => (is => 'rw'); -# Stringify to class -use overload '""' => sub { return ref shift }, fallback => 1; +no Moose; # Amount of data to read from input on each pass -our $CHUNKSIZE = 4096; +our $CHUNKSIZE = 64 * 1024; =head1 NAME @@ -30,13 +34,8 @@ See L. =head1 METHODS -=over 4 - -=item $self->finalize_output -, see finalize_body - -=item $self->finalize_body($c) +=head2 $self->finalize_body($c) Finalize body. Prints the response output. @@ -44,19 +43,24 @@ Finalize body. Prints the response output. 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 ); + my $body = $c->response->body; + no warnings 'uninitialized'; + if ( Scalar::Util::blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { + while ( !eof $body ) { + read $body, my ($buffer), $CHUNKSIZE; + last unless $self->write( $c, $buffer ); } - $c->response->body->close(); + close $body; } else { - $self->write( $c, $c->response->body ); + $self->write( $c, $body ); } } -=item $self->finalize_cookies($c) +=head2 $self->finalize_cookies($c) + +Create CGI::Simple::Cookie objects from $c->res->cookies, and set them as +response headers. =cut @@ -64,45 +68,60 @@ sub finalize_cookies { my ( $self, $c ) = @_; my @cookies; - while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { - - my $cookie = CGI::Cookie->new( - -name => $name, - -value => $cookie->{value}, - -expires => $cookie->{expires}, - -domain => $cookie->{domain}, - -path => $cookie->{path}, - -secure => $cookie->{secure} || 0 + my $response = $c->response; + + foreach my $name (keys %{ $response->cookies }) { + + my $val = $response->cookies->{$name}; + + my $cookie = ( + Scalar::Util::blessed($val) + ? $val + : CGI::Simple::Cookie->new( + -name => $name, + -value => $val->{value}, + -expires => $val->{expires}, + -domain => $val->{domain}, + -path => $val->{path}, + -secure => $val->{secure} || 0 + ) ); push @cookies, $cookie->as_string; } - if (@cookies) { - $c->res->headers->push_header( 'Set-Cookie' => join ',', @cookies ); + for my $cookie (@cookies) { + $response->headers->push_header( 'Set-Cookie' => $cookie ); } } -=item $self->finalize_error($c) +=head2 $self->finalize_error($c) + +Output an apropriate error message, called if there's an error in $c +after the dispatch has finished. Will output debug messages if Catalyst +is in debug mode, or a `please come back later` message otherwise. =cut sub finalize_error { my ( $self, $c ) = @_; - $c->res->headers->content_type('text/html'); - my $name = $c->config->{name} || 'Catalyst Application'; + $c->res->content_type('text/html; charset=utf-8'); + my $name = $c->config->{name} || join(' ', split('::', ref $c)); my ( $title, $error, $infos ); if ( $c->debug ) { # For pretty dumps - local $Data::Dumper::Terse = 1; - $error = join '', - map { '' . encode_entities($_) . '' } - @{ $c->error }; + $error = join '', map { + '

' + . encode_entities($_) + . '

' + } @{ $c->error }; $error ||= 'No output'; + $error = qq{
$error
}; $title = $name = "$name on Catalyst $Catalyst::VERSION"; + $name = "

$name

"; # Don't show context in the dump delete $c->req->{_context}; @@ -111,29 +130,15 @@ sub finalize_error { # 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; - my @infos; my $i = 0; - warn "BAAR"; for my $dump ( $c->dump_these ) { - warn "FOOO"; my $name = $dump->[0]; - my $value = encode_entities( Dumper $dump->[1] ); + my $value = encode_entities( dump( $dump->[1] )); push @infos, sprintf <<"EOF", $name, $value; -
- - %s - -
-
+

%s

-
%s
+
%s
EOF $i++; @@ -146,29 +151,33 @@ EOF $infos = <<"";
 (en) Please come back later
+(fr) SVP veuillez revenir plus tard
 (de) Bitte versuchen sie es spaeter nocheinmal
-(nl) Gelieve te komen later terug
+(at) Konnten's bitt'schoen spaeter nochmal reinschauen
 (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
+(dk) Venligst prov igen senere
+(pl) Prosze sprobowac pozniej
 
$name = ''; } $c->res->body( <<"" ); - + + + + $title -