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