| 1 | # ------------------------------------------------------------------------------ | ||
| 2 | # | ||
| 3 | # This is a patched version by Skriptke of CGI::Minimal ver 1.29 by | ||
| 4 | # Benjamin Franz <snowhare@nihongo.org> | ||
| 5 | # http://search.cpan.org/dist/CGI-Minimal/ | ||
| 6 | # | ||
| 7 | # Licensed under the GNU GPL. | ||
| 8 | # http://nes.sourceforge.net/ | ||
| 9 | # | ||
| 10 | # NES Version 1.03 | ||
| 11 | # | ||
| 12 | # Minimal.pm | ||
| 13 | # | ||
| 14 | # ------------------------------------------------------------------------------ | ||
| 15 | |||
| 16 | package Nes::Minimal; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | |||
| 20 | # I don't 'use warnings;' here because it pulls in ~ 40Kbytes of code and | ||
| 21 | # interferes with 5.005 and earlier versions of Perl. | ||
| 22 | # | ||
| 23 | # I don't use vars qw ($_query $VERSION $form_initial_read $_BUFFER); for | ||
| 24 | # because it also pulls in warnings under later versions of perl. | ||
| 25 | # The code is clean - but the pragmas cause performance issues. | ||
| 26 | |||
| 27 | $Nes::Minimal::_query = undef; | ||
| 28 | $Nes::Minimal::form_initial_read = undef; | ||
| 29 | $Nes::Minimal::_BUFFER = undef; | ||
| 30 | $Nes::Minimal::_allow_hybrid_post_get = 0; | ||
| 31 | $Nes::Minimal::_mod_perl = 0; | ||
| 32 | $Nes::Minimal::_no_subprocess_env = 0; | ||
| 33 | |||
| 34 | $Nes::Minimal::_use_tmp = 0; | ||
| 35 | $Nes::Minimal::_max_upload = 0; | ||
| 36 | $Nes::Minimal::_save_BUFFER = undef; | ||
| 37 | $Nes::Minimal::_save_BUFFER_String = undef; | ||
| 38 | $Nes::Minimal::_ERROR_max_upload = 0; | ||
| 39 | $Nes::Minimal::_ERROR_max_post = 0; | ||
| 40 | $Nes::Minimal:: = undef; | ||
| 41 | |||
| 42 | $Nes::Minimal::VERSION = "1.2902"; | ||
| 43 | |||
| 44 | if (exists ($ENV{'MOD_PERL'}) && (0 == $Nes::Minimal::_mod_perl)) { | ||
| 45 | local $| = 1; | ||
| 46 | my $env_mod_perl = $ENV{'MOD_PERL'}; | ||
| 47 | if ($env_mod_perl =~ m#^mod_perl/1.99#) { # Redhat's almost-but-not-quite ModPerl2.... | ||
| 48 | require Apache::compat; | ||
| 49 | require Nes::Minimal::Misc; | ||
| 50 | require Nes::Minimal::Multipart; | ||
| 51 | $Nes::Minimal::_mod_perl = 1; | ||
| 52 | |||
| 53 | } elsif (exists ($ENV{MOD_PERL_API_VERSION}) && ($ENV{MOD_PERL_API_VERSION} == 2)) { | ||
| 54 | require Apache2::RequestUtil; | ||
| 55 | require Apache2::RequestIO; | ||
| 56 | require APR::Pool; | ||
| 57 | require Nes::Minimal::Misc; | ||
| 58 | require Nes::Minimal::Multipart; | ||
| 59 | $Nes::Minimal::_mod_perl = 2; | ||
| 60 | |||
| 61 | } else { | ||
| 62 | require Apache; | ||
| 63 | require Nes::Minimal::Misc; | ||
| 64 | require Nes::Minimal::Multipart; | ||
| 65 | $Nes::Minimal::_mod_perl = 1; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | binmode STDIN; | ||
| 69 | reset_globals(); | ||
| 70 | |||
| 71 | #### | ||
| 72 | |||
| 73 | sub import { | ||
| 74 | my $class = shift; | ||
| 75 | my %flags = map { $_ => 1 } @_; | ||
| 76 | if ($flags{':preload'}) { | ||
| 77 | require Nes::Minimal::Misc; | ||
| 78 | require Nes::Minimal::Multipart; | ||
| 79 | } | ||
| 80 | $Nes::Minimal::_no_subprocess_env = $flags{':no_subprocess_env'}; | ||
| 81 | } | ||
| 82 | |||
| 83 | #### | ||
| 84 | |||
| 85 | sub new { | ||
| 86 | my $proto = shift; | ||
| 87 | my $pkg = __PACKAGE__; | ||
| 88 | |||
| 89 | if ($Nes::Minimal::form_initial_read) { | ||
| 90 | binmode STDIN; | ||
| 91 | $Nes::Minimal::_query->_read_form; | ||
| 92 | $Nes::Minimal::form_initial_read = 0; | ||
| 93 | } | ||
| 94 | if (1 == $Nes::Minimal::_mod_perl) { | ||
| 95 | Apache->request->register_cleanup(\&Nes::Minimal::reset_globals); | ||
| 96 | |||
| 97 | } elsif (2 == $Nes::Minimal::_mod_perl) { | ||
| 98 | Apache2::RequestUtil->request->pool->cleanup_register(\&Nes::Minimal::reset_globals); | ||
| 99 | } | ||
| 100 | |||
| 101 | return $Nes::Minimal::_query; | ||
| 102 | } | ||
| 103 | |||
| 104 | #### | ||
| 105 | |||
| 106 | sub reset_globals { | ||
| 107 | $Nes::Minimal::form_initial_read = 1; | ||
| 108 | $Nes::Minimal::_allow_hybrid_post_get = 0; | ||
| 109 | |||
| 110 | $Nes::Minimal::_save_BUFFER = undef; | ||
| 111 | $Nes::Minimal::_save_BUFFER_String = undef; | ||
| 112 | $Nes::Minimal::_use_tmp = 0; | ||
| 113 | $Nes::Minimal::_max_upload = 0; | ||
| 114 | $Nes::Minimal::_ERROR_max_upload = 0; | ||
| 115 | $Nes::Minimal::_ERROR_max_post = 0; | ||
| 116 | $Nes::Minimal::_sub_filter = undef; | ||
| 117 | |||
| 118 | $Nes::Minimal::_query = {}; | ||
| 119 | bless $Nes::Minimal::_query; | ||
| 120 | my $pkg = __PACKAGE__; | ||
| 121 | |||
| 122 | $Nes::Minimal::_BUFFER = undef; | ||
| 123 | max_read_size(1000000); | ||
| 124 | $Nes::Minimal::_query->{$pkg}->{'field_names'} = []; | ||
| 125 | $Nes::Minimal::_query->{$pkg}->{'field'} = {}; | ||
| 126 | $Nes::Minimal::_query->{$pkg}->{'form_truncated'} = undef; | ||
| 127 | |||
| 128 | $Nes::Minimal::_query->{$pkg}->{'from_file'} = {}; | ||
| 129 | |||
| 130 | return 1; # Keeps mod_perl from complaining | ||
| 131 | } | ||
| 132 | |||
| 133 | # For backward compatibility | ||
| 134 | sub _reset_globals { reset_globals; } | ||
| 135 | |||
| 136 | ### | ||
| 137 | |||
| 138 | sub subprocess_env { | ||
| 139 | if (2 == $Nes::Minimal::_mod_perl) { | ||
| 140 | Apache2::RequestUtil->request->subprocess_env; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | ### | ||
| 145 | |||
| 146 | sub allow_hybrid_post_get { | ||
| 147 | if (@_ > 0) { | ||
| 148 | $Nes::Minimal::_allow_hybrid_post_get = $_[0]; | ||
| 149 | } else { | ||
| 150 | return $Nes::Minimal::_allow_hybrid_post_get; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | sub use_tmp { | ||
| 155 | if (@_ > 0) { | ||
| 156 | $Nes::Minimal::_use_tmp = $_[0]; | ||
| 157 | } else { | ||
| 158 | return $$Nes::Minimal::_use_tmp; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | sub save_buffer { | ||
| 163 | if (@_ > 0) { | ||
| 164 | $Nes::Minimal::_save_BUFFER = $_[0]; | ||
| 165 | } else { | ||
| 166 | return $$Nes::Minimal::_save_BUFFER; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub sub_filter { | ||
| 171 | if (@_ > 0) { | ||
| 172 | $Nes::Minimal::_sub_filter = $_[0]; | ||
| 173 | } else { | ||
| 174 | return $Nes::Minimal::_sub_filter; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | sub max_upload { | ||
| 179 | if (@_ > 0) { | ||
| 180 | $Nes::Minimal::_max_upload = $_[0]; | ||
| 181 | } else { | ||
| 182 | return $$Nes::Minimal::_max_upload; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | ### | ||
| 189 | |||
| 190 | sub delete_all { | ||
| 191 | my $self = shift; | ||
| 192 | my $pkg = __PACKAGE__; | ||
| 193 | $Nes::Minimal::_query->{$pkg}->{'field_names'} = []; | ||
| 194 | $Nes::Minimal::_query->{$pkg}->{'field'} = {}; | ||
| 195 | $Nes::Minimal::_query->{$pkg}->{'from_file'} = {}; | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | #### | ||
| 200 | |||
| 201 | sub delete { | ||
| 202 | my $self = shift; | ||
| 203 | my $pkg = __PACKAGE__; | ||
| 204 | my $vars = $self->{$pkg}; | ||
| 205 | |||
| 206 | my @names_list = @_; | ||
| 207 | my %tagged_names = map { $_ => 1 } @names_list; | ||
| 208 | my @parm_names = @{$vars->{'field_names'}}; | ||
| 209 | my $fields = []; | ||
| 210 | my $data = $vars->{'field'}; | ||
| 211 | foreach my $parm (@parm_names) { | ||
| 212 | if ($tagged_names{$parm}) { | ||
| 213 | delete $data->{$parm}; | ||
| 214 | } else { | ||
| 215 | push (@$fields, $parm); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | $vars->{'field_names'} = $fields; | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | #### | ||
| 223 | |||
| 224 | sub upload { | ||
| 225 | my $self = shift; | ||
| 226 | my $pkg = __PACKAGE__; | ||
| 227 | my ($field_name) = @_; | ||
| 228 | my $vars = $self->{$pkg}; | ||
| 229 | |||
| 230 | return if !defined($vars->{'field'}->{$field_name}); | ||
| 231 | return $vars->{'from_file'}->{'fh'}->{$field_name} if exists $vars->{'from_file'}->{'fh'}->{$field_name}; | ||
| 232 | |||
| 233 | # if not tmp file, the data is in var in old style =< 1.29 | ||
| 234 | if ( !$vars->{'from_file'}->{$field_name} ) { | ||
| 235 | require IO::String; | ||
| 236 | $vars->{'from_file'}->{'fh'}->{$field_name} = IO::String->new($vars->{'field'}->{$field_name}->{'value'}[0]); | ||
| 237 | } else { | ||
| 238 | $vars->{'from_file'}->{'fh'}->{$field_name} = $vars->{'from_file'}->{$field_name}; | ||
| 239 | } | ||
| 240 | binmode $vars->{'from_file'}->{'fh'}->{$field_name}; | ||
| 241 | |||
| 242 | return $vars->{'from_file'}->{'fh'}->{$field_name}; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub upload_is_tmp { | ||
| 246 | my $self = shift; | ||
| 247 | my $pkg = __PACKAGE__; | ||
| 248 | my ($field_name) = @_; | ||
| 249 | my $vars = $self->{$pkg}; | ||
| 250 | |||
| 251 | return 1 if $vars->{'from_file'}->{$field_name}; | ||
| 252 | return 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub upload_max_size { | ||
| 256 | my $self = shift; | ||
| 257 | my $pkg = __PACKAGE__; | ||
| 258 | my $vars = $self->{$pkg}; | ||
| 259 | |||
| 260 | return 1 if $Nes::Minimal::_ERROR_max_upload; | ||
| 261 | return 0; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub post_max_size { | ||
| 265 | my $self = shift; | ||
| 266 | my $pkg = __PACKAGE__; | ||
| 267 | my $vars = $self->{$pkg}; | ||
| 268 | |||
| 269 | return 1 if $Nes::Minimal::_ERROR_max_post; | ||
| 270 | return 0; | ||
| 271 | } | ||
| 272 | |||
| 273 | #### | ||
| 274 | |||
| 275 | sub param { | ||
| 276 | my $self = shift; | ||
| 277 | my $pkg = __PACKAGE__; | ||
| 278 | |||
| 279 | if (1 < @_) { | ||
| 280 | my $n_parms = @_; | ||
| 281 | if (($n_parms % 2) == 1) { | ||
| 282 | require Carp; | ||
| 283 | Carp::confess("${pkg}::param() - Odd number of parameters (other than 1) passed"); | ||
| 284 | } | ||
| 285 | |||
| 286 | my $parms = { @_ }; | ||
| 287 | require Nes::Minimal::Misc; | ||
| 288 | $self->_internal_set($parms); | ||
| 289 | return; | ||
| 290 | |||
| 291 | } elsif ((1 == @_) and (ref ($_[0]) eq 'HASH')) { | ||
| 292 | my $parms = shift; | ||
| 293 | require Nes::Minimal::Misc; | ||
| 294 | $self->_internal_set($parms); | ||
| 295 | return; | ||
| 296 | } | ||
| 297 | |||
| 298 | # Requesting parameter values | ||
| 299 | |||
| 300 | my $vars = $self->{$pkg}; | ||
| 301 | my @result = (); | ||
| 302 | if ($#_ == -1) { | ||
| 303 | @result = @{$vars->{'field_names'}}; | ||
| 304 | |||
| 305 | } else { | ||
| 306 | my ($fname) = @_; | ||
| 307 | if (defined($vars->{'field'}->{$fname})) { | ||
| 308 | @result = @{$vars->{'field'}->{$fname}->{'value'}}; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | if (wantarray) { return @result; } | ||
| 313 | elsif ($#result > -1) { return $result[0]; } | ||
| 314 | return; | ||
| 315 | } | ||
| 316 | |||
| 317 | #### | ||
| 318 | |||
| 319 | sub raw { | ||
| 320 | return if (! defined $Nes::Minimal::_BUFFER); | ||
| 321 | return $$Nes::Minimal::_BUFFER; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub raw_saved { | ||
| 325 | my $self = shift; | ||
| 326 | my ( $buffer, $read_length ) = @_; | ||
| 327 | |||
| 328 | my $fh = $Nes::Minimal::_BUFFER_saved || $Nes::Minimal::_save_BUFFER_String; | ||
| 329 | |||
| 330 | return if !$fh; | ||
| 331 | return read($fh, $$buffer, $read_length); | ||
| 332 | } | ||
| 333 | |||
| 334 | #### | ||
| 335 | |||
| 336 | sub truncated { | ||
| 337 | my $pkg = __PACKAGE__; | ||
| 338 | shift->{$pkg}->{'form_truncated'}; | ||
| 339 | } | ||
| 340 | |||
| 341 | #### | ||
| 342 | |||
| 343 | sub max_read_size { | ||
| 344 | my $pkg = __PACKAGE__; | ||
| 345 | $Nes::Minimal::_query->{$pkg}->{'max_buffer'} = $_[0]; | ||
| 346 | } | ||
| 347 | |||
| 348 | #### | ||
| 349 | # Wrapper for form reading for GET, HEAD and POST methods | ||
| 350 | |||
| 351 | sub _read_form { | ||
| 352 | my $self = shift; | ||
| 353 | |||
| 354 | my $pkg = __PACKAGE__; | ||
| 355 | my $vars = $self->{$pkg}; | ||
| 356 | |||
| 357 | $vars->{'field'} = {}; | ||
| 358 | $vars->{'field_names'} = []; | ||
| 359 | |||
| 360 | my $req_method=$ENV{"REQUEST_METHOD"}; | ||
| 361 | if ((2 == $Nes::Minimal::_mod_perl) and (not defined $req_method)) { | ||
| 362 | $req_method = Apache2::RequestUtil->request->method; | ||
| 363 | } | ||
| 364 | |||
| 365 | if (! defined $req_method) { | ||
| 366 | # todo, no funciona cuando hacemos ./script.cgi, revisar | ||
| 367 | # my $input = <STDIN>; | ||
| 368 | # $input = '' if (! defined $input); | ||
| 369 | # $ENV{'QUERY_STRING'} = $input; | ||
| 370 | # chomp $ENV{'QUERY_STRING'}; | ||
| 371 | # $self->_read_get; | ||
| 372 | # --------------------------------------------------------- | ||
| 373 | return; | ||
| 374 | } | ||
| 375 | if ($req_method eq 'POST') { | ||
| 376 | $self->_read_post; | ||
| 377 | if ($Nes::Minimal::_allow_hybrid_post_get) { | ||
| 378 | $self->_read_get; | ||
| 379 | } | ||
| 380 | } elsif (($req_method eq 'GET') || ($req_method eq 'HEAD')) { | ||
| 381 | $self->_read_get; | ||
| 382 | } else { | ||
| 383 | my $package = __PACKAGE__; | ||
| 384 | require Carp; | ||
| 385 | Carp::carp($package . " - Unsupported HTTP request method of '$req_method'. Treating as 'GET'"); | ||
| 386 | $self->_read_get; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | #### | ||
| 391 | # Performs form reading for POST method | ||
| 392 | |||
| 393 | sub _read_post { | ||
| 394 | my $self = shift; | ||
| 395 | my $pkg = __PACKAGE__; | ||
| 396 | my $vars = $self->{$pkg}; | ||
| 397 | |||
| 398 | my $r; | ||
| 399 | if (2 == $Nes::Minimal::_mod_perl) { | ||
| 400 | $r = Apache2::RequestUtil->request; | ||
| 401 | } | ||
| 402 | |||
| 403 | my $read_length = $vars->{'max_buffer'}; | ||
| 404 | my $clen = $ENV{'CONTENT_LENGTH'}; | ||
| 405 | if ((2 == $Nes::Minimal::_mod_perl) and (not defined $clen)) { | ||
| 406 | $clen = $r->headers_in->get('Content-Length'); | ||
| 407 | } | ||
| 408 | if ($clen < $read_length) { | ||
| 409 | $read_length = $clen; | ||
| 410 | } | ||
| 411 | |||
| 412 | my $content_type = defined($ENV{'CONTENT_TYPE'}) ? $ENV{'CONTENT_TYPE'} : ''; | ||
| 413 | if ((!$content_type) and (2 == $Nes::Minimal::_mod_perl)) { | ||
| 414 | $content_type = $r->headers_in->get('Content-Type'); | ||
| 415 | } | ||
| 416 | |||
| 417 | my $bdry = $1 if $content_type =~ m/^multipart\/form-data; boundary=(.*)$/i; | ||
| 418 | |||
| 419 | my $buffer = ''; | ||
| 420 | my $read_bytes = 0; | ||
| 421 | |||
| 422 | if ( ($bdry && $Nes::Minimal::_sub_filter) || | ||
| 423 | ($bdry && $ENV{'CONTENT_LENGTH'} > $Nes::Minimal::_use_tmp && $Nes::Minimal::_use_tmp) ) { | ||
| 424 | |||
| 425 | if ($ENV{'CONTENT_LENGTH'} > $Nes::Minimal::_max_upload) { | ||
| 426 | $Nes::Minimal::_ERROR_max_upload = 1; | ||
| 427 | require Carp; | ||
| 428 | Carp::carp($pkg . " The POST is greater than max_upload: $ENV{'CONTENT_LENGTH'} > $Nes::Minimal::_max_upload"); | ||
| 429 | return; | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | if (2 == $Nes::Minimal::_mod_perl) { | ||
| 434 | $read_bytes = $self->_read_post_bdry(\$buffer,$bdry,$r); | ||
| 435 | } else { | ||
| 436 | $read_bytes = $self->_read_post_bdry(\$buffer,$bdry); | ||
| 437 | } | ||
| 438 | |||
| 439 | } else { | ||
| 440 | |||
| 441 | if ($ENV{'CONTENT_LENGTH'} > $vars->{'max_buffer'}) { | ||
| 442 | $Nes::Minimal::_ERROR_max_post = 1; | ||
| 443 | require Carp; | ||
| 444 | Carp::carp($pkg . " The POST is greater than max_post: $ENV{'CONTENT_LENGTH'} > $vars->{'max_buffer'}"); | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | |||
| 448 | if ($read_length) { | ||
| 449 | if (2 == $Nes::Minimal::_mod_perl) { | ||
| 450 | $read_bytes = $r->read($buffer,$read_length,0); | ||
| 451 | } else { | ||
| 452 | $read_bytes = read(STDIN, $buffer, $read_length,0); | ||
| 453 | } | ||
| 454 | $self->filter_data(\$buffer) if $Nes::Minimal::_sub_filter; | ||
| 455 | } | ||
| 456 | |||
| 457 | } | ||
| 458 | $Nes::Minimal::_BUFFER = \$buffer; | ||
| 459 | $vars->{'form_truncated'} = ($read_bytes < $clen) ? 1 : 0; | ||
| 460 | |||
| 461 | if ( !defined $Nes::Minimal::_BUFFER_saved ) { | ||
| 462 | require IO::String; | ||
| 463 | $Nes::Minimal::_save_BUFFER_String = IO::String->new($Nes::Minimal::_BUFFER); | ||
| 464 | } | ||
| 465 | |||
| 466 | # Boundaries are supposed to consist of only the following | ||
| 467 | # (1-70 of them, not ending in ' ') A-Za-z0-9 '()+,_-./:=? | ||
| 468 | |||
| 469 | if ( $content_type =~ m/^multipart\/form-data; boundary=(.*)$/i ) { | ||
| 470 | my $bdry = $1; | ||
| 471 | require Nes::Minimal::Multipart; | ||
| 472 | $self->_burst_multipart_buffer ($buffer,$bdry); | ||
| 473 | |||
| 474 | } else { | ||
| 475 | $self->_burst_URL_encoded_buffer($buffer,'[;&]'); | ||
| 476 | } | ||
| 477 | |||
| 478 | } | ||
| 479 | |||
| 480 | sub _read { | ||
| 481 | my $self = shift; | ||
| 482 | my ( $buffer, $read_length, $r ) = @_; | ||
| 483 | |||
| 484 | if ($r) { | ||
| 485 | return $r->read($$buffer, $read_length); | ||
| 486 | } else { | ||
| 487 | return read(STDIN, $$buffer, $read_length); | ||
| 488 | } | ||
| 489 | |||
| 490 | } | ||
| 491 | |||
| 492 | #### | ||
| 493 | # Use tmp file from big POST | ||
| 494 | |||
| 495 | sub _read_post_bdry { | ||
| 496 | my $self = shift; | ||
| 497 | my ( $buffer, $bdry, $r ) = @_; | ||
| 498 | my $pkg = __PACKAGE__; | ||
| 499 | my $vars = $self->{$pkg}; | ||
| 500 | |||
| 501 | my $use_tmp = $ENV{'CONTENT_LENGTH'} > $Nes::Minimal::_use_tmp && $Nes::Minimal::_use_tmp; | ||
| 502 | my $tmp_fh; | ||
| 503 | my $size_buffer = 8192; | ||
| 504 | my $rbuffer; | ||
| 505 | my $bbuffer; | ||
| 506 | my $buffer_fh; | ||
| 507 | my $data; | ||
| 508 | my $read_bytes = 0; | ||
| 509 | my $field_name = ''; | ||
| 510 | my $file_name = ''; | ||
| 511 | my $content_type = ''; | ||
| 512 | |||
| 513 | if ( $Nes::Minimal::_save_BUFFER ) { | ||
| 514 | require IO::File; | ||
| 515 | $buffer_fh = IO::File->new_tmpfile; | ||
| 516 | $Nes::Minimal::_BUFFER_saved = $buffer_fh; | ||
| 517 | } | ||
| 518 | |||
| 519 | while ( (my $readb = $self->_read(\$rbuffer, $size_buffer, $r)) || $data ) { | ||
| 520 | $read_bytes += $readb; | ||
| 521 | |||
| 522 | if ($read_bytes > $Nes::Minimal::_max_upload) { | ||
| 523 | $Nes::Minimal::_ERROR_max_upload = 1; | ||
| 524 | require Carp; | ||
| 525 | Carp::carp($pkg . " - The POST is greater than max_upload: $ENV{'CONTENT_LENGTH'} > $Nes::Minimal::_max_upload"); | ||
| 526 | last; | ||
| 527 | } | ||
| 528 | |||
| 529 | $data .= $rbuffer; | ||
| 530 | if ( $data =~ /^(--\Q$bdry\E\015\012Content-Disposition:[^\015\012]* name\=\"([^\"]*)\"(?:[^\015\012]* filename\=\"([^\"]*)"[^\015\012]*|[^\015\012]*)\015\012([^\015\012]*)\015\012)/si ) { | ||
| 531 | $field_name = $2; | ||
| 532 | $file_name = $3; | ||
| 533 | $content_type = $4; | ||
| 534 | $$buffer .= $1; | ||
| 535 | $data = $'; | ||
| 536 | print $buffer_fh $1 if $buffer_fh; | ||
| 537 | if ( $file_name && $use_tmp ) { | ||
| 538 | $data =~ s/([^\015\012]*\015\012)//si; | ||
| 539 | $$buffer .= $1; | ||
| 540 | print $buffer_fh $1 if $buffer_fh; | ||
| 541 | require IO::File; | ||
| 542 | $tmp_fh = IO::File->new_tmpfile; | ||
| 543 | binmode $tmp_fh; | ||
| 544 | $vars->{'from_file'}->{$field_name} = $tmp_fh; | ||
| 545 | } | ||
| 546 | next; | ||
| 547 | } elsif ( $data =~ /^(\015\012--\Q$bdry\E--.*)/si ) { | ||
| 548 | $$buffer .= $1; | ||
| 549 | print $buffer_fh $1 if $buffer_fh; | ||
| 550 | $data = ''; | ||
| 551 | last; | ||
| 552 | } | ||
| 553 | |||
| 554 | if ( $data =~ m/(\015\012)(--\Q$bdry\E\015\012)/ ) { | ||
| 555 | $data = $2.$'; | ||
| 556 | my $sdata = $`; | ||
| 557 | $self->filter_data(\$sdata) if $content_type =~ m/Content-Type: text/i || !$content_type; | ||
| 558 | if ( $file_name && $use_tmp ) { | ||
| 559 | $$buffer .= "$file_name\015\012"; | ||
| 560 | print $tmp_fh $sdata; | ||
| 561 | print $buffer_fh $sdata."\015\012" if $buffer_fh; | ||
| 562 | $file_name = ''; | ||
| 563 | $content_type = ''; | ||
| 564 | seek($tmp_fh, 0, 0); | ||
| 565 | } else { | ||
| 566 | $$buffer .= $sdata."\015\012"; | ||
| 567 | print $buffer_fh $sdata."\015\012" if $buffer_fh; | ||
| 568 | } | ||
| 569 | } else { | ||
| 570 | $self->filter_data(\$data) if $content_type =~ m/Content-Type: text/i || !$content_type; | ||
| 571 | $$buffer .= $data if !$file_name || !$use_tmp; | ||
| 572 | print $tmp_fh $data if $file_name && $use_tmp; | ||
| 573 | print $buffer_fh $data if $buffer_fh; | ||
| 574 | $data = ''; | ||
| 575 | } | ||
| 576 | |||
| 577 | } | ||
| 578 | |||
| 579 | seek($buffer_fh, 0, 0) if $buffer_fh; | ||
| 580 | return $read_bytes; | ||
| 581 | |||
| 582 | } | ||
| 583 | |||
| 584 | sub filter_data { | ||
| 585 | my $self = shift; | ||
| 586 | my ($data) = @_; | ||
| 587 | |||
| 588 | return if !$Nes::Minimal::_sub_filter; | ||
| 589 | |||
| 590 | $Nes::Minimal::_sub_filter->($data); | ||
| 591 | |||
| 592 | return; | ||
| 593 | } | ||
| 594 | |||
| 595 | #### | ||
| 596 | # GET and HEAD | ||
| 597 | |||
| 598 | sub _read_get { | ||
| 599 | my $self = shift; | ||
| 600 | |||
| 601 | my $buffer = ''; | ||
| 602 | my $req_method = $ENV{'REQUEST_METHOD'}; | ||
| 603 | if (1 == $Nes::Minimal::_mod_perl) { | ||
| 604 | $buffer = Apache->request->args; | ||
| 605 | } elsif (2 == $Nes::Minimal::_mod_perl) { | ||
| 606 | my $r = Apache2::RequestUtil->request; | ||
| 607 | $buffer = $r->args; | ||
| 608 | $r->discard_request_body(); | ||
| 609 | unless (exists($ENV{'REQUEST_METHOD'}) || $Nes::Minimal::_no_subprocess_env) { | ||
| 610 | $r->subprocess_env; | ||
| 611 | } | ||
| 612 | $req_method = $r->method unless ($req_method); | ||
| 613 | } else { | ||
| 614 | $buffer = $ENV{'QUERY_STRING'} if (defined $ENV{'QUERY_STRING'}); | ||
| 615 | } | ||
| 616 | if ($req_method ne 'POST') { | ||
| 617 | $Nes::Minimal::_BUFFER = \$buffer; | ||
| 618 | } | ||
| 619 | $self->_burst_URL_encoded_buffer($buffer,'[;&]'); | ||
| 620 | } | ||
| 621 | |||
| 622 | #### | ||
| 623 | # Bursts URL encoded buffers | ||
| 624 | # $buffer - data to be burst | ||
| 625 | # $spliton - split pattern | ||
| 626 | |||
| 627 | sub _burst_URL_encoded_buffer { | ||
| 628 | my $self = shift; | ||
| 629 | my $pkg = __PACKAGE__; | ||
| 630 | my $vars = $self->{$pkg}; | ||
| 631 | |||
| 632 | my ($buffer,$spliton)=@_; | ||
| 633 | |||
| 634 | my ($mime_type) = "text/plain"; | ||
| 635 | my ($filename) = ""; | ||
| 636 | |||
| 637 | my @pairs = $buffer ? split(/$spliton/, $buffer) : (); | ||
| 638 | |||
| 639 | foreach my $pair (@pairs) { | ||
| 640 | my ($name, $data) = split(/=/,$pair,2); | ||
| 641 | |||
| 642 | $name = '' unless (defined $name); | ||
| 643 | $name =~ s/\+/ /gs; | ||
| 644 | $name =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ | ||
| 645 | defined($1)? chr hex($1) : _utf8_chr(hex($2))/ge; | ||
| 646 | $data = '' unless (defined $data); | ||
| 647 | $data =~ s/\+/ /gs; | ||
| 648 | $data =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ | ||
| 649 | defined($1)? chr hex($1) : _utf8_chr(hex($2))/ge; | ||
| 650 | |||
| 651 | if (! defined ($vars->{'field'}->{$name}->{'count'})) { | ||
| 652 | push (@{$vars->{'field_names'}},$name); | ||
| 653 | $vars->{'field'}->{$name}->{'count'} = 0; | ||
| 654 | } | ||
| 655 | my $record = $vars->{'field'}->{$name}; | ||
| 656 | my $f_count = $record->{'count'}; | ||
| 657 | $record->{'count'}++; | ||
| 658 | $record->{'value'}->[$f_count] = $data; | ||
| 659 | $record->{'filename'}->[$f_count] = $filename; | ||
| 660 | $record->{'mime_type'}->[$f_count] = $mime_type; | ||
| 661 | } | ||
| 662 | } | ||
| 663 | |||
| 664 | #### | ||
| 665 | # | ||
| 666 | # _utf8_chr() taken from Nes::Util | ||
| 667 | # Copyright 1995-1998, Lincoln D. Stein. All rights reserved. | ||
| 668 | sub _utf8_chr { | ||
| 669 | my $c = shift(@_); | ||
| 670 | return chr($c) if $] >= 5.006; | ||
| 671 | |||
| 672 | if ($c < 0x80) { | ||
| 673 | return sprintf("%c", $c); | ||
| 674 | } elsif ($c < 0x800) { | ||
| 675 | return sprintf("%c%c", 0xc0 | ($c >> 6), 0x80 | ($c & 0x3f)); | ||
| 676 | } elsif ($c < 0x10000) { | ||
| 677 | return sprintf("%c%c%c", | ||
| 678 | 0xe0 | ($c >> 12), | ||
| 679 | 0x80 | (($c >> 6) & 0x3f), | ||
| 680 | 0x80 | ( $c & 0x3f)); | ||
| 681 | } elsif ($c < 0x200000) { | ||
| 682 | return sprintf("%c%c%c%c", | ||
| 683 | 0xf0 | ($c >> 18), | ||
| 684 | 0x80 | (($c >> 12) & 0x3f), | ||
| 685 | 0x80 | (($c >> 6) & 0x3f), | ||
| 686 | 0x80 | ( $c & 0x3f)); | ||
| 687 | } elsif ($c < 0x4000000) { | ||
| 688 | return sprintf("%c%c%c%c%c", | ||
| 689 | 0xf8 | ($c >> 24), | ||
| 690 | 0x80 | (($c >> 18) & 0x3f), | ||
| 691 | 0x80 | (($c >> 12) & 0x3f), | ||
| 692 | 0x80 | (($c >> 6) & 0x3f), | ||
| 693 | 0x80 | ( $c & 0x3f)); | ||
| 694 | |||
| 695 | } elsif ($c < 0x80000000) { | ||
| 696 | return sprintf("%c%c%c%c%c%c", | ||
| 697 | 0xfc | ($c >> 30), | ||
| 698 | 0x80 | (($c >> 24) & 0x3f), | ||
| 699 | 0x80 | (($c >> 18) & 0x3f), | ||
| 700 | 0x80 | (($c >> 12) & 0x3f), | ||
| 701 | 0x80 | (($c >> 6) & 0x3f), | ||
| 702 | 0x80 | ( $c & 0x3f)); | ||
| 703 | } else { | ||
| 704 | return _utf8_chr(0xfffd); | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | #### | ||
| 709 | |||
| 710 | sub htmlize { | ||
| 711 | my $self = shift; | ||
| 712 | |||
| 713 | my ($s)=@_; | ||
| 714 | return ('') if (! defined($s)); | ||
| 715 | $s =~ s/\&/\&/gs; | ||
| 716 | $s =~ s/>/\>/gs; | ||
| 717 | $s =~ s/</\</gs; | ||
| 718 | $s =~ s/"/\"/gs; | ||
| 719 | $s; | ||
| 720 | } | ||
| 721 | |||
| 722 | #### | ||
| 723 | |||
| 724 | sub url_encode { | ||
| 725 | my $self = shift; | ||
| 726 | my ($s)=@_; | ||
| 727 | return '' if (! defined ($s)); | ||
| 728 | $s= pack("C*", unpack("C*", $s)); | ||
| 729 | $s=~s/([^-_.a-zA-Z0-9])/sprintf("%%%02x",ord($1))/eg; | ||
| 730 | $s; | ||
| 731 | } | ||
| 732 | |||
| 733 | #### | ||
| 734 | |||
| 735 | sub param_mime { require Nes::Minimal::Multipart; &_internal_param_mime(@_); } | ||
| 736 | sub param_filename { require Nes::Minimal::Multipart; &_internal_param_filename(@_); } | ||
| 737 | sub date_rfc1123 { require Nes::Minimal::Misc; &_internal_date_rfc1123(@_); } | ||
| 738 | sub dehtmlize { require Nes::Minimal::Misc; &_internal_dehtmlize(@_); } | ||
| 739 | sub url_decode { require Nes::Minimal::Misc; &_internal_url_decode(@_); } | ||
| 740 | sub calling_parms_table { require Nes::Minimal::Misc; &_internal_calling_parms_table(@_); } | ||
| 741 | |||
| 742 | #### | ||
| 743 | |||
| 744 | 1; | ||
| 745 |