lib/Catalyst/Engine.pm

filter: Statementstmt 85.3% 239 / 280 Branchbran 70.0% 77 / 110 Conditioncond 50.0% 21 / 42 MC/DCmcdc 16.0% 4 / 25 Subroutinesub 80.4% 33 / 41 Podpod 95.4% 21 / 22 totaltotal 75.9% 395 / 520 CC 79 SCAR 25.9
CC
79
Cov
78%
finalize_body
41.8
run
27.8
__ANON__
12.0
CRAP 13.4
« lib/Catalyst/Utils.pm ↑ Summary lib/Catalyst.pm »
1 package Catalyst::Engine;
2  
3 156 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 156 with 'MooseX::Emulate::Class::Accessor::Fast';
5  
6 156 ▶use CGI::Simple::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 156 ▶use Data::Dump qw/dump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 156 ▶use Errno 'EWOULDBLOCK';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 156 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 156 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 156 ▶use Plack::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 156 ▶use Catalyst::EngineLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 156 ▶use Encode 2.21 'decode_utf8', 'encode', 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 156 ▶use Plack::Request::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 156 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 156 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 156 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Amount of data to read from input on each pass
20 156 our $CHUNKSIZE = 64 * 1024;
21  
22 # XXX - this is only here for compat, do not use!
23 156 has env => ( is => 'rw', writer => '_set_env' , weak_ref=>1);
24 156 my $WARN_ABOUT_ENV = 0;
25 0 ▶around env => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
26 0 my ($orig, $self, @args) = @_;
27 0 ▶ if(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
28 0 ▶ warn "env as a writer is deprecated, you probably need to upgrade Catalyst::Engine::PSGI"
Branch: 1 branch
BranchTrueFalseTotal
unless $WARN_ABOUT_ENV++ 0 0 0
29 unless $WARN_ABOUT_ENV++;
30 0 return $self->_set_env(@args);
31 }
32 0 return $self->$orig;
33 156 };
34  
35 # XXX - Only here for Engine::PSGI compat
36 0 ▶sub prepare_connection {
Subroutine prepare_connection: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($self, $ctx) = @_;
38 0 $ctx->request->prepare_connection;
39 }
40  
41 =head1 NAME
42  
43 Catalyst::Engine - The Catalyst Engine
44  
45 =head1 SYNOPSIS
46  
47 See L<Catalyst>.
48  
49 =head1 DESCRIPTION
50  
51 =head1 METHODS
52  
53  
54 =head2 $self->finalize_body($c)
55  
56 Finalize body. Prints the response output as blocking stream if it looks like
57 a filehandle, otherwise write it out all in one go. If there is no body in
58 the response, we assume you are handling it 'manually', such as for nonblocking
59 style or asynchronous streaming responses. You do this by calling L</write>
60 several times (which sends HTTP headers if needed) or you close over
61 C<< $response->write_fh >>.
62  
63 See L<Catalyst::Response/write> and L<Catalyst::Response/write_fh> for more.
64  
65 =cut
66  
67 929 ▶sub finalize_body {
Subroutine finalize_body: called CC=18 SCAR=37.3
Pod documented
68 929 my ( $self, $c ) = @_;
69 929 my $res = $c->response; # We use this all over
70  
71 ## If we've asked for the write 'filehandle' that means the application is
72 ## doing something custom and is expected to close the response
73 929 ▶ return if $res->_has_write_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $res->_has_write_fh 5 924 929
74  
75 924 my $body = $res->body; # save some typing
76 924 ▶ if($res->_has_response_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->_has_response_cb) { } 912 12 924
77 ## we have not called the response callback yet, so we are safe to send
78 ## the whole body to PSGI
79  
80 912 my @headers;
81 912 ▶ $res->headers->scan(sub { push @headers, @_ });
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 # We need to figure out what kind of body we have and normalize it to something
84 # PSGI can deal with
85 912 ▶ if(defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 870 42 912
86 # Handle objects first
87 870 ▶ if(blessed($body)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($body)) { } 12 858 870
elsif (ref $body) { } 6 852 858
88 12 ▶ if($body->can('getline')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body->can('getline')) { } 1 11 12
elsif ($body->can('read')) { } 0 11 11
89 # Body is an IO handle that meets the PSGI spec. Nothing to normalize
90 } elsif($body->can('read')) {
91  
92 # In the past, Catalyst only looked for ->read not ->getline. It is very possible
93 # that one might have an object that respected read but did not have getline.
94 # As a result, we need to handle this case for backcompat.
95  
96 # We will just do the old loop for now. In a future version of Catalyst this support
97 # will be removed and one will have to rewrite their custom object or use
98 # Plack::Middleware::AdaptFilehandleRead. In anycase support for this is officially
99 # deprecated and described as such as of 5.90060
100  
101 0 my $got;
102 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$got > 0 and do { do { $got = CORE::read($body, my $buffer, $Catalyst::Engine::CHUNKSIZE); $got = 0 unless $self->write($c, $buffer) } } 0 0 0
103 0 $got = read $body, my ($buffer), $CHUNKSIZE;
104 0 ▶ $got = 0 unless $self->write($c, $buffer );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->write($c, $buffer) 0 0 0
105 } while $got > 0;
106  
107 0 close $body;
108 0 return;
109 } else {
110 # Looks like for backcompat reasons we need to be able to deal
111 # with stringyfiable objects.
112 11 $body = ["$body"];
113 }
114 } elsif(ref $body) {
115 6 ▶ if( (ref($body) eq 'GLOB') or (ref($body) eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'GLOB' or ref $body eq 'ARRAY') { } 6 0 6
Condition33%1/3
ref $body eq "GLOB" or ref $body eq "ARRAY"
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $body eq "GLOB" or ref $body eq "ARRAY"
ref $body eq "GLOB" ref $body eq "ARRAY"
Truth table33%1/3
A or B
A= ref $body eq "GLOB"B= ref $body eq "ARRAY"
ABresult
1X1
011
000
116 # Again, PSGI can just accept this, no transform needed. We don't officially
117 # document the body as arrayref at this time (and there's not specific test
118 # cases. we support it because it simplifies some plack compatibility logic
119 # and we might make it official at some point.
120 } else {
121 0 $c->log->error("${\ref($body)} is not a valid value for Response->body");
122 0 return;
123 }
124 } else {
125 # Body is defined and not an object or reference. We assume a simple value
126 # and wrap it in an array for PSGI
127 852 $body = [$body];
128 }
129 } else {
130 # There's no body...
131 42 $body = [];
132 }
133 912 $res->_response_cb->([ $res->status, \@headers, $body]);
134 912 $res->_clear_response_cb;
135  
136 } else {
137 ## Now, if there's no response callback anymore, that means someone has
138 ## called ->write in order to stream 'some stuff along the way'. I think
139 ## for backcompat we still need to handle a ->body. I guess I could see
140 ## someone calling ->write to presend some stuff, and then doing the rest
141 ## via ->body, like in a template.
142  
143 ## We'll just use the old, existing code for this (or most of it)
144  
145 12 ▶ if(my $body = $res->body) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $res->body) 3 9 12
146  
147 3 ▶ if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($body) and $body->can('read') or ref $body eq 'GLOB') { } 0 3 3
Condition33%2/6
builtin::blessed($body) and $body->can("read") or ref $body eq "GLOB"
l!l&&r!l&&!r
003
builtin::blessed($body) and $body->can("read")
!ll&&!rl&&r
300
MC/DC0%0/3
builtin::blessed($body) and $body->can("read") or ref $body eq "GLOB"
builtin::blessed($body) $body->can("read") ref $body eq "GLOB"
Truth table20%1/5
A and B or C
A= builtin::blessed($body)B= $body->can("read")C= ref $body eq "GLOB"
ABCresult
11X1
0X11
1011
0X00
1000
148  
149 ## In this case we have no choice and will fall back on the old
150 ## manual streaming stuff. Not optimal. This is deprecated as of 5.900560+
151  
152 0 my $got;
153 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$got > 0 and do { do { $got = CORE::read($body, my $buffer, $Catalyst::Engine::CHUNKSIZE); $got = 0 unless $self->write($c, $buffer) } } 0 0 0
154 0 $got = read $body, my ($buffer), $CHUNKSIZE;
155 0 ▶ $got = 0 unless $self->write($c, $buffer );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->write($c, $buffer) 0 0 0
156 } while $got > 0;
157  
158 0 close $body;
159 }
160 else {
161  
162 # Case where body was set after calling ->write. We'd prefer not to
163 # support this, but I can see some use cases with the way most of the
164 # views work. Since body has already been encoded, we need to do
165 # an 'unencoded_write' here.
166 3 $self->unencoded_write( $c, $body );
167 }
168 }
169  
170 12 $res->_writer->close;
171 12 $res->_clear_writer;
172 }
173  
174 924 return;
175 }
176  
177 =head2 $self->finalize_cookies($c)
178  
179 Create CGI::Simple::Cookie objects from $c->res->cookies, and set them as
180 response headers.
181  
182 =cut
183  
184 929 ▶sub finalize_cookies {
Subroutine finalize_cookies: called CC=8 SCAR=21.0
Pod documented
185 929 my ( $self, $c ) = @_;
186  
187 929 my @cookies;
188 929 my $response = $c->response;
189  
190 929 foreach my $name (keys %{ $response->cookies }) {
191  
192 8 my $val = $response->cookies->{$name};
193  
194 my $cookie = (
195 blessed($val)
196 ? $val
197 : CGI::Simple::Cookie->new(
198 -name => $name,
199 -value => $val->{value},
200 -expires => $val->{expires},
201 -domain => $val->{domain},
202 -path => $val->{path},
203 -secure => $val->{secure} || 0,
204 -httponly => $val->{httponly} || 0,
205 -samesite => $val->{samesite},
206 )
207 8 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($val) ? : 1 7 8
Condition50%2/4
$val->{'secure'} || 0
l!l
07
$val->{'httponly'} || 0
l!l
07
MC/DC0%0/1
$val->{'secure'} || 0
$val->{'secure'}
MC/DC0%0/1
$val->{'httponly'} || 0
$val->{'httponly'}
Truth table50%1/2
A
A= $val->{'secure'}
Aresult
11
00
Truth table50%1/2
A
A= $val->{'httponly'}
Aresult
11
00
208 8 ▶ if (!defined $cookie) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cookie) 1 7 8
209 1 ▶ $c->log->warn("undef passed in '$name' cookie value - not setting cookie")
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
210 if $c->debug;
211 1 next;
212 }
213  
214 7 push @cookies, $cookie->as_string;
215 }
216  
217 929 for my $cookie (@cookies) {
218 7 $response->headers->push_header( 'Set-Cookie' => $cookie );
219 }
220 }
221  
222 =head2 $self->finalize_error($c)
223  
224 Output an appropriate error message. Called if there's an error in $c
225 after the dispatch has finished. Will output debug messages if Catalyst
226 is in debug mode, or a `please come back later` message otherwise.
227  
228 =cut
229  
230 31 ▶sub _dump_error_page_element {
Subroutine _dump_error_page_element: called CC=3 SCAR=11.2
231 31 my ($self, $i, $element) = @_;
232 31 my ($name, $val) = @{ $element };
233  
234 # This is fugly, but the metaclass is _HUGE_ and demands waaay too much
235 # scrolling. Suggestions for more pleasant ways to do this welcome.
236 local $val->{'__MOP__'} = "Stringified: "
237 31 ▶ . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'};
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" and exists $val->{"__MOP__"} 0 31 31
Condition66%2/3
ref $val eq "HASH" and exists $val->{"__MOP__"}
!ll&&!rl&&r
16150
MC/DC0%0/2
ref $val eq "HASH" and exists $val->{"__MOP__"}
ref $val eq "HASH" exists $val->{"__MOP__"}
Truth table66%2/3
A and B
A= ref $val eq "HASH"B= exists $val->{"__MOP__"}
ABresult
0X0
100
111
238  
239 31 my $text = encode_entities( dump( $val ));
240 31 sprintf <<"EOF", $name, $text;
241 <h2><a href="#" onclick="toggleDump('dump_$i'); return false">%s</a></h2>
242 <div id="dump_$i">
243 <pre wrap="">%s</pre>
244 </div>
245 EOF
246 }
247  
248 34 ▶sub finalize_error {
Subroutine finalize_error: called CC=6 SCAR=17.9
Pod documented
249 34 my ( $self, $c ) = @_;
250  
251 34 $c->res->content_type('text/html; charset=utf-8');
252 34 ▶ my $name = ref($c)->config->{name} || join(' ', split('::', ref $c));
Condition66%2/3
ref($c)->config->{'name'} || join(' ', split(/::/, ref $c, 0))
l!l&&r!l&&!r
21130
MC/DC0%0/2
ref($c)->config->{'name'} || join(' ', split(/::/, ref $c, 0))
ref($c)->config->{'name'} join(' ', split(/::/, ref $c, 0))
Truth table66%2/3
A || B
A= ref($c)->config->{'name'}B= join(' ', split(/::/, ref $c, 0))
ABresult
1X1
011
000
253  
254 # Prevent Catalyst::Plugin::Unicode::Encoding from running.
255 # This is a little nasty, but it's the best way to be clean whether or
256 # not the user has an encoding plugin.
257  
258 34 ▶ if ($c->can('encoding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("encoding")) 34 0 34
259 34 $c->{encoding} = '';
260 }
261  
262 34 my ( $title, $error, $infos );
263 34 ▶ if ( $c->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) { } 7 27 34
264  
265 # For pretty dumps
266 $error = join '', map {
267 9 '<p><code class="error">'
268 . encode_entities($_)
269 . '</code></p>'
270 7 } @{ $c->error };
271 7 ▶ $error ||= 'No output';
Condition50%1/2
$error ||= "No output"
l!l
70
MC/DC0%0/1
$error ||= "No output"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
272 7 $error = qq{<pre wrap="">$error</pre>};
273 7 $title = $name = "$name on Catalyst $Catalyst::VERSION";
274 7 $name = "<h1>$name</h1>";
275  
276 # Don't show context in the dump
277 7 $c->res->_clear_context;
278  
279 # Don't show body parser in the dump
280 7 $c->req->_clear_body;
281  
282 7 my @infos;
283 7 my $i = 0;
284 7 for my $dump ( $c->dump_these ) {
285 28 push @infos, $self->_dump_error_page_element($i, $dump);
286 28 $i++;
287 }
288 7 $infos = join "\n", @infos;
289 }
290 else {
291 27 $title = $name;
292 27 $error = '';
293 27 $infos = <<"";
294 <pre>
295 (en) Please come back later
296 (fr) SVP veuillez revenir plus tard
297 (de) Bitte versuchen sie es spaeter nocheinmal
298 (at) Konnten's bitt'schoen spaeter nochmal reinschauen
299 (no) Vennligst prov igjen senere
300 (dk) Venligst prov igen senere
301 (pl) Prosze sprobowac pozniej
302 (pt) Por favor volte mais tarde
303 (ru) Попробуйте еще раз позже
304 (ua) Спробуйте ще раз пізніше
305 (it) Per favore riprova più tardi
306 (cs) Vraťte se prosím později
307 </pre>
308  
309 27 $name = '';
310 }
311 34 $c->res->body( <<"" );
312 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
313 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
314 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
315 <head>
316 <meta http-equiv="Content-Language" content="en" />
317 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
318 <title>$title</title>
319 <script type="text/javascript">
320 <!--
321 function toggleDump (dumpElement) {
322 var e = document.getElementById( dumpElement );
323 if (e.style.display == "none") {
324 e.style.display = "";
325 }
326 else {
327 e.style.display = "none";
328 }
329 }
330 -->
331 </script>
332 <style type="text/css">
333 body {
334 font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
335 Tahoma, Arial, helvetica, sans-serif;
336 color: #333;
337 background-color: #eee;
338 margin: 0px;
339 padding: 0px;
340 }
341 :link, :link:hover, :visited, :visited:hover {
342 color: #000;
343 }
344 div.box {
345 position: relative;
346 background-color: #ccc;
347 border: 1px solid #aaa;
348 padding: 4px;
349 margin: 10px;
350 }
351 div.error {
352 background-color: #cce;
353 border: 1px solid #755;
354 padding: 8px;
355 margin: 4px;
356 margin-bottom: 10px;
357 }
358 div.infos {
359 background-color: #eee;
360 border: 1px solid #575;
361 padding: 8px;
362 margin: 4px;
363 margin-bottom: 10px;
364 }
365 div.name {
366 background-color: #cce;
367 border: 1px solid #557;
368 padding: 8px;
369 margin: 4px;
370 }
371 code.error {
372 display: block;
373 margin: 1em 0;
374 overflow: auto;
375 }
376 div.name h1, div.error p {
377 margin: 0;
378 }
379 h2 {
380 margin-top: 0;
381 margin-bottom: 10px;
382 font-size: medium;
383 font-weight: bold;
384 text-decoration: underline;
385 }
386 h1 {
387 font-size: medium;
388 font-weight: normal;
389 }
390 /* from http://users.tkk.fi/~tkarvine/linux/doc/pre-wrap/pre-wrap-css3-mozilla-opera-ie.html */
391 /* Browser specific (not valid) styles to make preformatted text wrap */
392 pre {
393 white-space: pre-wrap; /* css-3 */
394 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
395 white-space: -pre-wrap; /* Opera 4-6 */
396 white-space: -o-pre-wrap; /* Opera 7 */
397 word-wrap: break-word; /* Internet Explorer 5.5+ */
398 }
399 </style>
400 </head>
401 <body>
402 <div class="box">
403 <div class="error">$error</div>
404 <div class="infos">$infos</div>
405 <div class="name">$name</div>
406 </div>
407 </body>
408 </html>
409  
410 # Trick IE. Old versions of IE would display their own error page instead
411 # of ours if we'd give it less than 512 bytes.
412 34 $c->res->{body} .= ( ' ' x 512 );
413  
414 34 $c->res->{body} = Encode::encode("UTF-8", $c->res->{body});
415  
416 # Return 500
417 34 $c->res->status(500);
418 }
419  
420 =head2 $self->finalize_headers($c)
421  
422 Allows engines to write headers to response
423  
424 =cut
425  
426 0 ▶sub finalize_headers {
Subroutine finalize_headers: not called CC=2 SCAR=17.9
Pod documented
427 0 my ($self, $ctx) = @_;
428  
429 0 ▶ $ctx->finalize_headers unless $ctx->response->finalized_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->response->finalized_headers 0 0 0
430 0 return;
431 }
432  
433 =head2 $self->finalize_uploads($c)
434  
435 Clean up after uploads, deleting temp files.
436  
437 =cut
438  
439 929 ▶sub finalize_uploads {
Subroutine finalize_uploads: called CC=3 SCAR=11.0
Pod documented
440 929 my ( $self, $c ) = @_;
441  
442 # N.B. This code is theoretically entirely unneeded due to ->cleanup(1)
443 # on the HTTP::Body object.
444 929 my $request = $c->request;
445 929 foreach my $key (keys %{ $request->uploads }) {
446 18 my $upload = $request->uploads->{$key};
447 20 ▶ unlink grep { -e $_ } map { $_->tempname }
Branch: 1 branch
BranchTrueFalseTotal
ref $upload eq 'ARRAY' ? : 1 17 18
448 18 (ref $upload eq 'ARRAY' ? @{$upload} : ($upload));
449 }
450  
451 }
452  
453 =head2 $self->prepare_body($c)
454  
455 sets up the L<Catalyst::Request> object body using L<HTTP::Body>
456  
457 =cut
458  
459 931 ▶sub prepare_body {
Subroutine prepare_body: called CC=1 SCAR=0.0
Pod documented
460 931 my ( $self, $c ) = @_;
461  
462 931 $c->request->prepare_body;
463 }
464  
465 =head2 $self->prepare_body_chunk($c)
466  
467 Add a chunk to the request body.
468  
469 =cut
470  
471 # XXX - Can this be deleted?
472 0 ▶sub prepare_body_chunk {
Subroutine prepare_body_chunk: not called CC=1 SCAR=6.9
Pod documented
473 0 my ( $self, $c, $chunk ) = @_;
474  
475 0 $c->request->prepare_body_chunk($chunk);
476 }
477  
478 =head2 $self->prepare_body_parameters($c)
479  
480 Sets up parameters from body.
481  
482 =cut
483  
484 0 ▶sub prepare_body_parameters {
Subroutine prepare_body_parameters: not called CC=1 SCAR=6.9
Pod documented
485 0 my ( $self, $c ) = @_;
486  
487 0 $c->request->prepare_body_parameters;
488 }
489  
490 =head2 $self->prepare_parameters($c)
491  
492 Sets up parameters from query and post parameters.
493 If parameters have already been set up will clear
494 existing parameters and set up again.
495  
496 =cut
497  
498 930 ▶sub prepare_parameters {
Subroutine prepare_parameters: called CC=1 SCAR=0.0
Pod documented
499 930 my ( $self, $c ) = @_;
500  
501 930 $c->request->_clear_parameters;
502 930 return $c->request->parameters;
503 }
504  
505 =head2 $self->prepare_path($c)
506  
507 abstract method, implemented by engines.
508  
509 =cut
510  
511 942 ▶sub prepare_path {
Subroutine prepare_path: called CC=11 SCAR=24.0
Pod documented
512 942 my ($self, $ctx) = @_;
513  
514 942 my $env = $ctx->request->env;
515  
516 942 ▶ my $scheme = $ctx->request->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$ctx->request->secure ? : 7 935 942
517 942 ▶ my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME};
Condition100%3/3
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
l!l&&r!l&&!r
93651
MC/DC100%2/2
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
$env->{'HTTP_HOST'} $env->{'SERVER_NAME'}
Truth table100%3/3
A || B
A= $env->{'HTTP_HOST'}B= $env->{'SERVER_NAME'}
ABresult
1X1
011
000
518 942 ▶ my $port = $env->{SERVER_PORT} || 80;
Condition100%2/2
$env->{'SERVER_PORT'} || 80
l!l
93210
MC/DC100%1/1
$env->{'SERVER_PORT'} || 80
$env->{'SERVER_PORT'}
Truth table100%2/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
519 942 ▶ my $base_path = $env->{SCRIPT_NAME} || "/";
Condition100%2/2
$env->{'SCRIPT_NAME'} || '/'
l!l
10932
MC/DC100%1/1
$env->{'SCRIPT_NAME'} || '/'
$env->{'SCRIPT_NAME'}
Truth table100%2/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
520  
521 # set the request URI
522 942 my $path;
523 942 ▶ if (!$ctx->config->{use_request_uri_for_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ctx->config->{'use_request_uri_for_path'}) { } 323 619 942
524 323 my $path_info = $env->{PATH_INFO};
525 323 ▶ if ( exists $env->{REDIRECT_URL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{"REDIRECT_URL"}) 3 320 323
526 3 $base_path = $env->{REDIRECT_URL};
527 3 $base_path =~ s/\Q$path_info\E$//;
528 }
529 323 $path = $base_path . $path_info;
530 323 $path =~ s{^/+}{};
531 323 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
532 323 $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
533 }
534 else {
535 619 my $req_uri = $env->{REQUEST_URI};
536 619 $req_uri =~ s/\?.*$//;
537 619 $path = $req_uri;
538 619 $path =~ s{^/+}{};
539 }
540  
541 # Using URI directly is way too slow, so we construct the URLs manually
542 942 my $uri_class = "URI::$scheme";
543  
544 # HTTP_HOST will include the port even if it's 80/443
545 942 $host =~ s/:(?:80|443)$//;
546  
547 942 ▶ if ($port !~ /^(?:80|443)$/ && $host !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($port =~ /^(?:80|443)$/ or $host =~ /:/) 0 942 942
Condition33%1/3
$port =~ /^(?:80|443)$/ or $host =~ /:/
l!l&&r!l&&!r
94200
MC/DC0%0/2
$port =~ /^(?:80|443)$/ or $host =~ /:/
$port =~ /^(?:80|443)$/ $host =~ /:/
Truth table33%1/3
A or B
A= $port =~ /^(?:80|443)$/B= $host =~ /:/
ABresult
1X1
011
000
548 0 $host .= ":$port";
549 }
550  
551 942 ▶ my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '';
Branch: 1 branch
BranchTrueFalseTotal
$env->{'QUERY_STRING'} ? : 84 858 942
552 942 my $uri = $scheme . '://' . $host . '/' . $path . $query;
553  
554 942 $ctx->request->uri( (bless \$uri, $uri_class)->canonical );
555  
556 # set the base URI
557 # base must end in a slash
558 942 ▶ $base_path .= '/' unless $base_path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $base_path =~ m[/$] 8 934 942
559  
560 942 my $base_uri = $scheme . '://' . $host . $base_path;
561  
562 942 $ctx->request->base( bless \$base_uri, $uri_class );
563  
564 942 return;
565 }
566  
567 =head2 $self->prepare_request($c)
568  
569 =head2 $self->prepare_query_parameters($c)
570  
571 process the query string and extract query parameters.
572  
573 =cut
574  
575 934 ▶sub prepare_query_parameters {
Subroutine prepare_query_parameters: called CC=10 SCAR=23.0
Pod documented
576 934 my ($self, $c) = @_;
577 934 my $env = $c->request->env;
578 934 my $do_not_decode_query = $c->config->{do_not_decode_query};
579  
580 934 my $old_encoding;
581 934 ▶ if(my $new = $c->config->{default_query_encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new = $c->config->{"default_query_encoding"}) 1 933 934
582 1 $old_encoding = $c->encoding;
583 1 $c->encoding($new);
584 }
585  
586 934 ▶ my $check = $c->config->{do_not_check_query_encoding} ? undef :$c->_encode_check;
Branch: 1 branch
BranchTrueFalseTotal
$c->config->{'do_not_check_query_encoding'} ? : 0 934 934
587 283 ▶ my $decoder = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
588 283 my $str = shift;
589 283 ▶ return $str if $do_not_decode_query;
Branch: 1 branch
BranchTrueFalseTotal
if $do_not_decode_query 0 283 283
590 283 return $c->_handle_param_unicode_decoding($str, $check);
591 934 };
592  
593 my $query_string = exists $env->{QUERY_STRING}
594 ? $env->{QUERY_STRING}
595 934 ▶ : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $env->{'QUERY_STRING'} ? : 933 1 934
596  
597 934 $query_string =~ s/\A[&;]+//;
598  
599 934 my @unsplit_pairs = split /[&;]+/, $query_string;
600 934 my $p = Hash::MultiValue->new();
601  
602 934 my $is_first_pair = 1;
603 934 for my $pair (@unsplit_pairs) {
604 my ($name, $value)
605 144 ▶ = map { defined $_ ? $decoder->($self->unescape_uri($_)) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 283 5 288
606 ( split /=/, $pair, 2 )[0,1]; # slice forces two elements
607  
608 143 ▶ if ($is_first_pair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_pair) 84 59 143
609 # If the first pair has no equal sign, then it means the isindex
610 # flag is set.
611 84 ▶ $c->request->query_keywords($name) unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 79 84
612  
613 84 $is_first_pair = 0;
614 }
615  
616 143 $p->add( $name => $value );
617 }
618  
619  
620 933 ▶ $c->encoding($old_encoding) if $old_encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $old_encoding 1 932 933
621 933 ▶ $c->request->query_parameters( $c->request->_use_hash_multivalue ? $p : $p->mixed );
Branch: 1 branch
BranchTrueFalseTotal
$c->request->_use_hash_multivalue ? : 4 929 933
622 }
623  
624 =head2 $self->prepare_read($c)
625  
626 Prepare to read by initializing the Content-Length from headers.
627  
628 =cut
629  
630 933 ▶sub prepare_read {
Subroutine prepare_read: called CC=1 SCAR=0.0
Pod documented
631 933 my ( $self, $c ) = @_;
632  
633 # Initialize the amount of data we think we need to read
634 933 $c->request->_read_length;
635 }
636  
637 =head2 $self->prepare_request(@arguments)
638  
639 Populate the context object from the request object.
640  
641 =cut
642  
643 943 ▶sub prepare_request {
Subroutine prepare_request: called CC=2 SCAR=6.9
Pod documented
644 943 my ($self, $ctx, %args) = @_;
645 943 ▶ $ctx->log->psgienv($args{env}) if $ctx->log->can('psgienv');
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->log->can("psgienv") 892 51 943
646 943 $ctx->request->_set_env($args{env});
647 943 $self->_set_env($args{env}); # Nasty back compat!
648 943 $ctx->response->_set_response_cb($args{response_cb});
649 }
650  
651 =head2 $self->prepare_uploads($c)
652  
653 =cut
654  
655 929 ▶sub prepare_uploads {
Subroutine prepare_uploads: called CC=11 SCAR=24.2
Pod documented
656 929 my ( $self, $c ) = @_;
657  
658 929 my $request = $c->request;
659 929 ▶ return unless $request->_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $request->_body 883 46 929
660  
661 46 my $enc = $c->encoding;
662 46 my $uploads = $request->_body->upload;
663 46 my $parameters = $request->parameters;
664 46 foreach my $name (keys %$uploads) {
665 18 my $files = $uploads->{$name};
666 18 ▶ $name = $c->_handle_unicode_decoding($name) if $enc;
Branch: 1 branch
BranchTrueFalseTotal
if $enc 18 0 18
667 18 my @uploads;
668 18 ▶ for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $files eq 'ARRAY' ? : 1 17 18
669 20 my $headers = HTTP::Headers->new( %{ $upload->{headers} } );
670 20 my $filename = $upload->{filename};
671 20 ▶ $filename = $c->_handle_unicode_decoding($filename) if $enc;
Branch: 1 branch
BranchTrueFalseTotal
if $enc 20 0 20
672  
673 my $u = Catalyst::Request::Upload->new
674 (
675 size => $upload->{size},
676 type => scalar $headers->content_type,
677 charset => scalar $headers->content_type_charset,
678 headers => $headers,
679 tempname => $upload->{tempname},
680 20 filename => $filename,
681 );
682 20 push @uploads, $u;
683 }
684 18 ▶ $request->uploads->{$name} = @uploads > 1 ? \@uploads : $uploads[0];
Branch: 1 branch
BranchTrueFalseTotal
@uploads > 1 ? : 1 17 18
685  
686 # support access to the filename as a normal param
687 18 my @filenames = map { $_->{filename} } @uploads;
688 # append, if there's already params with this name
689 18 ▶ if (exists $parameters->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameters->{$name}) { } 2 16 18
690 2 ▶ if (ref $parameters->{$name} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parameters->{$name} eq 'ARRAY') { } 0 2 2
691 0 push @{ $parameters->{$name} }, @filenames;
692 }
693 else {
694 2 $parameters->{$name} = [ $parameters->{$name}, @filenames ];
695 }
696 }
697 else {
698 16 ▶ $parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0];
Branch: 1 branch
BranchTrueFalseTotal
@filenames > 1 ? : 1 15 16
699 }
700 }
701 }
702  
703 =head2 $self->write($c, $buffer)
704  
705 Writes the buffer to the client.
706  
707 =cut
708  
709 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
710 0 my ( $self, $c, $buffer ) = @_;
711  
712 0 $c->response->write($buffer);
713 }
714  
715 =head2 $self->unencoded_write($c, $buffer)
716  
717 Writes the buffer to the client without encoding. Necessary for
718 already encoded buffers. Used when a $c->write has been done
719 followed by $c->res->body.
720  
721 =cut
722  
723 3 ▶sub unencoded_write {
Subroutine unencoded_write: called CC=1 SCAR=0.0
Pod documented
724 3 my ( $self, $c, $buffer ) = @_;
725  
726 3 $c->response->unencoded_write($buffer);
727 }
728  
729 =head2 $self->read($c, [$maxlength])
730  
731 Reads from the input stream by calling C<< $self->read_chunk >>.
732  
733 Maintains the read_length and read_position counters as data is read.
734  
735 =cut
736  
737 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
Pod documented
738 0 my ( $self, $c, $maxlength ) = @_;
739  
740 0 $c->request->read($maxlength);
741 }
742  
743 =head2 $self->read_chunk($c, \$buffer, $length)
744  
745 Each engine implements read_chunk as its preferred way of reading a chunk
746 of data. Returns the number of bytes read. A return of 0 indicates that
747 there is no more data to be read.
748  
749 =cut
750  
751 0 ▶sub read_chunk {
Subroutine read_chunk: not called CC=1 SCAR=6.9
Pod documented
752 0 my ($self, $ctx) = (shift, shift);
753 0 return $ctx->request->read_chunk(@_);
754 }
755  
756 =head2 $self->run($app, $server)
757  
758 Start the engine. Builds a PSGI application and calls the
759 run method on the server passed in, which then causes the
760 engine to loop, handling requests..
761  
762 =cut
763  
764 1 ▶sub run {
Subroutine run: called CC=10 SCAR=33.2
Pod documented
765 1 my ($self, $app, $psgi, @args) = @_;
766 # @args left here rather than just a $options, $server for back compat with the
767 # old style scripts which send a few args, then a hashref
768  
769 # They should never actually be used in the normal case as the Plack engine is
770 # passed in got all the 'standard' args via the loader in the script already.
771  
772 # FIXME - we should stash the options in an attribute so that custom args
773 # like Gitalist's --git_dir are possible to get from the app without stupid tricks.
774 1 ▶ my $server = pop @args if (scalar @args && blessed $args[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args and builtin::blessed($args[-1]) 1 0 1
Condition33%1/3
scalar @args and builtin::blessed($args[-1])
!ll&&!rl&&r
001
MC/DC0%0/2
scalar @args and builtin::blessed($args[-1])
scalar @args builtin::blessed($args[-1])
Truth table33%1/3
A and B
A= scalar @argsB= builtin::blessed($args[-1])
ABresult
0X0
100
111
775 1 ▶ my $options = pop @args if (scalar @args && ref($args[-1]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args and ref $args[-1] eq "HASH" 1 0 1
Condition33%1/3
scalar @args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
scalar @args and ref $args[-1] eq "HASH"
scalar @args ref $args[-1] eq "HASH"
Truth table33%1/3
A and B
A= scalar @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
776 # Back compat hack for applications with old (non Catalyst::Script) scripts to work in FCGI.
777 1 ▶ if (scalar @args && !ref($args[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args and not ref $args[0]) 0 1 1
Condition33%1/3
scalar @args and not ref $args[0]
!ll&&!rl&&r
100
MC/DC0%0/2
scalar @args and not ref $args[0]
scalar @args not ref $args[0]
Truth table33%1/3
A and not(B)
A= scalar @argsB= not ref $args[0]
ABresult
0X0
100
111
778 0 ▶ if (my $listen = shift @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $listen = shift @args) 0 0 0
779 0 ▶ $options->{listen} ||= [$listen];
Condition0%0/2
$options->{"listen"} ||= [$listen]
l!l
00
MC/DC0%0/1
$options->{"listen"} ||= [$listen]
$options->{"listen"}
Truth table0%0/2
A
A= $options->{"listen"}
Aresult
11
00
780 }
781 }
782 1 ▶ if (! $server ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($server) 0 1 1
783 0 $server = Catalyst::EngineLoader->new(application_name => ref($self))->auto(%$options);
784 # We're not being called from a script, so auto detect what backend to
785 # run on. This should never happen, as mod_perl never calls ->run,
786 # instead the $app->handle method is called per request.
787 0 $app->log->warn("Not supplied a Plack engine, falling back to engine auto-loader (are your scripts ancient?)")
788 }
789 1 $app->run_options($options);
790 1 $server->run($psgi, $options);
791 }
792  
793 =head2 build_psgi_app ($app, @args)
794  
795 Builds and returns a PSGI application closure. (Raw, not wrapped in middleware)
796  
797 =cut
798  
799 136 ▶sub build_psgi_app {
Subroutine build_psgi_app: called CC=1 SCAR=0.0
Pod documented
800 136 my ($self, $app, @args) = @_;
801  
802 934 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
803 934 my ($env) = @_;
804  
805 934 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
806 934 my ($respond) = @_;
807 934 ▶ confess("Did not get a response callback for writer, cannot continue") unless $respond;
Branch: 1 branch
BranchTrueFalseTotal
unless $respond 0 934 934
808 934 $app->handle_request(env => $env, response_cb => $respond);
809 934 };
810 136 };
811 }
812  
813 =head2 $self->unescape_uri($uri)
814  
815 Unescapes a given URI using the most efficient method available. Engines such
816 as Apache may implement this using Apache's C-based modules, for example.
817  
818 =cut
819  
820 283 ▶sub unescape_uri {
Subroutine unescape_uri: called CC=2 SCAR=6.9
Pod documented
821 283 my ( $self, $str ) = @_;
822  
823 283 ▶ $str =~ s/(?:%([0-9A-Fa-f]{2})|\+)/defined $1 ? chr(hex($1)) : ' '/eg;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 79 1 80
824  
825 283 return $str;
826 }
827  
828 =head2 $self->finalize_output
829  
830 <obsolete>, see finalize_body
831  
832 =head2 $self->env
833  
834 Hash containing environment variables including many special variables inserted
835 by WWW server - like SERVER_*, REMOTE_*, HTTP_* ...
836  
837 Before accessing environment variables consider whether the same information is
838 not directly available via Catalyst objects $c->request, $c->engine ...
839  
840 BEWARE: If you really need to access some environment variable from your Catalyst
841 application you should use $c->engine->env->{VARNAME} instead of $ENV{VARNAME},
842 as in some environments the %ENV hash does not contain what you would expect.
843  
844 =head1 AUTHORS
845  
846 Catalyst Contributors, see Catalyst.pm
847  
848 =head1 COPYRIGHT
849  
850 This library is free software. You can redistribute it and/or modify it under
851 the same terms as Perl itself.
852  
853 =cut
854  
855 156 __PACKAGE__->meta->make_immutable;
856  
857 156 1;
« lib/Catalyst/Utils.pm ↑ Summary lib/Catalyst.pm »