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