| 1 | |||
| 2 | package Apache2::ASP::SimpleCGI; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use HTTP::Body; | ||
| 7 | |||
| 8 | |||
| 9 | #============================================================================== | ||
| 10 | sub new | ||
| 11 | { | ||
| 12 | my ($s, %args) = @_; | ||
| 13 | |||
| 14 | my %params = (); | ||
| 15 | my %upload_data = (); | ||
| 16 | no warnings 'uninitialized'; | ||
| 17 | if( length($args{querystring}) ) | ||
| 18 | { | ||
| 19 | foreach my $part ( split /&/, $args{querystring} ) | ||
| 20 | { | ||
| 21 | my ($k,$v) = map { $s->unescape($_) } split /\=/, $part; | ||
| 22 | |||
| 23 | if( exists($params{$k}) ) | ||
| 24 | { | ||
| 25 | if( ref($params{$k}) ) | ||
| 26 | { | ||
| 27 | push @{$params{$k}}, $v; | ||
| 28 | } | ||
| 29 | else | ||
| 30 | { | ||
| 31 | $params{$k} = [ $params{$k}, $v ]; | ||
| 32 | }# end if() | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | $params{$k} = $v; | ||
| 37 | }# end if() | ||
| 38 | }# end foreach() | ||
| 39 | }# end if() | ||
| 40 | |||
| 41 | if( $args{body} ) | ||
| 42 | { | ||
| 43 | my $body = HTTP::Body->new( $args{content_type}, $args{content_length} ); | ||
| 44 | $body->add( $args{body} ); | ||
| 45 | |||
| 46 | # Parse form values: | ||
| 47 | my $form_info = $body->param || { }; | ||
| 48 | if( keys(%$form_info) ) | ||
| 49 | { | ||
| 50 | foreach( keys(%$form_info) ) | ||
| 51 | { | ||
| 52 | $params{$_} = $form_info->{$_}; | ||
| 53 | }# end foreach() | ||
| 54 | }# end if() | ||
| 55 | |||
| 56 | # Parse uploaded data: | ||
| 57 | if( my $uploads = $body->upload ) | ||
| 58 | { | ||
| 59 | foreach my $name ( keys(%$uploads) ) | ||
| 60 | { | ||
| 61 | open my $ifh, '<', $uploads->{$name}->{tempname} | ||
| 62 | or die "Cannot open '$uploads->{$name}->{tempname}' for reading: $!"; | ||
| 63 | $upload_data{$name} = { | ||
| 64 | %{$uploads->{$name}}, | ||
| 65 | 'filehandle' => $ifh, | ||
| 66 | }; | ||
| 67 | }# end foreach() | ||
| 68 | }# end if() | ||
| 69 | }# end if() | ||
| 70 | |||
| 71 | return bless { | ||
| 72 | params => \%params, | ||
| 73 | uploads => \%upload_data, | ||
| 74 | %args | ||
| 75 | }, $s; | ||
| 76 | }# end new() | ||
| 77 | |||
| 78 | |||
| 79 | #============================================================================== | ||
| 80 | sub upload | ||
| 81 | { | ||
| 82 | my ($s, $key) = @_; | ||
| 83 | |||
| 84 | no warnings 'uninitialized'; | ||
| 85 | return exists( $s->{uploads}->{$key} ) ? $s->{uploads}->{$key}->{filehandle} : undef; | ||
| 86 | }# end upload() | ||
| 87 | |||
| 88 | |||
| 89 | #============================================================================== | ||
| 90 | sub upload_info | ||
| 91 | { | ||
| 92 | my ($s, $key, $info) = @_; | ||
| 93 | |||
| 94 | no warnings 'uninitialized'; | ||
| 95 | if( exists( $s->{uploads}->{$key} ) ) | ||
| 96 | { | ||
| 97 | my $upload = $s->{uploads}->{$key}; | ||
| 98 | if( exists( $upload->{$info} ) ) | ||
| 99 | { | ||
| 100 | return $upload->{$info}; | ||
| 101 | } | ||
| 102 | else | ||
| 103 | { | ||
| 104 | return undef; | ||
| 105 | }# end if() | ||
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | return undef; | ||
| 110 | }# end if() | ||
| 111 | }# end upload_info() | ||
| 112 | |||
| 113 | |||
| 114 | #============================================================================== | ||
| 115 | sub param | ||
| 116 | { | ||
| 117 | my ($s, $key) = @_; | ||
| 118 | |||
| 119 | if( defined($key) ) | ||
| 120 | { | ||
| 121 | if( ref($s->{params}->{$key}) ) | ||
| 122 | { | ||
| 123 | return wantarray ? @{ $s->{params}->{$key} } : $s->{params}->{$key}; | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | return $s->{params}->{$key}; | ||
| 128 | }# end if() | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | return keys(%{ $s->{params} }); | ||
| 133 | }# end if() | ||
| 134 | }# end param() | ||
| 135 | |||
| 136 | |||
| 137 | #============================================================================== | ||
| 138 | sub escape | ||
| 139 | { | ||
| 140 | my $toencode = $_[1]; | ||
| 141 | no warnings 'uninitialized'; | ||
| 142 | $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg; | ||
| 143 | $toencode; | ||
| 144 | }# end escape() | ||
| 145 | |||
| 146 | |||
| 147 | #============================================================================== | ||
| 148 | sub unescape | ||
| 149 | { | ||
| 150 | my ($s, $todecode) = @_; | ||
| 151 | return unless defined($todecode); | ||
| 152 | $todecode =~ tr/+/ /; # pluses become spaces | ||
| 153 | $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ | ||
| 154 | defined($1)? chr hex($1) : utf8_chr(hex($2))/ge; | ||
| 155 | return $todecode; | ||
| 156 | }# end unescape() | ||
| 157 | |||
| 158 | |||
| 159 | #============================================================================== | ||
| 160 | sub DESTROY | ||
| 161 | { | ||
| 162 | my $s = shift; | ||
| 163 | |||
| 164 | map { close($s->{uploads}->{$_}->{filehandle}) } | ||
| 165 | keys(%{$s->{uploads}}); | ||
| 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<Apache2::ASP::SimpleCGI> uses L<HTTP::Body> under the hood. | ||
| 207 | |||
| 208 | =head1 PUBLIC METHODS | ||
| 209 | |||
| 210 | =head2 new( %args ) | ||
| 211 | |||
| 212 | Returns a new C<Apache2::ASP::SimpleCGI> object. | ||
| 213 | |||
| 214 | C<%args> can contain C<content_type>, C<content_length>, C<querystring> and C<body>. | ||
| 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports. | ||
| 243 | |||
| 244 | =head1 HOMEPAGE | ||
| 245 | |||
| 246 | Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples | ||
| 247 | of Apache2::ASP in action. | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | John Drago L<mailto:jdrago_999@yahoo.com> | ||
| 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 |