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',
29 sub DEMOLISH { $_[0]->_writer->close if $_[0]->_has_writer }
31 has cookies => (is => 'rw', default => sub { {} });
32 has body => (is => 'rw', default => undef);
33 sub has_body { defined($_[0]->body) }
35 has location => (is => 'rw');
36 has status => (is => 'rw', default => 200);
37 has finalized_headers => (is => 'rw', default => 0);
40 isa => 'HTTP::Headers',
41 handles => [qw(content_encoding content_length content_type header)],
42 default => sub { HTTP::Headers->new() },
47 sub output { shift->body(@_) }
49 sub code { shift->status(@_) }
51 =head2 $self->write($buffer)
53 Writes the buffer to the client.
58 my ( $self, $buffer ) = @_;
60 # Finalize headers if someone manually writes output
61 $self->finalize_headers;
63 $buffer = q[] unless defined $buffer;
65 my $len = length($buffer);
66 $self->_writer->write($buffer);
71 =head2 $self->finalize_headers($c)
73 Abstract method, allows engines to write headers to response
77 sub finalize_headers {
80 # This is a less-than-pretty hack to avoid breaking the old
81 # Catalyst::Engine::PSGI. 5.9 Catalyst::Engine sets a response_cb and
82 # expects us to pass headers to it here, whereas Catalyst::Enngine::PSGI
83 # just pulls the headers out of $ctx->response in its run method and never
84 # sets response_cb. So take the lack of a response_cb as a sign that we
85 # don't need to set the headers.
87 return unless $self->_has_response_cb;
89 # If we already have a writer, we already did this, so don't do it again
90 return if $self->_has_writer;
93 $self->headers->scan(sub { push @headers, @_ });
95 my $writer = $self->_response_cb->([ $self->status, \@headers ]);
96 $self->_set_writer($writer);
97 $self->_clear_response_cb;
104 Catalyst::Response - stores output responding to the current client request
111 $res->content_encoding;
112 $res->content_length;
124 This is the Catalyst Response class, which provides methods for responding to
125 the current client request. The appropriate L<Catalyst::Engine> for your environment
126 will turn the Catalyst::Response into a HTTP Response and return it to the client.
130 =head2 $res->body( $text | $fh | $iohandle_object )
132 $c->response->body('Catalyst rocks!');
134 Sets or returns the output (text or binary data). If you are returning a large body,
135 you might want to use a L<IO::Handle> type of object (Something that implements the read method
136 in the same fashion), or a filehandle GLOB. Catalyst
137 will write it piece by piece into the response.
139 =head2 $res->has_body
141 Predicate which returns true when a body has been set.
145 Alias for $res->status.
147 =head2 $res->content_encoding
149 Shortcut for $res->headers->content_encoding.
151 =head2 $res->content_length
153 Shortcut for $res->headers->content_length.
155 =head2 $res->content_type
157 Shortcut for $res->headers->content_type.
159 This value is typically set by your view or plugin. For example,
160 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
161 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
165 Returns a reference to a hash containing cookies to be set. The keys of the
166 hash are the cookies' names, and their corresponding values are hash
167 references used to construct a L<CGI::Simple::Cookie> object.
169 $c->response->cookies->{foo} = { value => '123' };
171 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
172 parameters of the same name, except they are used without a leading dash.
173 Possible parameters are:
193 Shortcut for $res->headers->header.
197 Returns an L<HTTP::Headers> object, which can be used to set headers.
199 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
203 Alias for $res->body.
205 =head2 $res->redirect( $url, $status )
207 Causes the response to redirect to the specified URL. The default status is
210 $c->response->redirect( 'http://slashdot.org' );
211 $c->response->redirect( 'http://slashdot.org', 307 );
213 This is a convenience method that sets the Location header to the
214 redirect destination, and then sets the response status. You will
215 want to C< return > or C<< $c->detach() >> to interrupt the normal
216 processing flow if you want the redirect to occur straight away.
218 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
219 qualified (= C<http://...>, etc.) or that starts with a slash
220 (= C</path/here>). While it may work, it is not guaranteed to do the right
221 thing and is not a standard behaviour. You may opt to use uri_for() or
222 uri_for_action() instead.
230 my $location = shift;
231 my $status = shift || 302;
233 $self->location($location);
234 $self->status($status);
237 return $self->location;
240 =head2 $res->location
242 Sets or returns the HTTP 'Location'.
246 Sets or returns the HTTP status.
248 $c->response->status(404);
250 $res->code is an alias for this, to match HTTP::Response->code.
252 =head2 $res->write( $data )
254 Writes $data to the output stream.
256 =head2 $res->print( @data )
258 Prints @data to the output stream, separated by $,. This lets you pass
259 the response object to functions that want to write to an L<IO::Handle>.
263 Ensures that the response is flushed and closed at the end of the
276 defined $self->write($data) or return;
279 defined $self->write($,) or return;
280 defined $self->write($_) or return;
282 defined $self->write($\) or return;
289 Catalyst Contributors, see Catalyst.pm
293 This library is free software. You can redistribute it and/or modify
294 it under the same terms as Perl itself.
298 __PACKAGE__->meta->make_immutable;