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