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