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=e2f77a2f9375a659e2679b65e444a65efc7dc4f1;hp=3b4ceb0c5c3564d827dd8f2ef1a411390cb4f238;hb=46fff667a1229a59422010500311e48a6f9da824;hpb=88ba7793bb132b13ecea722fcc56313756a408b9 diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 3b4ceb0..e2f77a2 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -59,35 +59,80 @@ See L. Finalize body. Prints the response output as blocking stream if it looks like a filehandle, otherwise write it out all in one go. If there is no body in the response, we assume you are handling it 'manually', such as for nonblocking -style or asynchronous streaming responses. You do this by calling L<\write> -several times (which sends HTTP headers if needed) or you close over C<$response->write_fh>. +style or asynchronous streaming responses. You do this by calling L +several times (which sends HTTP headers if needed) or you close over +C<< $response->write_fh >>. -See L and L for more. +See L and L for more. =cut sub finalize_body { my ( $self, $c ) = @_; - return if $c->response->has_write_fh; - - my $body = $c->response->body; - 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 ); - } + my $res = $c->response; # We use this all over + + ## If we've asked for the write 'filehandle' that means the application is + ## doing something custom and is expected to close the response + return if $res->_has_write_fh; + + if($res->_has_response_cb) { + ## we have not called the response callback yet, so we are safe to send + ## the whole body to PSGI + + my @headers; + $res->headers->scan(sub { push @headers, @_ }); + + ## We need to figure out what kind of body we have... + my $body = $res->body; + if(defined $body) { + if(blessed($body) && $body->can('read') or ref($body) eq 'GLOB') { + # Body is a filehandle like thingy. We can jusrt send this along + # to plack without changing it. + } else { + # Looks like for backcompat reasons we need to be able to deal + # with stringyfiable objects. + $body = "$body" if blessed($body); # Assume there's some sort of overloading.. + $body = [$body]; + } + } else { + $body = [undef]; + } + + $res->_response_cb->([ $res->status, \@headers, $body]); + $res->_clear_response_cb; + + } else { + ## Now, if there's no response callback anymore, that means someone has + ## called ->write in order to stream 'some stuff along the way'. I think + ## for backcompat we still need to handle a ->body. I guess I could see + ## someone calling ->write to presend some stuff, and then doing the rest + ## via ->body, like in a template. + + ## We'll just use the old, existing code for this (or most of it) + + if(my $body = $res->body) { + no warnings 'uninitialized'; + if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { + + ## In this case we have no choice and will fall back on the old + ## manual streaming stuff. + + 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 ); + } + } - my $res = $c->response; - $res->_writer->close; - $res->_clear_writer; + $res->_writer->close; + $res->_clear_writer; + } return; } @@ -507,10 +552,6 @@ sub prepare_query_parameters { # (yes, index() is faster than a regex :)) if ( index( $query_string, '=' ) < 0 ) { $c->request->query_keywords($self->unescape_uri($query_string)); - $env->{'plack.request.query'} ||= Hash::MultiValue->new( - map { (URI::Escape::uri_unescape($_), '') } - split(/\+/, $query_string, -1)); - return; } @@ -542,10 +583,9 @@ sub prepare_query_parameters { } } - $env->{'plack.request.query'} ||= Hash::MultiValue->from_mixed(\%query); $c->request->query_parameters( $c->request->_use_hash_multivalue ? - $env->{'plack.request.query'}->clone : + Hash::MultiValue->from_mixed(\%query) : \%query); } @@ -588,7 +628,6 @@ sub prepare_uploads { my $uploads = $request->_body->upload; my $parameters = $request->parameters; - my @plack_uploads; foreach my $name (keys %$uploads) { my $files = $uploads->{$name}; my @uploads; @@ -603,14 +642,9 @@ sub prepare_uploads { filename => $upload->{filename}, ); push @uploads, $u; - - # Plack compatibility. - my %copy = (%$upload, headers=>$headers); - push @plack_uploads, $name, Plack::Request::Upload->new(%copy); } $request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0]; - # support access to the filename as a normal param my @filenames = map { $_->{filename} } @uploads; # append, if there's already params with this name @@ -626,8 +660,6 @@ sub prepare_uploads { $parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0]; } } - - $self->env->{'plack.request.upload'} ||= Hash::MultiValue->new(@plack_uploads); } =head2 $self->write($c, $buffer)