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',
33 builder=>'_build_write_fh');
37 $self->_context->finalize_headers unless
38 $self->finalized_headers;
44 return if $self->has_write_fh;
45 if($self->_has_writer) {
50 has cookies => (is => 'rw', default => sub { {} });
51 has body => (is => 'rw', default => undef);
52 sub has_body { defined($_[0]->body) }
54 has location => (is => 'rw');
55 has status => (is => 'rw', default => 200);
56 has finalized_headers => (is => 'rw', default => 0);
59 isa => 'HTTP::Headers',
60 handles => [qw(content_encoding content_length content_type header)],
61 default => sub { HTTP::Headers->new() },
68 clearer => '_clear_context',
71 sub output { shift->body(@_) }
73 sub code { shift->status(@_) }
76 my ( $self, $buffer ) = @_;
78 # Finalize headers if someone manually writes output
79 $self->_context->finalize_headers unless $self->finalized_headers;
81 $buffer = q[] unless defined $buffer;
83 my $len = length($buffer);
84 $self->_writer->write($buffer);
89 sub finalize_headers {
92 # This is a less-than-pretty hack to avoid breaking the old
93 # Catalyst::Engine::PSGI. 5.9 Catalyst::Engine sets a response_cb and
94 # expects us to pass headers to it here, whereas Catalyst::Enngine::PSGI
95 # just pulls the headers out of $ctx->response in its run method and never
96 # sets response_cb. So take the lack of a response_cb as a sign that we
97 # don't need to set the headers.
99 return unless $self->_has_response_cb;
101 # If we already have a writer, we already did this, so don't do it again
102 return if $self->_has_writer;
105 $self->headers->scan(sub { push @headers, @_ });
107 my $writer = $self->_response_cb->([ $self->status, \@headers ]);
108 $self->_set_writer($writer);
109 $self->_clear_response_cb;
116 Catalyst::Response - stores output responding to the current client request
123 $res->content_encoding;
124 $res->content_length;
136 This is the Catalyst Response class, which provides methods for responding to
137 the current client request. The appropriate L<Catalyst::Engine> for your environment
138 will turn the Catalyst::Response into a HTTP Response and return it to the client.
142 =head2 $res->body( $text | $fh | $iohandle_object )
144 $c->response->body('Catalyst rocks!');
146 Sets or returns the output (text or binary data). If you are returning a large body,
147 you might want to use a L<IO::Handle> type of object (Something that implements the read method
148 in the same fashion), or a filehandle GLOB. Catalyst
149 will write it piece by piece into the response.
151 =head2 $res->has_body
153 Predicate which returns true when a body has been set.
157 Alias for $res->status.
159 =head2 $res->content_encoding
161 Shortcut for $res->headers->content_encoding.
163 =head2 $res->content_length
165 Shortcut for $res->headers->content_length.
167 =head2 $res->content_type
169 Shortcut for $res->headers->content_type.
171 This value is typically set by your view or plugin. For example,
172 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
173 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
177 Returns a reference to a hash containing cookies to be set. The keys of the
178 hash are the cookies' names, and their corresponding values are hash
179 references used to construct a L<CGI::Simple::Cookie> object.
181 $c->response->cookies->{foo} = { value => '123' };
183 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
184 parameters of the same name, except they are used without a leading dash.
185 Possible parameters are:
205 Shortcut for $res->headers->header.
209 Returns an L<HTTP::Headers> object, which can be used to set headers.
211 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
215 Alias for $res->body.
217 =head2 $res->redirect( $url, $status )
219 Causes the response to redirect to the specified URL. The default status is
222 $c->response->redirect( 'http://slashdot.org' );
223 $c->response->redirect( 'http://slashdot.org', 307 );
225 This is a convenience method that sets the Location header to the
226 redirect destination, and then sets the response status. You will
227 want to C< return > or C<< $c->detach() >> to interrupt the normal
228 processing flow if you want the redirect to occur straight away.
230 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
231 qualified (= C<http://...>, etc.) or that starts with a slash
232 (= C</path/here>). While it may work, it is not guaranteed to do the right
233 thing and is not a standard behaviour. You may opt to use uri_for() or
234 uri_for_action() instead.
242 my $location = shift;
243 my $status = shift || 302;
245 $self->location($location);
246 $self->status($status);
249 return $self->location;
252 =head2 $res->location
254 Sets or returns the HTTP 'Location'.
258 Sets or returns the HTTP status.
260 $c->response->status(404);
262 $res->code is an alias for this, to match HTTP::Response->code.
264 =head2 $res->write( $data )
266 Writes $data to the output stream.
268 =head2 $res->write_fh
270 Returns a PSGI $writer object that has two methods, write and close. You can
271 close over this object for asynchronous and nonblocking applications. For
272 example (assuming you are using a supporting server, like L<Twiggy>
274 package AsyncExample::Controller::Root;
278 BEGIN { extends 'Catalyst::Controller' }
284 $write_fh->write("Finishing: $message\n");
289 sub anyevent :Local :Args(0) {
291 my $cb = $self->prepare_cb($c->res->write_fh);
294 $watcher = AnyEvent->timer(
297 $cb->(scalar localtime);
298 undef $watcher; # cancel circular-ref
302 =head2 $res->print( @data )
304 Prints @data to the output stream, separated by $,. This lets you pass
305 the response object to functions that want to write to an L<IO::Handle>.
307 =head2 $self->finalize_headers($c)
309 Writes headers to response if not already written
313 Ensures that the response is flushed and closed at the end of the
326 defined $self->write($data) or return;
329 defined $self->write($,) or return;
330 defined $self->write($_) or return;
332 defined $self->write($\) or return;
339 Catalyst Contributors, see Catalyst.pm
343 This library is free software. You can redistribute it and/or modify
344 it under the same terms as Perl itself.
348 __PACKAGE__->meta->make_immutable;