From: John Napiorkowski Date: Mon, 13 May 2013 15:17:11 +0000 (-0400) Subject: Merge branch 'master' of git.shadowcat.co.uk:Catalyst-Runtime into SicilianButtercup X-Git-Tag: 5.90040~3^2~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=87106f31bcbbacc350449a6fbe5d6de50b876057;hp=71dd05e848d417f454f18fa143e7272411e9cde9;p=catagits%2FCatalyst-Runtime.git Merge branch 'master' of git.shadowcat.co.uk:Catalyst-Runtime into SicilianButtercup --- diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 2367139..b8d29a4 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -54,12 +54,20 @@ See L. =head2 $self->finalize_body($c) -Finalize body. Prints the response output. +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 L<\write_fh>. + +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' ) { diff --git a/lib/Catalyst/Response.pm b/lib/Catalyst/Response.pm index 6dc661e..8559f36 100644 --- a/lib/Catalyst/Response.pm +++ b/lib/Catalyst/Response.pm @@ -26,7 +26,25 @@ has _writer => ( predicate => '_has_writer', ); -sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer } +has write_fh => ( + is=>'ro', + predicate=>'has_write_fh', + lazy_build=>1); + + sub _build_write_fh { + my $self = shift; + $self->_context->finalize_headers unless + $self->finalized_headers; + $self->_writer; + }; + +sub DEMOLISH { + my $self = shift; + return if $self->has_write_fh; + if($self->_has_writer) { + $self->_writer->close + } +} has cookies => (is => 'rw', default => sub { {} }); has body => (is => 'rw', default => undef); @@ -246,6 +264,40 @@ $res->code is an alias for this, to match HTTP::Response->code. Writes $data to the output stream. +=head2 $res->write_fh + +Returns a PSGI $writer object that has two methods, write and close. You can +close over this object for asynchronous and nonblocking applications. For +example (assuming you are using a supporting server, like L + + package AsyncExample::Controller::Root; + + use Moose; + + BEGIN { extends 'Catalyst::Controller' } + + sub prepare_cb { + my $write_fh = pop; + return sub { + my $message = shift; + $write_fh->write("Finishing: $message\n"); + $write_fh->close; + }; + } + + sub anyevent :Local :Args(0) { + my ($self, $c) = @_; + my $cb = $self->prepare_cb($c->res->write_fh); + + my $watcher; + $watcher = AnyEvent->timer( + after => 5, + cb => sub { + $cb->(scalar localtime); + undef $watcher; # cancel circular-ref + }); + } + =head2 $res->print( @data ) Prints @data to the output stream, separated by $,. This lets you pass diff --git a/lib/Catalyst/Upgrading.pod b/lib/Catalyst/Upgrading.pod index b6d3c8b..d1dd519 100644 --- a/lib/Catalyst/Upgrading.pod +++ b/lib/Catalyst/Upgrading.pod @@ -2,6 +2,16 @@ Catalyst::Upgrading - Instructions for upgrading to the latest Catalyst +=head1 Upgrading to Catalyst 5.90040 + +This version of L offers some support for using L and +L event loops in your application. These changes should work +fine for most applications however if you are already trying to perform +some streaming, minor changes in this area of the code might affect your +functionality. + + TDB: more on streaming, transfer encoding chunked, etc. + =head1 Upgrading to Catalyst 5.9 The major change is that L, a toolkit for using the L