| blib/lib/Gantry/Engine/CGI.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 48 | 326 | 14.7 |
| branch | 7 | 106 | 6.6 |
| condition | 7 | 71 | 9.8 |
| subroutine | 10 | 59 | 16.9 |
| pod | 53 | 53 | 100.0 |
| total | 125 | 615 | 20.3 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Gantry::Engine::CGI; | ||||||
| 2 | require Exporter; | ||||||
| 3 | |||||||
| 4 | 2 | 2 | 2334 | use strict; | |||
| 2 | 3 | ||||||
| 2 | 70 | ||||||
| 5 | 2 | 2 | 9 | use Carp qw( croak ); | |||
| 2 | 4 | ||||||
| 2 | 95 | ||||||
| 6 | 2 | 2 | 9 | use CGI::Simple; | |||
| 2 | 3 | ||||||
| 2 | 17 | ||||||
| 7 | 2 | 2 | 37 | use File::Basename; | |||
| 2 | 4 | ||||||
| 2 | 135 | ||||||
| 8 | 2 | 2 | 973 | use Gantry::Utils::DBConnHelper::Script; | |||
| 2 | 5 | ||||||
| 2 | 16 | ||||||
| 9 | |||||||
| 10 | 2 | 2 | 9 | use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); | |||
| 2 | 66 | ||||||
| 2 | 14644 | ||||||
| 11 | |||||||
| 12 | ############################################################ | ||||||
| 13 | # Variables # | ||||||
| 14 | ############################################################ | ||||||
| 15 | @ISA = qw( Exporter ); | ||||||
| 16 | @EXPORT = qw( | ||||||
| 17 | apache_param_hash | ||||||
| 18 | apache_uf_param_hash | ||||||
| 19 | apache_request | ||||||
| 20 | base_server | ||||||
| 21 | cgi_obj | ||||||
| 22 | config | ||||||
| 23 | cast_custom_error | ||||||
| 24 | consume_post_body | ||||||
| 25 | declined_response | ||||||
| 26 | dispatch_location | ||||||
| 27 | engine | ||||||
| 28 | engine_init | ||||||
| 29 | err_header_out | ||||||
| 30 | fish_location | ||||||
| 31 | fish_method | ||||||
| 32 | fish_path_info | ||||||
| 33 | fish_uri | ||||||
| 34 | fish_user | ||||||
| 35 | fish_config | ||||||
| 36 | get_auth_dbh | ||||||
| 37 | get_cached_config | ||||||
| 38 | get_config | ||||||
| 39 | get_dbh | ||||||
| 40 | get_post_body | ||||||
| 41 | locations | ||||||
| 42 | log_error | ||||||
| 43 | get_arg_hash | ||||||
| 44 | header_in | ||||||
| 45 | header_out | ||||||
| 46 | hostname | ||||||
| 47 | is_connection_secure | ||||||
| 48 | is_status_declined | ||||||
| 49 | port | ||||||
| 50 | print_output | ||||||
| 51 | redirect_response | ||||||
| 52 | remote_ip | ||||||
| 53 | send_http_header | ||||||
| 54 | set_cached_config | ||||||
| 55 | set_content_type | ||||||
| 56 | set_no_cache | ||||||
| 57 | set_req_params | ||||||
| 58 | status_const | ||||||
| 59 | send_error_output | ||||||
| 60 | success_code | ||||||
| 61 | server_root | ||||||
| 62 | file_upload | ||||||
| 63 | ); | ||||||
| 64 | |||||||
| 65 | @EXPORT_OK = qw( ); | ||||||
| 66 | |||||||
| 67 | ############################################################ | ||||||
| 68 | # Functions # | ||||||
| 69 | ############################################################ | ||||||
| 70 | |||||||
| 71 | #-------------------------------------------------- | ||||||
| 72 | # $self->new( { locations => {..}, config => {..} } ); | ||||||
| 73 | #-------------------------------------------------- | ||||||
| 74 | sub new { | ||||||
| 75 | 1 | 50 | 1 | 1 | 46 | my( $class, $self ) = ( shift, shift || {} ); | |
| 76 | |||||||
| 77 | 1 | 3 | bless $self, $class; | ||||
| 78 | |||||||
| 79 | 1 | 9 | my $config = $self->{config}; | ||||
| 80 | |||||||
| 81 | 1 | 50 | 4 | if ( $self->{config}{ GantryConfInstance } ) { | |||
| 82 | $config = $self->get_config( | ||||||
| 83 | $self->{config}{ GantryConfInstance }, | ||||||
| 84 | $self->{config}{ GantryConfFile }, | ||||||
| 85 | 0 | 0 | ); | ||||
| 86 | } | ||||||
| 87 | |||||||
| 88 | Gantry::Utils::DBConnHelper::Script->set_conn_info( | ||||||
| 89 | { | ||||||
| 90 | 1 | 15 | dbconn => $config->{dbconn}, | ||||
| 91 | dbuser => $config->{dbuser}, | ||||||
| 92 | dbpass => $config->{dbpass}, | ||||||
| 93 | } | ||||||
| 94 | ); | ||||||
| 95 | |||||||
| 96 | 1 | 8 | Gantry::Utils::DBConnHelper::Script->set_auth_conn_info( | ||||
| 97 | { | ||||||
| 98 | auth_dbconn => $config->{auth_dbconn}, | ||||||
| 99 | auth_dbuser => $config->{auth_dbuser}, | ||||||
| 100 | auth_dbpass => $config->{auth_dbpass}, | ||||||
| 101 | } | ||||||
| 102 | ); | ||||||
| 103 | |||||||
| 104 | 1 | 50 | 8 | $CGI::Simple::DISABLE_UPLOADS = $config->{disable_uploads} || 0; | |||
| 105 | 1 | 50 | 7 | $CGI::Simple::POST_MAX = $config->{post_max} ||'20000000000'; | |||
| 106 | |||||||
| 107 | 1 | 3 | return $self; | ||||
| 108 | |||||||
| 109 | } # end new | ||||||
| 110 | |||||||
| 111 | #-------------------------------------------------- | ||||||
| 112 | # $self->add_config( key, value ); | ||||||
| 113 | #-------------------------------------------------- | ||||||
| 114 | sub add_config { | ||||||
| 115 | 0 | 0 | 1 | 0 | my( $self, $key, $val ) = @_; | ||
| 116 | 0 | 0 | $self->{cgi_obj}{config}->{$key} = $val; | ||||
| 117 | |||||||
| 118 | } # end add_config | ||||||
| 119 | |||||||
| 120 | #-------------------------------------------------- | ||||||
| 121 | # $self->add_location( key, value ) | ||||||
| 122 | #-------------------------------------------------- | ||||||
| 123 | sub add_location { | ||||||
| 124 | 0 | 0 | 1 | 0 | my( $self, $key, $val ) = @_; | ||
| 125 | |||||||
| 126 | 0 | 0 | $self->{locations}->{$key} = $val; | ||||
| 127 | |||||||
| 128 | } # end add_location | ||||||
| 129 | |||||||
| 130 | #-------------------------------------------------- | ||||||
| 131 | # $self->consume_post_body(); | ||||||
| 132 | #-------------------------------------------------- | ||||||
| 133 | sub consume_post_body { | ||||||
| 134 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 135 | 0 | 0 | my $cgi = shift; | ||||
| 136 | |||||||
| 137 | 0 | 0 | my $content_length = $ENV{ CONTENT_LENGTH }; | ||||
| 138 | |||||||
| 139 | 0 | 0 | 0 | return unless $content_length; # nothing to consume | |||
| 140 | |||||||
| 141 | 0 | 0 | 0 | $content_length = 1e6 if $content_length > 1e6; # limit to ~ 1Meg | |||
| 142 | |||||||
| 143 | # just read STDIN | ||||||
| 144 | 0 | 0 | my $content; | ||||
| 145 | my $buffer; | ||||||
| 146 | 0 | 0 | while ( read( STDIN, $buffer, $content_length ) ) { | ||||
| 147 | 0 | 0 | $content .= $buffer; | ||||
| 148 | |||||||
| 149 | 0 | 0 | $content_length -= length $buffer; | ||||
| 150 | } | ||||||
| 151 | |||||||
| 152 | 0 | 0 | $self->{__POST_BODY__} = $content; | ||||
| 153 | } | ||||||
| 154 | |||||||
| 155 | #-------------------------------------------------- | ||||||
| 156 | # $self->get_post_body(); | ||||||
| 157 | #-------------------------------------------------- | ||||||
| 158 | sub get_post_body { | ||||||
| 159 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 160 | |||||||
| 161 | 0 | 0 | 0 | return $self->{__POST_BODY__} || $self->{ cgi_obj }->{__POST_BODY__}; | |||
| 162 | # the value is in the cgi_obj during testing | ||||||
| 163 | } | ||||||
| 164 | |||||||
| 165 | #-------------------------------------------------- | ||||||
| 166 | # $self->dispatch(); | ||||||
| 167 | #-------------------------------------------------- | ||||||
| 168 | sub dispatch { | ||||||
| 169 | 0 | 0 | 1 | 0 | my( $self ) = @_; | ||
| 170 | |||||||
| 171 | 0 | 0 | 0 | my @path = ( split( m|/|, $ENV{PATH_INFO}||'' ) ); | |||
| 172 | |||||||
| 173 | LOOP: | ||||||
| 174 | 0 | 0 | while ( @path ) { | ||||
| 175 | |||||||
| 176 | 0 | 0 | $self->{config}->{location} = join( '/', @path ); | ||||
| 177 | |||||||
| 178 | 0 | 0 | 0 | if ( defined $self->{locations}->{ $self->{config}->{location} } ) { | |||
| 179 | 0 | 0 | my $mod = $self->{locations}->{ $self->{config}->{location} }; | ||||
| 180 | |||||||
| 181 | 0 | 0 | 0 | die "module not defined for location $self->{config}->{location}" | |||
| 182 | unless $mod; | ||||||
| 183 | |||||||
| 184 | 0 | 0 | eval "use $mod"; | ||||
| 185 | 0 | 0 | 0 | if ( $@ ) { die $@; } | |||
| 0 | 0 | ||||||
| 186 | |||||||
| 187 | 0 | 0 | return $mod->handler( $self ); | ||||
| 188 | |||||||
| 189 | } | ||||||
| 190 | |||||||
| 191 | 0 | 0 | pop( @path ); | ||||
| 192 | |||||||
| 193 | } # end while path | ||||||
| 194 | |||||||
| 195 | 0 | 0 | $self->{config}->{location} = '/'; | ||||
| 196 | 0 | 0 | my $mod = $self->{locations}->{ '/' }; | ||||
| 197 | |||||||
| 198 | 0 | 0 | 0 | eval "use $mod" if $mod; | |||
| 199 | 0 | 0 | 0 | if ( $@ ) { die $@; } | |||
| 0 | 0 | ||||||
| 200 | |||||||
| 201 | 0 | 0 | return $mod->handler( $self ); | ||||
| 202 | |||||||
| 203 | } # end dispatch | ||||||
| 204 | |||||||
| 205 | #------------------------------------------------- | ||||||
| 206 | # Exported methods | ||||||
| 207 | #------------------------------------------------- | ||||||
| 208 | |||||||
| 209 | #------------------------------------------------- | ||||||
| 210 | # $self->file_upload( param_name ) | ||||||
| 211 | #------------------------------------------------- | ||||||
| 212 | sub file_upload { | ||||||
| 213 | 0 | 0 | 1 | 0 | my( $self, $param ) = @_; | ||
| 214 | |||||||
| 215 | 0 | 0 | 0 | die "file param required" if ! $param; | |||
| 216 | |||||||
| 217 | 0 | 0 | my $q = $self->cgi(); | ||||
| 218 | 0 | 0 | my $filename = $q->param( $param ); | ||||
| 219 | 0 | 0 | $filename =~ s/\\/\//g; | ||||
| 220 | |||||||
| 221 | 0 | 0 | my( $name, $path, $suffix ) = fileparse( | ||||
| 222 | $filename, | ||||||
| 223 | qr/\.(tar\.gz$|[^.]*)/ | ||||||
| 224 | ); | ||||||
| 225 | |||||||
| 226 | return( { | ||||||
| 227 | 0 | 0 | 0 | unique_key => time . rand( 6 ), | |||
| 228 | fullname => ( $name . $suffix ), | ||||||
| 229 | name => $name, | ||||||
| 230 | suffix => $suffix, | ||||||
| 231 | size => ( $q->upload_info( $filename, 'size' ) || 0 ), | ||||||
| 232 | mime => $q->upload_info( $filename, 'mime' ), | ||||||
| 233 | filehandle => $q->upload( $filename ), | ||||||
| 234 | } ); | ||||||
| 235 | |||||||
| 236 | } | ||||||
| 237 | |||||||
| 238 | #------------------------------------------------- | ||||||
| 239 | # $self->cast_custom_error( error ) | ||||||
| 240 | #------------------------------------------------- | ||||||
| 241 | sub cast_custom_error { | ||||||
| 242 | 0 | 0 | 1 | 0 | my( $self, $error_page, $die_msg ) = @_; | ||
| 243 | |||||||
| 244 | 0 | 0 | 0 | my $status = $self->status() ? $self->status() : '400 Bad Request'; | |||
| 245 | |||||||
| 246 | 0 | 0 | eval { | ||||
| 247 | 0 | 0 | print $self->cgi->header( | ||||
| 248 | -type => 'text/html', | ||||||
| 249 | -status => $status, | ||||||
| 250 | ); | ||||||
| 251 | }; | ||||||
| 252 | 0 | 0 | 0 | if ( $@ ) { | |||
| 253 | 0 | 0 | die "Error encountered in cast_custom_error: $@\n" | ||||
| 254 | . "I was trying to say $error_page\n"; | ||||||
| 255 | } | ||||||
| 256 | |||||||
| 257 | 0 | 0 | $self->print_output( $error_page ); | ||||
| 258 | |||||||
| 259 | 0 | 0 | return $status; | ||||
| 260 | |||||||
| 261 | } | ||||||
| 262 | |||||||
| 263 | #------------------------------------------------- | ||||||
| 264 | # $self->apache_param_hash( $req ) | ||||||
| 265 | #------------------------------------------------- | ||||||
| 266 | sub apache_param_hash { | ||||||
| 267 | 0 | 0 | 1 | 0 | my( $self ) = @_; | ||
| 268 | |||||||
| 269 | #my %hash_ref = $self->cgi->Vars; | ||||||
| 270 | #return( \%hash_ref ); | ||||||
| 271 | 0 | 0 | return( $self->cgi_obj->{params} ); | ||||
| 272 | |||||||
| 273 | } # end: apache_param_hash | ||||||
| 274 | |||||||
| 275 | #------------------------------------------------- | ||||||
| 276 | # $self->apache_uf_param_hash( $req ) | ||||||
| 277 | #------------------------------------------------- | ||||||
| 278 | sub apache_uf_param_hash { | ||||||
| 279 | 0 | 0 | 1 | 0 | my( $self ) = @_; | ||
| 280 | |||||||
| 281 | 0 | 0 | return( $self->cgi_obj->{uf_params} ); | ||||
| 282 | |||||||
| 283 | } # end: apache_uf_param_hash | ||||||
| 284 | |||||||
| 285 | #------------------------------------------------- | ||||||
| 286 | # $self->apache_request( ) | ||||||
| 287 | #------------------------------------------------- | ||||||
| 288 | sub apache_request { | ||||||
| 289 | 0 | 0 | 1 | 0 | my( $self, $r ) = @_; | ||
| 290 | |||||||
| 291 | } # end: apache_request | ||||||
| 292 | |||||||
| 293 | #------------------------------------------------- | ||||||
| 294 | # $self->base_server( $r ) | ||||||
| 295 | #------------------------------------------------- | ||||||
| 296 | sub base_server { | ||||||
| 297 | 0 | 0 | 1 | 0 | my( $self ) = ( shift ); | ||
| 298 | |||||||
| 299 | 0 | 0 | 0 | return( $ENV{HTTP_SERVER} || $ENV{HTTP_HOST} ); | |||
| 300 | |||||||
| 301 | } # end base_server | ||||||
| 302 | |||||||
| 303 | #------------------------------------------------- | ||||||
| 304 | # $self->hostname( ) | ||||||
| 305 | #------------------------------------------------- | ||||||
| 306 | sub hostname { | ||||||
| 307 | 0 | 0 | 1 | 0 | my( $self ) = ( shift ); | ||
| 308 | |||||||
| 309 | 0 | 0 | 0 | return( $ENV{HTTP_SERVER} || $ENV{HTTP_HOST} ); | |||
| 310 | |||||||
| 311 | } # end hostname | ||||||
| 312 | |||||||
| 313 | #-------------------------------------------------- | ||||||
| 314 | # $self->cgi_obj( $hash_ref ) | ||||||
| 315 | #-------------------------------------------------- | ||||||
| 316 | sub cgi_obj { | ||||||
| 317 | 1 | 1 | 1 | 7 | my( $self, $hash_ref ) = @_; | ||
| 318 | |||||||
| 319 | 1 | 50 | 4 | if ( defined $hash_ref ) { | |||
| 320 | 1 | 2 | $self->{cgi_obj} = $hash_ref; | ||||
| 321 | } | ||||||
| 322 | |||||||
| 323 | 1 | 3 | return $self->{cgi_obj}; | ||||
| 324 | } # end cgi_obj | ||||||
| 325 | |||||||
| 326 | #-------------------------------------------------- | ||||||
| 327 | # $self->config( $hash_ref ) | ||||||
| 328 | #-------------------------------------------------- | ||||||
| 329 | sub config { | ||||||
| 330 | 0 | 0 | 1 | 0 | my( $self, $hash_ref ) = @_; | ||
| 331 | |||||||
| 332 | 0 | 0 | 0 | if ( defined $hash_ref ) { | |||
| 333 | 0 | 0 | $self->{cgi_obj}{config} = $hash_ref; | ||||
| 334 | } | ||||||
| 335 | |||||||
| 336 | 0 | 0 | return $self->{cgi_obj}{config}; | ||||
| 337 | } # end config | ||||||
| 338 | |||||||
| 339 | #------------------------------------------------- | ||||||
| 340 | # $self->declined_response( ) | ||||||
| 341 | #------------------------------------------------- | ||||||
| 342 | sub declined_response { | ||||||
| 343 | 0 | 0 | 1 | 0 | my( $self, $action ) = @_; | ||
| 344 | |||||||
| 345 | 0 | 0 | print $self->cgi->header( | ||||
| 346 | -type => 'text/html', | ||||||
| 347 | -status => '404 Not Found', | ||||||
| 348 | ); | ||||||
| 349 | |||||||
| 350 | 0 | 0 | my $current_location = $self->config->{ location }; | ||||
| 351 | |||||||
| 352 | 0 | 0 | 0 | print( $self->custom_error( | |||
| 353 | "Declined - undefined method " |
||||||
| 354 | . "" | ||||||
| 355 | . "Method: $action " |
||||||
| 356 | . "Location: " . $current_location . " " |
||||||
| 357 | . "Module: " . ( | ||||||
| 358 | $self->locations->{ $current_location } | ||||||
| 359 | || 'No module defined for this location' ) | ||||||
| 360 | . "" | ||||||
| 361 | ) | ||||||
| 362 | ); | ||||||
| 363 | |||||||
| 364 | 0 | 0 | return '404 Not Found'; | ||||
| 365 | |||||||
| 366 | } # END declined_response | ||||||
| 367 | |||||||
| 368 | #------------------------------------------------- | ||||||
| 369 | # $self->dispatch_location( ) | ||||||
| 370 | #------------------------------------------------- | ||||||
| 371 | sub dispatch_location { | ||||||
| 372 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 373 | |||||||
| 374 | 0 | 0 | return( $ENV{ PATH_INFO }, $self->config->{location} ); | ||||
| 375 | } # END dispatch_location | ||||||
| 376 | |||||||
| 377 | #-------------------------------------------------- | ||||||
| 378 | # $self->engine | ||||||
| 379 | #-------------------------------------------------- | ||||||
| 380 | sub engine { | ||||||
| 381 | 0 | 0 | 1 | 0 | return __PACKAGE__; | ||
| 382 | } # engine | ||||||
| 383 | |||||||
| 384 | #------------------------------------------------- | ||||||
| 385 | # $self->engine_init( $cgi_obj ) | ||||||
| 386 | #------------------------------------------------- | ||||||
| 387 | sub engine_init { | ||||||
| 388 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 389 | 0 | 0 | my $cgi_obj = shift; | ||||
| 390 | 0 | 0 | my $c = new CGI::Simple(); | ||||
| 391 | |||||||
| 392 | 0 | 0 | 0 | $c->parse_query_string() if $ENV{ REQUEST_METHOD } eq 'POST'; | |||
| 393 | 0 | 0 | $self->cgi( $c ); | ||||
| 394 | |||||||
| 395 | # check for CGI::Simple errors | ||||||
| 396 | 0 | 0 | 0 | if ( $c->{'.cgi_error'} ) { | |||
| 397 | 0 | 0 | my $error = $c->{'.cgi_error'}; | ||||
| 398 | 0 | 0 | my ( $status ) = ( $error =~ s/^(\d+)\s+// ); | ||||
| 399 | 0 | 0 | 0 | $self->status( $status || 400 ); | |||
| 400 | 0 | 0 | die( "$error\n" ); | ||||
| 401 | } | ||||||
| 402 | |||||||
| 403 | # fix up params so the multiselects are arraays | ||||||
| 404 | 0 | 0 | my $params = {}; | ||||
| 405 | 0 | 0 | my $uf_params = {}; | ||||
| 406 | |||||||
| 407 | 0 | 0 | foreach my $field ( $c->param ) { | ||||
| 408 | 0 | 0 | my @values = $c->param( $field ); | ||||
| 409 | |||||||
| 410 | 0 | 0 | 0 | if ( scalar @values > 1 ) { | |||
| 411 | 0 | 0 | $uf_params->{$field} = [ @values ]; | ||||
| 412 | |||||||
| 413 | # Replace angle brackets and quotes with named-entity equivalents. | ||||||
| 414 | 0 | 0 | $_ =~ s/</g foreach @values; | ||||
| 415 | 0 | 0 | $_ =~ s/>/>/g foreach @values; | ||||
| 416 | 0 | 0 | $_ =~ s/"/"/g foreach @values; | ||||
| 417 | 0 | 0 | $_ =~ s/'/'/g foreach @values; | ||||
| 418 | |||||||
| 419 | # Trim leading / trailing whitespace. | ||||||
| 420 | 0 | 0 | $_ =~ s/^\s+//o foreach @values; | ||||
| 421 | 0 | 0 | $_ =~ s/\s+$//o foreach @values; | ||||
| 422 | |||||||
| 423 | 0 | 0 | $params->{$field} = [ @values ]; | ||||
| 424 | } | ||||||
| 425 | |||||||
| 426 | else { | ||||||
| 427 | 0 | 0 | $params->{$field} = $c->param( $field ); | ||||
| 428 | 0 | 0 | $uf_params->{$field} = $params->{$field}; | ||||
| 429 | |||||||
| 430 | # Replace angle brackets and quotes with named-entity equivalents. | ||||||
| 431 | 0 | 0 | $params->{$field} =~ s/</g; | ||||
| 432 | 0 | 0 | $params->{$field} =~ s/>/>/g; | ||||
| 433 | 0 | 0 | $params->{$field} =~ s/"/"/g; | ||||
| 434 | 0 | 0 | $params->{$field} =~ s/'/'/g; | ||||
| 435 | |||||||
| 436 | # Trim leading / trailing whitespace. | ||||||
| 437 | 0 | 0 | $params->{$field} =~ s/^\s+//o; | ||||
| 438 | 0 | 0 | $params->{$field} =~ s/\s+$//o; | ||||
| 439 | } | ||||||
| 440 | } | ||||||
| 441 | |||||||
| 442 | # add in the fieldnames | ||||||
| 443 | 0 | 0 | $params->{'.fieldnames'} = [ $c->param ]; | ||||
| 444 | 0 | 0 | $uf_params->{'.fieldnames'} = [ $c->param ]; | ||||
| 445 | |||||||
| 446 | # If the application has specified that it wants the unfiltered params | ||||||
| 447 | # by default, then make it happen. | ||||||
| 448 | 0 | 0 | 0 | 0 | if ($self->fish_config( 'unfiltered_params' ) && $self->fish_config( 'unfiltered_params' ) =~ /(1|on)/i) { | ||
| 449 | 0 | 0 | $cgi_obj->{params} = $uf_params; | ||||
| 450 | } | ||||||
| 451 | |||||||
| 452 | # Else, the application gets the request parameters filtered by default. | ||||||
| 453 | # NOTE: It's got access to the unfiltered hash, in case it needs a | ||||||
| 454 | # request/field to have the parameters in such a way. | ||||||
| 455 | else { | ||||||
| 456 | 0 | 0 | $cgi_obj->{params} = $params; | ||||
| 457 | 0 | 0 | $cgi_obj->{uf_params} = $uf_params; | ||||
| 458 | } | ||||||
| 459 | |||||||
| 460 | 0 | 0 | $self->cgi_obj( $cgi_obj ); | ||||
| 461 | |||||||
| 462 | } # END engine_init | ||||||
| 463 | |||||||
| 464 | #------------------------------------------------- | ||||||
| 465 | # $self->err_header_out( $header_key, $header_value ) | ||||||
| 466 | #------------------------------------------------- | ||||||
| 467 | 0 | 0 | 1 | 0 | sub err_header_out { | ||
| 468 | # Gantry.pm calls this for mod_perl's benefit. | ||||||
| 469 | } # end err_header_out | ||||||
| 470 | |||||||
| 471 | #------------------------------------------------- | ||||||
| 472 | # $self->fish_location( ) | ||||||
| 473 | #------------------------------------------------- | ||||||
| 474 | sub fish_location { | ||||||
| 475 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 476 | |||||||
| 477 | 0 | 0 | 0 | my $app_rootp = $self->fish_config( 'app_rootp' ) || ''; | |||
| 478 | 0 | 0 | 0 | my $location = $self->fish_config( 'location' ) || ''; | |||
| 479 | |||||||
| 480 | 0 | 0 | return( $app_rootp . $location ); | ||||
| 481 | } # END fish_location | ||||||
| 482 | |||||||
| 483 | #------------------------------------------------- | ||||||
| 484 | # $self->fish_method( ) | ||||||
| 485 | #------------------------------------------------- | ||||||
| 486 | sub fish_method { | ||||||
| 487 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 488 | |||||||
| 489 | 0 | 0 | return $ENV{ REQUEST_METHOD }; | ||||
| 490 | } # END fish_method | ||||||
| 491 | |||||||
| 492 | #------------------------------------------------- | ||||||
| 493 | # $self->fish_path_info( ) | ||||||
| 494 | #------------------------------------------------- | ||||||
| 495 | sub fish_path_info { | ||||||
| 496 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 497 | |||||||
| 498 | 0 | 0 | return $ENV{ PATH_INFO }; | ||||
| 499 | } # END fish_path_info | ||||||
| 500 | |||||||
| 501 | #------------------------------------------------- | ||||||
| 502 | # $self->fish_uri( ) | ||||||
| 503 | #------------------------------------------------- | ||||||
| 504 | sub fish_uri { | ||||||
| 505 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 506 | |||||||
| 507 | 0 | 0 | 0 | my $sn = $ENV{SCRIPT_NAME} || ''; | |||
| 508 | 0 | 0 | 0 | my $pi = $ENV{PATH_INFO} || ''; | |||
| 509 | |||||||
| 510 | 0 | 0 | return( "${sn}${pi}" ); | ||||
| 511 | } # END fish_uri | ||||||
| 512 | |||||||
| 513 | #------------------------------------------------- | ||||||
| 514 | # $self->fish_user( ) | ||||||
| 515 | #------------------------------------------------- | ||||||
| 516 | sub fish_user { | ||||||
| 517 | 0 | 0 | 1 | 0 | my $self = shift; | ||
| 518 | |||||||
| 519 | 0 | 0 | 0 | return $self->user() || $self->{cgi_obj}{config}{user} || ''; | |||
| 520 | } # END fish_user | ||||||
| 521 | |||||||
| 522 | #-------------------------------------------------- | ||||||
| 523 | # $self->fish_config( $param ) | ||||||
| 524 | #-------------------------------------------------- | ||||||
| 525 | sub fish_config { | ||||||
| 526 | 5 | 5 | 1 | 960 | my $self = shift; | ||
| 527 | 5 | 8 | my $param = shift; | ||||
| 528 | |||||||
| 529 | # see if there is Gantry::Conf data | ||||||
| 530 | 5 | 14 | my $conf = $self->get_config(); | ||||
| 531 | |||||||
| 532 | 5 | 50 | 33 | 15 | return $$conf{ $param } if ( defined $conf and defined $$conf{ $param } ); | ||
| 533 | |||||||
| 534 | # otherwise, look in the cgi engine object | ||||||
| 535 | # ... starting at the location levels | ||||||
| 536 | 5 | 50 | 15 | if ( $self->{ cgi_obj }{ config }{ GantryLocation } ) { | |||
| 537 | 5 | 9 | my $glocs = $self->{ cgi_obj }{ config }{ GantryLocation }; | ||||
| 538 | 5 | 15 | my $loc = $self->location; | ||||
| 539 | 5 | 17 | my @path = split( '/', $loc ); | ||||
| 540 | |||||||
| 541 | 5 | 16 | while( @path ) { | ||||
| 542 | |||||||
| 543 | 4 | 9 | my $path = join( '/', @path ); | ||||
| 544 | |||||||
| 545 | 4 | 100 | 66 | 25 | if ( defined $glocs->{ $path } | ||
| 546 | and | ||||||
| 547 | defined $glocs->{ $path }{ $param } | ||||||
| 548 | ) { | ||||||
| 549 | 3 | 13 | return $glocs->{ $path }{ $param }; | ||||
| 550 | } | ||||||
| 551 | |||||||
| 552 | 1 | 39 | pop @path; | ||||
| 553 | } | ||||||
| 554 | } | ||||||
| 555 | |||||||
| 556 | # ... then defaulting to the top level | ||||||
| 557 | 2 | 7 | return $self->{cgi_obj}{config}{ $param }; | ||||
| 558 | |||||||
| 559 | } | ||||||
| 560 | |||||||
| 561 | #-------------------------------------------------- | ||||||
| 562 | # $self->get_config | ||||||
| 563 | #-------------------------------------------------- | ||||||
| 564 | sub get_config { | ||||||
| 565 | 5 | 5 | 1 | 4 | my $self = shift; | ||
| 566 | 5 | 33 | 24 | my $instance = shift || $self->{cgi_obj}{config}{ GantryConfInstance }; | |||
| 567 | |||||||
| 568 | 5 | 50 | 15 | return unless defined $instance; | |||
| 569 | |||||||
| 570 | 0 | 0 | my $file = shift || $self->{cgi_obj}{config}{ GantryConfFile }; | ||||
| 571 | |||||||
| 572 | 0 | my $conf; | |||||
| 573 | 0 | my $cached = 0; | |||||
| 574 | 0 | my $location = ''; | |||||
| 575 | |||||||
| 576 | |||||||
| 577 | 0 | eval { | |||||
| 578 | 0 | $location = $self->location; | |||||
| 579 | }; | ||||||
| 580 | |||||||
| 581 | 0 | $conf = $self->get_cached_config( $instance, $location ); | |||||
| 582 | 0 | 0 | if ( defined $conf ) { | ||||
| 583 | 0 | return $conf; | |||||
| 584 | } | ||||||
| 585 | |||||||
| 586 | 0 | my $gantry_cache = 0; | |||||
| 587 | 0 | my $gantry_cache_key = ''; | |||||
| 588 | 0 | my $gantry_cache_hit = 0; | |||||
| 589 | 0 | eval { | |||||
| 590 | 0 | 0 | ++$gantry_cache if $self->cache_inited(); | ||||
| 591 | }; | ||||||
| 592 | |||||||
| 593 | # are we using gantry cache ? | ||||||
| 594 | 0 | 0 | if ( $gantry_cache ) { | ||||
| 595 | |||||||
| 596 | 0 | $self->cache_namespace('gantry'); | |||||
| 597 | |||||||
| 598 | # blow the gantry conf cache when server starts | ||||||
| 599 | 0 | 0 | if ( $self->engine_cycle() == 1 ) { | ||||
| 600 | |||||||
| 601 | 0 | eval { | |||||
| 602 | 0 | foreach my $key ( @{ $self->cache_keys() } ) { | |||||
| 0 | |||||||
| 603 | 0 | my @a = split( ':', $key ); | |||||
| 604 | 0 | 0 | if ( $a[0] eq 'gantryconf' ) { | ||||
| 605 | 0 | $self->cache_del( $key ); | |||||
| 606 | } | ||||||
| 607 | } | ||||||
| 608 | }; | ||||||
| 609 | } | ||||||
| 610 | |||||||
| 611 | # build cache key | ||||||
| 612 | 0 | 0 | $gantry_cache_key = join( ':', | ||||
| 613 | "gantryconf", | ||||||
| 614 | ( $self->namespace() || '' ), | ||||||
| 615 | $instance, | ||||||
| 616 | $location | ||||||
| 617 | ); | ||||||
| 618 | |||||||
| 619 | 0 | $conf = $self->cache_get( $gantry_cache_key ); | |||||
| 620 | |||||||
| 621 | 0 | 0 | ++$gantry_cache_hit if defined $conf; | ||||
| 622 | } | ||||||
| 623 | |||||||
| 624 | # There will be an error if this method was called during construction | ||||||
| 625 | # that is before their is a Gantry descended object as the invocant. | ||||||
| 626 | # In that case, we don't care about the location anyway. | ||||||
| 627 | 0 | require Gantry::Conf; | |||||
| 628 | |||||||
| 629 | 0 | 0 | $conf ||= Gantry::Conf->retrieve( | ||||
| 630 | { | ||||||
| 631 | instance => $instance, | ||||||
| 632 | config_file => $file, | ||||||
| 633 | location => $location | ||||||
| 634 | } | ||||||
| 635 | ); | ||||||
| 636 | |||||||
| 637 | 0 | 0 | if ( defined $conf ) { | ||||
| 638 | 0 | $self->set_cached_config( $instance, $location, $conf ); | |||||
| 639 | |||||||
| 640 | 0 | 0 | 0 | if ( $gantry_cache && ! $gantry_cache_hit ) { | |||
| 641 | 0 | $self->cache_set( $gantry_cache_key, $conf ); | |||||
| 642 | } | ||||||
| 643 | } | ||||||
| 644 | |||||||
| 645 | 0 | return $conf; | |||||
| 646 | |||||||
| 647 | } # END get_config | ||||||
| 648 | |||||||
| 649 | my %conf_cache; | ||||||
| 650 | |||||||
| 651 | sub get_cached_config { | ||||||
| 652 | 0 | 0 | 1 | my $self = shift; | |||
| 653 | 0 | my $instance = shift; | |||||
| 654 | 0 | my $location = shift; | |||||
| 655 | |||||||
| 656 | 0 | 0 | return $conf_cache{ $instance . $location } || undef; | ||||
| 657 | } | ||||||
| 658 | |||||||
| 659 | sub set_cached_config { | ||||||
| 660 | 0 | 0 | 1 | my $self = shift; | |||
| 661 | 0 | my $instance = shift; | |||||
| 662 | 0 | my $location = shift; # not using location, this cache good for one page | |||||
| 663 | 0 | my $conf = shift; | |||||
| 664 | |||||||
| 665 | 0 | $conf_cache{ $instance . $location } = $conf; | |||||
| 666 | } | ||||||
| 667 | |||||||
| 668 | #------------------------------------------------- | ||||||
| 669 | # $self->get_arg_hash | ||||||
| 670 | #------------------------------------------------- | ||||||
| 671 | sub get_arg_hash { | ||||||
| 672 | 0 | 0 | 1 | my( $self ) = @_; | |||
| 673 | |||||||
| 674 | #my %hash_ref = $self->cgi->Vars; | ||||||
| 675 | |||||||
| 676 | 0 | 0 | return wantarray ? %{ $self->cgi_obj->{params} } | ||||
| 0 | |||||||
| 677 | : $self->cgi_obj->{params}; | ||||||
| 678 | |||||||
| 679 | } # end get_arg_hash | ||||||
| 680 | |||||||
| 681 | #------------------------------------------------- | ||||||
| 682 | # $self->get_auth_dbh( ) | ||||||
| 683 | #------------------------------------------------- | ||||||
| 684 | sub get_auth_dbh { | ||||||
| 685 | 0 | 0 | 1 | Gantry::Utils::DBConnHelper::Script->get_auth_dbh; | |||
| 686 | } | ||||||
| 687 | |||||||
| 688 | #------------------------------------------------- | ||||||
| 689 | # $self->get_dbh( ) | ||||||
| 690 | #------------------------------------------------- | ||||||
| 691 | sub get_dbh { | ||||||
| 692 | 0 | 0 | 1 | Gantry::Utils::DBConnHelper::Script->get_dbh; | |||
| 693 | } | ||||||
| 694 | |||||||
| 695 | #------------------------------------------------- | ||||||
| 696 | # $self->header_in( ) | ||||||
| 697 | #------------------------------------------------- | ||||||
| 698 | sub header_in { | ||||||
| 699 | 0 | 0 | 1 | my( $self, $key ) = @_; | |||
| 700 | |||||||
| 701 | 0 | 0 | return $ENV{uc $key} || $ENV{$key} || ''; | ||||
| 702 | } # end header_in | ||||||
| 703 | |||||||
| 704 | #------------------------------------------------- | ||||||
| 705 | # $self->header_out( $header_key, $header_value ) | ||||||
| 706 | #------------------------------------------------- | ||||||
| 707 | sub header_out { | ||||||
| 708 | 0 | 0 | 1 | my( $self, $k, $v ) = @_; | |||
| 709 | |||||||
| 710 | # $self->{__HEADERS_OUT__}->{$k} = $v if defined $k; | ||||||
| 711 | # return( $self->{__HEADERS_OUT__} ); | ||||||
| 712 | |||||||
| 713 | 0 | return $self->response_headers( $k, $v ); | |||||
| 714 | |||||||
| 715 | } # end header_out | ||||||
| 716 | |||||||
| 717 | #-------------------------------------------------- | ||||||
| 718 | # $self->locations( $hash_ref ) | ||||||
| 719 | #-------------------------------------------------- | ||||||
| 720 | sub locations { | ||||||
| 721 | 0 | 0 | 1 | my( $self, $hash_ref ) = @_; | |||
| 722 | |||||||
| 723 | 0 | 0 | if ( defined $hash_ref ) { | ||||
| 724 | 0 | $self->{cgi_obj}{locations} = $hash_ref; | |||||
| 725 | } | ||||||
| 726 | |||||||
| 727 | 0 | return $self->{cgi_obj}{locations}; | |||||
| 728 | } # end locations | ||||||
| 729 | |||||||
| 730 | #-------------------------------------------------- | ||||||
| 731 | # $self->log_error( $text ) | ||||||
| 732 | #-------------------------------------------------- | ||||||
| 733 | sub log_error { | ||||||
| 734 | 0 | 0 | 1 | my ( $self, $text ) = @_; | |||
| 735 | |||||||
| 736 | 0 | warn "$text\n"; | |||||
| 737 | } | ||||||
| 738 | |||||||
| 739 | #------------------------------------------------- | ||||||
| 740 | # $self->redirect_response( ) | ||||||
| 741 | #------------------------------------------------- | ||||||
| 742 | sub redirect_response { | ||||||
| 743 | 0 | 0 | 1 | my $self = shift; | |||
| 744 | |||||||
| 745 | 0 | my $cookies = ''; | |||||
| 746 | 0 | foreach my $cookie ( @{ $self->cookie_stash() } ) { | |||||
| 0 | |||||||
| 747 | 0 | print "Set-Cookie: $cookie\n"; | |||||
| 748 | } | ||||||
| 749 | |||||||
| 750 | 0 | my $p = {}; | |||||
| 751 | 0 | $p->{uri} = $self->response_headers->{location}; | |||||
| 752 | |||||||
| 753 | 0 | print $self->cgi->redirect( $p ); | |||||
| 754 | |||||||
| 755 | 0 | return 302; | |||||
| 756 | } # END redirect_response | ||||||
| 757 | |||||||
| 758 | #------------------------------------------------- | ||||||
| 759 | # $self->remote_ip( $r ) | ||||||
| 760 | #------------------------------------------------- | ||||||
| 761 | sub remote_ip { | ||||||
| 762 | 0 | 0 | 1 | my( $self ) = ( shift, shift ); | |||
| 763 | |||||||
| 764 | 0 | return( $ENV{REMOTE_ADDR} ); | |||||
| 765 | |||||||
| 766 | } # end remote_ip | ||||||
| 767 | |||||||
| 768 | #------------------------------------------------- | ||||||
| 769 | # $self->print_output( $response_page ) | ||||||
| 770 | #------------------------------------------------- | ||||||
| 771 | sub print_output { | ||||||
| 772 | 0 | 0 | 1 | my $self = shift; | |||
| 773 | 0 | my $response_page = shift; | |||||
| 774 | |||||||
| 775 | 0 | print $response_page; | |||||
| 776 | |||||||
| 777 | } # print_output | ||||||
| 778 | |||||||
| 779 | #------------------------------------------------- | ||||||
| 780 | # $self->port( $r ) | ||||||
| 781 | #------------------------------------------------- | ||||||
| 782 | sub port { | ||||||
| 783 | 0 | 0 | 1 | my( $self ) = ( shift ); | |||
| 784 | |||||||
| 785 | 0 | return( $ENV{SERVER_PORT} ); | |||||
| 786 | |||||||
| 787 | } # end port | ||||||
| 788 | |||||||
| 789 | #------------------------------------------------- | ||||||
| 790 | # $self->server_root( $r ) | ||||||
| 791 | #------------------------------------------------- | ||||||
| 792 | sub server_root { | ||||||
| 793 | 0 | 0 | 1 | my( $self ) = ( shift ); | |||
| 794 | |||||||
| 795 | 0 | return( $ENV{HTTP_SERVER} ); | |||||
| 796 | |||||||
| 797 | } # end server_root | ||||||
| 798 | |||||||
| 799 | #------------------------------------------------- | ||||||
| 800 | # $self->status_const( 'OK | DECLINED | REDIRECT' ) | ||||||
| 801 | #------------------------------------------------- | ||||||
| 802 | sub status_const { | ||||||
| 803 | 0 | 0 | 1 | my( $self, $status ) = @_; | |||
| 804 | |||||||
| 805 | 0 | 0 | return '404' if uc $status eq 'DECLINED'; | ||||
| 806 | 0 | 0 | return '200' if uc $status eq 'OK'; | ||||
| 807 | 0 | 0 | return '301' if uc $status eq 'MOVED_PERMANENTLY'; | ||||
| 808 | 0 | 0 | return '302' if uc $status eq 'REDIRECT'; | ||||
| 809 | 0 | 0 | return '403' if uc $status eq 'FORBIDDEN'; | ||||
| 810 | 0 | 0 | return '401' if uc $status eq 'AUTH_REQUIRED'; | ||||
| 811 | 0 | 0 | return '401' if uc $status eq 'HTTP_UNAUTHORIZED'; | ||||
| 812 | 0 | 0 | return '400' if uc $status eq 'BAD_REQUEST'; | ||||
| 813 | 0 | 0 | return '500' if uc $status eq 'SERVER_ERROR'; | ||||
| 814 | |||||||
| 815 | 0 | die( "Undefined constant $status" ); | |||||
| 816 | |||||||
| 817 | |||||||
| 818 | } # end status_const | ||||||
| 819 | |||||||
| 820 | #------------------------------------------------- | ||||||
| 821 | # $self->is_connection_secure() | ||||||
| 822 | #------------------------------------------------- | ||||||
| 823 | sub is_connection_secure { | ||||||
| 824 | 0 | 0 | 1 | my $self = shift; | |||
| 825 | |||||||
| 826 | 0 | 0 | return $ENV{'SSL_PROTOCOL'} ? 1 : 0; | ||||
| 827 | } # END is_connection_secure | ||||||
| 828 | |||||||
| 829 | #------------------------------------------------- | ||||||
| 830 | # $self->is_status_declined( $status ) | ||||||
| 831 | #------------------------------------------------- | ||||||
| 832 | sub is_status_declined { | ||||||
| 833 | 0 | 0 | 1 | my $self = shift; | |||
| 834 | |||||||
| 835 | 0 | 0 | my $status = $self->status || ''; | ||||
| 836 | |||||||
| 837 | 0 | 0 | return 1 if ( $status eq 'DECLINED' ); | ||||
| 838 | } # END is_status_declined | ||||||
| 839 | |||||||
| 840 | #------------------------------------------------- | ||||||
| 841 | # $self->send_error_output( $@ ) | ||||||
| 842 | #------------------------------------------------- | ||||||
| 843 | sub send_error_output { | ||||||
| 844 | 0 | 0 | 1 | my $self = shift; | |||
| 845 | |||||||
| 846 | 0 | print $self->cgi->header( | |||||
| 847 | -type => 'text/html', | ||||||
| 848 | -status => '500 Server Error', | ||||||
| 849 | ); | ||||||
| 850 | |||||||
| 851 | 0 | $self->do_error( $@ ); | |||||
| 852 | 0 | print( $self->custom_error( $@ ) ); | |||||
| 853 | |||||||
| 854 | } # END send_error_output | ||||||
| 855 | |||||||
| 856 | #------------------------------------------------- | ||||||
| 857 | # $self->send_http_header( ) | ||||||
| 858 | #------------------------------------------------- | ||||||
| 859 | sub send_http_header { | ||||||
| 860 | 0 | 0 | 1 | my $self = shift; | |||
| 861 | |||||||
| 862 | 0 | my $cookies = ''; | |||||
| 863 | 0 | foreach my $cookie ( @{ $self->cookie_stash() } ) { | |||||
| 0 | |||||||
| 864 | 0 | print "Set-Cookie: $cookie\n"; | |||||
| 865 | } | ||||||
| 866 | |||||||
| 867 | 0 | my $header_for = $self->response_headers(); | |||||
| 868 | |||||||
| 869 | 0 | foreach my $variable ( keys %{ $header_for } ) { | |||||
| 0 | |||||||
| 870 | 0 | print "$variable: $header_for->{ $variable }\n"; | |||||
| 871 | } | ||||||
| 872 | |||||||
| 873 | 0 | 0 | print $self->cgi->header( | ||||
| 0 | |||||||
| 874 | -type => ( $self->content_type ? $self->content_type : 'text/html' ), | ||||||
| 875 | -status => ( $self->status() ? $self->status() : '200 OK' ), | ||||||
| 876 | ); | ||||||
| 877 | |||||||
| 878 | } # send_http_header | ||||||
| 879 | |||||||
| 880 | #------------------------------------------------- | ||||||
| 881 | # $self->set_content_type( ) | ||||||
| 882 | #------------------------------------------------- | ||||||
| 883 | 0 | 0 | 1 | sub set_content_type { | |||
| 884 | |||||||
| 885 | |||||||
| 886 | # This method is for mod_perl engines. They need to transfer | ||||||
| 887 | # the content_type from the site object to the apache request object. | ||||||
| 888 | # We don't need to do that. | ||||||
| 889 | |||||||
| 890 | } # set_content_type | ||||||
| 891 | |||||||
| 892 | #------------------------------------------------- | ||||||
| 893 | # $self->set_no_cache( ) | ||||||
| 894 | #------------------------------------------------- | ||||||
| 895 | sub set_no_cache { | ||||||
| 896 | 0 | 0 | 1 | my $self = shift; | |||
| 897 | |||||||
| 898 | 0 | 0 | $self->cgi->no_cache( 1 ) if $self->no_cache; | ||||
| 899 | } # set_no_cache | ||||||
| 900 | |||||||
| 901 | #------------------------------------------------- | ||||||
| 902 | # $self->set_req_params( ) | ||||||
| 903 | #------------------------------------------------- | ||||||
| 904 | sub set_req_params { | ||||||
| 905 | 0 | 0 | 1 | my $self = shift; | |||
| 906 | |||||||
| 907 | 0 | $self->params( $self->cgi_obj->{params} ); | |||||
| 908 | 0 | $self->uf_params( $self->cgi_obj->{uf_params} ); | |||||
| 909 | |||||||
| 910 | } # END set_req_params | ||||||
| 911 | |||||||
| 912 | #------------------------------------------------- | ||||||
| 913 | # $self->success_code( ) | ||||||
| 914 | #------------------------------------------------- | ||||||
| 915 | sub success_code { | ||||||
| 916 | |||||||
| 917 | 0 | 0 | 1 | return '200'; | |||
| 918 | # This is for mod_perl engines. They need to tell apache that | ||||||
| 919 | # things went well. | ||||||
| 920 | |||||||
| 921 | } # END success_code | ||||||
| 922 | |||||||
| 923 | sub parse_env { | ||||||
| 924 | 0 | 0 | 1 | my $data; | |||
| 925 | 0 | my $hash = {}; | |||||
| 926 | |||||||
| 927 | 0 | my $ParamSeparator = '&'; | |||||
| 928 | |||||||
| 929 | 0 | 0 | 0 | if ( defined $ENV{REQUEST_METHOD} | |||
| 0 | 0 | ||||||
| 0 | |||||||
| 930 | && $ENV{REQUEST_METHOD} eq "POST" ) { | ||||||
| 931 | |||||||
| 932 | 0 | read STDIN , $data , $ENV{CONTENT_LENGTH} ,0; | |||||
| 933 | |||||||
| 934 | 0 | 0 | if ( $ENV{QUERY_STRING} ) { | ||||
| 935 | 0 | $data .= $ParamSeparator . $ENV{QUERY_STRING}; | |||||
| 936 | } | ||||||
| 937 | |||||||
| 938 | } | ||||||
| 939 | elsif ( defined $ENV{REQUEST_METHOD} | ||||||
| 940 | && $ENV{REQUEST_METHOD} eq "GET" ) { | ||||||
| 941 | |||||||
| 942 | 0 | $data = $ENV{QUERY_STRING}; | |||||
| 943 | } | ||||||
| 944 | elsif ( defined $ENV{REQUEST_METHOD} ) { | ||||||
| 945 | 0 | print "Status: 405 Method Not Allowed\r\n\r\n"; | |||||
| 946 | 0 | exit; | |||||
| 947 | } | ||||||
| 948 | |||||||
| 949 | 0 | 0 | 0 | return {} unless (defined $data and $data ne ''); | |||
| 950 | |||||||
| 951 | |||||||
| 952 | 0 | $data =~ s/\?$//; | |||||
| 953 | 0 | my $i=0; | |||||
| 954 | |||||||
| 955 | 0 | my @items = grep {!/^$/} (split /$ParamSeparator/o, $data); | |||||
| 0 | |||||||
| 956 | 0 | my $thing; | |||||
| 957 | |||||||
| 958 | 0 | foreach $thing (@items) { | |||||
| 959 | |||||||
| 960 | 0 | my @res = $thing=~/^(.*?)=(.*)$/; | |||||
| 961 | 0 | my ( $name, $value, @value ); | |||||
| 962 | |||||||
| 963 | 0 | 0 | if ( $#res <= 0 ) { | ||||
| 964 | 0 | $name = $i++; | |||||
| 965 | 0 | $value = $thing; | |||||
| 966 | } | ||||||
| 967 | else { | ||||||
| 968 | 0 | ( $name, $value ) = @res; | |||||
| 969 | } | ||||||
| 970 | |||||||
| 971 | 0 | $name =~ tr/+/ /; | |||||
| 972 | 0 | $name =~ s/%(\w\w)/chr(hex $1)/ge; | |||||
| 0 | |||||||
| 973 | |||||||
| 974 | 0 | $value =~ tr/+/ /; | |||||
| 975 | 0 | $value =~ s/%(\w\w)/chr(hex $1)/ge; | |||||
| 0 | |||||||
| 976 | |||||||
| 977 | 0 | 0 | if ( $hash->{$name} ) { | ||||
| 978 | 0 | 0 | if ( ref $hash->{$name} ) { | ||||
| 979 | 0 | push( @{$hash->{$name}}, $value ); | |||||
| 0 | |||||||
| 980 | } | ||||||
| 981 | else { | ||||||
| 982 | 0 | $hash->{$name} = [ $hash->{$name}, $value]; | |||||
| 983 | } | ||||||
| 984 | } | ||||||
| 985 | else { | ||||||
| 986 | 0 | $hash->{$name} = $value; | |||||
| 987 | } | ||||||
| 988 | } | ||||||
| 989 | |||||||
| 990 | 0 | return( $hash ); | |||||
| 991 | } | ||||||
| 992 | |||||||
| 993 | #------------------------------------------------- | ||||||
| 994 | # $self->url_encode( ) | ||||||
| 995 | #------------------------------------------------- | ||||||
| 996 | sub url_encode { | ||||||
| 997 | 0 | 0 | 1 | my $self = shift; | |||
| 998 | 0 | my $value = shift; | |||||
| 999 | |||||||
| 1000 | 0 | return CGI::Simple::Util::escape( $value ); | |||||
| 1001 | } # END url_encode | ||||||
| 1002 | |||||||
| 1003 | #------------------------------------------------- | ||||||
| 1004 | # $self->url_decode( ) | ||||||
| 1005 | #------------------------------------------------- | ||||||
| 1006 | sub url_decode { | ||||||
| 1007 | 0 | 0 | 1 | my $self = shift; | |||
| 1008 | 0 | my $value = shift; | |||||
| 1009 | |||||||
| 1010 | 0 | return CGI::Simple::Util::unescape( $value ); | |||||
| 1011 | } # END url_decode | ||||||
| 1012 | |||||||
| 1013 | # EOF | ||||||
| 1014 | 1; | ||||||
| 1015 | |||||||
| 1016 | __END__ |