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