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