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