blib/lib/Plack/Middleware/Rewrite.pm

filter: Statementstmt 95.4% 63 / 66 Branchbran 89.4% 34 / 38 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 90.4% 123 / 136 CC 24 SCAR 27.2
CC
24
Cov
91%
call
22.6
__ANON__
2.1
__ANON__
2.0
CRAP 15.1
↑ Summary
1 3 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Plack::Middleware::Rewrite;
4  
5 3 our $VERSION = '2.102';
6  
7 3 ▶BEGIN { require Plack::Middleware; our @ISA = 'Plack::Middleware' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Plack::Util::Accessor qw( request response rules );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Plack::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 39 ▶sub call {
Subroutine call: called CC=22 SCAR=31.2
Pod documented
15 39 my $self = shift;
16 39 my ( $env ) = @_;
17  
18 39 my ( $app, $res, $legacy );
19 39 my ( $rules, $modify_cb ) = ( $self->request, $self->response );
20  
21 39 ▶ unless ( $rules or $modify_cb ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rules or $modify_cb) 19 20 39
Condition100%3/3
$rules or $modify_cb
l!l&&r!l&&!r
13719
MC/DC100%2/2
$rules or $modify_cb
$rules $modify_cb
Truth table100%3/3
A or B
A= $rulesB= $modify_cb
ABresult
1X1
011
000
22 19 $rules = $self->rules;
23 19 $legacy = 1;
24 }
25  
26 # call rules with $_ aliased to PATH_INFO
27 39 ▶ ( $res ) = map { scalar $rules->( $env ) } $env->{'PATH_INFO'}
Branch: 1 branch
BranchTrueFalseTotal
if $rules 32 7 39
28 if $rules;
29  
30 39 ▶ if ( $legacy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($legacy) { } 19 20 39
31 19 ▶ if ( 'CODE' eq ref $res ) { ( $modify_cb, $res ) = $res }
Branch: 3 branches
BranchTrueFalseTotal
if ('CODE' eq ref $res) { } 6 13 19
elsif ('ARRAY' eq ref $res) { } 4 9 13
elsif (ref $res) { } 0 9 9
32 4 ▶ elsif ( 'ARRAY' eq ref $res ) { undef $res if not @$res }
Branch: 1 branch
BranchTrueFalseTotal
unless @$res 1 3 4
33 0 elsif ( ref $res ) { undef $res }
34 else {
35 # upgrade scalar to response if it looks like an HTTP status
36 9 ▶ $res = ( defined $res and $res =~ /\A[1-5][0-9][0-9]\z/ )
Branch: 1 branch
BranchTrueFalseTotal
defined $res && $res =~ /\A[1-5][0-9][0-9]\z/ ? : 5 4 9
Condition100%3/3
defined $res and $res =~ /\A[1-5][0-9][0-9]\z/
!ll&&!rl&&r
135
MC/DC100%2/2
defined $res and $res =~ /\A[1-5][0-9][0-9]\z/
defined $res $res =~ /\A[1-5][0-9][0-9]\z/
Truth table100%3/3
A and B
A= defined $resB= $res =~ /\A[1-5][0-9][0-9]\z/
ABresult
0X0
100
111
37 ? [ $res, [], [] ]
38 : undef;
39 }
40 }
41 else {
42 0 ▶ if ( 'CODE' eq ref $res ) { ( $app, $res ) = $res }
Branch: 3 branches
BranchTrueFalseTotal
if ('CODE' eq ref $res) { } 0 20 20
elsif ('ARRAY' eq ref $res) { } 10 10 20
elsif (ref $res) { } 0 10 10
43 10 ▶ elsif ( 'ARRAY' eq ref $res ) { @$res = ( 303, [], [] ) if not @$res }
Branch: 1 branch
BranchTrueFalseTotal
unless @$res 1 9 10
44 0 elsif ( ref $res ) { die 'Unhandled reference type in request rewrite: ', overload::StrVal( $res ), "\n" }
45 10 else { undef $res }
46 }
47  
48 39 ▶ if ( $res ) { # external redirect, or explicit response
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 18 21 39
49 18 push @$res, map { [] } @$res .. 2;
50  
51 18 ▶ if ( $res->[0] =~ /\A3(?:0[0-35-9]|[1-9][0-9])\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] =~ /\A3(?:0[0-35-9]|[1-9][0-9])\z/) 11 7 18
52 11 my $dest = Plack::Util::header_get( $res->[1], 'Location' );
53 11 ▶ if ( not $dest ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dest) 9 2 11
54 9 $dest = Plack::Request->new( $env )->uri;
55 9 Plack::Util::header_set( $res->[1], Location => $dest );
56 }
57  
58 11 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (Plack::Util::content_length($res->[2]) or Plack::Util::header_exists($res->[1], "Content-Length")) 9 2 11
Condition66%2/3
Plack::Util::content_length($res->[2]) or Plack::Util::header_exists($res->[1], "Content-Length")
l!l&&r!l&&!r
029
MC/DC50%1/2
Plack::Util::content_length($res->[2]) or Plack::Util::header_exists($res->[1], "Content-Length")
Plack::Util::content_length($res->[2]) Plack::Util::header_exists($res->[1], "Content-Length")
Truth table66%2/3
A or B
A= Plack::Util::content_length($res->[2])B= Plack::Util::header_exists($res->[1], "Content-Length")
ABresult
1X1
011
000
59 Plack::Util::content_length( $res->[2] )
60 or Plack::Util::header_exists( $res->[1], 'Content-Length' )
61 ) {
62 9 my $href = Plack::Util::encode_html( $dest );
63 9 Plack::Util::header_set( $res->[1], qw( Content-Type text/html ) );
64 9 $res->[2] = [ qq'<!DOCTYPE html><title>Moved</title>This resource has moved to <a href="$href">a new address</a>.' ];
65 }
66 }
67 }
68 else { # internal redirect
69 21 ▶ $app ||= $self->app;
Condition33%1/3
$app ||= $self->app
l!l&&r!l&&!r
0210
MC/DC0%0/2
$app ||= $self->app
$app $self->app
Truth table33%1/3
A ||= B
A= $appB= $self->app
ABresult
1X1
011
000
70 21 $res = $app->( $env );
71 }
72  
73 39 ▶ return $res if not $modify_cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $modify_cb 26 13 39
74 13 ▶ Plack::Util::response_cb( $res, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
75 13 my $response = $_[0];
76 13 my $hdrs = Plack::Util::headers( $response->[1] );
77 13 ▶ $hdrs->{'status'} = sub { @_ ? $response->[0] = $_[0] : $response->[0] };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
78 13 my ( $result ) = map $modify_cb->( $env ), $hdrs;
79 13 ▶ return 'CODE' eq ref $result ? $result : ();
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $result ? : 2 11 13
80 13 } );
81 }
82  
83 3 1;
84  
85 __END__
↑ Summary