X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=1e56bdc89afe2d9145a6a655ccf93b4a7b999b30;hb=676bed72afa0c79b9f26e1d548a49dbfb6ea15c7;hp=089a9595412c230c9a2e051392945328c51c7b8a;hpb=7d22a5373b8e3c57c1c6a9cae3d246efb840a8ea;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 089a959..1e56bdc 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -1,7 +1,8 @@ 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'; @@ -9,13 +10,50 @@ use HTML::Entities; use HTTP::Body; use HTTP::Headers; use URI::QueryParam; -use Scalar::Util (); +use Moose::Util::TypeConstraints; +use Plack::Loader; +use Catalyst::EngineLoader; +use Encode (); +use utf8; + +use namespace::clean -except => 'meta'; + +has env => (is => 'ro', writer => '_set_env', clearer => '_clear_env'); + +my $WARN_ABOUT_ENV = 0; +around env => sub { + my ($orig, $self, @args) = @_; + if(@args) { + warn "env as a writer is deprecated, you probably need to upgrade Catalyst::Engine::PSGI" + unless $WARN_ABOUT_ENV++; + return $self->_set_env(@args); + } + return $self->$orig; +}; # input position and length -__PACKAGE__->mk_accessors(qw/read_position read_length/); +has read_length => (is => 'rw'); +has read_position => (is => 'rw'); + +has _prepared_write => (is => 'rw'); + +has _response_cb => ( + is => 'ro', + isa => 'CodeRef', + writer => '_set_response_cb', + clearer => '_clear_response_cb', + predicate => '_has_response_cb', +); + +subtype 'Catalyst::Engine::Types::Writer', + as duck_type([qw(write close)]); -# Stringify to class -use overload '""' => sub { return ref shift }, fallback => 1; +has _writer => ( + is => 'ro', + isa => 'Catalyst::Engine::Types::Writer', + writer => '_set_writer', + clearer => '_clear_writer', +); # Amount of data to read from input on each pass our $CHUNKSIZE = 64 * 1024; @@ -43,16 +81,24 @@ sub finalize_body { my ( $self, $c ) = @_; 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 ); - } + 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 { $self->write( $c, $body ); } + + $self->_writer->close; + $self->_clear_writer; + $self->_clear_env; + + return; } =head2 $self->finalize_cookies($c) @@ -66,13 +112,14 @@ sub finalize_cookies { my ( $self, $c ) = @_; my @cookies; + my $response = $c->response; - foreach my $name ( keys %{ $c->response->cookies } ) { + foreach my $name (keys %{ $response->cookies }) { - my $val = $c->response->cookies->{$name}; + my $val = $response->cookies->{$name}; my $cookie = ( - Scalar::Util::blessed($val) + blessed($val) ? $val : CGI::Simple::Cookie->new( -name => $name, @@ -80,31 +127,63 @@ sub finalize_cookies { -expires => $val->{expires}, -domain => $val->{domain}, -path => $val->{path}, - -secure => $val->{secure} || 0 + -secure => $val->{secure} || 0, + -httponly => $val->{httponly} || 0, ) ); + if (!defined $cookie) { + $c->log->warn("undef passed in '$name' cookie value - not setting cookie") + if $c->debug; + next; + } 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-