| 1 | package Plack::Middleware::FixMissingBodyInRedirect; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use parent qw( Plack::Middleware ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Plack::Middleware which sets body for redirect response, if it's not already set | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 14 | ▶ | sub call { | ||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 14 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | return $self->response_cb($self->app->(@_), sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||
| 17 | 14 | my $res = shift; | |||||||||||||||||||||||||||||||||
| 18 | 14 | ▶ | return unless $res->[0] >= 300 && $res->[0] < 400; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$res->[0] >= 300 and $res->[0] < 400
MC/DC0%0/2
$res->[0] >= 300 and $res->[0] < 400
$res->[0] >= 300
$res->[0] < 400
Truth table33%1/3
A and B
A= $res->[0] >= 300B= $res->[0] < 400
| |||||||||||||||||||||||||||||||||||
| 19 | 14 | my $headers = Plack::Util::headers($res->[1]); # first index contains HTTP header | |||||||||||||||||||||||||||||||||
| 20 | 14 | ▶ | if( $headers->exists('Location') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 21 | 14 | my $location = $headers->get("Location"); | |||||||||||||||||||||||||||||||||
| 22 | # checking if body (which is at index 2) is set or not | ||||||||||||||||||||||||||||||||||
| 23 | 14 | ▶ | if (@$res == 3 && !_is_body_set($res->[2])) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@$res == 3 and not _is_body_set($res->[2])
MC/DC100%2/2
@$res == 3 and not _is_body_set($res->[2])
@$res == 3
not _is_body_set($res->[2])
Truth table100%3/3
A and not(B)
A= @$res == 3B= not _is_body_set($res->[2])
| |||||||||||||||||||||||||||||||||||
| 24 | 5 | my $body = $self->_default_html_body($location); | |||||||||||||||||||||||||||||||||
| 25 | 5 | $res->[2] = [$body]; | |||||||||||||||||||||||||||||||||
| 26 | 5 | my $content_length = Plack::Util::content_length([$body]); | |||||||||||||||||||||||||||||||||
| 27 | 5 | $headers->set('Content-Length' => $content_length); | |||||||||||||||||||||||||||||||||
| 28 | 5 | $headers->set('Content-Type' => 'text/html; charset=utf-8'); | |||||||||||||||||||||||||||||||||
| 29 | 5 | return; | |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||
| 31 | ▶ | elsif (@$res == 2 || blessed($res->[2])) { | |||||||||||||||||||||||||||||||||
|
Condition100%3/3
@$res == 2 or builtin::blessed($res->[2])
MC/DC100%2/2
@$res == 2 or builtin::blessed($res->[2])
@$res == 2
builtin::blessed($res->[2])
Truth table100%3/3
A or B
A= @$res == 2B= builtin::blessed($res->[2])
| |||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if(! $headers->exists('Content-Type')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 33 | 2 | $headers->set('Content-Type' => 'text/html; charset=utf-8') | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | 4 | my $done; | |||||||||||||||||||||||||||||||||
| 36 | 8 | ▶ | return sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 37 | 8 | my $chunk = shift; | |||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | return $chunk if $done; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if (!defined $chunk) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 40 | 2 | $done = 1; | |||||||||||||||||||||||||||||||||
| 41 | 2 | return $self->_default_html_body($location); | |||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||
| 43 | elsif (length $chunk) { | ||||||||||||||||||||||||||||||||||
| 44 | 2 | $done = 1; | |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | 2 | return $chunk; | |||||||||||||||||||||||||||||||||
| 47 | 4 | }; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||
| 50 | 14 | }); | |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | sub _default_html_body { | ||||||||||||||||||||||||||||||||
|
Subroutine
_default_html_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 54 | 7 | my ($self_or_class, $location) = @_; | |||||||||||||||||||||||||||||||||
| 55 | 7 | my $encoded_location = encode_entities($location); | |||||||||||||||||||||||||||||||||
| 56 | 7 | return <<"EOF"; | |||||||||||||||||||||||||||||||||
| 57 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | ||||||||||||||||||||||||||||||||||
| 58 | <html xmlns="http://www.w3.org/1999/xhtml"> | ||||||||||||||||||||||||||||||||||
| 59 | <head> | ||||||||||||||||||||||||||||||||||
| 60 | <title>Moved</title> | ||||||||||||||||||||||||||||||||||
| 61 | </head> | ||||||||||||||||||||||||||||||||||
| 62 | <body> | ||||||||||||||||||||||||||||||||||
| 63 | <p>This item has moved <a href="$encoded_location">here</a>.</p> | ||||||||||||||||||||||||||||||||||
| 64 | </body> | ||||||||||||||||||||||||||||||||||
| 65 | </html> | ||||||||||||||||||||||||||||||||||
| 66 | EOF | ||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 12 | ▶ | sub _is_body_set { | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_body_set: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 70 | 12 | my $body = shift; | |||||||||||||||||||||||||||||||||
| 71 | 12 | ▶ | if (ref $body eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | return grep { defined && length } @$body; | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $_ && length $_
MC/DC100%2/2
defined $_ && length $_
defined $_
length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | ▶ | elsif (Plack::Util::is_real_fh($body) && -f $body && -z _) { | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
&Plack::Util::is_real_fh($body) and -f $body and -z _
&Plack::Util::is_real_fh($body) and -f $body
MC/DC66%2/3
&Plack::Util::is_real_fh($body) and -f $body and -z _
&Plack::Util::is_real_fh($body)
-f $body
-z _
Truth table75%3/4
A and B and C
A= &Plack::Util::is_real_fh($body)B= -f $bodyC= -z _
| |||||||||||||||||||||||||||||||||||
| 75 | 1 | return 0; | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | 3 | return 1; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||||||