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