blib/lib/WebDyne/CGI/Simple.pm

filter: Statementstmt 61.5% 85 / 138 Branchbran 54.1% 13 / 24 Conditioncond 40.0% 14 / 35 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 60.7% 17 / 28 Podpod 62.5% 5 / 8 totaltotal 55.4% 138 / 249 CC 32 SCAR 32.0
CC
32
Cov
57%
new
68.2
uploads
30.0
param_utf8
8.1
CRAP 24.5
« blib/lib/WebDyne/Chain.pm ↑ Summary lib/WebDyne/CGI/Simple.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 42 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 42 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 42 ▶no warnings qw(uninitialized redefine once);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # WebDyne Modules
25 #
26 42 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 42 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 # External modules
31 #
32 42 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 42 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 42 ▶use CGI::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 42 $CGI::Simple::MOD_PERL=0;
36 42 @ISA=qw(CGI::Simple);
37 42 ▶*CGI::Simple::_mod_perl=sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39  
40 # Version information
41 #
42 42 $VERSION='3.029';
43  
44  
45 # CGI upload vars
46 #
47 42 $CGI::Simple::DISABLE_UPLOADS=$WEBDYNE_CGI_DISABLE_UPLOADS;
48 42 $CGI::Simple::POST_MAX=$WEBDYNE_CGI_POST_MAX;
49  
50  
51 # Debug load
52 #
53 42 0 && debug("Loading %s version $VERSION", __PACKAGE__);
54  
55  
56 #==============================================================================
57  
58 260 ▶sub new {
Subroutine new: called CC=17 SCAR=42.2
Pod documented
59  
60  
61 # New instance of WebDyne::CGI::Common
62 #
63 260 my ($class, $r, %param)=@_;
64 260 0 && debug("class: $class, r: $r, param: %s, caller %s", Dumper(\%param, [caller(0)]));
65
66
67 # Get from cache or construct
68 #
69 260 my $cgi_or;
70 260 ▶ unless ($cgi_or=$r->{'_CGI'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cgi_or = $r->{"_CGI"}) 260 0 260
71
72
73 # Need to construct
74 #
75 # Match the media type independently of optional charset parameters.
76 #
77 260 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (($r->headers_in('content-type') || '') =~ m[\Aapplication/x-www-form-urlencoded(?:\s*;|\s*\z)]i) { } 1 259 260
elsif ($r->content_length or ($r->headers_in('content-type') || '') =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i) { } 0 259 259
Condition100%2/2
$r->headers_in('content-type') || ''
l!l
1259
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 1 0 && debug('detected application/x-www-form-urlencoded form submission, reading body');
87 1 my $body=$r->body();
88 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
89 1 0 && debug("args: %s, body:$body", $r->args);
90 1 $cgi_or=CGI::Simple->new(join('&', grep {$_} $r->args, $body));
91 }
92 ▶ elsif ($r->content_length() ||
Condition40%2/5
$r->content_length or ($r->headers_in("content-type") || "") =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i
l!l&&r!l&&!r
00259
$r->headers_in('content-type') || ''
l!l
0259
MC/DC0%0/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 table33%1/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 0 my $length=$r->content_length();
98 0 my $fh;
99 0 ▶ if (defined($length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) { } 0 0 0
100 0 $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 0 0 && debug('detected other POST submission, reading body from %s', $fh);
112 0 local $ENV{'REQUEST_METHOD'}='GET';
113 0 $cgi_or=CGI::Simple->new($r->args);
114 0 local $ENV{'CONTENT_LENGTH'}=$length;
115 0 ▶ local $ENV{'CONTENT_TYPE'} ||= ($r->headers_in('Content-Type') || 'multipart/form-data');
Condition0%0/5
local $ENV{"CONTENT_TYPE"} ||= $r->headers_in("Content-Type") || "multipart/form-data"
l!l&&r!l&&!r
000
$r->headers_in('Content-Type') || 'multipart/form-data'
l!l
00
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 table0%0/3
A ||= B
A= local $ENV{"CONTENT_TYPE"}B= $r->headers_in('Content-Type')
ABresult
1X1
011
000
116 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
117 0 0 && debug('fh: %s, content_length: %s, content_type: %s, request_method: %s', $fh, @ENV{qw(CONTENT_LENGTH CONTENT_TYPE REQUEST_METHOD)});
118 0 local *STDIN=$fh;
119 0 $cgi_or->_parse_multipart($fh);
120 }
121 else {
122
123 # Don't need POST body, just get query param args
124 #
125 259 0 && debug('non POST submissions, just using args: %s', $r->args);
126 259 ▶ local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'GET');
Condition40%2/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "GET"
l!l&&r!l&&!r
02590
$r->method || 'GET'
l!l
2590
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 259 $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 260 ▶ $cgi_or->{'_cookie_header'}=$r->headers_in('Cookie') || '';
Condition100%2/2
$r->headers_in('Cookie') || ''
l!l
2258
MC/DC100%1/1
$r->headers_in('Cookie') || ''
$r->headers_in('Cookie')
Truth table100%2/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 260 0 && debug("cgi_or: $cgi_or, %s", Dumper($cgi_or));
143
144  
145 # Bless and return
146 #
147 260 return bless($cgi_or, __PACKAGE__);
148
149 }
150  
151  
152 8 ▶sub cookie {
Subroutine cookie: called CC=1 SCAR=0.0
Pod documented
153  
154 8 my $self=shift();
155 8 local $ENV{'HTTP_COOKIE'}=$self->{'_cookie_header'};
156 8 local $ENV{'COOKIE'}='';
157 8 return $self->SUPER::cookie(@_);
158  
159 }
160  
161  
162 3 ▶sub raw_cookie {
Subroutine raw_cookie: called CC=1 SCAR=0.0
Pod documented
163  
164 3 my $self=shift();
165 3 local $ENV{'HTTP_COOKIE'}=$self->{'_cookie_header'};
166 3 local $ENV{'COOKIE'}='';
167 3 return $self->SUPER::raw_cookie(@_);
168  
169 }
170  
171  
172 18 ▶sub param_utf8 {
Subroutine param_utf8: called CC=8 SCAR=21.0
Pod undocumented
173  
174 18 my $self=shift();
175 18 require Carp;
176 18 ▶ (@_==1 && defined($_[0])) ||
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 and defined $_[0] or Carp::croak("param_utf8 requires one parameter name") 18 0 18
Condition33%1/3
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0018
MC/DC0%0/2
@_ == 1 and defined $_[0]
@_ == 1 defined $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= defined $_[0]
ABresult
0X0
100
111
177 Carp::croak('param_utf8 requires one parameter name');
178 18 require Encode;
179  
180 # Decode copies only, preserving the ordinary byte-oriented parameter API.
181 #
182 18 ▶ my @value=wantarray ? $self->param(@_) : scalar($self->param(@_));
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 14 18
183 18 foreach my $value (@value) {
184 18 ▶ next if !defined($value) || utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or utf8::is_utf8($value) 4 14 18
Condition100%3/3
not defined $value or utf8::is_utf8($value)
l!l&&r!l&&!r
2214
MC/DC100%2/2
not defined $value or utf8::is_utf8($value)
not defined $value utf8::is_utf8($value)
Truth table100%3/3
not(A) or B
A= not defined $valueB= utf8::is_utf8($value)
ABresult
1X1
011
000
185 14 $value=Encode::decode('UTF-8', $value, Encode::FB_CROAK());
186 }
187 16 ▶ return wantarray ? @value : $value[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 12 16
188  
189 }
190  
191  
192 438 ▶sub Vars {
Subroutine Vars: called CC=5 SCAR=19.3
Pod documented
193  
194 # Simulate Plack::Request Hash::MultiValue response
195 #
196 438 my ($self, $hr)=@_;
197 438 ▶ if ($hr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr) { } 0 438 438
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 438 my @pairs;
210 438 foreach my $param ($self->param()) {
211 74 my @values=$self->param($param);
212 74 map { push @pairs, ( $param => $_ ) } @values;
213 }
214 438 $hr=Hash::MultiValue->new(@pairs)
215 }
216  
217 0 ▶ return wantarray ? %{$hr} : $hr
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 438 438
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 0 ▶sub upload {
Subroutine upload: not called CC=1 SCAR=6.9
Pod documented
230  
231 0 local $ENV{'CONTENT_TYPE'}='multipart/form-data';
232 0 return shift()->SUPER::upload(@_);
233
234 }
235  
236  
237 0 ▶sub uploads {
Subroutine uploads: not called CC=5 SCAR=34.0
Pod undocumented
238  
239 # Replicate Plack::Request::Uploads->uploads()
240 #
241 0 my $self=shift();
242 0 my @pairs;
243 0 foreach my $param ($self->param()) {
244
245 0 my @fn = $self->param($param);
246 0 0 && debug('fn: %s', Dumper(\@fn));
247 0 ▶ next unless @fn;
Branch: 1 branch
BranchTrueFalseTotal
unless @fn 0 0 0
248  
249 0 foreach my $fn (@fn) {
250 0 ▶ next unless $fn; # skip undef
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
251 0 my $fh = $self->upload($fn);
252 my $upload_or = WebDyne::CGI::Simple::Upload->new(
253 filename => $fn,
254 size => $self->{'.tmpfiles'}{$fn}{'size'},
255 0 mime => $self->{'.tmpfiles'}{$fn}{'mime'},
256 fh => $fh,
257 );
258 0 push @pairs, ($param => $upload_or);
259 }
260 }
261 0 0 && debug('pairs: %s', Dumper(\@pairs));
262  
263 0 return Hash::MultiValue->new(@pairs);
264 }
265  
266  
267 # Emulate Plack::Request::Upload object
268 #
269 package WebDyne::CGI::Simple::Upload;
270  
271 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 42 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
275 0 my ($class, %args) = @_;
276 0 return bless \%args, $class;
277 }
278 0 ▶sub filename { $_[0]->{'filename'} }
Subroutine filename: not called CC=1 SCAR=6.9
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 42 1;
293 __END__
« blib/lib/WebDyne/Chain.pm ↑ Summary lib/WebDyne/CGI/Simple.pm »