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