blib/lib/Plack/Middleware/Lint.pm

filter: Statementstmt 88.9% 97 / 109 Branchbran 81.0% 60 / 74 Conditioncond 70.7% 29 / 41 MC/DCmcdc 54.5% 12 / 22 Subroutinesub 100% 13 / 13 Podpod 20.0% 1 / 5 totaltotal 80.3% 212 / 264 CC 54 SCAR 31.4
CC
54
Cov
83%
validate_env
35.0
validate_res
25.6
wrap
6.6
CRAP 23.2
« blib/lib/Plack/Runner.pm ↑ Summary blib/lib/Plack/Handler/CGI.pm »
1 package Plack::Middleware::Lint;
2 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 46 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=18.9
Pod undocumented
10 27 my($self, $app) = @_;
11  
12 27 ▶ unless (reftype $app eq 'CODE' or overload::Method($app, '&{}')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($app) eq "CODE" or overload::Method($app, "&{}")) 0 27 27
Condition33%1/3
builtin::reftype($app) eq "CODE" or overload::Method($app, "&{}")
l!l&&r!l&&!r
2700
MC/DC0%0/2
builtin::reftype($app) eq "CODE" or overload::Method($app, "&{}")
builtin::reftype($app) eq "CODE" overload::Method($app, "&{}")
Truth table33%1/3
A or B
A= builtin::reftype($app) eq "CODE"B= overload::Method($app, "&{}")
ABresult
1X1
011
000
13 0 ▶ die("PSGI app should be a code reference: ", (defined $app ? $app : "undef"));
Branch: 1 branch
BranchTrueFalseTotal
defined $app ? : 0 0 0
14 }
15  
16 27 $self->SUPER::wrap($app);
17 }
18  
19 34 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
20 34 my $self = shift;
21 34 my $env = shift;
22  
23 34 $self->validate_env($env);
24 26 my $res = $self->app->($env);
25 26 return $self->validate_res($res);
26 }
27  
28 34 ▶sub validate_env {
Subroutine validate_env: called CC=24 SCAR=35.6
Pod undocumented
29 34 my ($self, $env) = @_;
30 34 ▶ unless ($env->{REQUEST_METHOD}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"REQUEST_METHOD"}) 1 33 34
31 1 die('Missing env param: REQUEST_METHOD');
32 }
33 33 ▶ unless ($env->{REQUEST_METHOD} =~ /^[A-Z]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"REQUEST_METHOD"} =~ /^[A-Z]+$/) 1 32 33
34 1 die("Invalid env param: REQUEST_METHOD($env->{REQUEST_METHOD})");
35 }
36 32 ▶ unless (defined($env->{SCRIPT_NAME})) { # allows empty string
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $env->{"SCRIPT_NAME"}) 0 32 32
37 0 die('Missing mandatory env param: SCRIPT_NAME');
38 }
39 32 ▶ if ($env->{SCRIPT_NAME} eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SCRIPT_NAME"} eq "/") 1 31 32
40 1 die('SCRIPT_NAME must not be /');
41 }
42 31 ▶ unless (defined($env->{PATH_INFO})) { # allows empty string
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $env->{"PATH_INFO"}) 0 31 31
43 0 die('Missing mandatory env param: PATH_INFO');
44 }
45 31 ▶ if ($env->{PATH_INFO} ne '' && $env->{PATH_INFO} !~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"PATH_INFO"} ne "" and not $env->{"PATH_INFO"} =~ m[^/]) 1 30 31
Condition100%3/3
$env->{"PATH_INFO"} ne "" and not $env->{"PATH_INFO"} =~ m[^/]
!ll&&!rl&&r
1291
MC/DC100%2/2
$env->{"PATH_INFO"} ne "" and not $env->{"PATH_INFO"} =~ m[^/]
$env->{"PATH_INFO"} ne "" not $env->{"PATH_INFO"} =~ m[^/]
Truth table100%3/3
A and not(B)
A= $env->{"PATH_INFO"} ne ""B= not $env->{"PATH_INFO"} =~ m[^/]
ABresult
0X0
100
111
46 1 die("PATH_INFO must begin with / ($env->{PATH_INFO})");
47 }
48 30 ▶ unless (defined($env->{SERVER_NAME})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $env->{"SERVER_NAME"}) 0 30 30
49 0 die('Missing mandatory env param: SERVER_NAME');
50 }
51 30 ▶ if ($env->{SERVER_NAME} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SERVER_NAME"} eq "") 0 30 30
52 0 die('SERVER_NAME must not be empty string');
53 }
54 30 ▶ unless (defined($env->{SERVER_PORT})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $env->{"SERVER_PORT"}) 1 29 30
55 1 die('Missing mandatory env param: SERVER_PORT');
56 }
57 29 ▶ if ($env->{SERVER_PORT} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SERVER_PORT"} eq "") 0 29 29
58 0 die('SERVER_PORT must not be empty string');
59 }
60 29 ▶ if (defined($env->{SERVER_PROTOCOL}) and $env->{SERVER_PROTOCOL} !~ m{^HTTP/\d}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"SERVER_PROTOCOL"} and not $env->{"SERVER_PROTOCOL"} =~ m[^HTTP/\d]) 1 28 29
Condition66%2/3
defined $env->{"SERVER_PROTOCOL"} and not $env->{"SERVER_PROTOCOL"} =~ m[^HTTP/\d]
!ll&&!rl&&r
0281
MC/DC50%1/2
defined $env->{"SERVER_PROTOCOL"} and not $env->{"SERVER_PROTOCOL"} =~ m[^HTTP/\d]
defined $env->{"SERVER_PROTOCOL"} not $env->{"SERVER_PROTOCOL"} =~ m[^HTTP/\d]
Truth table66%2/3
A and not(B)
A= defined $env->{"SERVER_PROTOCOL"}B= not $env->{"SERVER_PROTOCOL"} =~ m[^HTTP/\d]
ABresult
0X0
100
111
61 1 die("Invalid SERVER_PROTOCOL: $env->{SERVER_PROTOCOL}");
62 }
63 28 for my $param (qw/version url_scheme input errors multithread multiprocess/) {
64 168 ▶ unless (exists $env->{"psgi.$param"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$env{"psgi.$param"}) 0 168 168
65 0 die("Missing psgi.$param");
66 }
67 }
68 28 ▶ unless (ref($env->{'psgi.version'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $env->{"psgi.version"} eq "ARRAY") 1 27 28
69 1 die("psgi.version should be ArrayRef: $env->{'psgi.version'}");
70 }
71 27 ▶ unless (scalar(@{$env->{'psgi.version'}}) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$env->{"psgi.version"};} == 2) 0 27 27
72 0 die('psgi.version should contain 2 elements, not ', scalar(@{$env->{'psgi.version'}}));
73 }
74 27 ▶ unless ($env->{'psgi.url_scheme'} =~ /^https?$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"psgi.url_scheme"} =~ /^https?$/) 0 27 27
75 0 die("psgi.url_scheme should be 'http' or 'https': ", $env->{'psgi.url_scheme'});
76 }
77 27 ▶ if ($env->{"psgi.version"}->[1] == 1) { # 1.1
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgi.version"}[1] == 1) 27 0 27
78 27 for my $param (qw(streaming nonblocking run_once)) {
79 81 ▶ unless (exists $env->{"psgi.$param"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$env{"psgi.$param"}) 0 81 81
80 0 die("Missing psgi.$param");
81 }
82 }
83 }
84 27 ▶ if ($env->{HTTP_CONTENT_TYPE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_CONTENT_TYPE"}) 1 26 27
85 1 die('HTTP_CONTENT_TYPE should not exist');
86 }
87 26 ▶ if ($env->{HTTP_CONTENT_LENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_CONTENT_LENGTH"}) 0 26 26
88 0 die('HTTP_CONTENT_LENGTH should not exist');
89 }
90 }
91  
92 5 ▶sub is_possibly_fh {
Subroutine is_possibly_fh: called CC=3 SCAR=11.0
Pod undocumented
93 5 my $fh = shift;
94  
95 ref $fh eq 'GLOB' &&
96 1 *{$fh}{IO} &&
97 5 ▶ *{$fh}{IO}->can('getline');
Branch: 1 branch
BranchTrueFalseTotal
ref $fh eq "GLOB" and *{$fh;}{"IO"} and *{$fh;}{"IO"}->can("getline") 1 4 5
Condition100%2/2
ref $fh eq "GLOB" and *{$fh;}{"IO"}
!ll
41
MC/DC100%1/1
ref $fh eq "GLOB" and *{$fh;}{"IO"}
ref $fh eq "GLOB"
Truth table100%2/2
A
A= ref $fh eq "GLOB"
Aresult
00
11
98 }
99  
100 28 ▶sub validate_res {
Subroutine validate_res: called CC=25 SCAR=32.4
Pod undocumented
101 28 my ($self, $res, $streaming) = @_;
102  
103 28 ▶ unless (ref($res) eq 'ARRAY' or ref($res) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res eq "ARRAY" or ref $res eq "CODE") 1 27 28
Condition100%3/3
ref $res eq "ARRAY" or ref $res eq "CODE"
l!l&&r!l&&!r
2521
MC/DC100%2/2
ref $res eq "ARRAY" or ref $res eq "CODE"
ref $res eq "ARRAY" ref $res eq "CODE"
Truth table100%3/3
A or B
A= ref $res eq "ARRAY"B= ref $res eq "CODE"
ABresult
1X1
011
000
104 1 die("Response should be array ref or code ref: $res");
105 }
106  
107 27 ▶ if (ref $res eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "CODE") 2 25 27
108 2 ▶ return $self->response_cb($res, sub { $self->validate_res(@_, 1) });
Subroutine __ANON__: called CC=1 SCAR=0.0
109 }
110  
111 25 ▶ unless (@$res == 3 || ($streaming && @$res == 2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$res == 3 or $streaming and @$res == 2) 1 24 25
Condition50%3/6
@$res == 3 or $streaming and @$res == 2
l!l&&r!l&&!r
2401
$streaming and @$res == 2
!ll&&!rl&&r
100
MC/DC33%1/3
@$res == 3 or $streaming and @$res == 2
@$res == 3 $streaming @$res == 2
Truth table50%2/4
A or B and C
A= @$res == 3B= $streamingC= @$res == 2
ABCresult
1XX1
0111
00X0
0100
112 1 die('Response needs to be 3 element array, or 2 element in streaming');
113 }
114  
115 24 ▶ unless ($res->[0] =~ /^\d+$/ && $res->[0] >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] =~ /^\d+$/ and $res->[0] >= 100) 1 23 24
Condition66%2/3
$res->[0] =~ /^\d+$/ and $res->[0] >= 100
!ll&&!rl&&r
0123
MC/DC50%1/2
$res->[0] =~ /^\d+$/ and $res->[0] >= 100
$res->[0] =~ /^\d+$/ $res->[0] >= 100
Truth table66%2/3
A and B
A= $res->[0] =~ /^\d+$/B= $res->[0] >= 100
ABresult
0X0
100
111
116 1 die("Status code needs to be an integer greater than or equal to 100: $res->[0]");
117 }
118  
119 23 ▶ unless (ref $res->[1] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res->[1] eq "ARRAY") 1 22 23
120 1 die("Headers needs to be an array ref: $res->[1]");
121 }
122  
123 22 my @copy = @{$res->[1]};
124 22 ▶ unless (@copy % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@copy % 2 == 0) 1 21 22
125 1 die('The number of response headers needs to be even, not odd(', scalar(@copy), ')');
126 }
127  
128 21 ▶ while(my($key, $val) = splice(@copy, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@copy, 0, 2)) 16 10 26
129 16 ▶ if (lc $key eq 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $key eq "status") 1 15 16
130 1 die('Response headers MUST NOT contain a key named Status');
131 }
132 15 ▶ if ($key =~ /[:\r\n]|[-_]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /[:\r\n]|[-_]$/) 4 11 15
133 4 die("Response headers MUST NOT contain a key with : or newlines, or that end in - or _. Header: $key");
134 }
135 11 ▶ unless ($key =~ /^[a-zA-Z][0-9a-zA-Z\-_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^[a-zA-Z][0-9a-zA-Z\-_]*$/) 2 9 11
136 2 die("Response headers MUST consist only of letters, digits, _ or - and MUST start with a letter. Header: $key");
137 }
138 9 ▶ if ($val =~ /[\000-\037]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /[\000-\037]/) 2 7 9
139 2 die("Response headers MUST NOT contain characters below octal \037. Header: $key. Value: $val");
140 }
141 7 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 2 5 7
142 2 die("Response headers MUST be a defined string. Header: $key");
143 }
144 }
145  
146 # @$res == 2 is only right in psgi.streaming, and it's already checked.
147 10 ▶ unless (@$res == 2 ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@$res == 2 or ref $res->[2] eq "ARRAY" or &Plack::Util::is_real_fh($res->[2]) or is_possibly_fh($res->[2]) or builtin::blessed($res->[2]) and $res->[2]->can("getline")) 4 6 10
Condition66%10/15
@$res == 2 or ref $res->[2] eq "ARRAY" or &Plack::Util::is_real_fh($res->[2]) or is_possibly_fh($res->[2]) or builtin::blessed($res->[2]) and $res->[2]->can("getline")
l!l&&r!l&&!r
604
@$res == 2 or ref $res->[2] eq "ARRAY" or &Plack::Util::is_real_fh($res->[2]) or is_possibly_fh($res->[2])
l!l&&r!l&&!r
514
@$res == 2 or ref $res->[2] eq "ARRAY" or &Plack::Util::is_real_fh($res->[2])
l!l&&r!l&&!r
505
@$res == 2 or ref $res->[2] eq "ARRAY"
l!l&&r!l&&!r
055
builtin::blessed($res->[2]) and $res->[2]->can("getline")
!ll&&!rl&&r
400
MC/DC33%2/6
@$res == 2 or ref $res->[2] eq "ARRAY" or &Plack::Util::is_real_fh($res->[2]) or is_possibly_fh($res->[2]) or builtin::blessed($res->[2]) and $res->[2]->can("getline")
@$res == 2 ref $res->[2] eq "ARRAY" &Plack::Util::is_real_fh($res->[2]) is_possibly_fh($res->[2]) builtin::blessed($res->[2]) $res->[2]->can("getline")
Truth table42%3/7
A or B or C or D or E and F
A= @$res == 2B= ref $res->[2] eq "ARRAY"C= &Plack::Util::is_real_fh($res->[2])D= is_possibly_fh($res->[2])E= builtin::blessed($res->[2])F= $res->[2]->can("getline")
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
0000111
00000X0
0000100
148 ref $res->[2] eq 'ARRAY' ||
149 Plack::Util::is_real_fh($res->[2]) ||
150 is_possibly_fh($res->[2]) ||
151 (blessed($res->[2]) && $res->[2]->can('getline'))) {
152 4 die("Body should be an array ref or filehandle: $res->[2]");
153 }
154  
155 6 ▶ if (ref $res->[2] eq 'ARRAY' && grep _has_wide_char($_), @{$res->[2]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res->[2] eq "ARRAY" and grep _has_wide_char($_), @{$$res[2];}) 1 5 6
Condition100%3/3
ref $res->[2] eq "ARRAY" and grep _has_wide_char($_), @{$$res[2];}
!ll&&!rl&&r
141
MC/DC100%2/2
ref $res->[2] eq "ARRAY" and grep _has_wide_char($_), @{$$res[2];}
ref $res->[2] eq "ARRAY" grep _has_wide_char($_), @{$$res[2];}
Truth table100%3/3
A and B
A= ref $res->[2] eq "ARRAY"B= grep _has_wide_char($_), @{$$res[2];}
ABresult
0X0
100
111
156 1 die("Body must be bytes and should not contain wide characters (UTF-8 strings)");
157 }
158  
159 5 return $res;
160 }
161  
162 # NOTE: Some modules like HTML:: or XML:: could possibly generate
163 # ASCII/Latin-1 strings with utf8 flags on. They're actually safe to
164 # print, so there's no need to give warnings about it.
165 5 ▶sub _has_wide_char {
Subroutine _has_wide_char: called CC=2 SCAR=6.9
166 5 my $str = shift;
167 5 ▶ utf8::is_utf8($str) && $str =~ /[^\x00-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($str) and $str =~ /[^\x00-\xff]/ 3 2 5
168 }
169  
170 46 1;
171 __END__
« blib/lib/Plack/Runner.pm ↑ Summary blib/lib/Plack/Handler/CGI.pm »