X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=e770f325fb97fd8ade0cbe9ecd6dbd4ec8af6260;hb=0bf3b41caa076beabccbb8f486ffddc1991d4f81;hp=7f7a661b088950e44ae03c003079a47a338ce5e4;hpb=fa32ac82eacd5c38b4b4dda10c472901a59c72fe;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 7f7a661..e770f32 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -1,22 +1,30 @@ package Catalyst::Engine; -use strict; -use base 'Class::Accessor::Fast'; +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 Encode (); +use utf8; + +use namespace::clean -except => 'meta'; + +has env => (is => 'rw'); # 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; +has _prepared_write => (is => 'rw'); # Amount of data to read from input on each pass -our $CHUNKSIZE = 4096; +our $CHUNKSIZE = 64 * 1024; =head1 NAME @@ -40,11 +48,14 @@ Finalize body. Prints the response output. sub finalize_body { my ( $self, $c ) = @_; my $body = $c->response->body; - if ( ref $body && ( $body->can('read') || ref($body) eq 'GLOB' ) ) { - while ( !eof $body ) { - read $body, my ($buffer), $CHUNKSIZE; - last unless $self->write( $c, $buffer ); - } + no warnings 'uninitialized'; + if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { + my $got; + do { + $got = read $body, my ($buffer), $CHUNKSIZE; + $got = 0 unless $self->write( $c, $buffer ); + } while $got > 0; + close $body; } else { @@ -63,41 +74,73 @@ sub finalize_cookies { my ( $self, $c ) = @_; my @cookies; - - foreach my $name ( keys %{ $c->response->cookies } ) { - - my $val = $c->response->cookies->{$name}; - - my $cookie = CGI::Simple::Cookie->new( - -name => $name, - -value => $val->{value}, - -expires => $val->{expires}, - -domain => $val->{domain}, - -path => $val->{path}, - -secure => $val->{secure} || 0 + my $response = $c->response; + + foreach my $name (keys %{ $response->cookies }) { + + my $val = $response->cookies->{$name}; + + my $cookie = ( + 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, + -httponly => $val->{httponly} || 0, + ) ); push @cookies, $cookie->as_string; } for my $cookie (@cookies) { - $c->res->headers->push_header( 'Set-Cookie' => $cookie ); + $response->headers->push_header( 'Set-Cookie' => $cookie ); } } =head2 $self->finalize_error($c) -Output an apropriate error message, called if there's an error in $c +Output an appropriate 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 _dump_error_page_element { + my ($self, $i, $element) = @_; + my ($name, $val) = @{ $element }; + + # This is fugly, but the metaclass is _HUGE_ and demands waaay too much + # scrolling. Suggestions for more pleasant ways to do this welcome. + local $val->{'__MOP__'} = "Stringified: " + . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'}; + + my $text = encode_entities( dump( $val )); + sprintf <<"EOF", $name, $text; +
%s+
%s-