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