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