1 package Catalyst::Response;
5 use Moose::Util::TypeConstraints;
6 use namespace::autoclean;
7 use Scalar::Util 'blessed';
9 with 'MooseX::Emulate::Class::Accessor::Fast';
14 writer => '_set_response_cb',
15 clearer => '_clear_response_cb',
16 predicate => '_has_response_cb',
19 subtype 'Catalyst::Engine::Types::Writer',
20 as duck_type([qw(write close)]);
24 isa => 'Catalyst::Engine::Types::Writer', #Pointless since we control how this is built
25 #writer => '_set_writer', Now that its lazy I think this is safe to remove
26 clearer => '_clear_writer',
27 predicate => '_has_writer',
29 builder => '_build_writer',
35 ## These two lines are probably crap now...
36 $self->_context->finalize_headers unless
37 $self->finalized_headers;
40 $self->headers->scan(sub { push @headers, @_ });
42 my $writer = $self->_response_cb->([ $self->status, \@headers ]);
43 $self->_clear_response_cb;
50 predicate=>'_has_write_fh',
52 builder=>'_build_write_fh',
55 sub _build_write_fh { shift ->_writer }
59 return if $self->_has_write_fh;
60 if($self->_has_writer) {
65 has cookies => (is => 'rw', default => sub { {} });
66 has body => (is => 'rw', default => undef);
67 sub has_body { defined($_[0]->body) }
69 has location => (is => 'rw');
70 has status => (is => 'rw', default => 200);
71 has finalized_headers => (is => 'rw', default => 0);
74 isa => 'HTTP::Headers',
75 handles => [qw(content_encoding content_length content_type header)],
76 default => sub { HTTP::Headers->new() },
83 clearer => '_clear_context',
86 before [qw(status headers content_encoding content_length content_type header)] => sub {
89 $self->_context->log->warn(
90 "Useless setting a header value after finalize_headers called." .
91 " Not what you want." )
92 if ( $self->finalized_headers && @_ );
95 sub output { shift->body(@_) }
97 sub code { shift->status(@_) }
100 my ( $self, $buffer ) = @_;
102 # Finalize headers if someone manually writes output
103 $self->_context->finalize_headers unless $self->finalized_headers;
105 $buffer = q[] unless defined $buffer;
106 $buffer = $self->_context->encoding->encode( $buffer, $self->_context->_encode_check );
108 my $len = length($buffer);
109 $self->_writer->write($buffer);
114 sub finalize_headers {
119 sub from_psgi_response {
120 my ($self, $psgi_res) = @_;
121 if(blessed($psgi_res) && $psgi_res->can('as_psgi')) {
122 $psgi_res = $psgi_res->as_psgi;
124 if(ref $psgi_res eq 'ARRAY') {
125 my ($status, $headers, $body) = @$psgi_res;
126 $self->status($status);
127 $self->headers(HTTP::Headers->new(@$headers));
129 } elsif(ref $psgi_res eq 'CODE') {
131 my $response = shift;
132 my ($status, $headers, $maybe_body) = @$response;
133 $self->status($status);
134 $self->headers(HTTP::Headers->new(@$headers));
135 if(defined $maybe_body) {
136 $self->body($maybe_body);
138 return $self->write_fh;
142 die "You can't set a Catalyst response from that, expect a valid PSGI response";
148 Catalyst::Response - stores output responding to the current client request
155 $res->content_encoding;
156 $res->content_length;
168 This is the Catalyst Response class, which provides methods for responding to
169 the current client request. The appropriate L<Catalyst::Engine> for your environment
170 will turn the Catalyst::Response into a HTTP Response and return it to the client.
174 =head2 $res->body( $text | $fh | $iohandle_object )
176 $c->response->body('Catalyst rocks!');
178 Sets or returns the output (text or binary data). If you are returning a large body,
179 you might want to use a L<IO::Handle> type of object (Something that implements the read method
180 in the same fashion), or a filehandle GLOB. Catalyst
181 will write it piece by piece into the response.
183 When using a L<IO::Handle> type of object and no content length has been
184 already set in the response headers Catalyst will make a reasonable attempt
185 to determine the size of the Handle. Depending on the implementation of your
186 handle object, setting the content length may fail. If it is at all possible
187 for you to determine the content length of your handle object,
188 it is recommended that you set the content length in the response headers
189 yourself, which will be respected and sent by Catalyst in the response.
191 Please note that the object needs to implement C<getline>, not just
194 Starting from version 5.90060, when using an L<IO::Handle> object, you
195 may want to use L<Plack::Middleware::XSendfile>, to delegate the
196 actual serving to the frontend server. To do so, you need to pass to
197 C<body> an IO object with a C<path> method. This can be achieved in
200 Either using L<Plack::Util>:
202 my $fh = IO::File->new($file, 'r');
203 Plack::Util::set_io_path($fh, $file);
205 Or using L<IO::File::WithPath>
207 my $fh = IO::File::WithPath->new($file, 'r');
209 And then passing the filehandle to body and setting headers, if needed.
211 $c->response->body($fh);
212 $c->response->headers->content_type('text/plain');
213 $c->response->headers->content_length(-s $file);
214 $c->response->headers->last_modified((stat($file))[9]);
216 L<Plack::Middleware::XSendfile> can be loaded in the application so:
221 # other middlewares here...
225 B<Beware> that loading the middleware without configuring the
226 webserver to set the request header C<X-Sendfile-Type> to a supported
227 type (C<X-Accel-Redirect> for nginx, C<X-Sendfile> for Apache and
228 Lighttpd), could lead to the disclosure of private paths to malicious
229 clients setting that header.
231 Nginx needs the additional X-Accel-Mapping header to be set in the
232 webserver configuration, so the middleware will replace the absolute
233 path of the IO object with the internal nginx path. This is also
234 useful to prevent a buggy app to server random files from the
235 filesystem, as it's an internal redirect.
237 An nginx configuration for FastCGI could look so:
240 server_name example.com;
242 location /private/repo/ {
246 location /private/staging/ {
248 alias /my/app/staging/;
251 include /etc/nginx/fastcgi_params;
252 fastcgi_param SCRIPT_NAME '';
253 fastcgi_param PATH_INFO $fastcgi_script_name;
254 fastcgi_param HTTP_X_SENDFILE_TYPE X-Accel-Redirect;
255 fastcgi_param HTTP_X_ACCEL_MAPPING /my/app=/private;
256 fastcgi_pass unix:/my/app/run/app.sock;
260 In the example above, passing filehandles with a local path matching
261 /my/app/staging or /my/app/repo will be served by nginx. Passing paths
262 with other locations will lead to an internal server error.
264 Setting the body to a filehandle without the C<path> method bypasses
265 the middleware completely.
267 For Apache and Lighttpd, the mapping doesn't apply and setting the
268 X-Sendfile-Type is enough.
270 =head2 $res->has_body
272 Predicate which returns true when a body has been set.
276 Alias for $res->status.
278 =head2 $res->content_encoding
280 Shortcut for $res->headers->content_encoding.
282 =head2 $res->content_length
284 Shortcut for $res->headers->content_length.
286 =head2 $res->content_type
288 Shortcut for $res->headers->content_type.
290 This value is typically set by your view or plugin. For example,
291 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
292 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
296 Returns a reference to a hash containing cookies to be set. The keys of the
297 hash are the cookies' names, and their corresponding values are hash
298 references used to construct a L<CGI::Simple::Cookie> object.
300 $c->response->cookies->{foo} = { value => '123' };
302 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
303 parameters of the same name, except they are used without a leading dash.
304 Possible parameters are:
324 Shortcut for $res->headers->header.
328 Returns an L<HTTP::Headers> object, which can be used to set headers.
330 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
334 Alias for $res->body.
336 =head2 $res->redirect( $url, $status )
338 Causes the response to redirect to the specified URL. The default status is
341 $c->response->redirect( 'http://slashdot.org' );
342 $c->response->redirect( 'http://slashdot.org', 307 );
344 This is a convenience method that sets the Location header to the
345 redirect destination, and then sets the response status. You will
346 want to C< return > or C<< $c->detach() >> to interrupt the normal
347 processing flow if you want the redirect to occur straight away.
349 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
350 qualified (= C<http://...>, etc.) or that starts with a slash
351 (= C</path/here>). While it may work, it is not guaranteed to do the right
352 thing and is not a standard behaviour. You may opt to use uri_for() or
353 uri_for_action() instead.
355 B<Note:> If $url is an object that does ->as_string (such as L<URI>, which is
356 what you get from ->uri_for) we automatically call that to stringify. This
357 should ease the common case usage
359 return $c->res->redirect( $c->uri_for(...));
367 my $location = shift;
368 my $status = shift || 302;
370 if(blessed($location) && $location->can('as_string')) {
371 $location = $location->as_string;
374 $self->location($location);
375 $self->status($status);
378 return $self->location;
381 =head2 $res->location
383 Sets or returns the HTTP 'Location'.
387 Sets or returns the HTTP status.
389 $c->response->status(404);
391 $res->code is an alias for this, to match HTTP::Response->code.
393 =head2 $res->write( $data )
395 Writes $data to the output stream. Calling this method will finalize your
396 headers and send the headers and status code response to the client (so changing
397 them afterwards is a waste... be sure to set your headers correctly first).
399 You may call this as often as you want throughout your response cycle. You may
400 even set a 'body' afterward. So for example you might write your HTTP headers
401 and the HEAD section of your document and then set the body from a template
402 driven from a database. In some cases this can seem to the client as if you had
403 a faster overall response (but note that unless your server support chunked
404 body your content is likely to get queued anyway (L<Starman> and most other
405 http 1.1 webservers support this).
407 If there is an encoding set, we encode each line of the response (the default
410 =head2 $res->write_fh
412 Returns a PSGI $writer object that has two methods, write and close. You can
413 close over this object for asynchronous and nonblocking applications. For
414 example (assuming you are using a supporting server, like L<Twiggy>
416 package AsyncExample::Controller::Root;
420 BEGIN { extends 'Catalyst::Controller' }
426 $write_fh->write("Finishing: $message\n");
431 sub anyevent :Local :Args(0) {
433 my $cb = $self->prepare_cb($c->res->write_fh);
436 $watcher = AnyEvent->timer(
439 $cb->(scalar localtime);
440 undef $watcher; # cancel circular-ref
444 Like the 'write' method, calling this will finalize headers. Unlike 'write' when you
445 can this it is assumed you are taking control of the response so the body is never
446 finalized (there isn't one anyway) and you need to call the close method.
448 =head2 $res->print( @data )
450 Prints @data to the output stream, separated by $,. This lets you pass
451 the response object to functions that want to write to an L<IO::Handle>.
453 =head2 $self->finalize_headers($c)
455 Writes headers to response if not already written
457 =head2 from_psgi_response
459 Given a PSGI response (either three element ARRAY reference OR coderef expecting
460 a $responder) set the response from it.
462 Properly supports streaming and delayed response and / or async IO if running
463 under an expected event loop.
465 If passed an object, will expect that object to do a method C<as_psgi>.
469 package MyApp::Web::Controller::Test;
471 use base 'Catalyst::Controller';
472 use Plack::App::Directory;
475 my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })
478 sub myaction :Local Args {
480 $c->res->from_psgi_response($app->($c->req->env));
483 Please note this does not attempt to map or nest your PSGI application under
484 the Controller and Action namespace or path.
488 Ensures that the response is flushed and closed at the end of the
501 defined $self->write($data) or return;
504 defined $self->write($,) or return;
505 defined $self->write($_) or return;
507 defined $self->write($\) or return;
514 Catalyst Contributors, see Catalyst.pm
518 This library is free software. You can redistribute it and/or modify
519 it under the same terms as Perl itself.
523 __PACKAGE__->meta->make_immutable;