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