X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=7f0a11b045ecceb30762d4c15beead3d162c3d03;hb=6f409682667c23a00552d0573c157ee3487c29bc;hp=f378d57d6f4de367b7ac872bcaa21c7c0f20f61b;hpb=fbcc39ad23f2bbecf5d84c9ba581e6af86fcd460;p=catagits%2FCatalyst-Runtime.git
diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm
index f378d57..7f0a11b 100644
--- a/lib/Catalyst/Engine.pm
+++ b/lib/Catalyst/Engine.pm
@@ -7,13 +7,17 @@ use Data::Dumper;
use HTML::Entities;
use HTTP::Body;
use HTTP::Headers;
+use URI::QueryParam;
# input position and length
-__PACKAGE__->mk_accessors( qw/read_position read_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
Catalyst::Engine - The Catalyst Engine
@@ -40,8 +44,16 @@ Finalize body. Prints the response output.
sub finalize_body {
my ( $self, $c ) = @_;
-
- $self->write( $c, $c->response->output );
+ if ( ref $c->response->body && $c->response->body->can('read') ) {
+ while ( !$c->response->body->eof() ) {
+ $c->response->body->read( my $buffer, $CHUNKSIZE );
+ last unless $self->write( $c, $buffer );
+ }
+ $c->response->body->close();
+ }
+ else {
+ $self->write( $c, $c->response->body );
+ }
}
=item $self->finalize_cookies($c)
@@ -66,8 +78,8 @@ sub finalize_cookies {
push @cookies, $cookie->as_string;
}
- if (@cookies) {
- $c->res->headers->push_header( 'Set-Cookie' => join ',', @cookies );
+ for my $cookie (@cookies) {
+ $c->res->headers->push_header( 'Set-Cookie' => $cookie );
}
}
@@ -78,7 +90,7 @@ sub finalize_cookies {
sub finalize_error {
my ( $self, $c ) = @_;
- $c->res->headers->content_type('text/html');
+ $c->res->content_type('text/html; charset=utf-8');
my $name = $c->config->{name} || 'Catalyst Application';
my ( $title, $error, $infos );
@@ -87,10 +99,11 @@ sub finalize_error {
# For pretty dumps
local $Data::Dumper::Terse = 1;
$error = join '',
- map { '' . encode_entities($_) . '
' }
+ map { '
' . encode_entities($_) . '
' }
@{ $c->error };
$error ||= 'No output';
$title = $name = "$name on Catalyst $Catalyst::VERSION";
+ $name = "$name
";
# Don't show context in the dump
delete $c->req->{_context};
@@ -105,15 +118,21 @@ sub finalize_error {
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;
+
+
+EOF
+ $i++;
+ }
+ $infos = join "\n", @infos;
}
else {
$title = $name;
@@ -133,9 +152,26 @@ sub finalize_error {
$name = '';
}
$c->res->body( <<"" );
-
+
+
+
+
$title
+
@@ -206,7 +260,7 @@ sub finalize_headers { }
sub finalize_read {
my ( $self, $c ) = @_;
-
+
undef $self->{_prepared_read};
}
@@ -240,14 +294,24 @@ sub prepare_body {
unless ( $c->request->{_body} ) {
$c->request->{_body} = HTTP::Body->new( $type, $self->read_length );
}
-
+
if ( $self->read_length > 0 ) {
- while ( my $buffer = $self->read( $c ) ) {
- $c->request->{_body}->add( $buffer );
+ while ( my $buffer = $self->read($c) ) {
+ $c->prepare_body_chunk($buffer);
}
}
}
+=item $self->prepare_body_chunk($c)
+
+=cut
+
+sub prepare_body_chunk {
+ my ( $self, $c, $chunk ) = @_;
+
+ $c->request->{_body}->add($chunk);
+}
+
=item $self->prepare_body_parameters($c)
=cut
@@ -320,7 +384,19 @@ sub prepare_path { }
=cut
-sub prepare_query_parameters { }
+sub prepare_query_parameters {
+ my ( $self, $c, $query_string ) = @_;
+
+ # replace semi-colons
+ $query_string =~ s/;/&/g;
+
+ my $u = URI->new( '', 'http' );
+ $u->query($query_string);
+ for my $key ( $u->query_param ) {
+ my @vals = $u->query_param($key);
+ $c->request->query_parameters->{$key} = @vals > 1 ? [@vals] : $vals[0];
+ }
+}
=item $self->prepare_read($c)
@@ -328,9 +404,9 @@ sub prepare_query_parameters { }
sub prepare_read {
my ( $self, $c ) = @_;
-
+
# Reset the read position
- $self->read_position( 0 );
+ $self->read_position(0);
}
=item $self->prepare_request(@arguments)
@@ -360,6 +436,11 @@ sub prepare_uploads {
push @uploads, $u;
}
$c->request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0];
+
+ # support access to the filename as a normal param
+ my @filenames = map { $_->{filename} } @uploads;
+ $c->request->parameters->{$name} =
+ @filenames > 1 ? \@filenames : $filenames[0];
}
}
@@ -375,18 +456,18 @@ sub prepare_write { }
sub read {
my ( $self, $c, $maxlength ) = @_;
-
+
unless ( $self->{_prepared_read} ) {
- $self->prepare_read( $c );
+ $self->prepare_read($c);
$self->{_prepared_read} = 1;
}
-
+
my $remaining = $self->read_length - $self->read_position;
- $maxlength ||= $self->read_length;
-
+ $maxlength ||= $CHUNKSIZE;
+
# Are we done reading?
if ( $remaining <= 0 ) {
- $self->finalize_read( $c );
+ $self->finalize_read($c);
return;
}
@@ -397,9 +478,8 @@ sub read {
return $buffer;
}
else {
- Catalyst::Exception->throw(
- message => "Unknown error reading input: $!"
- );
+ Catalyst::Exception->throw(
+ message => "Unknown error reading input: $!" );
}
}
@@ -433,15 +513,13 @@ sub run { }
sub write {
my ( $self, $c, $buffer ) = @_;
-
+
unless ( $self->{_prepared_write} ) {
- $self->prepare_write( $c );
+ $self->prepare_write($c);
$self->{_prepared_write} = 1;
}
-
- my $handle = $c->response->handle;
-
- print $handle $buffer;
+
+ print STDOUT $buffer;
}
=back