| 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.027'; | |||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | # New instance of WebDyne::CGI::Common | ||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||
| 63 | 9 | ▶ | my ($class, $r, %param)=@_; | ||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=35.9
Pod
documented
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | # Need to construct | ||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||
| 75 | # Match the media type independently of optional charset parameters. | ||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||
| 77 | 9 | ▶ | if ( | ||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
$r->headers_in('content-type') || ''
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')
| |||||||||||||||||||||||||
| 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"
$r->method || 'POST'
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
| |||||||||||||||||||||||||
| 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
$r->headers_in('content-type') || ''
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')
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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"
$r->headers_in('Content-Type') || 'multipart/form-data'
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')
| |||||||||||||||||||||||||
| 116 | 1 | ▶ | local $ENV{'REQUEST_METHOD'} ||= ($r->method() || 'POST'); | ||||||||||||||||||||||
|
Condition40%2/5
local $ENV{"REQUEST_METHOD"} ||= $r->method || "POST"
$r->method || 'POST'
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
| |||||||||||||||||||||||||
| 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"
$r->method || 'GET'
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
| |||||||||||||||||||||||||
| 127 | 8 | $cgi_or=CGI::Simple->new($r->args); | |||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||
| 133 | # Add any param | ||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||
| 135 | 9 | map { $cgi_or->param($_, $param{$_}) } keys %param; | |||||||||||||||||||||||
| 136 | 9 | debug("cgi_or: $cgi_or, %s", Dumper($cgi_or)); | |||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||
| 139 | # Bless and return | ||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||
| 141 | 9 | return bless($cgi_or, __PACKAGE__); | |||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | sub Vars { | ||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||
| 148 | # Simulate Plack::Request Hash::MultiValue response | ||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||
| 150 | 8 | ▶ | my ($self, $hr)=@_; | ||||||||||||||||||||||
|
Subroutine
Vars: called CC=5 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||
| 151 | 8 | ▶ | if ($hr) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 | 8 | my @pairs; | |||||||||||||||||||||||
| 164 | 8 | foreach my $param ($self->param()) { | |||||||||||||||||||||||
| 165 | 0 | my @values=$self->param($param); | |||||||||||||||||||||||
| 166 | 0 | map { push @pairs, ( $param => $_ ) } @values; | |||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||
| 168 | 8 | $hr=Hash::MultiValue->new(@pairs) | |||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||
| 171 | 8 | ▶ | return wantarray ? %{$hr} : $hr | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 | 6 | ▶ | local $ENV{'CONTENT_TYPE'}='multipart/form-data'; | ||||||||||||||||||||||
|
Subroutine
upload: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||
| 186 | 6 | return shift()->SUPER::upload(@_); | |||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||
| 191 | sub uploads { | ||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||
| 193 | # Replicate Plack::Request::Uploads->uploads() | ||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||
| 195 | 2 | ▶ | my $self=shift(); | ||||||||||||||||||||||
|
Subroutine
uploads: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||
| 196 | 2 | my @pairs; | |||||||||||||||||||||||
| 197 | 2 | foreach my $param ($self->param()) { | |||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||
| 199 | 4 | my @fn = $self->param($param); | |||||||||||||||||||||||
| 200 | 4 | debug('fn: %s', Dumper(\@fn)); | |||||||||||||||||||||||
| 201 | 4 | ▶ | next unless @fn; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||
| 203 | 4 | foreach my $fn (@fn) { | |||||||||||||||||||||||
| 204 | 4 | ▶ | next unless $fn; # skip undef | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 205 | 4 | my $fh = $self->upload($fn); | |||||||||||||||||||||||
| 206 | my $upload_or = WebDyne::CGI::Simple::Upload->new( | ||||||||||||||||||||||||
| 207 | filename => $fn, | ||||||||||||||||||||||||
| 208 | size => $self->{'.tmpfiles'}{$fn}{'size'}, | ||||||||||||||||||||||||
| 209 | 4 | mime => $self->{'.tmpfiles'}{$fn}{'mime'}, | |||||||||||||||||||||||
| 210 | fh => $fh, | ||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||
| 212 | 4 | push @pairs, ($param => $upload_or); | |||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||
| 215 | 2 | debug('pairs: %s', Dumper(\@pairs)); | |||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||
| 217 | 2 | return Hash::MultiValue->new(@pairs); | |||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||
| 221 | # Emulate Plack::Request::Upload object | ||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||
| 223 | package WebDyne::CGI::Simple::Upload; | ||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||
| 225 | 5 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 226 | 5 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||
| 228 | sub new { | ||||||||||||||||||||||||
| 229 | 4 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 230 | 4 | return bless \%args, $class; | |||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||
| 232 | 3 | ▶ | sub filename { $_[0]->{'filename'} } | ||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 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 | 5 | 1; | |||||||||||||||||||||||
| 247 | __END__ | ||||||||||||||||||||||||