1 package Catalyst::Engine;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
6 use CGI::Simple::Cookie;
7 use Data::Dump qw/dump/;
8 use Errno 'EWOULDBLOCK';
14 use Catalyst::EngineLoader;
16 use Plack::Request::Upload;
20 use namespace::clean -except => 'meta';
22 # Amount of data to read from input on each pass
23 our $CHUNKSIZE = 64 * 1024;
25 # XXX - this is only here for compat, do not use!
26 has env => ( is => 'rw', writer => '_set_env' );
27 my $WARN_ABOUT_ENV = 0;
29 my ($orig, $self, @args) = @_;
31 warn "env as a writer is deprecated, you probably need to upgrade Catalyst::Engine::PSGI"
32 unless $WARN_ABOUT_ENV++;
33 return $self->_set_env(@args);
38 # XXX - Only here for Engine::PSGI compat
39 sub prepare_connection {
40 my ($self, $ctx) = @_;
41 $ctx->request->prepare_connection;
46 Catalyst::Engine - The Catalyst Engine
57 =head2 $self->finalize_body($c)
59 Finalize body. Prints the response output as blocking stream if it looks like
60 a filehandle, otherwise write it out all in one go. If there is no body in
61 the response, we assume you are handling it 'manually', such as for nonblocking
62 style or asynchronous streaming responses. You do this by calling L</write>
63 several times (which sends HTTP headers if needed) or you close over
64 C<< $response->write_fh >>.
66 See L<Catalyst::Response/write> and L<Catalyst::Response/write_fh> for more.
71 my ( $self, $c ) = @_;
72 return if $c->response->_has_write_fh;
74 my $body = $c->response->body;
75 no warnings 'uninitialized';
76 if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) {
79 $got = read $body, my ($buffer), $CHUNKSIZE;
80 $got = 0 unless $self->write( $c, $buffer );
86 $self->write( $c, $body );
89 my $res = $c->response;
96 =head2 $self->finalize_cookies($c)
98 Create CGI::Simple::Cookie objects from $c->res->cookies, and set them as
103 sub finalize_cookies {
104 my ( $self, $c ) = @_;
107 my $response = $c->response;
109 foreach my $name (keys %{ $response->cookies }) {
111 my $val = $response->cookies->{$name};
116 : CGI::Simple::Cookie->new(
118 -value => $val->{value},
119 -expires => $val->{expires},
120 -domain => $val->{domain},
121 -path => $val->{path},
122 -secure => $val->{secure} || 0,
123 -httponly => $val->{httponly} || 0,
126 if (!defined $cookie) {
127 $c->log->warn("undef passed in '$name' cookie value - not setting cookie")
132 push @cookies, $cookie->as_string;
135 for my $cookie (@cookies) {
136 $response->headers->push_header( 'Set-Cookie' => $cookie );
140 =head2 $self->finalize_error($c)
142 Output an appropriate error message. Called if there's an error in $c
143 after the dispatch has finished. Will output debug messages if Catalyst
144 is in debug mode, or a `please come back later` message otherwise.
148 sub _dump_error_page_element {
149 my ($self, $i, $element) = @_;
150 my ($name, $val) = @{ $element };
152 # This is fugly, but the metaclass is _HUGE_ and demands waaay too much
153 # scrolling. Suggestions for more pleasant ways to do this welcome.
154 local $val->{'__MOP__'} = "Stringified: "
155 . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'};
157 my $text = encode_entities( dump( $val ));
158 sprintf <<"EOF", $name, $text;
159 <h2><a href="#" onclick="toggleDump('dump_$i'); return false">%s</a></h2>
161 <pre wrap="">%s</pre>
167 my ( $self, $c ) = @_;
169 $c->res->content_type('text/html; charset=utf-8');
170 my $name = ref($c)->config->{name} || join(' ', split('::', ref $c));
172 # Prevent Catalyst::Plugin::Unicode::Encoding from running.
173 # This is a little nasty, but it's the best way to be clean whether or
174 # not the user has an encoding plugin.
176 if ($c->can('encoding')) {
180 my ( $title, $error, $infos );
184 $error = join '', map {
185 '<p><code class="error">'
186 . encode_entities($_)
189 $error ||= 'No output';
190 $error = qq{<pre wrap="">$error</pre>};
191 $title = $name = "$name on Catalyst $Catalyst::VERSION";
192 $name = "<h1>$name</h1>";
194 # Don't show context in the dump
195 $c->res->_clear_context;
197 # Don't show body parser in the dump
198 $c->req->_clear_body;
202 for my $dump ( $c->dump_these ) {
203 push @infos, $self->_dump_error_page_element($i, $dump);
206 $infos = join "\n", @infos;
213 (en) Please come back later
214 (fr) SVP veuillez revenir plus tard
215 (de) Bitte versuchen sie es spaeter nocheinmal
216 (at) Konnten's bitt'schoen spaeter nochmal reinschauen
217 (no) Vennligst prov igjen senere
218 (dk) Venligst prov igen senere
219 (pl) Prosze sprobowac pozniej
220 (pt) Por favor volte mais tarde
221 (ru) Попробуйте еще раз позже
222 (ua) Спробуйте ще раз пізніше
227 $c->res->body( <<"" );
228 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
229 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
230 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
232 <meta http-equiv="Content-Language" content="en" />
233 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
234 <title>$title</title>
235 <script type="text/javascript">
237 function toggleDump (dumpElement) {
238 var e = document.getElementById( dumpElement );
239 if (e.style.display == "none") {
240 e.style.display = "";
243 e.style.display = "none";
248 <style type="text/css">
250 font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
251 Tahoma, Arial, helvetica, sans-serif;
253 background-color: #eee;
257 :link, :link:hover, :visited, :visited:hover {
262 background-color: #ccc;
263 border: 1px solid #aaa;
268 background-color: #cce;
269 border: 1px solid #755;
275 background-color: #eee;
276 border: 1px solid #575;
282 background-color: #cce;
283 border: 1px solid #557;
292 div.name h1, div.error p {
300 text-decoration: underline;
306 /* from http://users.tkk.fi/~tkarvine/linux/doc/pre-wrap/pre-wrap-css3-mozilla-opera-ie.html */
307 /* Browser specific (not valid) styles to make preformatted text wrap */
309 white-space: pre-wrap; /* css-3 */
310 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
311 white-space: -pre-wrap; /* Opera 4-6 */
312 white-space: -o-pre-wrap; /* Opera 7 */
313 word-wrap: break-word; /* Internet Explorer 5.5+ */
319 <div class="error">$error</div>
320 <div class="infos">$infos</div>
321 <div class="name">$name</div>
326 # Trick IE. Old versions of IE would display their own error page instead
327 # of ours if we'd give it less than 512 bytes.
328 $c->res->{body} .= ( ' ' x 512 );
330 $c->res->{body} = Encode::encode("UTF-8", $c->res->{body});
333 $c->res->status(500);
336 =head2 $self->finalize_headers($c)
338 Allows engines to write headers to response
342 sub finalize_headers {
343 my ($self, $ctx) = @_;
345 $ctx->finalize_headers unless $ctx->response->finalized_headers;
349 =head2 $self->finalize_uploads($c)
351 Clean up after uploads, deleting temp files.
355 sub finalize_uploads {
356 my ( $self, $c ) = @_;
358 # N.B. This code is theoretically entirely unneeded due to ->cleanup(1)
359 # on the HTTP::Body object.
360 my $request = $c->request;
361 foreach my $key (keys %{ $request->uploads }) {
362 my $upload = $request->uploads->{$key};
363 unlink grep { -e $_ } map { $_->tempname }
364 (ref $upload eq 'ARRAY' ? @{$upload} : ($upload));
369 =head2 $self->prepare_body($c)
371 sets up the L<Catalyst::Request> object body using L<HTTP::Body>
376 my ( $self, $c ) = @_;
378 $c->request->prepare_body;
381 =head2 $self->prepare_body_chunk($c)
383 Add a chunk to the request body.
387 # XXX - Can this be deleted?
388 sub prepare_body_chunk {
389 my ( $self, $c, $chunk ) = @_;
391 $c->request->prepare_body_chunk($chunk);
394 =head2 $self->prepare_body_parameters($c)
396 Sets up parameters from body.
400 sub prepare_body_parameters {
401 my ( $self, $c ) = @_;
403 $c->request->prepare_body_parameters;
406 =head2 $self->prepare_parameters($c)
408 Sets up parameters from query and post parameters.
409 If parameters have already been set up will clear
410 existing parameters and set up again.
414 sub prepare_parameters {
415 my ( $self, $c ) = @_;
417 $c->request->_clear_parameters;
418 return $c->request->parameters;
421 =head2 $self->prepare_path($c)
423 abstract method, implemented by engines.
428 my ($self, $ctx) = @_;
430 my $env = $ctx->request->env;
432 my $scheme = $ctx->request->secure ? 'https' : 'http';
433 my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME};
434 my $port = $env->{SERVER_PORT} || 80;
435 my $base_path = $env->{SCRIPT_NAME} || "/";
437 # set the request URI
439 if (!$ctx->config->{use_request_uri_for_path}) {
440 my $path_info = $env->{PATH_INFO};
441 if ( exists $env->{REDIRECT_URL} ) {
442 $base_path = $env->{REDIRECT_URL};
443 $base_path =~ s/\Q$path_info\E$//;
445 $path = $base_path . $path_info;
447 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
448 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
451 my $req_uri = $env->{REQUEST_URI};
452 $req_uri =~ s/\?.*$//;
457 # Using URI directly is way too slow, so we construct the URLs manually
458 my $uri_class = "URI::$scheme";
460 # HTTP_HOST will include the port even if it's 80/443
461 $host =~ s/:(?:80|443)$//;
463 if ($port !~ /^(?:80|443)$/ && $host !~ /:/) {
467 my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '';
468 my $uri = $scheme . '://' . $host . '/' . $path . $query;
470 $ctx->request->uri( (bless \$uri, $uri_class)->canonical );
473 # base must end in a slash
474 $base_path .= '/' unless $base_path =~ m{/$};
476 my $base_uri = $scheme . '://' . $host . $base_path;
478 $ctx->request->base( bless \$base_uri, $uri_class );
483 =head2 $self->prepare_request($c)
485 =head2 $self->prepare_query_parameters($c)
487 process the query string and extract query parameters.
491 sub prepare_query_parameters {
493 my $env = $c->request->env;
495 if(my $query_obj = $env->{'plack.request.query'}) {
496 $c->request->query_parameters(
497 $c->request->_use_hash_multivalue ?
499 $query_obj->as_hashref_mixed);
503 my $query_string = exists $env->{QUERY_STRING}
504 ? $env->{QUERY_STRING}
507 # Check for keywords (no = signs)
508 # (yes, index() is faster than a regex :))
509 if ( index( $query_string, '=' ) < 0 ) {
510 $c->request->query_keywords($self->unescape_uri($query_string));
516 # replace semi-colons
517 $query_string =~ s/;/&/g;
519 my @params = grep { length $_ } split /&/, $query_string;
521 for my $item ( @params ) {
524 = map { $self->unescape_uri($_) }
525 split( /=/, $item, 2 );
527 $param = $self->unescape_uri($item) unless defined $param;
529 if ( exists $query{$param} ) {
530 if ( ref $query{$param} ) {
531 push @{ $query{$param} }, $value;
534 $query{$param} = [ $query{$param}, $value ];
538 $query{$param} = $value;
542 $c->request->query_parameters(
543 $c->request->_use_hash_multivalue ?
544 Hash::MultiValue->from_mixed(\%query) :
548 =head2 $self->prepare_read($c)
550 Prepare to read by initializing the Content-Length from headers.
555 my ( $self, $c ) = @_;
557 # Initialize the amount of data we think we need to read
558 $c->request->_read_length;
561 =head2 $self->prepare_request(@arguments)
563 Populate the context object from the request object.
567 sub prepare_request {
568 my ($self, $ctx, %args) = @_;
569 $ctx->log->psgienv($args{env}) if $ctx->log->can('psgienv');
570 $ctx->request->_set_env($args{env});
571 $self->_set_env($args{env}); # Nasty back compat!
572 $ctx->response->_set_response_cb($args{response_cb});
575 =head2 $self->prepare_uploads($c)
579 sub prepare_uploads {
580 my ( $self, $c ) = @_;
582 my $request = $c->request;
583 return unless $request->_body;
585 my $uploads = $request->_body->upload;
586 my $parameters = $request->parameters;
587 foreach my $name (keys %$uploads) {
588 my $files = $uploads->{$name};
590 for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) {
591 my $headers = HTTP::Headers->new( %{ $upload->{headers} } );
592 my $u = Catalyst::Request::Upload->new
594 size => $upload->{size},
595 type => scalar $headers->content_type,
597 tempname => $upload->{tempname},
598 filename => $upload->{filename},
602 $request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0];
604 # support access to the filename as a normal param
605 my @filenames = map { $_->{filename} } @uploads;
606 # append, if there's already params with this name
607 if (exists $parameters->{$name}) {
608 if (ref $parameters->{$name} eq 'ARRAY') {
609 push @{ $parameters->{$name} }, @filenames;
612 $parameters->{$name} = [ $parameters->{$name}, @filenames ];
616 $parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0];
621 =head2 $self->write($c, $buffer)
623 Writes the buffer to the client.
628 my ( $self, $c, $buffer ) = @_;
630 $c->response->write($buffer);
633 =head2 $self->read($c, [$maxlength])
635 Reads from the input stream by calling C<< $self->read_chunk >>.
637 Maintains the read_length and read_position counters as data is read.
642 my ( $self, $c, $maxlength ) = @_;
644 $c->request->read($maxlength);
647 =head2 $self->read_chunk($c, \$buffer, $length)
649 Each engine implements read_chunk as its preferred way of reading a chunk
650 of data. Returns the number of bytes read. A return of 0 indicates that
651 there is no more data to be read.
656 my ($self, $ctx) = (shift, shift);
657 return $ctx->request->read_chunk(@_);
660 =head2 $self->run($app, $server)
662 Start the engine. Builds a PSGI application and calls the
663 run method on the server passed in, which then causes the
664 engine to loop, handling requests..
669 my ($self, $app, $psgi, @args) = @_;
670 # @args left here rather than just a $options, $server for back compat with the
671 # old style scripts which send a few args, then a hashref
673 # They should never actually be used in the normal case as the Plack engine is
674 # passed in got all the 'standard' args via the loader in the script already.
676 # FIXME - we should stash the options in an attribute so that custom args
677 # like Gitalist's --git_dir are possible to get from the app without stupid tricks.
678 my $server = pop @args if (scalar @args && blessed $args[-1]);
679 my $options = pop @args if (scalar @args && ref($args[-1]) eq 'HASH');
680 # Back compat hack for applications with old (non Catalyst::Script) scripts to work in FCGI.
681 if (scalar @args && !ref($args[0])) {
682 if (my $listen = shift @args) {
683 $options->{listen} ||= [$listen];
687 $server = Catalyst::EngineLoader->new(application_name => ref($self))->auto(%$options);
688 # We're not being called from a script, so auto detect what backend to
689 # run on. This should never happen, as mod_perl never calls ->run,
690 # instead the $app->handle method is called per request.
691 $app->log->warn("Not supplied a Plack engine, falling back to engine auto-loader (are your scripts ancient?)")
693 $app->run_options($options);
694 $server->run($psgi, $options);
697 =head2 build_psgi_app ($app, @args)
699 Builds and returns a PSGI application closure. (Raw, not wrapped in middleware)
704 my ($self, $app, @args) = @_;
711 confess("Did not get a response callback for writer, cannot continue") unless $respond;
712 $app->handle_request(env => $env, response_cb => $respond);
717 =head2 $self->unescape_uri($uri)
719 Unescapes a given URI using the most efficient method available. Engines such
720 as Apache may implement this using Apache's C-based modules, for example.
725 my ( $self, $str ) = @_;
727 $str =~ s/(?:%([0-9A-Fa-f]{2})|\+)/defined $1 ? chr(hex($1)) : ' '/eg;
732 =head2 $self->finalize_output
734 <obsolete>, see finalize_body
738 Hash containing environment variables including many special variables inserted
739 by WWW server - like SERVER_*, REMOTE_*, HTTP_* ...
741 Before accessing environment variables consider whether the same information is
742 not directly available via Catalyst objects $c->request, $c->engine ...
744 BEWARE: If you really need to access some environment variable from your Catalyst
745 application you should use $c->engine->env->{VARNAME} instead of $ENV{VARNAME},
746 as in some environments the %ENV hash does not contain what you would expect.
750 Catalyst Contributors, see Catalyst.pm
754 This library is free software. You can redistribute it and/or modify it under
755 the same terms as Perl itself.
759 __PACKAGE__->meta->make_immutable;