blib/lib/Plack/Middleware/ErrorDocument.pm

filter: Statementstmt 100% 75 / 75 Branchbran 75.0% 21 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 94.2% 114 / 121 CC 18 SCAR 20.5
CC
18
Cov
93%
__ANON__
14.1
__ANON__
3.0
__ANON__
3.0
CRAP 7.8
« blib/lib/Plack/Loader/Restarter.pm ↑ Summary blib/lib/Plack/Middleware/HTTPExceptions.pm »
1 package Plack::Middleware::ErrorDocument;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::Util::Accessor qw( subrequest );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use HTTP::Status qw(is_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
12 11 my $self = shift;
13 11 my $env = shift;
14  
15 11 my $r = $self->app->($env);
16  
17 11 ▶ $self->response_cb($r, sub {
Subroutine __ANON__: called CC=14 SCAR=26.5
18 11 my $r = shift;
19 11 ▶ unless (is_error($r->[0]) && exists $self->{$r->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_error($r->[0]) and exists $$self{$r->[0]}) 6 5 11
Condition100%3/3
&is_error($r->[0]) and exists $$self{$r->[0]}
!ll&&!rl&&r
425
MC/DC100%2/2
&is_error($r->[0]) and exists $$self{$r->[0]}
&is_error($r->[0]) exists $$self{$r->[0]}
Truth table100%3/3
A and B
A= &is_error($r->[0])B= exists $$self{$r->[0]}
ABresult
0X0
100
111
20 6 return;
21 }
22  
23 5 my $path = $self->{$r->[0]};
24 5 ▶ if ($self->subrequest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subrequest) { } 3 2 5
25 3 for my $key (keys %$env) {
26 67 ▶ unless ($key =~ /^psgi/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^psgi/) 40 27 67
27 40 $env->{'psgix.errordocument.' . $key} = $env->{$key};
28 }
29 }
30  
31 # the env is shared with outer middleware (e.g. AccessLog),
32 # so restore the original request after the subrequest
33 3 my @keys = qw(REQUEST_METHOD REQUEST_URI PATH_INFO QUERY_STRING CONTENT_LENGTH);
34 3 ▶ my %orig = map { exists $env->{$_} ? ($_ => $env->{$_}) : () } @keys;
Branch: 1 branch
BranchTrueFalseTotal
exists $env->{$_} ? : 15 0 15
35  
36 # TODO: What if SCRIPT_NAME is not empty?
37 3 $env->{REQUEST_METHOD} = 'GET';
38 3 $env->{REQUEST_URI} = $path;
39 3 $env->{PATH_INFO} = $path;
40 3 $env->{QUERY_STRING} = '';
41 3 delete $env->{CONTENT_LENGTH};
42  
43 3 my $sub_r = eval { $self->app->($env) };
44 3 my $err = $@;
45  
46 3 for my $key (@keys) {
47 15 ▶ exists $orig{$key} ? ($env->{$key} = $orig{$key}) : delete $env->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $orig{$key} ? : 15 0 15
48 }
49 3 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 3 3
50 3 ▶ if ($sub_r->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_r->[0] == 200) 3 0 3
51 3 $r->[1] = $sub_r->[1];
52 3 ▶ if (@$r == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$r == 3) { } 2 1 3
53 2 $r->[2] = $sub_r->[2];
54 }
55 else {
56 1 my $full_sub_response = '';
57 1 ▶ Plack::Util::foreach($sub_r->[2], sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 $full_sub_response .= $_[0];
59 1 });
60  
61 1 my $returned;
62 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
63 2 ▶ if ($returned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returned) 1 1 2
64 1 ▶ return defined($_[0]) ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 1 1
65 }
66 1 $returned = 1;
67 1 return $full_sub_response;
68 }
69 1 }
70 }
71 # TODO: allow 302 here?
72 } else {
73 2 my $h = Plack::Util::headers($r->[1]);
74 2 $h->remove('Content-Length');
75 2 $h->remove('Content-Encoding');
76 2 $h->remove('Transfer-Encoding');
77 2 $h->set('Content-Type', Plack::MIME->mime_type($path));
78  
79 2 ▶ open my $fh, "<", $path or die "$path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "${path}: $!" 2 0 2
80 2 ▶ if ($r->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->[2]) { } 1 1 2
81 1 $r->[2] = $fh;
82 } else {
83 1 my $done;
84 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
85 2 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 1 1 2
86 1 $done = 1;
87 1 return join '', <$fh>;
88 }
89 1 ▶ return defined $_[0] ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 1 1
90 1 };
91 };
92 }
93 11 });
94 }
95  
96 3 1;
97  
98 __END__
« blib/lib/Plack/Loader/Restarter.pm ↑ Summary blib/lib/Plack/Middleware/HTTPExceptions.pm »