blib/lib/CGI/PSGI.pm

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