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