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