1 package Catalyst::Response;
5 use Moose::Util::TypeConstraints;
6 use namespace::autoclean;
8 with 'MooseX::Emulate::Class::Accessor::Fast';
13 writer => '_set_response_cb',
14 clearer => '_clear_response_cb',
15 predicate => '_has_response_cb',
18 subtype 'Catalyst::Engine::Types::Writer',
19 as duck_type([qw(write close)]);
23 isa => 'Catalyst::Engine::Types::Writer',
24 writer => '_set_writer',
25 clearer => '_clear_writer',
26 predicate => '_has_writer',
31 predicate=>'has_write_fh',
36 $self->_context->finalize_headers unless
37 $self->finalized_headers;
43 return if $self->has_write_fh;
44 if($self->_has_writer) {
49 has cookies => (is => 'rw', default => sub { {} });
50 has body => (is => 'rw', default => undef);
51 sub has_body { defined($_[0]->body) }
53 has location => (is => 'rw');
54 has status => (is => 'rw', default => 200);
55 has finalized_headers => (is => 'rw', default => 0);
58 isa => 'HTTP::Headers',
59 handles => [qw(content_encoding content_length content_type header)],
60 default => sub { HTTP::Headers->new() },
67 clearer => '_clear_context',
70 sub output { shift->body(@_) }
72 sub code { shift->status(@_) }
75 my ( $self, $buffer ) = @_;
77 # Finalize headers if someone manually writes output
78 $self->_context->finalize_headers unless $self->finalized_headers;
80 $buffer = q[] unless defined $buffer;
82 my $len = length($buffer);
83 $self->_writer->write($buffer);
88 sub finalize_headers {
91 # This is a less-than-pretty hack to avoid breaking the old
92 # Catalyst::Engine::PSGI. 5.9 Catalyst::Engine sets a response_cb and
93 # expects us to pass headers to it here, whereas Catalyst::Enngine::PSGI
94 # just pulls the headers out of $ctx->response in its run method and never
95 # sets response_cb. So take the lack of a response_cb as a sign that we
96 # don't need to set the headers.
98 return unless $self->_has_response_cb;
100 # If we already have a writer, we already did this, so don't do it again
101 return if $self->_has_writer;
104 $self->headers->scan(sub { push @headers, @_ });
106 my $writer = $self->_response_cb->([ $self->status, \@headers ]);
107 $self->_set_writer($writer);
108 $self->_clear_response_cb;
115 Catalyst::Response - stores output responding to the current client request
122 $res->content_encoding;
123 $res->content_length;
135 This is the Catalyst Response class, which provides methods for responding to
136 the current client request. The appropriate L<Catalyst::Engine> for your environment
137 will turn the Catalyst::Response into a HTTP Response and return it to the client.
141 =head2 $res->body( $text | $fh | $iohandle_object )
143 $c->response->body('Catalyst rocks!');
145 Sets or returns the output (text or binary data). If you are returning a large body,
146 you might want to use a L<IO::Handle> type of object (Something that implements the read method
147 in the same fashion), or a filehandle GLOB. Catalyst
148 will write it piece by piece into the response.
150 =head2 $res->has_body
152 Predicate which returns true when a body has been set.
156 Alias for $res->status.
158 =head2 $res->content_encoding
160 Shortcut for $res->headers->content_encoding.
162 =head2 $res->content_length
164 Shortcut for $res->headers->content_length.
166 =head2 $res->content_type
168 Shortcut for $res->headers->content_type.
170 This value is typically set by your view or plugin. For example,
171 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
172 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
176 Returns a reference to a hash containing cookies to be set. The keys of the
177 hash are the cookies' names, and their corresponding values are hash
178 references used to construct a L<CGI::Simple::Cookie> object.
180 $c->response->cookies->{foo} = { value => '123' };
182 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
183 parameters of the same name, except they are used without a leading dash.
184 Possible parameters are:
204 Shortcut for $res->headers->header.
208 Returns an L<HTTP::Headers> object, which can be used to set headers.
210 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
214 Alias for $res->body.
216 =head2 $res->redirect( $url, $status )
218 Causes the response to redirect to the specified URL. The default status is
221 $c->response->redirect( 'http://slashdot.org' );
222 $c->response->redirect( 'http://slashdot.org', 307 );
224 This is a convenience method that sets the Location header to the
225 redirect destination, and then sets the response status. You will
226 want to C< return > or C<< $c->detach() >> to interrupt the normal
227 processing flow if you want the redirect to occur straight away.
229 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
230 qualified (= C<http://...>, etc.) or that starts with a slash
231 (= C</path/here>). While it may work, it is not guaranteed to do the right
232 thing and is not a standard behaviour. You may opt to use uri_for() or
233 uri_for_action() instead.
241 my $location = shift;
242 my $status = shift || 302;
244 $self->location($location);
245 $self->status($status);
248 return $self->location;
251 =head2 $res->location
253 Sets or returns the HTTP 'Location'.
257 Sets or returns the HTTP status.
259 $c->response->status(404);
261 $res->code is an alias for this, to match HTTP::Response->code.
263 =head2 $res->write( $data )
265 Writes $data to the output stream.
267 =head2 $res->write_fh
269 Returns a PSGI $writer object that has two methods, write and close. You can
270 close over this object for asynchronous and nonblocking applications. For
271 example (assuming you are using a supporting server, like L<Twiggy>
273 package AsyncExample::Controller::Root;
277 BEGIN { extends 'Catalyst::Controller' }
283 $write_fh->write("Finishing: $message\n");
288 sub anyevent :Local :Args(0) {
290 my $cb = $self->prepare_cb($c->res->write_fh);
293 $watcher = AnyEvent->timer(
296 $cb->(scalar localtime);
297 undef $watcher; # cancel circular-ref
301 =head2 $res->print( @data )
303 Prints @data to the output stream, separated by $,. This lets you pass
304 the response object to functions that want to write to an L<IO::Handle>.
306 =head2 $self->finalize_headers($c)
308 Writes headers to response if not already written
312 Ensures that the response is flushed and closed at the end of the
325 defined $self->write($data) or return;
328 defined $self->write($,) or return;
329 defined $self->write($_) or return;
331 defined $self->write($\) or return;
338 Catalyst Contributors, see Catalyst.pm
342 This library is free software. You can redistribute it and/or modify
343 it under the same terms as Perl itself.
347 __PACKAGE__->meta->make_immutable;