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