blib/lib/CGI/PSGI.pm

filter: Statementstmt 84.5% 126 / 149 Branchbran 77.6% 59 / 76 Conditioncond 79.4% 31 / 39 MC/DCmcdc 57.1% 12 / 21 Subroutinesub 85.7% 12 / 14 Podpod 66.6% 4 / 6 totaltotal 80.0% 244 / 305 CC 61 SCAR 29.4
CC
61
Cov
82%
psgi_header
32.6
read_from_stdin
20.0
_rearrange_params
12.2
CRAP 18.9
↑ Summary
1 package CGI::PSGI;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 our $VERSION = '0.17';
6  
7 12 ▶use base qw(CGI);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 37 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
10 37 my($class, $env) = @_;
11 37 CGI::initialize_globals();
12  
13 37 my $self = bless {
14 psgi_env => $env,
15 use_tempfile => 1,
16 }, $class;
17  
18 37 local *ENV = $env;
19 37 local $CGI::MOD_PERL = 0;
20 # never fall back to CGI.pm's offline debugging mode, which reads the
21 # params from the process' own @ARGV: older CGI.pm does that for any
22 # unrecognised REQUEST_METHOD, e.g. OPTIONS (CVE-2026-120637), and all
23 # versions do it for an env without one
24 37 local $CGI::DEBUG = 0;
25 37 $self->SUPER::init;
26  
27 37 $self;
28 }
29  
30 0 ▶sub env {
Subroutine env: not called CC=1 SCAR=6.9
Pod documented
31 0 $_[0]->{psgi_env};
32 }
33  
34 # CGI.pm expects read() on STDIN semantics: the full $len bytes unless EOF or
35 # an error. psgi.input->read may return fewer (like sysread), so keep reading
36 # until $len, EOF (0) or an error (undef). Returns the number of bytes read,
37 # or the 0/undef of the first read if nothing was read.
38 25 ▶sub read_from_client {
Subroutine read_from_client: called CC=5 SCAR=16.1
Pod undocumented
39 25 my($self, $buff, $len, $offset) = @_;
40 25 my $input = $self->{psgi_env}{'psgi.input'};
41 25 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
1312
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
42  
43 25 my $total = 0;
44 25 ▶ while ($total < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total < $len) 8371 21 8392
45 8371 my $read = $input->read($$buff, $len - $total, $offset + $total);
46 8371 ▶ return $total || $read unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 4 8367 8371
Condition66%2/3
$total || $read
l!l&&r!l&&!r
202
MC/DC50%1/2
$total || $read
$total $read
Truth table66%2/3
A || B
A= $totalB= $read
ABresult
1X1
011
000
47 8367 $total += $read;
48 }
49 21 return $total;
50 }
51  
52 # copied from CGI.pm. Deprecated: no CGI.pm since (at least) 4.04 calls it,
53 # and it will be removed in a future release.
54 0 ▶sub read_from_stdin {
Subroutine read_from_stdin: not called CC=4 SCAR=30.0
Pod undocumented
55 0 my($self, $buff) = @_;
56  
57 0 my($eoffound) = 0;
58 0 my($localbuf) = '';
59 0 my($tempbuf) = '';
60 0 my($bufsiz) = 1024;
61 0 my($res);
62  
63 0 ▶ while ($eoffound == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eoffound == 0) 0 0 0
64 0 $res = $self->{psgi_env}{'psgi.input'}->read($tempbuf, $bufsiz, 0);
65  
66 0 ▶ if ( !defined($res) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
67 # TODO: how to do error reporting ?
68 0 $eoffound = 1;
69 0 last;
70 }
71 0 ▶ if ( $res == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == 0) 0 0 0
72 0 $eoffound = 1;
73 0 last;
74 }
75 0 $localbuf .= $tempbuf;
76 }
77  
78 0 $$buff = $localbuf;
79  
80 0 return $res;
81 }
82  
83 # Like CGI::Util::_rearrange_params (private to CGI.pm, so not used
84 # directly): maps -named or positional arguments onto the slots in $order
85 # and returns the slots, plus a hash ref of the leftover named arguments.
86 88 ▶sub _rearrange_params {
Subroutine _rearrange_params: called CC=12 SCAR=25.0
87 88 my($order, @param) = @_;
88 88 ▶ return [] unless @param;
Branch: 1 branch
BranchTrueFalseTotal
unless @param 1 87 88
89  
90 87 ▶ if (ref($param[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param[0] eq 'HASH') { } 0 87 87
91 0 @param = %{$param[0]};
92 } else {
93 return \@param
94 87 ▶ unless defined($param[0]) && substr($param[0],0,1) eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param[0] and substr($param[0], 0, 1) eq "-" 32 55 87
Condition66%2/3
defined $param[0] and substr($param[0], 0, 1) eq "-"
!ll&&!rl&&r
03255
MC/DC50%1/2
defined $param[0] and substr($param[0], 0, 1) eq "-"
defined $param[0] substr($param[0], 0, 1) eq "-"
Truth table66%2/3
A and B
A= defined $param[0]B= substr($param[0], 0, 1) eq "-"
ABresult
0X0
100
111
95 }
96  
97 55 my(%pos, $i);
98 55 $i = 0;
99 55 for my $slot (@$order) {
100 451 ▶ $pos{lc $_} = $i for ref($slot) eq 'ARRAY' ? @$slot : $slot;
Branch: 1 branch
BranchTrueFalseTotal
ref $slot eq 'ARRAY' ? : 110 341 451
101 451 $i++;
102 }
103  
104 55 my %params = @param;
105 55 my(@result, %leftover);
106 55 $#result = $#$order;
107  
108 # favour CONTENT-TYPE over TYPE etc. when both are given, as CGI.pm does
109 # (same comparator as CGI.pm, for compatibility)
110 55 ▶ for my $k (sort { $a =~ /content/i ? 1 : $b =~ /content/i ? -1 : $a cmp $b } keys %params) {
Branch: 2 branches
BranchTrueFalseTotal
$a =~ /content/i ? : 2 112 114
$b =~ /content/i ? : 4 108 112
111 129 (my $key = lc $k) =~ s/^-//;
112 129 ▶ if (exists $pos{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pos{$key}) { } 94 35 129
113 94 $result[$pos{$key}] = $params{$k};
114 } else {
115 35 $leftover{$key} = $params{$k};
116 }
117 }
118  
119 55 return \@result, \%leftover;
120 }
121  
122 # Unfold CRLF + whitespace (RFC 822) in place, and die on any other newline.
123 138 ▶sub _check_header_values {
Subroutine _check_header_values: called CC=5 SCAR=16.1
124 138 for my $header (@_) {
125 623 ▶ next unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 284 339 623
126 339 $header =~ s/$CGI::CRLF(\s)/$1/g;
127 339 ▶ if ($header =~ m/$CGI::CRLF|\015|\012/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /$CGI::CRLF|\015|\012/) 17 322 339
128 # shorten very long values in the diagnostic
129 17 ▶ $header = substr($header,0,72).'...' if length $header > 72;
Branch: 1 branch
BranchTrueFalseTotal
if length $header > 72 0 17 17
130 17 die "Invalid header value contains a newline not followed by whitespace: $header";
131 }
132 }
133 }
134  
135 # Ported from CGI::header. Values are used as given: like CGI.pm 5.02 they are
136 # no longer HTML unescaped, which decoded entities such as &#13;&#10; after
137 # the newline check (CVE-2026-120639).
138 72 ▶sub psgi_header {
Subroutine psgi_header: called CC=32 SCAR=34.8
Pod documented
139 72 my($self, @p) = @_;
140  
141 72 my(@header);
142  
143 72 my $order = [['TYPE','CONTENT_TYPE','CONTENT-TYPE'],
144 'STATUS',['COOKIE','COOKIES','SET-COOKIE'],'TARGET',
145 'EXPIRES','NPH','CHARSET',
146 'ATTACHMENT','P3P'];
147 72 my($result, $leftover) = _rearrange_params($order, @p);
148 my($type,$status,$cookie,$target,$expires,$nph,$charset,$attachment,$p3p) =
149 72 @{$result}[0 .. $#$order];
150  
151 # extra headers: positional ones given as name=value or as a ready-made
152 # "Name: value" line (which CGI.pm 5.02 sends as it is), and leftover
153 # named ones, where -x_foo => 'bar' becomes X-foo: bar
154 72 my @other_headers;
155 72 my @lines = grep defined, @{$result}[@$order .. $#$result];
156 # check the whole lines first, so a newline anywhere in one dies
157 # rather than the line being skipped
158 72 _check_header_values(@lines);
159 66 for my $line (@lines) {
160 # Don't use \s because of perl bug 21951
161 23 ▶ if (my($name, $value) = $line =~ /([^ \r\n\t=]+)=\"?(.+?)\"?$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $line =~ /([^ \r\n\t=]+)=\"?(.+?)\"?$/s) { } 5 18 23
elsif (() = $line =~ /^([!#\$%&'*+.^_`|~0-9A-Za-z-]+):[ \t]*(.*?)[ \t]*$/s) { } 13 5 18
162 5 push @other_headers, ucfirst(lc $name), $value;
163 } elsif (($name, $value) = $line =~ /^([!#\$%&'*+.^_`|~0-9A-Za-z-]+):[ \t]*(.*?)[ \t]*$/s) {
164 # CGI.pm ucfirsts the whole line
165 13 push @other_headers, ucfirst($name), $value;
166 }
167 # anything else (no colon, or not a valid header name) can't be
168 # made a header, and is skipped
169 }
170 66 ▶ for my $name (sort keys %{ $leftover || {} }) {
Condition100%2/2
$leftover || {}
l!l
4422
MC/DC100%1/1
$leftover || {}
$leftover
Truth table100%2/2
A
A= $leftover
Aresult
11
00
171 28 ▶ next unless defined $leftover->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $leftover->{$name} 0 28 28
172 28 (my $header = lc $name) =~ tr/_/-/;
173 28 push @other_headers, ucfirst($header), "$leftover->{$name}";
174 }
175  
176 # Since $cookie and $p3p may be array references, stringify them
177 # before the newline checks.
178 66 my @cookie;
179 66 ▶ for (ref($cookie) eq 'ARRAY' ? @$cookie : $cookie) {
Branch: 1 branch
BranchTrueFalseTotal
ref $cookie eq 'ARRAY' ? : 5 61 66
180 66 ▶ my $cs = UNIVERSAL::isa($_,'CGI::Cookie') ? $_->as_string : $_;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'CGI::Cookie') ? : 1 65 66
181 66 ▶ push @cookie, $cs if defined $cs and $cs ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $cs and $cs ne "" 7 59 66
Condition100%3/3
defined $cs and $cs ne ""
!ll&&!rl&&r
38217
MC/DC100%2/2
defined $cs and $cs ne ""
defined $cs $cs ne ""
Truth table100%3/3
A and B
A= defined $csB= $cs ne ""
ABresult
0X0
100
111
182 }
183 66 ▶ $p3p = join(" ", @$p3p) if ref($p3p) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $p3p eq "ARRAY" 2 64 66
184  
185 # header names and values, per RFC 822
186 66 _check_header_values($type,$status,@cookie,$target,$expires,$nph,$charset,$attachment,$p3p,@other_headers);
187  
188 55 ▶ $type ||= 'text/html' unless defined($type);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 12 43 55
Condition50%1/2
$type ||= "text/html"
l!l
012
MC/DC0%0/1
$type ||= "text/html"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
189 55 ▶ if (defined $charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $charset) { } 22 33 55
190 22 $self->charset($charset);
191 } else {
192 33 ▶ $charset = $self->charset if $type =~ /^text\//;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/] 19 14 33
193 }
194 55 ▶ $charset ||= '';
Condition100%2/2
$charset ||= ""
l!l
2035
MC/DC100%1/1
$charset ||= ""
$charset
Truth table100%2/2
A
A= $charset
Aresult
11
00
195  
196 55 ▶ $type .= "; charset=$charset"
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "" and not $type =~ /\bcharset\b/ and defined $charset and $charset ne "" 20 35 55
Condition77%7/9
$type ne "" and not $type =~ /\bcharset\b/ and defined $charset and $charset ne ""
!ll&&!rl&&r
82720
$type ne "" and not $type =~ /\bcharset\b/ and defined $charset
!ll&&!rl&&r
8047
$type ne "" and not $type =~ /\bcharset\b/
!ll&&!rl&&r
8047
MC/DC50%2/4
$type ne "" and not $type =~ /\bcharset\b/ and defined $charset and $charset ne ""
$type ne "" not $type =~ /\bcharset\b/ defined $charset $charset ne ""
Truth table60%3/5
A and not(B) and C and D
A= $type ne ""B= not $type =~ /\bcharset\b/C= defined $charsetD= $charset ne ""
ABCDresult
0XXX0
10XX0
110X0
11100
11111
197 if $type ne ''
198 and $type !~ /\bcharset\b/
199 and defined $charset
200 and $charset ne '';
201  
202 55 ▶ push(@header, "Window-Target", $target) if $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target 0 55 55
203 55 ▶ push(@header, "P3P", qq(policyref="/w3c/p3p.xml", CP="$p3p")) if $p3p;
Branch: 1 branch
BranchTrueFalseTotal
if $p3p 1 54 55
204  
205 # push all the cookies -- there may be several
206 55 push(@header, map { ("Set-Cookie", $_) } @cookie);
207  
208 # if the user indicates an expiration time, then we need
209 # both an Expires and a Date header (so that the browser is
210 # uses OUR clock)
211 55 ▶ push(@header,"Expires", CGI::expires($expires,'http'))
Branch: 1 branch
BranchTrueFalseTotal
if $expires 0 55 55
212 if $expires;
213 55 ▶ push(@header,"Date", CGI::expires(0,'http')) if $expires || $cookie || $nph;
Branch: 1 branch
BranchTrueFalseTotal
if $expires or $cookie or $nph 5 50 55
Condition66%4/6
$expires or $cookie or $nph
l!l&&r!l&&!r
5050
$expires or $cookie
l!l&&r!l&&!r
0550
MC/DC33%1/3
$expires or $cookie or $nph
$expires $cookie $nph
Truth table50%2/4
A or B or C
A= $expiresB= $cookieC= $nph
ABCresult
1XX1
01X1
0011
0000
214 55 ▶ push(@header,"Pragma", "no-cache") if $self->cache();
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache 0 55 55
215 55 ▶ if ($attachment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attachment) 2 53 55
216 # the filename is a quoted-string (RFC 9110 5.6.4), so a " or \ in
217 # it must be escaped, or it could inject other parameters
218 # (CVE-2026-120647)
219 2 (my $filename = $attachment) =~ s/(["\\])/\\$1/g;
220 2 push(@header,"Content-Disposition", "attachment; filename=\"$filename\"");
221 }
222 55 push(@header, @other_headers);
223  
224 55 ▶ push(@header,"Content-Type", $type) if $type ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "" 47 8 55
225  
226 55 ▶ $status ||= "200";
Condition100%2/2
$status ||= 200
l!l
1342
MC/DC100%1/1
$status ||= 200
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
227 55 $status =~ s/\D*$//;
228  
229 55 return $status, \@header;
230 }
231  
232 # Ported from CGI.pm's redirect() method. Like CGI.pm 5.02, values are passed
233 # on as given: HTML unescaping them decoded entities in a (possibly app
234 # validated) Location, e.g. "/&#47evil.example/" became "//evil.example/"
235 # (CVE-2026-120638).
236 16 ▶sub psgi_redirect {
Subroutine psgi_redirect: called CC=8 SCAR=22.0
Pod documented
237 16 my ($self,@p) = @_;
238 16 my $order = [['LOCATION','URI','URL'],'TARGET','STATUS',['COOKIE','COOKIES','SET-COOKIE'],'NPH'];
239 16 my($result, $leftover) = _rearrange_params($order, @p);
240 16 my($url,$target,$status,$cookie,$nph,@other) = @$result;
241 16 ▶ $status = '302 Found' unless defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 16 0 16
242 16 ▶ $url ||= $self->self_url;
Condition66%2/3
$url ||= $self->self_url
l!l&&r!l&&!r
1330
MC/DC0%0/2
$url ||= $self->self_url
$url $self->self_url
Truth table66%2/3
A ||= B
A= $urlB= $self->self_url
ABresult
1X1
011
000
243 16 my(@o);
244 # extra positional parameters, given as name=value
245 0 ▶ for (@other) { next unless defined; tr/\"//d; push(@o,split("=",$_,2)); }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
246 # extra named parameters
247 16 ▶ push(@o, map { ("-$_" => $leftover->{$_}) } sort keys %{ $leftover || {} });
Condition100%2/2
$leftover || {}
l!l
115
MC/DC100%1/1
$leftover || {}
$leftover
Truth table100%2/2
A
A= $leftover
Aresult
11
00
248 16 unshift(@o,
249 '-Status' => $status,
250 '-Location'=> $url,
251 '-nph' => $nph);
252 16 ▶ unshift(@o,'-Target'=>$target) if $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target 0 16 16
253 16 unshift(@o,'-Type'=>'');
254 16 ▶ unshift(@o,'-Cookie'=>$cookie) if $cookie;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie 3 13 16
255 16 return $self->psgi_header(@o);
256 }
257  
258 # The list is auto generated and modified with:
259 # perl -nle '/^sub (\w+)/ and $sub=$1; \
260 # /^}\s*$/ and do { print $sub if $code{$sub} =~ /([\%\$]ENV|http\()/; undef $sub };\
261 # $code{$sub} .= "$_\n" if $sub; \
262 # /^\s*package [^C]/ and exit' \
263 # `perldoc -l CGI`
264 12 for my $method (qw(
265 url_param
266 url
267 cookie
268 raw_cookie
269 _name_and_path_from_env
270 request_method
271 content_type
272 path_translated
273 request_uri
274 Accept
275 user_agent
276 virtual_host
277 remote_host
278 remote_addr
279 referer
280 server_name
281 server_software
282 virtual_port
283 server_port
284 server_protocol
285 http
286 https
287 remote_ident
288 auth_type
289 remote_user
290 user_name
291 read_multipart
292 read_multipart_related
293 env_query_string
294 )) {
295 # skip methods the installed CGI.pm doesn't have (env_query_string
296 # is new in CGI.pm 4.x)
297 348 ▶ next unless CGI->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless "CGI"->can($method) 0 348 348
298 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
299 123 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
300 123 my $self = shift;
301 123 my $super = "SUPER::$method";
302 123 local *ENV = $self->{psgi_env};
303 123 $self->$super(@_);
304 348 };
305 }
306  
307 36 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
308 36 my $self = shift;
309 36 CGI::initialize_globals();
310 }
311  
312 12 1;
313 __END__
↑ Summary