blib/lib/WebDyne/CGI/Simple.pm

filter: Statementstmt 50.8% 61 / 120 Branchbran 31.2% 5 / 16 Conditioncond 18.5% 5 / 27 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 56.0% 14 / 25 Podpod 60.0% 3 / 5 totaltotal 43.1% 88 / 204 CC 24 SCAR 37.6
CC
24
Cov
44%
new
115.0
uploads
30.0
Vars
6.9
CRAP 42.8
« blib/lib/WebDyne/Install.pm ↑ Summary blib/lib/WebDyne/HTML/TreeBuilder.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 34 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 34 ▶no warnings qw(uninitialized redefine once);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # WebDyne Modules
25 #
26 34 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 34 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 # External modules
31 #
32 34 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 34 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 34 ▶use CGI::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 34 $CGI::Simple::MOD_PERL=0;
36 34 @ISA=qw(CGI::Simple);
37 34 ▶*CGI::Simple::_mod_perl=sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39  
40 # Version information
41 #
42 34 $VERSION='3.028';
43  
44  
45 # CGI upload vars
46 #
47 34 $CGI::Simple::DISABLE_UPLOADS=$WEBDYNE_CGI_DISABLE_UPLOADS;
48 34 $CGI::Simple::POST_MAX=$WEBDYNE_CGI_POST_MAX;
49  
50  
51 # Debug load
52 #
53 34 0 && debug("Loading %s version $VERSION", __PACKAGE__);
54  
55  
56 #==============================================================================
57  
58 sub new {
59  
60  
61 # New instance of WebDyne::CGI::Common
62 #
63 18 ▶ my ($class, $r, %param)=@_;
Subroutine new: called CC=16 SCAR=47.4
Pod documented
64 18 0 && debug("class: $class, r: $r, param: %s, caller %s", Dumper(\%param, [caller(0)]));
65
66
67 # Get from cache or construct
68 #
69 18 my $cgi_or;
70 18 ▶ unless ($cgi_or=$r->{'_CGI'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cgi_or = $r->{"_CGI"}) 18 0 18
71
72
73 # Need to construct
74 #
75 # Match the media type independently of optional charset parameters.
76 #
77 18 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (($r->headers_in('content-type') || '') =~ m[\Aapplication/x-www-form-urlencoded(?:\s*;|\s*\z)]i) { } 0 18 18
elsif ($r->content_length or ($r->headers_in('content-type') || '') =~ m[\Amultipart/form-data(?:\s*;|\s*\z)]i) { } 0 18 18
Condition50%1/2
$r->headers_in('content-type') || ''
l!l
018
MC/DC0%0/1
$r->headers_in('content-type') || ''
$r->headers_in('content-type')
Truth table50%1/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 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 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() ||
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
0018
$r->headers_in('content-type') || ''
l!l
018
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 18 0 && debug('non POST submissions, just using args: %s', $r->args);
126 18 ▶ local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'GET');
Condition40%2/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "GET"
l!l&&r!l&&!r
0180
$r->method || 'GET'
l!l
180
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 18 $cgi_or=CGI::Simple->new($r->args);
128 }
129  
130 }
131
132
133 # Add any param
134 #
135 18 map { $cgi_or->param($_, $param{$_}) } keys %param;
136 18 0 && debug("cgi_or: $cgi_or, %s", Dumper($cgi_or));
137
138  
139 # Bless and return
140 #
141 18 return bless($cgi_or, __PACKAGE__);
142
143 }
144  
145  
146 sub Vars {
147  
148 # Simulate Plack::Request Hash::MultiValue response
149 #
150 27 ▶ my ($self, $hr)=@_;
Subroutine Vars: called CC=5 SCAR=19.3
Pod documented
151 27 ▶ if ($hr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr) { } 0 27 27
152
153 # Pushing back into CGI
154 #
155 0 $self->delete_all();
156 0 foreach my $param (keys %{$hr}) {
157 0 $self->param($param, $hr->get_all($param));
158 }
159 0 return $hr;
160
161 }
162 else {
163 27 my @pairs;
164 27 foreach my $param ($self->param()) {
165 26 my @values=$self->param($param);
166 26 map { push @pairs, ( $param => $_ ) } @values;
167 }
168 27 $hr=Hash::MultiValue->new(@pairs)
169 }
170  
171 27 ▶ return wantarray ? %{$hr} : $hr
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 27 27
172  
173 }
174  
175  
176 sub env {
177  
178 0 ▶ return \%ENV
Subroutine env: not called CC=1 SCAR=6.9
Pod undocumented
179
180 }
181  
182  
183 sub upload {
184  
185 0 ▶ local $ENV{'CONTENT_TYPE'}='multipart/form-data';
Subroutine upload: not called CC=1 SCAR=6.9
Pod documented
186 0 return shift()->SUPER::upload(@_);
187
188 }
189  
190  
191 sub uploads {
192  
193 # Replicate Plack::Request::Uploads->uploads()
194 #
195 0 ▶ my $self=shift();
Subroutine uploads: not called CC=5 SCAR=34.0
Pod undocumented
196 0 my @pairs;
197 0 foreach my $param ($self->param()) {
198
199 0 my @fn = $self->param($param);
200 0 0 && debug('fn: %s', Dumper(\@fn));
201 0 ▶ next unless @fn;
Branch: 1 branch
BranchTrueFalseTotal
unless @fn 0 0 0
202  
203 0 foreach my $fn (@fn) {
204 0 ▶ next unless $fn; # skip undef
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
205 0 my $fh = $self->upload($fn);
206 my $upload_or = WebDyne::CGI::Simple::Upload->new(
207 filename => $fn,
208 size => $self->{'.tmpfiles'}{$fn}{'size'},
209 0 mime => $self->{'.tmpfiles'}{$fn}{'mime'},
210 fh => $fh,
211 );
212 0 push @pairs, ($param => $upload_or);
213 }
214 }
215 0 0 && debug('pairs: %s', Dumper(\@pairs));
216  
217 0 return Hash::MultiValue->new(@pairs);
218 }
219  
220  
221 # Emulate Plack::Request::Upload object
222 #
223 package WebDyne::CGI::Simple::Upload;
224  
225 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 34 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 sub new {
229 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=1 SCAR=6.9
230 0 return bless \%args, $class;
231 }
232 0 ▶sub filename { $_[0]->{'filename'} }
Subroutine filename: not called CC=1 SCAR=6.9
233 0 ▶sub size { $_[0]->{'size'} }
Subroutine size: not called CC=1 SCAR=6.9
234 sub content {
235 0 ▶ my $self = shift;
Subroutine content: not called CC=1 SCAR=6.9
236 0 seek ($self->{'fh'}, 0, 0);
237 0 local $/;
238 0 my $fh=$self->{'fh'};
239 0 return <$fh>;
240 }
241 0 ▶sub fh { $_[0]->{'fh'} }
Subroutine fh: not called CC=1 SCAR=6.9
242 0 ▶sub path { $_[0]->{'fh'} }
Subroutine path: not called CC=1 SCAR=6.9
243 0 ▶sub content_type { $_[0]->{'mime'} }
Subroutine content_type: not called CC=1 SCAR=6.9
244 0 ▶sub basename { basename($_[0]->{'filename'}) }
Subroutine basename: not called CC=1 SCAR=6.9
245  
246 34 1;
247 __END__
« blib/lib/WebDyne/Install.pm ↑ Summary blib/lib/WebDyne/HTML/TreeBuilder.pm »