X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=56486f6378a4c6097b2d27380a602ec31c50b9de;hb=8f076801b5c41543fdc20859452ed2cea1e1f82f;hp=db352bd87cfc9db09b07e58989f907253d2f9638;hpb=210420bc3ac02e7f9ece45292c83f120331c3870;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index db352bd..56486f6 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -1,22 +1,49 @@ package Catalyst::Engine; -use strict; -use base 'Class::Accessor::Fast'; -use CGI::Cookie; -use Data::Dumper; +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 Moose::Util::TypeConstraints; +use Plack::Loader; +use Plack::Middleware::Conditional; +use Plack::Middleware::ReverseProxy; +use Catalyst::EngineLoader; +use Encode (); +use utf8; + +use namespace::clean -except => 'meta'; + +has env => (is => 'ro', writer => '_set_env', clearer => '_clear_env'); # 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', +); -# Stringify to class -use overload '""' => sub { return ref shift }, fallback => 1; +has _writer => ( + is => 'ro', + isa => duck_type([qw(write close)]), + writer => '_set_writer', + clearer => '_clear_writer', +); # Amount of data to read from input on each pass -our $CHUNKSIZE = 4096; +our $CHUNKSIZE = 64 * 1024; =head1 NAME @@ -40,21 +67,31 @@ 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 { $self->write( $c, $body ); } + + $self->_writer->close; + $self->_clear_writer; + $self->_clear_env; + + return; } =head2 $self->finalize_cookies($c) -Create CGI::Cookies from $c->res->cookies, and set them as response headers. +Create CGI::Simple::Cookie objects from $c->res->cookies, and set them as +response headers. =cut @@ -62,44 +99,78 @@ 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 = ( + 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+
'
. encode_entities($_)
@@ -111,30 +182,16 @@ sub finalize_error {
$name = "
$name
";
# Don't show context in the dump
- delete $c->req->{_context};
- delete $c->res->{_context};
+ $c->req->_clear_context;
+ $c->res->_clear_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;
+ $c->req->_clear_body;
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
-
(en) Please come back later
-(fr)
+(fr) SVP veuillez revenir plus tard
(de) Bitte versuchen sie es spaeter nocheinmal
(at) Konnten's bitt'schoen spaeter nochmal reinschauen
(no) Vennligst prov igjen senere
(dk) Venligst prov igen senere
(pl) Prosze sprobowac pozniej
+(pt) Por favor volte mais tarde
+(ru) ÐопÑобÑйÑе еÑе Ñаз позже
+(ua) СпÑобÑйÑе Ñе Ñаз пÑзнÑÑе
$name = '';
@@ -180,13 +240,13 @@ EOF
body {
font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
Tahoma, Arial, helvetica, sans-serif;
- color: #ddd;
+ color: #333;
background-color: #eee;
margin: 0px;
padding: 0px;
}
:link, :link:hover, :visited, :visited:hover {
- color: #ddd;
+ color: #000;
}
div.box {
position: relative;
@@ -194,30 +254,26 @@ EOF
border: 1px solid #aaa;
padding: 4px;
margin: 10px;
- -moz-border-radius: 10px;
}
div.error {
- background-color: #977;
+ background-color: #cce;
border: 1px solid #755;
padding: 8px;
margin: 4px;
margin-bottom: 10px;
- -moz-border-radius: 10px;
}
div.infos {
- background-color: #797;
+ background-color: #eee;
border: 1px solid #575;
padding: 8px;
margin: 4px;
margin-bottom: 10px;
- -moz-border-radius: 10px;
}
div.name {
- background-color: #779;
+ background-color: #cce;
border: 1px solid #557;
padding: 8px;
margin: 4px;
- -moz-border-radius: 10px;
}
code.error {
display: block;
@@ -240,7 +296,7 @@ EOF
}
/* from http://users.tkk.fi/~tkarvine/linux/doc/pre-wrap/pre-wrap-css3-mozilla-opera-ie.html */
/* Browser specific (not valid) styles to make preformatted text wrap */
- pre {
+ pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
@@ -258,10 +314,12 @@ EOF