basic support for delayed writes/async with docs
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
CommitLineData
fc7ec1d9 1package Catalyst::Response;
2
059c085b 3use Moose;
6680c772 4use HTTP::Headers;
faa02805 5use Moose::Util::TypeConstraints;
6use namespace::autoclean;
fc7ec1d9 7
531f1ab6 8with 'MooseX::Emulate::Class::Accessor::Fast';
9
faa02805 10has _response_cb => (
11 is => 'ro',
12 isa => 'CodeRef',
13 writer => '_set_response_cb',
14 clearer => '_clear_response_cb',
15 predicate => '_has_response_cb',
16);
17
18subtype 'Catalyst::Engine::Types::Writer',
19 as duck_type([qw(write close)]);
20
21has _writer => (
22 is => 'ro',
23 isa => 'Catalyst::Engine::Types::Writer',
24 writer => '_set_writer',
25 clearer => '_clear_writer',
26 predicate => '_has_writer',
27);
28
29sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer }
30
6680c772 31has cookies => (is => 'rw', default => sub { {} });
ffb43803 32has body => (is => 'rw', default => undef);
33sub has_body { defined($_[0]->body) }
99a543ee 34
059c085b 35has location => (is => 'rw');
6680c772 36has status => (is => 'rw', default => 200);
37has finalized_headers => (is => 'rw', default => 0);
059c085b 38has headers => (
39 is => 'rw',
9c331634 40 isa => 'HTTP::Headers',
059c085b 41 handles => [qw(content_encoding content_length content_type header)],
6680c772 42 default => sub { HTTP::Headers->new() },
43 required => 1,
44 lazy => 1,
059c085b 45);
258733f1 46has _context => (
47 is => 'rw',
48 weak_ref => 1,
49 clearer => '_clear_context',
50);
fc7ec1d9 51
059c085b 52sub output { shift->body(@_) }
53
aa9e8261 54sub code { shift->status(@_) }
55
9c4288ea 56sub write {
57 my ( $self, $buffer ) = @_;
58
59 # Finalize headers if someone manually writes output
89ba65d5 60 $self->_context->finalize_headers unless $self->finalized_headers;
9c4288ea 61
62 $buffer = q[] unless defined $buffer;
63
64 my $len = length($buffer);
65 $self->_writer->write($buffer);
66
67 return $len;
68}
69
9c4288ea 70sub finalize_headers {
71 my ($self) = @_;
72
73 # This is a less-than-pretty hack to avoid breaking the old
74 # Catalyst::Engine::PSGI. 5.9 Catalyst::Engine sets a response_cb and
75 # expects us to pass headers to it here, whereas Catalyst::Enngine::PSGI
76 # just pulls the headers out of $ctx->response in its run method and never
77 # sets response_cb. So take the lack of a response_cb as a sign that we
78 # don't need to set the headers.
79
80 return unless $self->_has_response_cb;
81
82 # If we already have a writer, we already did this, so don't do it again
83 return if $self->_has_writer;
84
85 my @headers;
86 $self->headers->scan(sub { push @headers, @_ });
87
88 my $writer = $self->_response_cb->([ $self->status, \@headers ]);
89 $self->_set_writer($writer);
90 $self->_clear_response_cb;
91
92 return;
93}
94
fc7ec1d9 95=head1 NAME
96
910410b8 97Catalyst::Response - stores output responding to the current client request
fc7ec1d9 98
99=head1 SYNOPSIS
100
fbcc39ad 101 $res = $c->response;
102 $res->body;
aa9e8261 103 $res->code;
fbcc39ad 104 $res->content_encoding;
105 $res->content_length;
106 $res->content_type;
107 $res->cookies;
fbcc39ad 108 $res->header;
109 $res->headers;
110 $res->output;
111 $res->redirect;
112 $res->status;
113 $res->write;
b22c6668 114
fc7ec1d9 115=head1 DESCRIPTION
116
910410b8 117This is the Catalyst Response class, which provides methods for responding to
46372e65 118the current client request. The appropriate L<Catalyst::Engine> for your environment
119will turn the Catalyst::Response into a HTTP Response and return it to the client.
b22c6668 120
121=head1 METHODS
fc7ec1d9 122
08a2c908 123=head2 $res->body( $text | $fh | $iohandle_object )
e060fe05 124
125 $c->response->body('Catalyst rocks!');
06e1b616 126
46372e65 127Sets or returns the output (text or binary data). If you are returning a large body,
2f381252 128you might want to use a L<IO::Handle> type of object (Something that implements the read method
46372e65 129in the same fashion), or a filehandle GLOB. Catalyst
130will write it piece by piece into the response.
06e1b616 131
02570318 132=head2 $res->has_body
133
134Predicate which returns true when a body has been set.
135
aa9e8261 136=head2 $res->code
137
138Alias for $res->status.
139
b5ecfcf0 140=head2 $res->content_encoding
b5176d9e 141
910410b8 142Shortcut for $res->headers->content_encoding.
b5176d9e 143
b5ecfcf0 144=head2 $res->content_length
b5176d9e 145
910410b8 146Shortcut for $res->headers->content_length.
b5176d9e 147
b5ecfcf0 148=head2 $res->content_type
b5176d9e 149
910410b8 150Shortcut for $res->headers->content_type.
b5176d9e 151
87e9f9ab 152This value is typically set by your view or plugin. For example,
153L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
154it found, while L<Catalyst::View::TT> defaults to C<text/html>.
155
b5ecfcf0 156=head2 $res->cookies
fc7ec1d9 157
910410b8 158Returns a reference to a hash containing cookies to be set. The keys of the
159hash are the cookies' names, and their corresponding values are hash
7e743798 160references used to construct a L<CGI::Simple::Cookie> object.
fc7ec1d9 161
162 $c->response->cookies->{foo} = { value => '123' };
163
7e743798 164The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
910410b8 165parameters of the same name, except they are used without a leading dash.
166Possible parameters are:
ac965e92 167
b0ad47c1 168=over
ac965e92 169
71453caf 170=item value
ac965e92 171
71453caf 172=item expires
ac965e92 173
71453caf 174=item domain
ac965e92 175
71453caf 176=item path
177
178=item secure
179
b21bc468 180=item httponly
181
71453caf 182=back
ac965e92 183
b5ecfcf0 184=head2 $res->header
fbcc39ad 185
910410b8 186Shortcut for $res->headers->header.
fbcc39ad 187
b5ecfcf0 188=head2 $res->headers
fc7ec1d9 189
910410b8 190Returns an L<HTTP::Headers> object, which can be used to set headers.
fc7ec1d9 191
192 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
193
b5ecfcf0 194=head2 $res->output
fc7ec1d9 195
910410b8 196Alias for $res->body.
fc7ec1d9 197
b5ecfcf0 198=head2 $res->redirect( $url, $status )
fc7ec1d9 199
2f381252 200Causes the response to redirect to the specified URL. The default status is
201C<302>.
fc7ec1d9 202
73a52566 203 $c->response->redirect( 'http://slashdot.org' );
204 $c->response->redirect( 'http://slashdot.org', 307 );
205
2f381252 206This is a convenience method that sets the Location header to the
207redirect destination, and then sets the response status. You will
ee24f3a8 208want to C< return > or C<< $c->detach() >> to interrupt the normal
2f381252 209processing flow if you want the redirect to occur straight away.
210
824a5eb0 211B<Note:> do not give a relative URL as $url, i.e: one that is not fully
212qualified (= C<http://...>, etc.) or that starts with a slash
213(= C</path/here>). While it may work, it is not guaranteed to do the right
214thing and is not a standard behaviour. You may opt to use uri_for() or
215uri_for_action() instead.
216
73a52566 217=cut
218
219sub redirect {
220 my $self = shift;
fbcc39ad 221
222 if (@_) {
73a52566 223 my $location = shift;
f1bbebac 224 my $status = shift || 302;
73a52566 225
226 $self->location($location);
227 $self->status($status);
228 }
229
230 return $self->location;
231}
fc7ec1d9 232
059c085b 233=head2 $res->location
234
235Sets or returns the HTTP 'Location'.
236
b5ecfcf0 237=head2 $res->status
fc7ec1d9 238
910410b8 239Sets or returns the HTTP status.
fc7ec1d9 240
241 $c->response->status(404);
aa9e8261 242
243$res->code is an alias for this, to match HTTP::Response->code.
b0ad47c1 244
b5ecfcf0 245=head2 $res->write( $data )
fbcc39ad 246
247Writes $data to the output stream.
248
e4cc83b2 249=head2 $res->print( @data )
250
251Prints @data to the output stream, separated by $,. This lets you pass
252the response object to functions that want to write to an L<IO::Handle>.
253
8738b8fe 254=head2 $self->finalize_headers($c)
255
256Writes headers to response if not already written
257
faa02805 258=head2 DEMOLISH
259
260Ensures that the response is flushed and closed at the end of the
261request.
262
263=head2 meta
264
265Provided by Moose
266
e4cc83b2 267=cut
268
269sub print {
270 my $self = shift;
271 my $data = shift;
272
273 defined $self->write($data) or return;
274
275 for (@_) {
276 defined $self->write($,) or return;
277 defined $self->write($_) or return;
278 }
fe3083a8 279 defined $self->write($\) or return;
b0ad47c1 280
e4cc83b2 281 return 1;
282}
283
910410b8 284=head1 AUTHORS
fc7ec1d9 285
2f381252 286Catalyst Contributors, see Catalyst.pm
fc7ec1d9 287
288=head1 COPYRIGHT
289
b0ad47c1 290This library is free software. You can redistribute it and/or modify
61b1e958 291it under the same terms as Perl itself.
fc7ec1d9 292
293=cut
294
e5ecd5bc 295__PACKAGE__->meta->make_immutable;
296
fc7ec1d9 2971;