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