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