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