=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<Catalyst::Response\write> and L<Catalyst::Response\write_fh> 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' ) {
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);
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<Twiggy>
+
+ 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
Catalyst::Upgrading - Instructions for upgrading to the latest Catalyst
+=head1 Upgrading to Catalyst 5.90040
+
+This version of L<Catalyst> offers some support for using L<AnyEvent> and
+L<IO::Async> 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<Plack>, a toolkit for using the L<PSGI>