| 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
Condition100%3/3
&is_error($r->[0]) and exists $$self{$r->[0]}
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]}
| |||||||||||||||||||||||||||||
| 20 | 6 | return; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | my $path = $self->{$r->[0]}; | |||||||||||||||||||||||||||
| 24 | 5 | ▶ | if ($self->subrequest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 3 | for my $key (keys %$env) { | |||||||||||||||||||||||||||
| 26 | 67 | ▶ | unless ($key =~ /^psgi/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | die $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | if ($sub_r->[0] == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 3 | $r->[1] = $sub_r->[1]; | |||||||||||||||||||||||||||
| 52 | 3 | ▶ | if (@$r == 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return defined($_[0]) ? '' : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | if ($r->[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | 1 | $done = 1; | |||||||||||||||||||||||||||
| 87 | 1 | return join '', <$fh>; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | return defined $_[0] ? '' : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | }; | |||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 11 | }); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | 1; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||