File Coverage

blib/lib/WebDyne/CGI/Simple.pm
Criterion Covered Total %
statement 47 79 59.4
branch 1 6 16.6
condition 1 2 50.0
subroutine 13 23 56.5
pod 2 4 50.0
total 64 114 56.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of WebDyne.
3             #
4             # This software is copyright (c) 2026 by Andrew Speer .
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             #
12             #
13             package WebDyne::CGI::Simple;
14              
15              
16             # Pragma
17             #
18 3     3   27 use strict qw(vars);
  3         8  
  3         180  
19 3     3   20 use vars qw($VERSION $AUTOLOAD @ISA);
  3         7  
  3         249  
20 3     3   21 use warnings;
  3         8  
  3         215  
21 3     3   17 no warnings qw(uninitialized);
  3         5  
  3         185  
22              
23              
24             # WebDyne Modules
25             #
26 3     3   17 use WebDyne::Constant;
  3         6  
  3         29  
27 3     3   26 use WebDyne::Util;
  3         9  
  3         28  
28              
29              
30             # External modules
31             #
32 3     3   23 use Data::Dumper;
  3         7  
  3         221  
33 3     3   1934 use Hash::MultiValue;
  3         8762  
  3         161  
34 3     3   26 use CGI::Simple;
  3         4  
  3         32  
35             @ISA=qw(CGI::Simple);
36              
37              
38             # Version information
39             #
40             $VERSION='2.075';
41              
42              
43             # CGI upload vars
44             #
45             $CGI::Simple::DISABLE_UPLOADS=$WEBDYNE_CGI_DISABLE_UPLOADS;
46             $CGI::Simple::POST_MAX=$WEBDYNE_CGI_POST_MAX;
47              
48              
49             # Debug load
50             #
51             0 && debug("Loading %s version $VERSION", __PACKAGE__);
52              
53              
54             #==============================================================================
55              
56             sub new {
57              
58              
59             # New instance of CGI::Common
60             #
61 9     9 1 43 my ($class, $r, %param)=@_;
62 9         17 0 && debug("class: $class, r: $r, param: %s", Dumper(\%param));
63 9   50     64 my $cgi_or=CGI::Simple->new($r) ||
64             return err('unable to get CGI::Simple objedt');
65 9         1679 my $self=bless($cgi_or, __PACKAGE__);
66 9         24 map { $self->param($_, $param{$_}) } keys %param;
  0         0  
67 9         60 return $self;
68            
69             }
70              
71              
72             sub Vars {
73              
74             # Simulate Plack::Request Hash::MultiValue response
75             #
76 18     18 1 44 my ($self, $hr)=@_;
77 18 50       44 if ($hr) {
78            
79             # Pushing back into CGI
80             #
81 0         0 $self->delete_all();
82 0         0 foreach my $param (keys %{$hr}) {
  0         0  
83 0         0 $self->param($param, $hr->get_all($param));
84             }
85 0         0 return $hr;
86            
87             }
88             else {
89 18         26 my @pairs;
90 18         75 foreach my $param ($self->param()) {
91 26         198 my @values=$self->param($param);
92 26         251 map { push @pairs, ( $param => $_ ) } @values;
  34         121  
93             }
94 18         182 return Hash::MultiValue->new(@pairs)
95             }
96              
97             }
98              
99              
100             sub env {
101              
102 0     0 0   return \%ENV
103            
104             }
105              
106              
107             sub uploads {
108              
109             # Replicate Plack::Request::Uploads->uploads()
110             #
111 0     0 0   my $self=shift();
112 0           my @pairs;
113 0           foreach my $param ($self->param()) {
114            
115 0           my @fn = $self->upload_info();
116 0 0         next unless @fn;
117              
118 0           foreach my $fn (@fn) {
119 0 0         next unless $fn; # skip undef
120 0           my $fh = $self->upload($fn);
121 0           my $size = -s $fh;
122 0           my $upload_or = WebDyne::CGI::Simple::Upload->new(
123             filename => $fn,
124             size => $size,
125             mime => $self->upload_info($fn, 'mime'),
126             fh => $fh,
127             tempfile => $fh,
128             );
129 0           push @pairs, ($param => $upload_or);
130             }
131             }
132              
133 0           return Hash::MultiValue->new(@pairs);
134             }
135              
136              
137             # Emulate Plack::Request::Upload object
138             #
139             package WebDyne::CGI::Simple::Upload;
140 3     3   1749 use strict;
  3         6  
  3         108  
141 3     3   18 use File::Basename qw();
  3         5  
  3         925  
142              
143             sub new {
144 0     0     my ($class, %args) = @_;
145 0           return bless \%args, $class;
146             }
147 0     0     sub filename { $_[0]->{'filename'} }
148 0     0     sub size { $_[0]->{'size'} }
149             sub content {
150 0     0     my $self = shift;
151 0           seek ($self->{'fh'}, 0, 0);
152 0           local $/;
153 0           my $fh=$self->{'fh'};
154 0           return <$fh>;
155             }
156 0     0     sub fh { $_[0]->{'fh'} }
157 0     0     sub path { $_[0]->{'tempfile'} }
158 0     0     sub content_type { $_[0]->{'mime'} }
159 0     0     sub basename { &File::Basename::basename($_[0]->{'filename'}) }
160              
161             1;