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