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