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