0ef4c52ddf4ebe626a164e7d4ead178b5611c11f
[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 use Scalar::Util 'blessed';
8 use Catalyst::Response::Writer;
9
10 with 'MooseX::Emulate::Class::Accessor::Fast';
11
12 has _response_cb => (
13     is      => 'ro',
14     isa     => 'CodeRef', 
15     writer  => '_set_response_cb',
16     clearer => '_clear_response_cb',
17     predicate => '_has_response_cb',
18 );
19
20 subtype 'Catalyst::Engine::Types::Writer',
21     as duck_type([qw(write close)]);
22
23 has _writer => (
24     is      => 'ro',
25     isa     => 'Catalyst::Engine::Types::Writer', #Pointless since we control how this is built
26     #writer  => '_set_writer', Now that its lazy I think this is safe to remove
27     clearer => '_clear_writer',
28     predicate => '_has_writer',
29     lazy      => 1,
30     builder => '_build_writer',
31 );
32
33 sub _build_writer {
34     my $self = shift;
35
36     ## These two lines are probably crap now...
37     $self->_context->finalize_headers unless
38       $self->finalized_headers;
39
40     my @headers;
41     $self->headers->scan(sub { push @headers, @_ });
42
43     my $writer = $self->_response_cb->([ $self->status, \@headers ]);
44     $self->_clear_response_cb;
45
46     return $writer;
47 }
48
49 has write_fh => (
50   is=>'ro',
51   predicate=>'_has_write_fh',
52   lazy=>1,
53   builder=>'_build_write_fh',
54 );
55
56 sub _build_write_fh {
57   my $writer = $_[0]->_writer; # We need to get the finalize headers side effect...
58   my $requires_encoding = $_[0]->content_type =~ m/$Catalyst::DEFAULT_ENCODE_CONTENT_TYPE_MATCH/;
59   my %fields = (
60     _writer => $writer,
61     _encoding => $_[0]->encoding,
62     _requires_encoding => $requires_encoding,
63   );
64
65   return bless \%fields, 'Catalyst::Response::Writer';
66 }
67
68 sub DEMOLISH {
69   my $self = shift;
70   return if $self->_has_write_fh;
71   if($self->_has_writer) {
72     $self->_writer->close
73   }
74 }
75
76 has cookies   => (is => 'rw', default => sub { {} });
77 has body      => (is => 'rw', default => undef);
78 sub has_body { defined($_[0]->body) }
79
80 has location  => (is => 'rw');
81 has status    => (is => 'rw', default => 200);
82 has finalized_headers => (is => 'rw', default => 0);
83 has headers   => (
84   is      => 'rw',
85   isa => 'HTTP::Headers',
86   handles => [qw(content_encoding content_length content_type header)],
87   default => sub { HTTP::Headers->new() },
88   required => 1,
89   lazy => 1,
90 );
91 has _context => (
92   is => 'rw',
93   weak_ref => 1,
94   clearer => '_clear_context',
95 );
96
97 has encoding => (is=>'ro');
98
99 before [qw(status headers content_encoding content_length content_type header)] => sub {
100   my $self = shift;
101
102   $self->_context->log->warn( 
103     "Useless setting a header value after finalize_headers called." .
104     " Not what you want." )
105       if ( $self->finalized_headers && @_ );
106 };
107
108 sub output { shift->body(@_) }
109
110 sub code   { shift->status(@_) }
111
112 sub write {
113     my ( $self, $buffer ) = @_;
114
115     # Finalize headers if someone manually writes output
116     $self->_context->finalize_headers unless $self->finalized_headers;
117
118     $buffer = q[] unless defined $buffer;
119
120     $buffer = $self->_context->encoding->encode( $buffer, $self->_context->_encode_check )
121       if $self->_context->encoding && $self->content_type =~ /$Catalyst::DEFAULT_ENCODE_CONTENT_TYPE_MATCH/;
122
123     my $len = length($buffer);
124     $self->_writer->write($buffer);
125
126     return $len;
127 }
128
129 sub finalize_headers {
130     my ($self) = @_;
131     return;
132 }
133
134 sub from_psgi_response {
135     my ($self, $psgi_res) = @_;
136     if(blessed($psgi_res) && $psgi_res->can('as_psgi')) {
137       $psgi_res = $psgi_res->as_psgi;
138     }
139     if(ref $psgi_res eq 'ARRAY') {
140         my ($status, $headers, $body) = @$psgi_res;
141         $self->status($status);
142         $self->headers(HTTP::Headers->new(@$headers));
143         $self->body($body);
144     } elsif(ref $psgi_res eq 'CODE') {
145         $psgi_res->(sub {
146             my $response = shift;
147             my ($status, $headers, $maybe_body) = @$response;
148             $self->status($status);
149             $self->headers(HTTP::Headers->new(@$headers));
150             if(defined $maybe_body) {
151                 $self->body($maybe_body);
152             } else {
153                 return $self->write_fh;
154             }
155         });  
156      } else {
157         die "You can't set a Catalyst response from that, expect a valid PSGI response";
158     }
159 }
160
161 =head1 NAME
162
163 Catalyst::Response - stores output responding to the current client request
164
165 =head1 SYNOPSIS
166
167     $res = $c->response;
168     $res->body;
169     $res->code;
170     $res->content_encoding;
171     $res->content_length;
172     $res->content_type;
173     $res->cookies;
174     $res->header;
175     $res->headers;
176     $res->output;
177     $res->redirect;
178     $res->status;
179     $res->write;
180
181 =head1 DESCRIPTION
182
183 This is the Catalyst Response class, which provides methods for responding to
184 the current client request. The appropriate L<Catalyst::Engine> for your environment
185 will turn the Catalyst::Response into a HTTP Response and return it to the client.
186
187 =head1 METHODS
188
189 =head2 $res->body( $text | $fh | $iohandle_object )
190
191     $c->response->body('Catalyst rocks!');
192
193 Sets or returns the output (text or binary data). If you are returning a large body,
194 you might want to use a L<IO::Handle> type of object (Something that implements the read method
195 in the same fashion), or a filehandle GLOB. Catalyst
196 will write it piece by piece into the response.
197
198 When using a L<IO::Handle> type of object and no content length has been
199 already set in the response headers Catalyst will make a reasonable attempt
200 to determine the size of the Handle. Depending on the implementation of your
201 handle object, setting the content length may fail. If it is at all possible
202 for you to determine the content length of your handle object, 
203 it is recommended that you set the content length in the response headers
204 yourself, which will be respected and sent by Catalyst in the response.
205
206 Please note that the object needs to implement C<getline>, not just
207 C<read>.
208
209 Starting from version 5.90060, when using an L<IO::Handle> object, you
210 may want to use L<Plack::Middleware::XSendfile>, to delegate the
211 actual serving to the frontend server. To do so, you need to pass to
212 C<body> an IO object with a C<path> method. This can be achieved in
213 two ways.
214
215 Either using L<Plack::Util>:
216
217   my $fh = IO::File->new($file, 'r');
218   Plack::Util::set_io_path($fh, $file);
219
220 Or using L<IO::File::WithPath>
221
222   my $fh = IO::File::WithPath->new($file, 'r');
223
224 And then passing the filehandle to body and setting headers, if needed.
225
226   $c->response->body($fh);
227   $c->response->headers->content_type('text/plain');
228   $c->response->headers->content_length(-s $file);
229   $c->response->headers->last_modified((stat($file))[9]);
230
231 L<Plack::Middleware::XSendfile> can be loaded in the application so:
232
233  __PACKAGE__->config(
234      psgi_middleware => [
235          'XSendfile',
236          # other middlewares here...
237         ],
238  );
239
240 B<Beware> that loading the middleware without configuring the
241 webserver to set the request header C<X-Sendfile-Type> to a supported
242 type (C<X-Accel-Redirect> for nginx, C<X-Sendfile> for Apache and
243 Lighttpd), could lead to the disclosure of private paths to malicious
244 clients setting that header.
245
246 Nginx needs the additional X-Accel-Mapping header to be set in the
247 webserver configuration, so the middleware will replace the absolute
248 path of the IO object with the internal nginx path. This is also
249 useful to prevent a buggy app to server random files from the
250 filesystem, as it's an internal redirect.
251
252 An nginx configuration for FastCGI could look so:
253
254  server {
255      server_name example.com;
256      root /my/app/root;
257      location /private/repo/ {
258          internal;
259          alias /my/app/repo/;
260      }
261      location /private/staging/ {
262          internal;
263          alias /my/app/staging/;
264      }
265      location @proxy {
266          include /etc/nginx/fastcgi_params;
267          fastcgi_param SCRIPT_NAME '';
268          fastcgi_param PATH_INFO   $fastcgi_script_name;
269          fastcgi_param HTTP_X_SENDFILE_TYPE X-Accel-Redirect;
270          fastcgi_param HTTP_X_ACCEL_MAPPING /my/app=/private;
271          fastcgi_pass  unix:/my/app/run/app.sock;
272     }
273  }
274
275 In the example above, passing filehandles with a local path matching
276 /my/app/staging or /my/app/repo will be served by nginx. Passing paths
277 with other locations will lead to an internal server error.
278
279 Setting the body to a filehandle without the C<path> method bypasses
280 the middleware completely.
281
282 For Apache and Lighttpd, the mapping doesn't apply and setting the
283 X-Sendfile-Type is enough.
284
285 =head2 $res->has_body
286
287 Predicate which returns true when a body has been set.
288
289 =head2 $res->code
290
291 Alias for $res->status.
292
293 =head2 $res->content_encoding
294
295 Shortcut for $res->headers->content_encoding.
296
297 =head2 $res->content_length
298
299 Shortcut for $res->headers->content_length.
300
301 =head2 $res->content_type
302
303 Shortcut for $res->headers->content_type.
304
305 This value is typically set by your view or plugin. For example,
306 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
307 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
308
309 =head2 $res->cookies
310
311 Returns a reference to a hash containing cookies to be set. The keys of the
312 hash are the cookies' names, and their corresponding values are hash
313 references used to construct a L<CGI::Simple::Cookie> object.
314
315     $c->response->cookies->{foo} = { value => '123' };
316
317 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
318 parameters of the same name, except they are used without a leading dash.
319 Possible parameters are:
320
321 =over
322
323 =item value
324
325 =item expires
326
327 =item domain
328
329 =item path
330
331 =item secure
332
333 =item httponly
334
335 =back
336
337 =head2 $res->header
338
339 Shortcut for $res->headers->header.
340
341 =head2 $res->headers
342
343 Returns an L<HTTP::Headers> object, which can be used to set headers.
344
345     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
346
347 =head2 $res->output
348
349 Alias for $res->body.
350
351 =head2 $res->redirect( $url, $status )
352
353 Causes the response to redirect to the specified URL. The default status is
354 C<302>.
355
356     $c->response->redirect( 'http://slashdot.org' );
357     $c->response->redirect( 'http://slashdot.org', 307 );
358
359 This is a convenience method that sets the Location header to the
360 redirect destination, and then sets the response status.  You will
361 want to C< return > or C<< $c->detach() >> to interrupt the normal
362 processing flow if you want the redirect to occur straight away.
363
364 B<Note:> do not give a relative URL as $url, i.e: one that is not fully
365 qualified (= C<http://...>, etc.) or that starts with a slash
366 (= C</path/here>). While it may work, it is not guaranteed to do the right
367 thing and is not a standard behaviour. You may opt to use uri_for() or
368 uri_for_action() instead.
369
370 B<Note:> If $url is an object that does ->as_string (such as L<URI>, which is
371 what you get from ->uri_for) we automatically call that to stringify.  This
372 should ease the common case usage
373
374     return $c->res->redirect( $c->uri_for(...));
375
376 =cut
377
378 sub redirect {
379     my $self = shift;
380
381     if (@_) {
382         my $location = shift;
383         my $status   = shift || 302;
384
385         if(blessed($location) && $location->can('as_string')) {
386             $location = $location->as_string;
387         }
388
389         $self->location($location);
390         $self->status($status);
391     }
392
393     return $self->location;
394 }
395
396 =head2 $res->location
397
398 Sets or returns the HTTP 'Location'.
399
400 =head2 $res->status
401
402 Sets or returns the HTTP status.
403
404     $c->response->status(404);
405
406 $res->code is an alias for this, to match HTTP::Response->code.
407
408 =head2 $res->write( $data )
409
410 Writes $data to the output stream.  Calling this method will finalize your
411 headers and send the headers and status code response to the client (so changing
412 them afterwards is a waste... be sure to set your headers correctly first).
413
414 You may call this as often as you want throughout your response cycle.  You may
415 even set a 'body' afterward.  So for example you might write your HTTP headers
416 and the HEAD section of your document and then set the body from a template
417 driven from a database.  In some cases this can seem to the client as if you had
418 a faster overall response (but note that unless your server support chunked
419 body your content is likely to get queued anyway (L<Starman> and most other 
420 http 1.1 webservers support this).
421
422 If there is an encoding set, we encode each line of the response (the default
423 encoding is UTF-8).
424
425 =head2 $res->write_fh
426
427 Returns an instance of L<Catalyst::Response::Writer>, which is a lightweight
428 decorator over the PSGI C<$writer> object (see L<PSGI.pod\Delayed-Response-and-Streaming-Body>).
429
430 In addition to proxying the C<write> and C<close> method from the underlying PSGI
431 writer, this proxy object knows any application wide encoding, and provides a method
432 C<write_encoded> that will properly encode your written lines based upon your
433 encoding settings.  By default in L<Catalyst> responses are UTF-8 encoded and this
434 is the encoding used if you respond via C<write_encoded>.  If you want to handle
435 encoding yourself, you can use the C<write> method directly.
436
437 Encoding only applies to content types for which it matters.  Currently the following
438 content types are assumed to need encoding: text (including HTML), xml and javascript.
439
440 We provide access to this object so that you can properly close over it for use in
441 asynchronous and nonblocking applications.  For example (assuming you are using a supporting
442 server, like L<Twiggy>:
443
444     package AsyncExample::Controller::Root;
445
446     use Moose;
447
448     BEGIN { extends 'Catalyst::Controller' }
449
450     sub prepare_cb {
451       my $write_fh = pop;
452       return sub {
453         my $message = shift;
454         $write_fh->write("Finishing: $message\n");
455         $write_fh->close;
456       };
457     }
458
459     sub anyevent :Local :Args(0) {
460       my ($self, $c) = @_;
461       my $cb = $self->prepare_cb($c->res->write_fh);
462
463       my $watcher;
464       $watcher = AnyEvent->timer(
465         after => 5,
466         cb => sub {
467           $cb->(scalar localtime);
468           undef $watcher; # cancel circular-ref
469         });
470     }
471
472 Like the 'write' method, calling this will finalize headers. Unlike 'write' when you
473 can this it is assumed you are taking control of the response so the body is never
474 finalized (there isn't one anyway) and you need to call the close method.
475
476 =head2 $res->print( @data )
477
478 Prints @data to the output stream, separated by $,.  This lets you pass
479 the response object to functions that want to write to an L<IO::Handle>.
480
481 =head2 $self->finalize_headers($c)
482
483 Writes headers to response if not already written
484
485 =head2 from_psgi_response
486
487 Given a PSGI response (either three element ARRAY reference OR coderef expecting
488 a $responder) set the response from it.
489
490 Properly supports streaming and delayed response and / or async IO if running
491 under an expected event loop.
492
493 If passed an object, will expect that object to do a method C<as_psgi>.
494
495 Example:
496
497     package MyApp::Web::Controller::Test;
498
499     use base 'Catalyst::Controller';
500     use Plack::App::Directory;
501
502
503     my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })
504       ->to_app;
505
506     sub myaction :Local Args {
507       my ($self, $c) = @_;
508       $c->res->from_psgi_response($app->($c->req->env));
509     }
510
511 Please note this does not attempt to map or nest your PSGI application under
512 the Controller and Action namespace or path.  
513
514 =head2 DEMOLISH
515
516 Ensures that the response is flushed and closed at the end of the
517 request.
518
519 =head2 meta
520
521 Provided by Moose
522
523 =cut
524
525 sub print {
526     my $self = shift;
527     my $data = shift;
528
529     defined $self->write($data) or return;
530
531     for (@_) {
532         defined $self->write($,) or return;
533         defined $self->write($_) or return;
534     }
535     defined $self->write($\) or return;
536
537     return 1;
538 }
539
540 =head1 AUTHORS
541
542 Catalyst Contributors, see Catalyst.pm
543
544 =head1 COPYRIGHT
545
546 This library is free software. You can redistribute it and/or modify
547 it under the same terms as Perl itself.
548
549 =cut
550
551 __PACKAGE__->meta->make_immutable;
552
553 1;