lib/WebDyne/CGI/Simple.pm

filter: Statementstmt 65.2% 90 / 138 Branchbran 37.5% 9 / 24 Conditioncond 34.2% 12 / 35 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 64.2% 18 / 28 Podpod 62.5% 5 / 8 totaltotal 54.6% 136 / 249 CC 32 SCAR 31.3
CC
32
Cov
56%
param_utf8
72.0
new
39.1
Vars
9.9
CRAP 22.9
« blib/lib/WebDyne/CGI/Simple.pm ↑ Summary blib/lib/WebDyne/HTML/Tiny.pm »
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package WebDyne::CGI::Simple;
14  
15  
16 # Pragma
17 #
18 5 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶no warnings qw(uninitialized redefine once);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # WebDyne Modules
25 #
26 5 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 # External modules
31 #
32 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use CGI::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 $CGI::Simple::MOD_PERL=0;
36 5 @ISA=qw(CGI::Simple);
37 5 ▶*CGI::Simple::_mod_perl=sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39  
40 # Version information
41 #
42 5 $VERSION='3.029';
43  
44  
45 # CGI upload vars
46 #
47 5 $CGI::Simple::DISABLE_UPLOADS=$WEBDYNE_CGI_DISABLE_UPLOADS;
48 5 $CGI::Simple::POST_MAX=$WEBDYNE_CGI_POST_MAX;
49  
50  
51 # Debug load
52 #
53 5 debug("Loading %s version $VERSION", __PACKAGE__);
54  
55  
56 #==============================================================================
57  
58 9 ▶sub new {
Subroutine new: called CC=17 SCAR=36.7
Pod documented
59  
60  
61 # New instance of WebDyne::CGI::Common
62 #
63 9 my ($class, $r, %param)=@_;
64 9 debug("class: $class, r: $r, param: %s, caller %s", Dumper(\%param, [caller(0)]));
65
66
67 # Get from cache or construct
68 #
69 9 my $cgi_or;
70 9 ▶ unless ($cgi_or=$r->{'_CGI'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cgi_or = $r->{"_CGI"}) 9 0 9
71
72
73 # Need to construct
74 #
75 # Match the media type independently of optional charset parameters.
76 #
77 9 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (($r->headers_in('content-type') || '') =~ m[\Aapplication/x-www-form-urlencoded(?:\s*;|\s*\z)]i) { } 0 9 9
elsif ($r->content_length or ($r->headers_in('content-type') || '') =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i) { } 1 8 9
Condition100%2/2
$r->headers_in('content-type') || ''
l!l
18
MC/DC100%1/1
$r->headers_in('content-type') || ''
$r->headers_in('content-type')
Truth table100%2/2
A
A= $r->headers_in('content-type')
Aresult
11
00
78 ($r->headers_in('content-type') || '') =~ m{\Aapplication/x-www-form-urlencoded(?:\s*;|\s*\z)}i
79 # Apache can supply a decoded body without Content-Length.
80 #
81 #&& $r->content_length()
82 ) {
83
84 # Normal form POST so can read body in
85 #
86 0 debug('detected application/x-www-form-urlencoded form submission, reading body');
87 0 my $body=$r->body();
88 0 ▶ local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'POST');
Condition0%0/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "POST"
l!l&&r!l&&!r
000
$r->method || 'POST'
l!l
00
MC/DC0%0/2
local $ENV{"REQUEST_METHOD"} ||= $r->method || "POST"
local $ENV{"REQUEST_METHOD"} $r->method
Truth table0%0/3
A ||= B
A= local $ENV{"REQUEST_METHOD"}B= $r->method
ABresult
1X1
011
000
89 0 debug("args: %s, body:$body", $r->args);
90 0 $cgi_or=CGI::Simple->new(join('&', grep {$_} $r->args, $body));
91 }
92 ▶ elsif ($r->content_length() ||
Condition60%3/5
$r->content_length or ($r->headers_in("content-type") || "") =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i
l!l&&r!l&&!r
108
$r->headers_in('content-type') || ''
l!l
08
MC/DC50%1/2
$r->content_length or ($r->headers_in("content-type") || "") =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i
$r->content_length $r->headers_in('content-type')
Truth table66%2/3
A or B
A= $r->content_lengthB= $r->headers_in('content-type')
ABresult
1X1
011
000
93 ($r->headers_in('content-type') || '') =~ m{\Amultipart/form-data(?:\s*;|\s*\z)}i) {
94
95 # Need to read in some other POST content, e.g. file upload. Need to manipulate CGI::Simple internals here, pretty ugly.
96 #
97 1 my $length=$r->content_length();
98 1 my $fh;
99 1 ▶ if (defined($length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) { } 1 0 1
100 1 $fh=$r->input();
101 }
102 else {
103 # CGI::Simple requires a length for multipart parsing. Buffer
104 # through the bounded adapter reader only when it is absent.
105 #
106 0 require IO::String;
107 0 my $body=$r->body();
108 0 $length=length($body);
109 0 $fh=IO::String->new($body);
110 }
111 1 debug('detected other POST submission, reading body from %s', $fh);
112 1 local $ENV{'REQUEST_METHOD'}='GET';
113 1 $cgi_or=CGI::Simple->new($r->args);
114 1 local $ENV{'CONTENT_LENGTH'}=$length;
115 1 ▶ local $ENV{'CONTENT_TYPE'} ||= ($r->headers_in('Content-Type') || 'multipart/form-data');
Condition40%2/5
local $ENV{"CONTENT_TYPE"} ||= $r->headers_in("Content-Type") || "multipart/form-data"
l!l&&r!l&&!r
010
$r->headers_in('Content-Type') || 'multipart/form-data'
l!l
10
MC/DC0%0/2
local $ENV{"CONTENT_TYPE"} ||= $r->headers_in("Content-Type") || "multipart/form-data"
local $ENV{"CONTENT_TYPE"} $r->headers_in('Content-Type')
Truth table33%1/3
A ||= B
A= local $ENV{"CONTENT_TYPE"}B= $r->headers_in('Content-Type')
ABresult
1X1
011
000
116 1 ▶ local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'POST');
Condition40%2/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "POST"
l!l&&r!l&&!r
010
$r->method || 'POST'
l!l
10
MC/DC0%0/2
local $ENV{"REQUEST_METHOD"} ||= $r->method || "POST"
local $ENV{"REQUEST_METHOD"} $r->method
Truth table33%1/3
A ||= B
A= local $ENV{"REQUEST_METHOD"}B= $r->method
ABresult
1X1
011
000
117 1 debug('fh: %s, content_length: %s, content_type: %s, request_method: %s', $fh, @ENV{qw(CONTENT_LENGTH CONTENT_TYPE REQUEST_METHOD)});
118 1 local *STDIN=$fh;
119 1 $cgi_or->_parse_multipart($fh);
120 }
121 else {
122
123 # Don't need POST body, just get query param args
124 #
125 8 debug('non POST submissions, just using args: %s', $r->args);
126 8 ▶ local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'GET');
Condition40%2/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "GET"
l!l&&r!l&&!r
080
$r->method || 'GET'
l!l
80
MC/DC0%0/2
local $ENV{"REQUEST_METHOD"} ||= $r->method || "GET"
local $ENV{"REQUEST_METHOD"} $r->method
Truth table33%1/3
A ||= B
A= local $ENV{"REQUEST_METHOD"}B= $r->method
ABresult
1X1
011
000
127 8 $cgi_or=CGI::Simple->new($r->args);
128 }
129  
130  
131 # Keep cookie reads tied to this request, including requests with
132 # no Cookie header. CGI::Simple otherwise reads the process environment.
133 #
134 9 ▶ $cgi_or->{'_cookie_header'}=$r->headers_in('Cookie') || '';
Condition50%1/2
$r->headers_in('Cookie') || ''
l!l
09
MC/DC0%0/1
$r->headers_in('Cookie') || ''
$r->headers_in('Cookie')
Truth table50%1/2
A
A= $r->headers_in('Cookie')
Aresult
11
00
135  
136 }
137
138
139 # Add any param
140 #
141 0 map { $cgi_or->param($_, $param{$_}) } keys %param;
142 9 debug("cgi_or: $cgi_or, %s", Dumper($cgi_or));
143
144  
145 # Bless and return
146 #
147 9 return bless($cgi_or, __PACKAGE__);
148
149 }
150  
151  
152 0 ▶sub cookie {
Subroutine cookie: not called CC=1 SCAR=6.9
Pod documented
153  
154 0 my $self=shift();
155 0 local $ENV{'HTTP_COOKIE'}=$self->{'_cookie_header'};
156 0 local $ENV{'COOKIE'}='';
157 0 return $self->SUPER::cookie(@_);
158  
159 }
160  
161  
162 0 ▶sub raw_cookie {
Subroutine raw_cookie: not called CC=1 SCAR=6.9
Pod documented
163  
164 0 my $self=shift();
165 0 local $ENV{'HTTP_COOKIE'}=$self->{'_cookie_header'};
166 0 local $ENV{'COOKIE'}='';
167 0 return $self->SUPER::raw_cookie(@_);
168  
169 }
170  
171  
172 0 ▶sub param_utf8 {
Subroutine param_utf8: not called CC=8 SCAR=42.8
Pod undocumented
173  
174 0 my $self=shift();
175 0 require Carp;
176 0 ▶ (@_==1 && defined($_[0])) ||
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and defined $_[0] or Carp::croak("param_utf8 requires one parameter name") 0 0 0
Condition0%0/3
@_ == 1 and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and defined $_[0]
@_ == 1 defined $_[0]
Truth table0%0/3
A and B
A= @_ == 1B= defined $_[0]
ABresult
0X0
100
111
177 Carp::croak('param_utf8 requires one parameter name');
178 0 require Encode;
179  
180 # Decode copies only, preserving the ordinary byte-oriented parameter API.
181 #
182 0 ▶ my @value=wantarray ? $self->param(@_) : scalar($self->param(@_));
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
183 0 foreach my $value (@value) {
184 0 ▶ next if !defined($value) || utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or utf8::is_utf8($value) 0 0 0
Condition0%0/3
not defined $value or utf8::is_utf8($value)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or utf8::is_utf8($value)
not defined $value utf8::is_utf8($value)
Truth table0%0/3
not(A) or B
A= not defined $valueB= utf8::is_utf8($value)
ABresult
1X1
011
000
185 0 $value=Encode::decode('UTF-8', $value, Encode::FB_CROAK());
186 }
187 0 ▶ return wantarray ? @value : $value[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
188  
189 }
190  
191  
192 8 ▶sub Vars {
Subroutine Vars: called CC=5 SCAR=22.9
Pod documented
193  
194 # Simulate Plack::Request Hash::MultiValue response
195 #
196 8 my ($self, $hr)=@_;
197 8 ▶ if ($hr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr) { } 0 8 8
198
199 # Pushing back into CGI
200 #
201 0 $self->delete_all();
202 0 foreach my $param (keys %{$hr}) {
203 0 $self->param($param, $hr->get_all($param));
204 }
205 0 return $hr;
206
207 }
208 else {
209 8 my @pairs;
210 8 foreach my $param ($self->param()) {
211 0 my @values=$self->param($param);
212 0 map { push @pairs, ( $param => $_ ) } @values;
213 }
214 8 $hr=Hash::MultiValue->new(@pairs)
215 }
216  
217 0 ▶ return wantarray ? %{$hr} : $hr
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
218  
219 }
220  
221  
222 0 ▶sub env {
Subroutine env: not called CC=1 SCAR=6.9
Pod undocumented
223  
224 0 return \%ENV
225
226 }
227  
228  
229 6 ▶sub upload {
Subroutine upload: called CC=1 SCAR=0.0
Pod documented
230  
231 6 local $ENV{'CONTENT_TYPE'}='multipart/form-data';
232 6 return shift()->SUPER::upload(@_);
233
234 }
235  
236  
237 2 ▶sub uploads {
Subroutine uploads: called CC=5 SCAR=16.2
Pod undocumented
238  
239 # Replicate Plack::Request::Uploads->uploads()
240 #
241 2 my $self=shift();
242 2 my @pairs;
243 2 foreach my $param ($self->param()) {
244
245 4 my @fn = $self->param($param);
246 4 debug('fn: %s', Dumper(\@fn));
247 4 ▶ next unless @fn;
Branch: 1 branch
BranchTrueFalseTotal
unless @fn 0 4 4
248  
249 4 foreach my $fn (@fn) {
250 4 ▶ next unless $fn; # skip undef
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 4 4
251 4 my $fh = $self->upload($fn);
252 my $upload_or = WebDyne::CGI::Simple::Upload->new(
253 filename => $fn,
254 size => $self->{'.tmpfiles'}{$fn}{'size'},
255 4 mime => $self->{'.tmpfiles'}{$fn}{'mime'},
256 fh => $fh,
257 );
258 4 push @pairs, ($param => $upload_or);
259 }
260 }
261 2 debug('pairs: %s', Dumper(\@pairs));
262  
263 2 return Hash::MultiValue->new(@pairs);
264 }
265  
266  
267 # Emulate Plack::Request::Upload object
268 #
269 package WebDyne::CGI::Simple::Upload;
270  
271 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 5 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
275 4 my ($class, %args) = @_;
276 4 return bless \%args, $class;
277 }
278 3 ▶sub filename { $_[0]->{'filename'} }
Subroutine filename: called CC=1 SCAR=0.0
279 0 ▶sub size { $_[0]->{'size'} }
Subroutine size: not called CC=1 SCAR=6.9
280 0 ▶sub content {
Subroutine content: not called CC=1 SCAR=6.9
281 0 my $self = shift;
282 0 seek ($self->{'fh'}, 0, 0);
283 0 local $/;
284 0 my $fh=$self->{'fh'};
285 0 return <$fh>;
286 }
287 0 ▶sub fh { $_[0]->{'fh'} }
Subroutine fh: not called CC=1 SCAR=6.9
288 0 ▶sub path { $_[0]->{'fh'} }
Subroutine path: not called CC=1 SCAR=6.9
289 0 ▶sub content_type { $_[0]->{'mime'} }
Subroutine content_type: not called CC=1 SCAR=6.9
290 0 ▶sub basename { basename($_[0]->{'filename'}) }
Subroutine basename: not called CC=1 SCAR=6.9
291  
292 5 1;
293 __END__
« blib/lib/WebDyne/CGI/Simple.pm ↑ Summary blib/lib/WebDyne/HTML/Tiny.pm »