Put prepare_connection back as Engine::PSGI uses it
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
CommitLineData
fc7ec1d9 1package Catalyst::Response;
2
059c085b 3use Moose;
6680c772 4use HTTP::Headers;
faa02805 5use Moose::Util::TypeConstraints;
6use namespace::autoclean;
fc7ec1d9 7
531f1ab6 8with 'MooseX::Emulate::Class::Accessor::Fast';
9
faa02805 10has _response_cb => (
11 is => 'ro',
12 isa => 'CodeRef',
13 writer => '_set_response_cb',
14 clearer => '_clear_response_cb',
15 predicate => '_has_response_cb',
16);
17
18subtype 'Catalyst::Engine::Types::Writer',
19 as duck_type([qw(write close)]);
20
21has _writer => (
22 is => 'ro',
23 isa => 'Catalyst::Engine::Types::Writer',
24 writer => '_set_writer',
25 clearer => '_clear_writer',
26 predicate => '_has_writer',
27);
28
29sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer }
30
6680c772 31has cookies => (is => 'rw', default => sub { {} });
ffb43803 32has body => (is => 'rw', default => undef);
33sub has_body { defined($_[0]->body) }
99a543ee 34
059c085b 35has location => (is => 'rw');
6680c772 36has status => (is => 'rw', default => 200);
37has finalized_headers => (is => 'rw', default => 0);
059c085b 38has headers => (
39 is => 'rw',
9c331634 40 isa => 'HTTP::Headers',
059c085b 41 handles => [qw(content_encoding content_length content_type header)],
6680c772 42 default => sub { HTTP::Headers->new() },
43 required => 1,
44 lazy => 1,
059c085b 45);
059c085b 46has _context => (
47 is => 'rw',
48 weak_ref => 1,
0fc2d522 49 handles => ['write'],
02570318 50 clearer => '_clear_context',
059c085b 51);
fc7ec1d9 52
059c085b 53sub output { shift->body(@_) }
54
aa9e8261 55sub code { shift->status(@_) }
56
fc7ec1d9 57=head1 NAME
58
910410b8 59Catalyst::Response - stores output responding to the current client request
fc7ec1d9 60
61=head1 SYNOPSIS
62
fbcc39ad 63 $res = $c->response;
64 $res->body;
aa9e8261 65 $res->code;
fbcc39ad 66 $res->content_encoding;
67 $res->content_length;
68 $res->content_type;
69 $res->cookies;
fbcc39ad 70 $res->header;
71 $res->headers;
72 $res->output;
73 $res->redirect;
74 $res->status;
75 $res->write;
b22c6668 76
fc7ec1d9 77=head1 DESCRIPTION
78
910410b8 79This is the Catalyst Response class, which provides methods for responding to
46372e65 80the current client request. The appropriate L<Catalyst::Engine> for your environment
81will turn the Catalyst::Response into a HTTP Response and return it to the client.
b22c6668 82
83=head1 METHODS
fc7ec1d9 84
08a2c908 85=head2 $res->body( $text | $fh | $iohandle_object )
e060fe05 86
87 $c->response->body('Catalyst rocks!');
06e1b616 88
46372e65 89Sets or returns the output (text or binary data). If you are returning a large body,
2f381252 90you might want to use a L<IO::Handle> type of object (Something that implements the read method
46372e65 91in the same fashion), or a filehandle GLOB. Catalyst
92will write it piece by piece into the response.
06e1b616 93
02570318 94=head2 $res->has_body
95
96Predicate which returns true when a body has been set.
97
aa9e8261 98=head2 $res->code
99
100Alias for $res->status.
101
b5ecfcf0 102=head2 $res->content_encoding
b5176d9e 103
910410b8 104Shortcut for $res->headers->content_encoding.
b5176d9e 105
b5ecfcf0 106=head2 $res->content_length
b5176d9e 107
910410b8 108Shortcut for $res->headers->content_length.
b5176d9e 109
b5ecfcf0 110=head2 $res->content_type
b5176d9e 111
910410b8 112Shortcut for $res->headers->content_type.
b5176d9e 113
87e9f9ab 114This value is typically set by your view or plugin. For example,
115L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
116it found, while L<Catalyst::View::TT> defaults to C<text/html>.
117
b5ecfcf0 118=head2 $res->cookies
fc7ec1d9 119
910410b8 120Returns a reference to a hash containing cookies to be set. The keys of the
121hash are the cookies' names, and their corresponding values are hash
7e743798 122references used to construct a L<CGI::Simple::Cookie> object.
fc7ec1d9 123
124 $c->response->cookies->{foo} = { value => '123' };
125
7e743798 126The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
910410b8 127parameters of the same name, except they are used without a leading dash.
128Possible parameters are:
ac965e92 129
b0ad47c1 130=over
ac965e92 131
71453caf 132=item value
ac965e92 133
71453caf 134=item expires
ac965e92 135
71453caf 136=item domain
ac965e92 137
71453caf 138=item path
139
140=item secure
141
b21bc468 142=item httponly
143
71453caf 144=back
ac965e92 145
b5ecfcf0 146=head2 $res->header
fbcc39ad 147
910410b8 148Shortcut for $res->headers->header.
fbcc39ad 149
b5ecfcf0 150=head2 $res->headers
fc7ec1d9 151
910410b8 152Returns an L<HTTP::Headers> object, which can be used to set headers.
fc7ec1d9 153
154 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
155
b5ecfcf0 156=head2 $res->output
fc7ec1d9 157
910410b8 158Alias for $res->body.
fc7ec1d9 159
b5ecfcf0 160=head2 $res->redirect( $url, $status )
fc7ec1d9 161
2f381252 162Causes the response to redirect to the specified URL. The default status is
163C<302>.
fc7ec1d9 164
73a52566 165 $c->response->redirect( 'http://slashdot.org' );
166 $c->response->redirect( 'http://slashdot.org', 307 );
167
2f381252 168This is a convenience method that sets the Location header to the
169redirect destination, and then sets the response status. You will
ee24f3a8 170want to C< return > or C<< $c->detach() >> to interrupt the normal
2f381252 171processing flow if you want the redirect to occur straight away.
172
824a5eb0 173B<Note:> do not give a relative URL as $url, i.e: one that is not fully
174qualified (= C<http://...>, etc.) or that starts with a slash
175(= C</path/here>). While it may work, it is not guaranteed to do the right
176thing and is not a standard behaviour. You may opt to use uri_for() or
177uri_for_action() instead.
178
73a52566 179=cut
180
181sub redirect {
182 my $self = shift;
fbcc39ad 183
184 if (@_) {
73a52566 185 my $location = shift;
f1bbebac 186 my $status = shift || 302;
73a52566 187
188 $self->location($location);
189 $self->status($status);
190 }
191
192 return $self->location;
193}
fc7ec1d9 194
059c085b 195=head2 $res->location
196
197Sets or returns the HTTP 'Location'.
198
b5ecfcf0 199=head2 $res->status
fc7ec1d9 200
910410b8 201Sets or returns the HTTP status.
fc7ec1d9 202
203 $c->response->status(404);
aa9e8261 204
205$res->code is an alias for this, to match HTTP::Response->code.
b0ad47c1 206
b5ecfcf0 207=head2 $res->write( $data )
fbcc39ad 208
209Writes $data to the output stream.
210
e4cc83b2 211=head2 $res->print( @data )
212
213Prints @data to the output stream, separated by $,. This lets you pass
214the response object to functions that want to write to an L<IO::Handle>.
215
faa02805 216=head2 DEMOLISH
217
218Ensures that the response is flushed and closed at the end of the
219request.
220
221=head2 meta
222
223Provided by Moose
224
e4cc83b2 225=cut
226
227sub print {
228 my $self = shift;
229 my $data = shift;
230
231 defined $self->write($data) or return;
232
233 for (@_) {
234 defined $self->write($,) or return;
235 defined $self->write($_) or return;
236 }
fe3083a8 237 defined $self->write($\) or return;
b0ad47c1 238
e4cc83b2 239 return 1;
240}
241
910410b8 242=head1 AUTHORS
fc7ec1d9 243
2f381252 244Catalyst Contributors, see Catalyst.pm
fc7ec1d9 245
246=head1 COPYRIGHT
247
b0ad47c1 248This library is free software. You can redistribute it and/or modify
61b1e958 249it under the same terms as Perl itself.
fc7ec1d9 250
251=cut
252
e5ecd5bc 253__PACKAGE__->meta->make_immutable;
254
fc7ec1d9 2551;