Easy/Request.pm

filter: Statementstmt 100% 144 / 144 Branchbran 86.6% 52 / 60 Conditioncond 72.7% 16 / 22 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 91.4% 235 / 257 CC 47 SCAR 21.8
CC
47
Cov
94%
new
16.0
_decode_utf8
10.1
_read_post
8.1
CRAP 8.8
↑ Summary Easy/Util.pm »
1 package CGI::Easy::Request;
2 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = 'v2.0.1';
9  
10 3 ▶use CGI::Easy::Util qw( uri_unescape_plus burst_urlencoded burst_multipart );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI::Escape qw( uri_unescape );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use constant MiB => 1024*1024; ## no critic(Capitalization)
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 my $MAX_POST = MiB;
17  
18 40 ▶sub new {
Subroutine new: called CC=16 SCAR=27.7
Pod documented
19 40 my ($class, $opt) = @_;
20 my $self = {
21 scheme => $ENV{HTTPS} ? 'https' : 'http',
22 host => undef,
23 port => $ENV{SERVER_PORT},
24 path => undef,
25 GET => {}, # for GET, HEAD, DELETE, …
26 POST => {}, # for POST, PUT
27 filename => {},
28 mimetype => {},
29 cookie => {},
30 REMOTE_ADDR => $ENV{REMOTE_ADDR},
31 REMOTE_PORT => $ENV{REMOTE_PORT},
32 AUTH_TYPE => $ENV{AUTH_TYPE},
33 REMOTE_USER => $ENV{REMOTE_USER},
34 40 ▶ REMOTE_PASS => undef,
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HTTPS'} ? : 1 39 40
35 ENV => { %ENV },
36 STDIN => q{},
37 error => q{},
38 };
39 40 bless $self, $class;
40  
41 40 my $pre = $opt->{frontend_prefix};
42 40 ▶ if (defined $pre) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pre) 2 38 40
43 2 $pre = uc $pre;
44 2 $pre =~ s/-/_/xmsg;
45 2 ▶ if (defined $ENV{"HTTP_${pre}REMOTE_ADDR"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_${pre}REMOTE_ADDR"}) 1 1 2
46 1 $self->{REMOTE_ADDR} = $ENV{"HTTP_${pre}REMOTE_ADDR"};
47 1 $self->{REMOTE_PORT} = $ENV{"HTTP_${pre}REMOTE_PORT"};
48 1 ▶ $self->{scheme} = $ENV{"HTTP_${pre}HTTPS"} ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"HTTP_${pre}HTTPS"} ? : 1 0 1
49 }
50 }
51  
52 40 my $host = $ENV{HTTP_HOST};
53 40 my $path = $ENV{REQUEST_URI};
54 40 ▶ if ($path =~ s{\A\w+://(?:[^/@]*@)?([^/]+)}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[\A\w+://(?:[^/\@]*@)?([^/]+)][]msx) 2 38 40
55 2 $host = $1;
56 }
57 40 $host =~ s{:\d+\z}{}xms;
58 40 $path =~ s{[?].*}{}xms;
59 40 $path = uri_unescape($path); # WARNING nginx allow %2F, apache didn't
60 40 ▶ if (!length $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $path) 1 39 40
61 1 $path = q{/};
62 }
63 40 $self->{host} = $host;
64 40 $self->{path} = $path;
65  
66 40 ▶ if ($ENV{HTTP_AUTHORIZATION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_AUTHORIZATION"}) 3 37 40
67 3 ▶ if ($ENV{HTTP_AUTHORIZATION} =~ /\ABasic\s+(\S+)\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_AUTHORIZATION"} =~ /\ABasic\s+(\S+)\z/msx) 2 1 3
68 2 my ($user, $pass) = split /:/xms, decode_base64($1), 2;
69 2 ▶ if (defined $pass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pass) 2 0 2
70 2 $self->{AUTH_TYPE} = 'Basic';
71 2 $self->{REMOTE_USER} = $user;
72 2 $self->{REMOTE_PASS} = $pass;
73 }
74 }
75 3 ▶ if (!defined $self->{REMOTE_PASS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"REMOTE_PASS"}) 1 2 3
76 1 $self->{error} = 'failed to parse HTTP_AUTHORIZATION';
77 }
78 }
79  
80 40 $self->_read_cookie();
81  
82 40 ▶ if ($ENV{REQUEST_METHOD} eq 'POST' || $ENV{REQUEST_METHOD} eq 'PUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'REQUEST_METHOD'} eq 'POST' or $ENV{'REQUEST_METHOD'} eq 'PUT') { } 14 26 40
Condition100%3/3
$ENV{"REQUEST_METHOD"} eq "POST" or $ENV{"REQUEST_METHOD"} eq "PUT"
l!l&&r!l&&!r
13126
MC/DC100%2/2
$ENV{"REQUEST_METHOD"} eq "POST" or $ENV{"REQUEST_METHOD"} eq "PUT"
$ENV{"REQUEST_METHOD"} eq "POST" $ENV{"REQUEST_METHOD"} eq "PUT"
Truth table100%3/3
A or B
A= $ENV{"REQUEST_METHOD"} eq "POST"B= $ENV{"REQUEST_METHOD"} eq "PUT"
ABresult
1X1
011
000
83 14 $self->_read_post($opt->{max_post});
84 14 ▶ if ($opt->{post_with_get}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"post_with_get"}) 2 12 14
85 2 $self->_read_get();
86 }
87 } else {
88 26 $self->_read_get();
89 }
90  
91 40 ▶ if (!$opt->{keep_all_values}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"keep_all_values"}) 39 1 40
92 39 $self->_force_scalar_params();
93 }
94  
95 40 ▶ if (!$opt->{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"raw"}) 37 3 40
96 37 $self->_decode_utf8();
97 }
98  
99 40 return $self;
100 }
101  
102 14 ▶sub param {
Subroutine param: called CC=8 SCAR=20.8
Pod documented
103 14 my ($self, $name) = @_;
104 14 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 12 2 14
105 12 my @result;
106 12 for my $method (qw( POST GET )) {
107 24 ▶ if (exists $self->{$method}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$method}{$name}) 16 8 24
108 16 my $value = $self->{$method}{$name};
109 16 ▶ push @result, ref $value ? @{$value} : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 6 10 16
110 }
111 }
112 12 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 6 12
113 }
114 else {
115 2 ▶ my %p = map { $_ => 1 } keys %{$self->{POST} || {}}, keys %{$self->{GET} || {}};
Condition50%2/4
$self->{'POST'} || {}
l!l
20
$self->{'GET'} || {}
l!l
20
MC/DC0%0/1
$self->{'POST'} || {}
$self->{'POST'}
MC/DC0%0/1
$self->{'GET'} || {}
$self->{'GET'}
Truth table50%1/2
A
A= $self->{'POST'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'GET'}
Aresult
11
00
116 2 return keys %p;
117 }
118 }
119  
120 39 ▶sub _force_scalar_params {
Subroutine _force_scalar_params: called CC=5 SCAR=16.1
121 39 my ($self) = @_;
122 39 for my $p ($self->{GET}, $self->{POST}, $self->{filename}, $self->{mimetype}) {
123 156 ▶ for my $name (keys %{ $p || {} }) {
Condition50%1/2
$p || {}
l!l
1560
MC/DC0%0/1
$p || {}
$p
Truth table50%1/2
A
A= $p
Aresult
11
00
124 54 ▶ if ($name !~ /\[\]\z/xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /\[\]\z/msx) 44 10 54
125 44 $p->{ $name } = $p->{ $name }[0];
126 }
127 }
128 }
129 39 return;
130 }
131  
132 37 ▶sub _decode_utf8 {
Subroutine _decode_utf8: called CC=10 SCAR=23.1
133 37 my ($self) = @_;
134 37 utf8::decode($self->{path});
135 37 for my $key (qw( GET POST filename mimetype cookie )) {
136 185 my %tmp;
137 185 ▶ for my $name (keys %{ $self->{$key} || {} }) {
Condition50%1/2
$self->{$key} || {}
l!l
1850
MC/DC0%0/1
$self->{$key} || {}
$self->{$key}
Truth table50%1/2
A
A= $self->{$key}
Aresult
11
00
138 63 ▶ if (ref $self->{$key}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$key}{$name}) { } 16 47 63
139 16 for my $i (0 .. $#{ $self->{$key}{$name} }) {
140 22 ▶ if (!($key eq 'POST' && defined $self->{mimetype}{$name}[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key eq "POST" and defined $self->{"mimetype"}{$name}[$i]) 22 0 22
Condition66%2/3
$key eq "POST" and defined $self->{"mimetype"}{$name}[$i]
!ll&&!rl&&r
1480
MC/DC0%0/2
$key eq "POST" and defined $self->{"mimetype"}{$name}[$i]
$key eq "POST" defined $self->{"mimetype"}{$name}[$i]
Truth table66%2/3
A and B
A= $key eq "POST"B= defined $self->{"mimetype"}{$name}[$i]
ABresult
0X0
100
111
141 22 utf8::decode($self->{$key}{$name}[$i]);
142 }
143 }
144 }
145 else {
146 47 ▶ if (!($key eq 'POST' && defined $self->{mimetype}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key eq "POST" and defined $self->{"mimetype"}{$name}) 45 2 47
Condition100%3/3
$key eq "POST" and defined $self->{"mimetype"}{$name}
!ll&&!rl&&r
31142
MC/DC100%2/2
$key eq "POST" and defined $self->{"mimetype"}{$name}
$key eq "POST" defined $self->{"mimetype"}{$name}
Truth table100%3/3
A and B
A= $key eq "POST"B= defined $self->{"mimetype"}{$name}
ABresult
0X0
100
111
147 45 utf8::decode($self->{$key}{$name});
148 }
149 }
150 63 my $namestr = $name; utf8::decode($namestr);
151 63 $tmp{ $namestr } = $self->{$key}{$name};
152 }
153 185 $self->{$key} = \%tmp;
154 }
155 37 return;
156 }
157  
158 40 ▶sub _read_cookie {
Subroutine _read_cookie: called CC=5 SCAR=16.2
159 40 my ($self) = @_;
160 40 ▶ foreach (split /;\s?/xms, $self->{ENV}{HTTP_COOKIE} || q{}) {
Condition100%2/2
$self->{'ENV'}{'HTTP_COOKIE'} || ''
l!l
634
MC/DC100%1/1
$self->{'ENV'}{'HTTP_COOKIE'} || ''
$self->{'ENV'}{'HTTP_COOKIE'}
Truth table100%2/2
A
A= $self->{'ENV'}{'HTTP_COOKIE'}
Aresult
11
00
161 9 s/\s*(.*?)\s*/$1/xms;
162 9 my ($key, $value) = split /=/xms, $_, 2;
163 # Some foreign cookies are not in name=value format, so ignore them.
164 9 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 9 9
165 9 $key = uri_unescape_plus($key);
166 9 $value = uri_unescape_plus($value);
167 # A bug in Netscape can cause several cookies with same name to
168 # appear. The FIRST one in HTTP_COOKIE is the most recent version.
169 9 ▶ next if exists $self->{cookie}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"cookie"}{$key} 0 9 9
170 9 $self->{cookie}{$key} = $value;
171 }
172 40 return;
173 }
174  
175 28 ▶sub _read_get {
Subroutine _read_get: called CC=1 SCAR=0.0
176 28 my $self = shift;
177 28 $self->{GET} = burst_urlencoded($self->{ENV}{QUERY_STRING});
178 28 return;
179 }
180  
181 14 ▶sub _read_post {
Subroutine _read_post: called CC=8 SCAR=20.9
182 14 my ($self, $max_post) = @_;
183  
184 14 ▶ $max_post ||= $MAX_POST;
Condition66%2/3
$max_post ||= $MAX_POST
l!l&&r!l&&!r
1130
MC/DC0%0/2
$max_post ||= $MAX_POST
$max_post $MAX_POST
Truth table66%2/3
A ||= B
A= $max_postB= $MAX_POST
ABresult
1X1
011
000
185 14 ▶ if ($self->{ENV}{CONTENT_LENGTH} > $max_post) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENV"}{"CONTENT_LENGTH"} > $max_post) 1 13 14
186 1 $self->{error} = 'POST body too large';
187 1 return;
188 }
189  
190 13 my $buffer = q{};
191 13 ▶ if ($self->{ENV}{CONTENT_LENGTH} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENV"}{"CONTENT_LENGTH"} > 0) 13 0 13
192 13 binmode STDIN;
193 13 my $n = read STDIN, $buffer, $self->{ENV}{CONTENT_LENGTH}, 0;
194 13 $self->{STDIN} = $buffer;
195 13 ▶ if ($n != $self->{ENV}{CONTENT_LENGTH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != $self->{"ENV"}{"CONTENT_LENGTH"}) 1 12 13
196 1 $self->{error} = 'POST body incomplete';
197 1 return;
198 }
199 }
200  
201 # Boundaries are supposed to consist of only the following
202 # (1-70 of them, not ending in ' ') A-Za-z0-9 '()+,_-./:=?
203 12 my $multipart = qr{\Amultipart/form-data;\s+boundary=(.*)\z}xmsi;
204 12 ▶ if ($self->{ENV}{CONTENT_TYPE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENV"}{"CONTENT_TYPE"}) 12 0 12
205 12 ▶ if ($self->{ENV}{CONTENT_TYPE} =~ m/$multipart/xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'ENV'}{'CONTENT_TYPE'} =~ /$multipart/msx) { } 2 10 12
elsif ($self->{'ENV'}{'CONTENT_TYPE'} eq 'application/x-www-form-urlencoded') { } 10 0 10
206 2 my $boundary = $1;
207 2 @{$self}{'POST','filename','mimetype'}
208 = burst_multipart($buffer, $boundary);
209 }
210 elsif ($self->{ENV}{CONTENT_TYPE} eq 'application/x-www-form-urlencoded') {
211 10 $self->{POST} = burst_urlencoded($buffer);
212 }
213 }
214 12 return;
215 }
216  
217  
218 3 1; # Magic true value required at end of module
219 __END__
↑ Summary Easy/Util.pm »