File Coverage

blib/lib/Apache2/ASP/SimpleCGI.pm
Criterion Covered Total %
statement 21 78 26.9
branch 0 30 0.0
condition 0 2 0.0
subroutine 7 14 50.0
pod 6 6 100.0
total 34 130 26.1


line stmt bran cond sub pod time code
1              
2             package Apache2::ASP::SimpleCGI;
3            
4 23     23   102 use strict;
  23         63  
  23         716  
5 23     23   94 use warnings 'all';
  23         34  
  23         903  
6 23     23   107 use HTTP::Body;
  23         33  
  23         1195  
7            
8            
9             #==============================================================================
10             sub new
11             {
12 0     0 1   my ($s, %args) = @_;
13            
14 0           my %params = ();
15 0           my %upload_data = ();
16 23     23   94 no warnings 'uninitialized';
  23         34  
  23         8175  
17 0 0         if( length($args{querystring}) )
18             {
19 0           foreach my $part ( split /&/, $args{querystring} )
20             {
21 0           my ($k,$v) = map { $s->unescape($_) } split /\=/, $part;
  0            
22            
23 0 0         if( exists($params{$k}) )
24             {
25 0 0         if( ref($params{$k}) )
26             {
27 0           push @{$params{$k}}, $v;
  0            
28             }
29             else
30             {
31 0           $params{$k} = [ $params{$k}, $v ];
32             }# end if()
33             }
34             else
35             {
36 0           $params{$k} = $v;
37             }# end if()
38             }# end foreach()
39             }# end if()
40            
41 0 0         if( $args{body} )
42             {
43 0           my $body = HTTP::Body->new( $args{content_type}, $args{content_length} );
44 0           $body->add( $args{body} );
45            
46             # Parse form values:
47 0   0       my $form_info = $body->param || { };
48 0 0         if( keys(%$form_info) )
49             {
50 0           foreach( keys(%$form_info) )
51             {
52 0           $params{$_} = $form_info->{$_};
53             }# end foreach()
54             }# end if()
55            
56             # Parse uploaded data:
57 0 0         if( my $uploads = $body->upload )
58             {
59 0           foreach my $name ( keys(%$uploads) )
60             {
61             open my $ifh, '<', $uploads->{$name}->{tempname}
62 0 0         or die "Cannot open '$uploads->{$name}->{tempname}' for reading: $!";
63             $upload_data{$name} = {
64 0           %{$uploads->{$name}},
  0            
65             'filehandle' => $ifh,
66             };
67             }# end foreach()
68             }# end if()
69             }# end if()
70            
71 0           return bless {
72             params => \%params,
73             uploads => \%upload_data,
74             %args
75             }, $s;
76             }# end new()
77            
78            
79             #==============================================================================
80             sub upload
81             {
82 0     0 1   my ($s, $key) = @_;
83            
84 23     23   125 no warnings 'uninitialized';
  23         27  
  23         1769  
85 0 0         return exists( $s->{uploads}->{$key} ) ? $s->{uploads}->{$key}->{filehandle} : undef;
86             }# end upload()
87            
88            
89             #==============================================================================
90             sub upload_info
91             {
92 0     0 1   my ($s, $key, $info) = @_;
93            
94 23     23   95 no warnings 'uninitialized';
  23         32  
  23         3802  
95 0 0         if( exists( $s->{uploads}->{$key} ) )
96             {
97 0           my $upload = $s->{uploads}->{$key};
98 0 0         if( exists( $upload->{$info} ) )
99             {
100 0           return $upload->{$info};
101             }
102             else
103             {
104 0           return undef;
105             }# end if()
106             }
107             else
108             {
109 0           return undef;
110             }# end if()
111             }# end upload_info()
112            
113            
114             #==============================================================================
115             sub param
116             {
117 0     0 1   my ($s, $key) = @_;
118            
119 0 0         if( defined($key) )
120             {
121 0 0         if( ref($s->{params}->{$key}) )
122             {
123 0 0         return wantarray ? @{ $s->{params}->{$key} } : $s->{params}->{$key};
  0            
124             }
125             else
126             {
127 0           return $s->{params}->{$key};
128             }# end if()
129             }
130             else
131             {
132 0           return keys(%{ $s->{params} });
  0            
133             }# end if()
134             }# end param()
135            
136            
137             #==============================================================================
138             sub escape
139             {
140 0     0 1   my $toencode = $_[1];
141 23     23   106 no warnings 'uninitialized';
  23         36  
  23         5286  
142 0           $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg;
  0            
143 0           $toencode;
144             }# end escape()
145            
146            
147             #==============================================================================
148             sub unescape
149             {
150 0     0 1   my ($s, $todecode) = @_;
151 0 0         return unless defined($todecode);
152 0           $todecode =~ tr/+/ /; # pluses become spaces
153 0           $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
154 0 0         defined($1)? chr hex($1) : utf8_chr(hex($2))/ge;
155 0           return $todecode;
156             }# end unescape()
157              
158            
159             #==============================================================================
160             sub DESTROY
161             {
162 0     0     my $s = shift;
163            
164 0           map { close($s->{uploads}->{$_}->{filehandle}) }
165 0           keys(%{$s->{uploads}});
  0            
166             }# end DESTROY()
167            
168            
169             1;# return true:
170              
171             =pod
172              
173             =head1 NAME
174              
175             Apache2::ASP::SimpleCGI - Basic CGI functionality
176              
177             =head1 SYNOPSIS
178              
179             use Apache2::ASP::SimpleCGI;
180            
181             my $cgi = Apache2::ASP::SimpleCGI->new(
182             content_type => 'multipart/form-data',
183             content_length => 1200,
184             querystring => 'mode=create&uploadID=234234',
185             body => ...
186             );
187            
188             my $val = $cgi->param('mode');
189             foreach my $key ( $cgi->param )
190             {
191             print $key . ' --> ' . $cgi->param( $key ) . "\n";
192             }# end foreach()
193            
194             my $escaped = $cgi->escape( 'Hello world' );
195             my $unescaped = $cgi->unescape( 'Hello+world' );
196            
197             my $upload = $cgi->upload('filename');
198            
199             my $filehandle = $cgi->upload_info('filename', 'filehandle' );
200              
201             =head1 DESCRIPTION
202              
203             This package provides basic CGI functionality and is also used for testing and
204             in the API enironment.
205              
206             C uses L under the hood.
207              
208             =head1 PUBLIC METHODS
209              
210             =head2 new( %args )
211              
212             Returns a new C object.
213              
214             C<%args> can contain C, C, C and C.
215              
216             =head2 param( [$key] )
217              
218             If C<$key> is given, returns the value of the form or querystring parameter by that name.
219              
220             If C<$key> is not given, returns a list of all parameter names.
221              
222             =head2 escape( $str )
223              
224             Returns a URL-encoded version of C<$str>.
225              
226             =head2 unescape( $str )
227              
228             Returns a URL-decoded version of C<$str>.
229              
230             =head2 upload( $field_name )
231              
232             Returns all of the information we have about a file upload named C<$field_name>.
233              
234             =head2 upload_info( $field_name, $item_name )
235              
236             Returns just that part of C<$field_name>'s upload info.
237            
238             =head1 BUGS
239            
240             It's possible that some bugs have found their way into this release.
241            
242             Use RT L to submit bug reports.
243            
244             =head1 HOMEPAGE
245            
246             Please visit the Apache2::ASP homepage at L to see examples
247             of Apache2::ASP in action.
248            
249             =head1 AUTHOR
250            
251             John Drago L
252            
253             =head1 COPYRIGHT AND LICENSE
254            
255             Copyright 2007 John Drago, All rights reserved.
256            
257             This software is free software. It may be used and distributed under the
258             same terms as Perl itself.
259            
260             =cut