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