Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Engine; |
2 | |
7fa2c9c1 |
3 | use Moose; |
4 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
5 | |
fa32ac82 |
6 | use CGI::Simple::Cookie; |
f63c03e4 |
7 | use Data::Dump qw/dump/; |
d04b2ffd |
8 | use Errno 'EWOULDBLOCK'; |
fc7ec1d9 |
9 | use HTML::Entities; |
fbcc39ad |
10 | use HTTP::Body; |
fc7ec1d9 |
11 | use HTTP::Headers; |
e0616220 |
12 | use URI::QueryParam; |
a1791811 |
13 | use Plack::Loader; |
b1ededd4 |
14 | use Catalyst::EngineLoader; |
361ba9b2 |
15 | use Encode (); |
16 | use utf8; |
fbcc39ad |
17 | |
d495753a |
18 | use namespace::clean -except => 'meta'; |
19 | |
faa02805 |
20 | # Amount of data to read from input on each pass |
21 | our $CHUNKSIZE = 64 * 1024; |
a50e5b46 |
22 | |
faa02805 |
23 | # XXX - this is only here for compat, do not use! |
24 | has env => ( is => 'rw', writer => '_set_env' ); |
0c6352ff |
25 | my $WARN_ABOUT_ENV = 0; |
26 | around env => sub { |
27 | my ($orig, $self, @args) = @_; |
28 | if(@args) { |
29 | warn "env as a writer is deprecated, you probably need to upgrade Catalyst::Engine::PSGI" |
30 | unless $WARN_ABOUT_ENV++; |
31 | return $self->_set_env(@args); |
32 | } |
33 | return $self->$orig; |
34 | }; |
35 | |
fc7ec1d9 |
36 | =head1 NAME |
37 | |
38 | Catalyst::Engine - The Catalyst Engine |
39 | |
40 | =head1 SYNOPSIS |
41 | |
42 | See L<Catalyst>. |
43 | |
44 | =head1 DESCRIPTION |
45 | |
23f9d934 |
46 | =head1 METHODS |
fc7ec1d9 |
47 | |
cd3bb248 |
48 | |
b5ecfcf0 |
49 | =head2 $self->finalize_body($c) |
06e1b616 |
50 | |
fbcc39ad |
51 | Finalize body. Prints the response output. |
06e1b616 |
52 | |
53 | =cut |
54 | |
fbcc39ad |
55 | sub finalize_body { |
56 | my ( $self, $c ) = @_; |
7257e9db |
57 | my $body = $c->response->body; |
f9b6d612 |
58 | no warnings 'uninitialized'; |
7e95ba12 |
59 | if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { |
be1c9503 |
60 | my $got; |
61 | do { |
1235b30f |
62 | $got = read $body, my ($buffer), $CHUNKSIZE; |
3a64ecc9 |
63 | $got = 0 unless $self->write( $c, $buffer ); |
be1c9503 |
64 | } while $got > 0; |
65 | |
7257e9db |
66 | close $body; |
f4a57de4 |
67 | } |
68 | else { |
7257e9db |
69 | $self->write( $c, $body ); |
f4a57de4 |
70 | } |
ca3023fc |
71 | |
faa02805 |
72 | my $res = $c->response; |
73 | $res->_writer->close; |
74 | $res->_clear_writer; |
030674d0 |
75 | |
ca3023fc |
76 | return; |
fbcc39ad |
77 | } |
6dc87a0f |
78 | |
b5ecfcf0 |
79 | =head2 $self->finalize_cookies($c) |
6dc87a0f |
80 | |
fa32ac82 |
81 | Create CGI::Simple::Cookie objects from $c->res->cookies, and set them as |
82 | response headers. |
4ab87e27 |
83 | |
6dc87a0f |
84 | =cut |
85 | |
86 | sub finalize_cookies { |
fbcc39ad |
87 | my ( $self, $c ) = @_; |
6dc87a0f |
88 | |
fbcc39ad |
89 | my @cookies; |
7fa2c9c1 |
90 | my $response = $c->response; |
c82ed742 |
91 | |
91772de9 |
92 | foreach my $name (keys %{ $response->cookies }) { |
93 | |
94 | my $val = $response->cookies->{$name}; |
fbcc39ad |
95 | |
2832cb5d |
96 | my $cookie = ( |
7e95ba12 |
97 | blessed($val) |
2832cb5d |
98 | ? $val |
99 | : CGI::Simple::Cookie->new( |
100 | -name => $name, |
101 | -value => $val->{value}, |
102 | -expires => $val->{expires}, |
103 | -domain => $val->{domain}, |
104 | -path => $val->{path}, |
b21bc468 |
105 | -secure => $val->{secure} || 0, |
106 | -httponly => $val->{httponly} || 0, |
2832cb5d |
107 | ) |
6dc87a0f |
108 | ); |
0f12bef2 |
109 | if (!defined $cookie) { |
110 | $c->log->warn("undef passed in '$name' cookie value - not setting cookie") |
111 | if $c->debug; |
112 | next; |
113 | } |
6dc87a0f |
114 | |
fbcc39ad |
115 | push @cookies, $cookie->as_string; |
6dc87a0f |
116 | } |
6dc87a0f |
117 | |
b39840da |
118 | for my $cookie (@cookies) { |
7fa2c9c1 |
119 | $response->headers->push_header( 'Set-Cookie' => $cookie ); |
fbcc39ad |
120 | } |
121 | } |
969647fd |
122 | |
b5ecfcf0 |
123 | =head2 $self->finalize_error($c) |
969647fd |
124 | |
6e5b548e |
125 | Output an appropriate error message. Called if there's an error in $c |
4ab87e27 |
126 | after the dispatch has finished. Will output debug messages if Catalyst |
127 | is in debug mode, or a `please come back later` message otherwise. |
128 | |
969647fd |
129 | =cut |
130 | |
c96cdcef |
131 | sub _dump_error_page_element { |
132 | my ($self, $i, $element) = @_; |
133 | my ($name, $val) = @{ $element }; |
134 | |
135 | # This is fugly, but the metaclass is _HUGE_ and demands waaay too much |
136 | # scrolling. Suggestions for more pleasant ways to do this welcome. |
137 | local $val->{'__MOP__'} = "Stringified: " |
1565e158 |
138 | . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'}; |
c96cdcef |
139 | |
140 | my $text = encode_entities( dump( $val )); |
141 | sprintf <<"EOF", $name, $text; |
142 | <h2><a href="#" onclick="toggleDump('dump_$i'); return false">%s</a></h2> |
143 | <div id="dump_$i"> |
144 | <pre wrap="">%s</pre> |
145 | </div> |
146 | EOF |
147 | } |
148 | |
969647fd |
149 | sub finalize_error { |
fbcc39ad |
150 | my ( $self, $c ) = @_; |
969647fd |
151 | |
7299a7b4 |
152 | $c->res->content_type('text/html; charset=utf-8'); |
df960201 |
153 | my $name = ref($c)->config->{name} || join(' ', split('::', ref $c)); |
361ba9b2 |
154 | |
155 | # Prevent Catalyst::Plugin::Unicode::Encoding from running. |
156 | # This is a little nasty, but it's the best way to be clean whether or |
157 | # not the user has an encoding plugin. |
158 | |
159 | if ($c->can('encoding')) { |
160 | $c->{encoding} = ''; |
161 | } |
969647fd |
162 | |
163 | my ( $title, $error, $infos ); |
164 | if ( $c->debug ) { |
62d9b030 |
165 | |
166 | # For pretty dumps |
b5ecfcf0 |
167 | $error = join '', map { |
168 | '<p><code class="error">' |
169 | . encode_entities($_) |
170 | . '</code></p>' |
171 | } @{ $c->error }; |
969647fd |
172 | $error ||= 'No output'; |
2666dd3b |
173 | $error = qq{<pre wrap="">$error</pre>}; |
969647fd |
174 | $title = $name = "$name on Catalyst $Catalyst::VERSION"; |
d82cc9ae |
175 | $name = "<h1>$name</h1>"; |
fbcc39ad |
176 | |
177 | # Don't show context in the dump |
02570318 |
178 | $c->req->_clear_context; |
179 | $c->res->_clear_context; |
fbcc39ad |
180 | |
181 | # Don't show body parser in the dump |
0f56bbcf |
182 | $c->req->_clear_body; |
fbcc39ad |
183 | |
c6ef5e69 |
184 | my @infos; |
185 | my $i = 0; |
c6ef5e69 |
186 | for my $dump ( $c->dump_these ) { |
c96cdcef |
187 | push @infos, $self->_dump_error_page_element($i, $dump); |
c6ef5e69 |
188 | $i++; |
189 | } |
190 | $infos = join "\n", @infos; |
969647fd |
191 | } |
192 | else { |
193 | $title = $name; |
194 | $error = ''; |
195 | $infos = <<""; |
196 | <pre> |
197 | (en) Please come back later |
0c2b4ac0 |
198 | (fr) SVP veuillez revenir plus tard |
969647fd |
199 | (de) Bitte versuchen sie es spaeter nocheinmal |
d82cc9ae |
200 | (at) Konnten's bitt'schoen spaeter nochmal reinschauen |
969647fd |
201 | (no) Vennligst prov igjen senere |
d82cc9ae |
202 | (dk) Venligst prov igen senere |
203 | (pl) Prosze sprobowac pozniej |
2f381252 |
204 | (pt) Por favor volte mais tarde |
b31c0f2e |
205 | (ru) Попробуйте еще раз позже |
206 | (ua) Спробуйте ще раз пізніше |
969647fd |
207 | </pre> |
208 | |
209 | $name = ''; |
210 | } |
e060fe05 |
211 | $c->res->body( <<"" ); |
7299a7b4 |
212 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
213 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
214 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
969647fd |
215 | <head> |
7299a7b4 |
216 | <meta http-equiv="Content-Language" content="en" /> |
217 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
969647fd |
218 | <title>$title</title> |
7299a7b4 |
219 | <script type="text/javascript"> |
c6ef5e69 |
220 | <!-- |
221 | function toggleDump (dumpElement) { |
7299a7b4 |
222 | var e = document.getElementById( dumpElement ); |
223 | if (e.style.display == "none") { |
224 | e.style.display = ""; |
c6ef5e69 |
225 | } |
226 | else { |
7299a7b4 |
227 | e.style.display = "none"; |
c6ef5e69 |
228 | } |
229 | } |
230 | --> |
231 | </script> |
969647fd |
232 | <style type="text/css"> |
233 | body { |
234 | font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana, |
235 | Tahoma, Arial, helvetica, sans-serif; |
34d28dfd |
236 | color: #333; |
969647fd |
237 | background-color: #eee; |
238 | margin: 0px; |
239 | padding: 0px; |
240 | } |
c6ef5e69 |
241 | :link, :link:hover, :visited, :visited:hover { |
34d28dfd |
242 | color: #000; |
c6ef5e69 |
243 | } |
969647fd |
244 | div.box { |
9619f23c |
245 | position: relative; |
969647fd |
246 | background-color: #ccc; |
247 | border: 1px solid #aaa; |
248 | padding: 4px; |
249 | margin: 10px; |
969647fd |
250 | } |
251 | div.error { |
34d28dfd |
252 | background-color: #cce; |
969647fd |
253 | border: 1px solid #755; |
254 | padding: 8px; |
255 | margin: 4px; |
256 | margin-bottom: 10px; |
969647fd |
257 | } |
258 | div.infos { |
34d28dfd |
259 | background-color: #eee; |
969647fd |
260 | border: 1px solid #575; |
261 | padding: 8px; |
262 | margin: 4px; |
263 | margin-bottom: 10px; |
969647fd |
264 | } |
265 | div.name { |
34d28dfd |
266 | background-color: #cce; |
969647fd |
267 | border: 1px solid #557; |
268 | padding: 8px; |
269 | margin: 4px; |
969647fd |
270 | } |
7f8e0078 |
271 | code.error { |
272 | display: block; |
273 | margin: 1em 0; |
274 | overflow: auto; |
7f8e0078 |
275 | } |
9619f23c |
276 | div.name h1, div.error p { |
277 | margin: 0; |
278 | } |
279 | h2 { |
280 | margin-top: 0; |
281 | margin-bottom: 10px; |
282 | font-size: medium; |
283 | font-weight: bold; |
284 | text-decoration: underline; |
285 | } |
286 | h1 { |
287 | font-size: medium; |
288 | font-weight: normal; |
289 | } |
2666dd3b |
290 | /* from http://users.tkk.fi/~tkarvine/linux/doc/pre-wrap/pre-wrap-css3-mozilla-opera-ie.html */ |
291 | /* Browser specific (not valid) styles to make preformatted text wrap */ |
b0ad47c1 |
292 | pre { |
2666dd3b |
293 | white-space: pre-wrap; /* css-3 */ |
294 | white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ |
295 | white-space: -pre-wrap; /* Opera 4-6 */ |
296 | white-space: -o-pre-wrap; /* Opera 7 */ |
297 | word-wrap: break-word; /* Internet Explorer 5.5+ */ |
298 | } |
969647fd |
299 | </style> |
300 | </head> |
301 | <body> |
302 | <div class="box"> |
303 | <div class="error">$error</div> |
304 | <div class="infos">$infos</div> |
305 | <div class="name">$name</div> |
306 | </div> |
307 | </body> |
308 | </html> |
309 | |
4b66aa19 |
310 | # Trick IE. Old versions of IE would display their own error page instead |
311 | # of ours if we'd give it less than 512 bytes. |
d82cc9ae |
312 | $c->res->{body} .= ( ' ' x 512 ); |
313 | |
361ba9b2 |
314 | $c->res->{body} = Encode::encode("UTF-8", $c->res->{body}); |
315 | |
d82cc9ae |
316 | # Return 500 |
33117422 |
317 | $c->res->status(500); |
969647fd |
318 | } |
319 | |
b5ecfcf0 |
320 | =head2 $self->finalize_headers($c) |
fc7ec1d9 |
321 | |
4ab87e27 |
322 | Abstract method, allows engines to write headers to response |
323 | |
fc7ec1d9 |
324 | =cut |
325 | |
44d28c7d |
326 | sub finalize_headers { |
327 | my ($self, $ctx) = @_; |
328 | |
3ed5a3fb |
329 | # This is a less-than-pretty hack to avoid breaking the old |
330 | # Catalyst::Engine::PSGI. 5.9 Catalyst::Engine sets a response_cb and |
331 | # expects us to pass headers to it here, whereas Catalyst::Enngine::PSGI |
332 | # just pulls the headers out of $ctx->response in its run method and never |
333 | # sets response_cb. So take the lack of a response_cb as a sign that we |
334 | # don't need to set the headers. |
335 | |
faa02805 |
336 | return unless ($ctx->response->_has_response_cb); |
3ed5a3fb |
337 | |
44d28c7d |
338 | my @headers; |
339 | $ctx->response->headers->scan(sub { push @headers, @_ }); |
340 | |
faa02805 |
341 | my $writer = $ctx->response->_response_cb->([ $ctx->response->status, \@headers ]); |
342 | $ctx->response->_set_writer($writer); |
343 | $ctx->response->_clear_response_cb; |
44d28c7d |
344 | |
345 | return; |
346 | } |
fc7ec1d9 |
347 | |
b5ecfcf0 |
348 | =head2 $self->finalize_uploads($c) |
fc7ec1d9 |
349 | |
4ab87e27 |
350 | Clean up after uploads, deleting temp files. |
351 | |
fc7ec1d9 |
352 | =cut |
353 | |
fbcc39ad |
354 | sub finalize_uploads { |
355 | my ( $self, $c ) = @_; |
99fe1710 |
356 | |
671123ba |
357 | # N.B. This code is theoretically entirely unneeded due to ->cleanup(1) |
358 | # on the HTTP::Body object. |
7fa2c9c1 |
359 | my $request = $c->request; |
91772de9 |
360 | foreach my $key (keys %{ $request->uploads }) { |
361 | my $upload = $request->uploads->{$key}; |
7fa2c9c1 |
362 | unlink grep { -e $_ } map { $_->tempname } |
363 | (ref $upload eq 'ARRAY' ? @{$upload} : ($upload)); |
c85ff642 |
364 | } |
7fa2c9c1 |
365 | |
fc7ec1d9 |
366 | } |
367 | |
b5ecfcf0 |
368 | =head2 $self->prepare_body($c) |
fc7ec1d9 |
369 | |
4ab87e27 |
370 | sets up the L<Catalyst::Request> object body using L<HTTP::Body> |
371 | |
fc7ec1d9 |
372 | =cut |
373 | |
fbcc39ad |
374 | sub prepare_body { |
375 | my ( $self, $c ) = @_; |
99fe1710 |
376 | |
398f13db |
377 | $c->request->prepare_body; |
fc7ec1d9 |
378 | } |
379 | |
b5ecfcf0 |
380 | =head2 $self->prepare_body_chunk($c) |
4bd82c41 |
381 | |
4ab87e27 |
382 | Add a chunk to the request body. |
383 | |
4bd82c41 |
384 | =cut |
385 | |
398f13db |
386 | # XXX - Can this be deleted? |
4bd82c41 |
387 | sub prepare_body_chunk { |
388 | my ( $self, $c, $chunk ) = @_; |
4f5ebacd |
389 | |
398f13db |
390 | $c->request->prepare_body_chunk($chunk); |
4bd82c41 |
391 | } |
392 | |
b5ecfcf0 |
393 | =head2 $self->prepare_body_parameters($c) |
06e1b616 |
394 | |
b0ad47c1 |
395 | Sets up parameters from body. |
4ab87e27 |
396 | |
06e1b616 |
397 | =cut |
398 | |
fbcc39ad |
399 | sub prepare_body_parameters { |
400 | my ( $self, $c ) = @_; |
b0ad47c1 |
401 | |
398f13db |
402 | $c->request->prepare_body_parameters; |
fbcc39ad |
403 | } |
0556eb49 |
404 | |
b5ecfcf0 |
405 | =head2 $self->prepare_cookies($c) |
fc7ec1d9 |
406 | |
fa32ac82 |
407 | Parse cookies from header. Sets a L<CGI::Simple::Cookie> object. |
4ab87e27 |
408 | |
fc7ec1d9 |
409 | =cut |
410 | |
6dc87a0f |
411 | sub prepare_cookies { |
fbcc39ad |
412 | my ( $self, $c ) = @_; |
6dc87a0f |
413 | |
414 | if ( my $header = $c->request->header('Cookie') ) { |
fa32ac82 |
415 | $c->req->cookies( { CGI::Simple::Cookie->parse($header) } ); |
6dc87a0f |
416 | } |
417 | } |
fc7ec1d9 |
418 | |
b5ecfcf0 |
419 | =head2 $self->prepare_headers($c) |
fc7ec1d9 |
420 | |
421 | =cut |
422 | |
44d28c7d |
423 | sub prepare_headers { |
424 | my ($self, $ctx) = @_; |
425 | |
faa02805 |
426 | my $env = $ctx->request->env; |
44d28c7d |
427 | my $headers = $ctx->request->headers; |
428 | |
429 | for my $header (keys %{ $env }) { |
430 | next unless $header =~ /^(HTTP|CONTENT|COOKIE)/i; |
431 | (my $field = $header) =~ s/^HTTPS?_//; |
432 | $field =~ tr/_/-/; |
433 | $headers->header($field => $env->{$header}); |
434 | } |
435 | } |
fc7ec1d9 |
436 | |
b5ecfcf0 |
437 | =head2 $self->prepare_parameters($c) |
fc7ec1d9 |
438 | |
4ab87e27 |
439 | sets up parameters from query and post parameters. |
440 | |
fc7ec1d9 |
441 | =cut |
442 | |
fbcc39ad |
443 | sub prepare_parameters { |
444 | my ( $self, $c ) = @_; |
fc7ec1d9 |
445 | |
1cbdfa9b |
446 | $c->request->parameters; |
fbcc39ad |
447 | } |
448 | |
b5ecfcf0 |
449 | =head2 $self->prepare_path($c) |
fc7ec1d9 |
450 | |
4ab87e27 |
451 | abstract method, implemented by engines. |
452 | |
fc7ec1d9 |
453 | =cut |
454 | |
44d28c7d |
455 | sub prepare_path { |
456 | my ($self, $ctx) = @_; |
457 | |
faa02805 |
458 | my $env = $ctx->request->env; |
44d28c7d |
459 | |
460 | my $scheme = $ctx->request->secure ? 'https' : 'http'; |
461 | my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME}; |
462 | my $port = $env->{SERVER_PORT} || 80; |
463 | my $base_path = $env->{SCRIPT_NAME} || "/"; |
464 | |
465 | # set the request URI |
661de072 |
466 | my $path; |
467 | if (!$ctx->config->{use_request_uri_for_path}) { |
4904ee27 |
468 | my $path_info = $env->{PATH_INFO}; |
469 | if ( exists $env->{REDIRECT_URL} ) { |
470 | $base_path = $env->{REDIRECT_URL}; |
471 | $base_path =~ s/\Q$path_info\E$//; |
472 | } |
473 | $path = $base_path . $path_info; |
661de072 |
474 | $path =~ s{^/+}{}; |
475 | $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go; |
476 | $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE |
477 | } |
478 | else { |
479 | my $req_uri = $env->{REQUEST_URI}; |
480 | $req_uri =~ s/\?.*$//; |
481 | $path = $req_uri; |
482 | $path =~ s{^/+}{}; |
483 | } |
44d28c7d |
484 | |
485 | # Using URI directly is way too slow, so we construct the URLs manually |
486 | my $uri_class = "URI::$scheme"; |
487 | |
488 | # HTTP_HOST will include the port even if it's 80/443 |
489 | $host =~ s/:(?:80|443)$//; |
490 | |
491 | if ($port !~ /^(?:80|443)$/ && $host !~ /:/) { |
492 | $host .= ":$port"; |
493 | } |
494 | |
44d28c7d |
495 | my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : ''; |
496 | my $uri = $scheme . '://' . $host . '/' . $path . $query; |
497 | |
4ee03d72 |
498 | $ctx->request->uri( (bless \$uri, $uri_class)->canonical ); |
44d28c7d |
499 | |
500 | # set the base URI |
501 | # base must end in a slash |
502 | $base_path .= '/' unless $base_path =~ m{/$}; |
503 | |
504 | my $base_uri = $scheme . '://' . $host . $base_path; |
505 | |
506 | $ctx->request->base( bless \$base_uri, $uri_class ); |
507 | |
508 | return; |
509 | } |
fc7ec1d9 |
510 | |
b5ecfcf0 |
511 | =head2 $self->prepare_request($c) |
fc7ec1d9 |
512 | |
b5ecfcf0 |
513 | =head2 $self->prepare_query_parameters($c) |
fc7ec1d9 |
514 | |
4ab87e27 |
515 | process the query string and extract query parameters. |
516 | |
fc7ec1d9 |
517 | =cut |
518 | |
e0616220 |
519 | sub prepare_query_parameters { |
44d28c7d |
520 | my ($self, $c) = @_; |
521 | |
faa02805 |
522 | my $env = $c->request->env; |
523 | my $query_string = exists $env->{QUERY_STRING} |
524 | ? $env->{QUERY_STRING} |
44d28c7d |
525 | : ''; |
b0ad47c1 |
526 | |
3b4d1251 |
527 | # Check for keywords (no = signs) |
528 | # (yes, index() is faster than a regex :)) |
933ba403 |
529 | if ( index( $query_string, '=' ) < 0 ) { |
3b4d1251 |
530 | $c->request->query_keywords( $self->unescape_uri($query_string) ); |
933ba403 |
531 | return; |
532 | } |
533 | |
534 | my %query; |
e0616220 |
535 | |
536 | # replace semi-colons |
537 | $query_string =~ s/;/&/g; |
b0ad47c1 |
538 | |
2f381252 |
539 | my @params = grep { length $_ } split /&/, $query_string; |
e0616220 |
540 | |
933ba403 |
541 | for my $item ( @params ) { |
b0ad47c1 |
542 | |
543 | my ($param, $value) |
933ba403 |
544 | = map { $self->unescape_uri($_) } |
e5542b70 |
545 | split( /=/, $item, 2 ); |
b0ad47c1 |
546 | |
933ba403 |
547 | $param = $self->unescape_uri($item) unless defined $param; |
b0ad47c1 |
548 | |
933ba403 |
549 | if ( exists $query{$param} ) { |
550 | if ( ref $query{$param} ) { |
551 | push @{ $query{$param} }, $value; |
552 | } |
553 | else { |
554 | $query{$param} = [ $query{$param}, $value ]; |
555 | } |
556 | } |
557 | else { |
558 | $query{$param} = $value; |
559 | } |
e0616220 |
560 | } |
933ba403 |
561 | $c->request->query_parameters( \%query ); |
e0616220 |
562 | } |
fbcc39ad |
563 | |
b5ecfcf0 |
564 | =head2 $self->prepare_read($c) |
fbcc39ad |
565 | |
4ab87e27 |
566 | prepare to read from the engine. |
567 | |
fbcc39ad |
568 | =cut |
fc7ec1d9 |
569 | |
fbcc39ad |
570 | sub prepare_read { |
571 | my ( $self, $c ) = @_; |
4f5ebacd |
572 | |
878b821c |
573 | # Initialize the amount of data we think we need to read |
faa02805 |
574 | $c->request->_read_length; |
fbcc39ad |
575 | } |
fc7ec1d9 |
576 | |
b5ecfcf0 |
577 | =head2 $self->prepare_request(@arguments) |
fc7ec1d9 |
578 | |
4ab87e27 |
579 | Populate the context object from the request object. |
580 | |
fc7ec1d9 |
581 | =cut |
582 | |
44d28c7d |
583 | sub prepare_request { |
584 | my ($self, $ctx, %args) = @_; |
faa02805 |
585 | $ctx->request->_set_env($args{env}); |
586 | $self->_set_env($args{env}); # Nasty back compat! |
587 | $ctx->response->_set_response_cb($args{response_cb}); |
44d28c7d |
588 | } |
fc7ec1d9 |
589 | |
b5ecfcf0 |
590 | =head2 $self->prepare_uploads($c) |
c9afa5fc |
591 | |
fbcc39ad |
592 | =cut |
593 | |
594 | sub prepare_uploads { |
595 | my ( $self, $c ) = @_; |
7fa2c9c1 |
596 | |
597 | my $request = $c->request; |
0f56bbcf |
598 | return unless $request->_body; |
7fa2c9c1 |
599 | |
0f56bbcf |
600 | my $uploads = $request->_body->upload; |
7fa2c9c1 |
601 | my $parameters = $request->parameters; |
91772de9 |
602 | foreach my $name (keys %$uploads) { |
603 | my $files = $uploads->{$name}; |
fbcc39ad |
604 | my @uploads; |
7fa2c9c1 |
605 | for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) { |
606 | my $headers = HTTP::Headers->new( %{ $upload->{headers} } ); |
607 | my $u = Catalyst::Request::Upload->new |
608 | ( |
609 | size => $upload->{size}, |
a160c98d |
610 | type => scalar $headers->content_type, |
7fa2c9c1 |
611 | headers => $headers, |
612 | tempname => $upload->{tempname}, |
613 | filename => $upload->{filename}, |
614 | ); |
fbcc39ad |
615 | push @uploads, $u; |
616 | } |
7fa2c9c1 |
617 | $request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0]; |
f4a57de4 |
618 | |
c4bed79a |
619 | # support access to the filename as a normal param |
620 | my @filenames = map { $_->{filename} } @uploads; |
a7e05d9d |
621 | # append, if there's already params with this name |
7fa2c9c1 |
622 | if (exists $parameters->{$name}) { |
623 | if (ref $parameters->{$name} eq 'ARRAY') { |
624 | push @{ $parameters->{$name} }, @filenames; |
a7e05d9d |
625 | } |
626 | else { |
7fa2c9c1 |
627 | $parameters->{$name} = [ $parameters->{$name}, @filenames ]; |
a7e05d9d |
628 | } |
629 | } |
630 | else { |
7fa2c9c1 |
631 | $parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0]; |
a7e05d9d |
632 | } |
fbcc39ad |
633 | } |
634 | } |
635 | |
b5ecfcf0 |
636 | =head2 $self->read($c, [$maxlength]) |
fbcc39ad |
637 | |
ea72fece |
638 | Reads from the input stream by calling C<< $self->read_chunk >>. |
639 | |
640 | Maintains the read_length and read_position counters as data is read. |
641 | |
fbcc39ad |
642 | =cut |
643 | |
644 | sub read { |
645 | my ( $self, $c, $maxlength ) = @_; |
4f5ebacd |
646 | |
f083854e |
647 | $c->request->read($maxlength); |
fbcc39ad |
648 | } |
fc7ec1d9 |
649 | |
87f50436 |
650 | =head2 $self->read_chunk($c, \$buffer, $length) |
23f9d934 |
651 | |
10011c19 |
652 | Each engine implements read_chunk as its preferred way of reading a chunk |
ea72fece |
653 | of data. Returns the number of bytes read. A return of 0 indicates that |
654 | there is no more data to be read. |
fc7ec1d9 |
655 | |
fbcc39ad |
656 | =cut |
61b1e958 |
657 | |
e6b46d80 |
658 | sub read_chunk { |
ce7abbda |
659 | my ($self, $ctx) = (shift, shift); |
87f50436 |
660 | return $ctx->request->read_chunk(@_); |
e6b46d80 |
661 | } |
61b1e958 |
662 | |
b5ecfcf0 |
663 | =head2 $self->read_length |
ca39d576 |
664 | |
fbcc39ad |
665 | The length of input data to be read. This is obtained from the Content-Length |
666 | header. |
fc7ec1d9 |
667 | |
b5ecfcf0 |
668 | =head2 $self->read_position |
fc7ec1d9 |
669 | |
fbcc39ad |
670 | The amount of input data that has already been read. |
63b763c5 |
671 | |
9560b708 |
672 | =head2 $self->run($app, $server) |
63b763c5 |
673 | |
9560b708 |
674 | Start the engine. Builds a PSGI application and calls the |
acbecf08 |
675 | run method on the server passed in, which then causes the |
676 | engine to loop, handling requests.. |
4ab87e27 |
677 | |
fbcc39ad |
678 | =cut |
fc7ec1d9 |
679 | |
44d28c7d |
680 | sub run { |
51857616 |
681 | my ($self, $app, $psgi, @args) = @_; |
acbecf08 |
682 | # @args left here rather than just a $options, $server for back compat with the |
683 | # old style scripts which send a few args, then a hashref |
684 | |
685 | # They should never actually be used in the normal case as the Plack engine is |
686 | # passed in got all the 'standard' args via the loader in the script already. |
687 | |
688 | # FIXME - we should stash the options in an attribute so that custom args |
689 | # like Gitalist's --git_dir are possible to get from the app without stupid tricks. |
1e5dad00 |
690 | my $server = pop @args if (scalar @args && blessed $args[-1]); |
691 | my $options = pop @args if (scalar @args && ref($args[-1]) eq 'HASH'); |
ccb13b15 |
692 | # Back compat hack for applications with old (non Catalyst::Script) scripts to work in FCGI. |
693 | if (scalar @args && !ref($args[0])) { |
694 | if (my $listen = shift @args) { |
695 | $options->{listen} ||= [$listen]; |
696 | } |
697 | } |
acbecf08 |
698 | if (! $server ) { |
f7a3f8fd |
699 | $server = Catalyst::EngineLoader->new(application_name => ref($self))->auto(%$options); |
f7f55b2f |
700 | # We're not being called from a script, so auto detect what backend to |
701 | # run on. This should never happen, as mod_perl never calls ->run, |
702 | # instead the $app->handle method is called per request. |
acbecf08 |
703 | $app->log->warn("Not supplied a Plack engine, falling back to engine auto-loader (are your scripts ancient?)") |
704 | } |
aee7cdcc |
705 | $app->run_options($options); |
acbecf08 |
706 | $server->run($psgi, $options); |
a1791811 |
707 | } |
44d28c7d |
708 | |
9560b708 |
709 | =head2 build_psgi_app ($app, @args) |
710 | |
711 | Builds and returns a PSGI application closure, wrapping it in the reverse proxy |
712 | middleware if the using_frontend_proxy config setting is set. |
713 | |
714 | =cut |
715 | |
22a5833d |
716 | sub build_psgi_app { |
a1791811 |
717 | my ($self, $app, @args) = @_; |
c2f4a965 |
718 | |
fcffcb05 |
719 | return sub { |
44d28c7d |
720 | my ($env) = @_; |
721 | |
722 | return sub { |
723 | my ($respond) = @_; |
faa02805 |
724 | $app->handle_request(env => $env, response_cb => $respond); |
44d28c7d |
725 | }; |
726 | }; |
727 | } |
fc7ec1d9 |
728 | |
b5ecfcf0 |
729 | =head2 $self->write($c, $buffer) |
fc7ec1d9 |
730 | |
e512dd24 |
731 | Writes the buffer to the client. |
4ab87e27 |
732 | |
fc7ec1d9 |
733 | =cut |
734 | |
fbcc39ad |
735 | sub write { |
736 | my ( $self, $c, $buffer ) = @_; |
4f5ebacd |
737 | |
faa02805 |
738 | my $response = $c->response; |
b0ad47c1 |
739 | |
681086e7 |
740 | $buffer = q[] unless defined $buffer; |
b0ad47c1 |
741 | |
44d28c7d |
742 | my $len = length($buffer); |
faa02805 |
743 | $c->res->_writer->write($buffer); |
b0ad47c1 |
744 | |
44d28c7d |
745 | return $len; |
fc7ec1d9 |
746 | } |
747 | |
933ba403 |
748 | =head2 $self->unescape_uri($uri) |
749 | |
6a44fe01 |
750 | Unescapes a given URI using the most efficient method available. Engines such |
751 | as Apache may implement this using Apache's C-based modules, for example. |
933ba403 |
752 | |
753 | =cut |
754 | |
755 | sub unescape_uri { |
8c7d83e1 |
756 | my ( $self, $str ) = @_; |
7d22a537 |
757 | |
758 | $str =~ s/(?:%([0-9A-Fa-f]{2})|\+)/defined $1 ? chr(hex($1)) : ' '/eg; |
759 | |
8c7d83e1 |
760 | return $str; |
933ba403 |
761 | } |
34d28dfd |
762 | |
4ab87e27 |
763 | =head2 $self->finalize_output |
764 | |
765 | <obsolete>, see finalize_body |
766 | |
0c76ec45 |
767 | =head2 $self->env |
768 | |
6356febf |
769 | Hash containing environment variables including many special variables inserted |
0c76ec45 |
770 | by WWW server - like SERVER_*, REMOTE_*, HTTP_* ... |
771 | |
6356febf |
772 | Before accessing environment variables consider whether the same information is |
0c76ec45 |
773 | not directly available via Catalyst objects $c->request, $c->engine ... |
774 | |
6356febf |
775 | BEWARE: If you really need to access some environment variable from your Catalyst |
0c76ec45 |
776 | application you should use $c->engine->env->{VARNAME} instead of $ENV{VARNAME}, |
ae7da8f5 |
777 | as in some environments the %ENV hash does not contain what you would expect. |
0c76ec45 |
778 | |
fbcc39ad |
779 | =head1 AUTHORS |
780 | |
2f381252 |
781 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
782 | |
783 | =head1 COPYRIGHT |
784 | |
536bee89 |
785 | This library is free software. You can redistribute it and/or modify it under |
fc7ec1d9 |
786 | the same terms as Perl itself. |
787 | |
788 | =cut |
789 | |
790 | 1; |