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