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