| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::ParseFormData | ||
| 4 | # Last Modification: Thu Oct 23 11:44:58 WEST 2003 | ||
| 5 | # | ||
| 6 | # Copyright (c) 2003 Henrique Dias <hdias@aesbuc.pt>. All rights reserved. | ||
| 7 | # This module is free software; you can redistribute it and/or modify | ||
| 8 | # it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | ############################################################################## | ||
| 11 | package Apache::ParseFormData; | ||
| 12 | |||
| 13 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Apache::Log; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Apache::Const -compile => qw(OK M_POST M_GET FORBIDDEN HTTP_REQUEST_ENTITY_TOO_LARGE); | ||
| 16 | use Apache::RequestIO (); | ||
| 17 | use APR::Table; | ||
| 18 | use IO::File; | ||
| 19 | use POSIX qw(tmpnam); | ||
| 20 | require Exporter; | ||
| 21 | our @ISA = qw(Exporter Apache::RequestRec); | ||
| 22 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 23 | our @EXPORT = qw(); | ||
| 24 | our $VERSION = '0.09'; | ||
| 25 | require 5; | ||
| 26 | |||
| 27 | use constant NELTS => 10; | ||
| 28 | use constant BUFFLENGTH => 1024; | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $proto = shift; | ||
| 32 | my $class = ref($proto) || $proto; | ||
| 33 | my $self = shift; | ||
| 34 | my %args = ( | ||
| 35 | temp_dir => "/tmp", | ||
| 36 | disable_uploads => 0, | ||
| 37 | post_max => 0, | ||
| 38 | @_, | ||
| 39 | ); | ||
| 40 | my $table = APR::Table::make($self->pool, NELTS); | ||
| 41 | $self->pnotes('apr_req' => $table); | ||
| 42 | bless ($self, $class); | ||
| 43 | |||
| 44 | if(my $data = $self->headers_in->get('cookie')) { | ||
| 45 | &_parse_query($self, $data, " *; *"); | ||
| 46 | } | ||
| 47 | if($self->method_number == Apache::M_POST) { | ||
| 48 | $self->pnotes('apr_req_result' => &parse_content($self, \%args)); | ||
| 49 | } elsif($self->method_number == Apache::M_GET) { | ||
| 50 | my $data = $self->args(); | ||
| 51 | &_parse_query($self, $data) if($data); | ||
| 52 | $self->pnotes('apr_req_result' => Apache::OK); | ||
| 53 | } | ||
| 54 | return($self); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub DESTROY { | ||
| 58 | my $self = shift; | ||
| 59 | for my $v (values(%{$self->pnotes('upload')})) { | ||
| 60 | my $path = $v->[1]; | ||
| 61 | unlink($path) if(-e $path); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub parse_result { $_[0]->pnotes('apr_req_result') } | ||
| 66 | |||
| 67 | sub parms { $_[0]->pnotes('apr_req') } | ||
| 68 | |||
| 69 | sub _parse_query { | ||
| 70 | my $r = shift; | ||
| 71 | my $query_string = shift; | ||
| 72 | my $re = shift || "&"; | ||
| 73 | |||
| 74 | my %hash = (); | ||
| 75 | for(split(/$re/, $query_string)) { | ||
| 76 | my ($n, $v) = split(/=/); | ||
| 77 | defined($v) or $v = ""; | ||
| 78 | &decode_chars($n); | ||
| 79 | &decode_chars($v); | ||
| 80 | push(@{$hash{$n}}, $v); | ||
| 81 | } | ||
| 82 | $r->param(%hash); | ||
| 83 | return(); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub decode_chars { | ||
| 87 | $_[0] =~ tr/+/ /; | ||
| 88 | $_[0] =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/egi; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub set_cookie { | ||
| 92 | my $self = shift; | ||
| 93 | my $args = { | ||
| 94 | name => "", | ||
| 95 | value => "", | ||
| 96 | path => "/", | ||
| 97 | expires => "", | ||
| 98 | secure => 0, | ||
| 99 | domain => "", | ||
| 100 | @_, | ||
| 101 | }; | ||
| 102 | $args->{'name'} or return(); | ||
| 103 | my @a = ( | ||
| 104 | join("=", $args->{'name'}, $args->{'value'}), | ||
| 105 | join("=", "path", $args->{'path'}), | ||
| 106 | ); | ||
| 107 | push(@a, join("=", "expires", &cookie_expire($args->{'expires'}))) if($args->{'expires'}); | ||
| 108 | push(@a, join("=", "secure", $args->{'secure'})) if($args->{'secure'}); | ||
| 109 | push(@a, join("=", "domain", $args->{'domain'})) if($args->{'domain'}); | ||
| 110 | $self->headers_out->{'Set-Cookie'} = join(";", @a); | ||
| 111 | $self->param($args->{'name'} => $args->{'value'}); | ||
| 112 | return(); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub cookie_expire { | ||
| 116 | my $time = shift; | ||
| 117 | my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime($time); | ||
| 118 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
| 119 | my @weekday = qw(Sun Mon Tue Wed Thu Fri Sat); | ||
| 120 | return sprintf("%3s, %02d-%3s-%04d %02d:%02d:%02d GMT", $weekday[$wday], $mday, $months[$mon], $year+1900, $hour, $min, $sec); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub upload { | ||
| 124 | my $self = shift; | ||
| 125 | my $name = shift || ""; | ||
| 126 | return($name ? @{$self->pnotes('upload')->{$name}} : keys(%{$self->pnotes('upload')})); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub parse_content { | ||
| 130 | my $r = shift; | ||
| 131 | my $args = shift; | ||
| 132 | |||
| 133 | my $buf = ""; | ||
| 134 | $r->setup_client_block; | ||
| 135 | $r->should_client_block or return ''; | ||
| 136 | my $ct = $r->headers_in->get('content-type'); | ||
| 137 | |||
| 138 | if($args->{'disable_uploads'} && index($ct, "multipart/form-data") > -1) { | ||
| 139 | my $error_str = "[Apache::ParseFormData] file upload forbidden"; | ||
| 140 | $r->notes->set("error-notes" => $error_str); | ||
| 141 | $r->log_error($error_str); | ||
| 142 | return(Apache::FORBIDDEN); | ||
| 143 | } | ||
| 144 | my $rm = $r->remaining; | ||
| 145 | if($args->{'post_max'} && ($rm > $args->{'post_max'})) { | ||
| 146 | my $pm = $args->{'post_max'}; | ||
| 147 | my $error_str = "[Apache::ParseFormData] entity too large ($rm, max=$pm)"; | ||
| 148 | $r->notes->set("error-notes" => $error_str); | ||
| 149 | $r->log_error($error_str); | ||
| 150 | return(Apache::HTTP_REQUEST_ENTITY_TOO_LARGE); | ||
| 151 | } | ||
| 152 | if($ct =~ /^multipart\/form-data; boundary=(.+)$/) { | ||
| 153 | my $boundary = $1; | ||
| 154 | my $lenbdr = length("--$boundary"); | ||
| 155 | $r->get_client_block($buf, $lenbdr+2); | ||
| 156 | $buf = substr($buf, $lenbdr); | ||
| 157 | $buf =~ s/[\n\r]+//; | ||
| 158 | my $iter = -1; | ||
| 159 | my @data = (); | ||
| 160 | &multipart_data($r, $args, \@data, $boundary, BUFFLENGTH, 1, $buf, $iter); | ||
| 161 | my %uploads = (); | ||
| 162 | for(@data) { | ||
| 163 | if(exists($_->{'headers'}->{'content-disposition'})) { | ||
| 164 | my @a = split(/ *; */, $_->{'headers'}->{'content-disposition'}); | ||
| 165 | if(shift(@a) eq "form-data") { | ||
| 166 | if(scalar(@a) == 1) { | ||
| 167 | my ($key) = ($a[0] =~ /name=\"([^\"]+)\"/); | ||
| 168 | $r->param($key => $_->{'values'} || ""); | ||
| 169 | } else { | ||
| 170 | (ref($_->{'values'}) eq "ARRAY") or next; | ||
| 171 | my ($fh, $path) = @{$_->{'values'}}; | ||
| 172 | seek($fh, 0, 0); | ||
| 173 | my %hash = ( | ||
| 174 | filename => "", | ||
| 175 | type => exists($_->{'headers'}->{'content-type'}) ? $_->{'headers'}->{'content-type'} : "", | ||
| 176 | size => ($fh->stat())[7], | ||
| 177 | ); | ||
| 178 | my $param = ""; | ||
| 179 | for(@a) { | ||
| 180 | my ($name, $value) = (/([^=]+)=\"([^\"]+)\"/); | ||
| 181 | if($name eq "name") { | ||
| 182 | $uploads{$value} = [$fh, $path]; | ||
| 183 | $param = $value; | ||
| 184 | } else { | ||
| 185 | $hash{$name} = $value; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | $r->param($param => \%hash); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | $r->pnotes('upload' => \%uploads); | ||
| 194 | } else { | ||
| 195 | my $len = $r->headers_in->get('content-length'); | ||
| 196 | $r->get_client_block($buf, $len); | ||
| 197 | &_parse_query($r, $buf) if($buf); | ||
| 198 | } | ||
| 199 | return(Apache::OK); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub extract_headers { | ||
| 203 | my $raw = shift; | ||
| 204 | my %hash = (); | ||
| 205 | for(split(/\r?\n/, $raw)) { | ||
| 206 | s/[\r\n]+$//; | ||
| 207 | $_ or next; | ||
| 208 | my ($h, $v) = split(/ *: */, $_, 2); | ||
| 209 | $hash{lc($h)} = $v; | ||
| 210 | } | ||
| 211 | $_[0] = \%hash; | ||
| 212 | return(exists($hash{'content-type'})); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub output_data { | ||
| 216 | my $dest = shift; | ||
| 217 | my $data = shift; | ||
| 218 | |||
| 219 | if(ref($dest->{values}) eq "ARRAY") { | ||
| 220 | my $fh = $dest->{values}->[0]; | ||
| 221 | print $fh $data; | ||
| 222 | } else { $dest->{values} .= $data; } | ||
| 223 | } | ||
| 224 | |||
| 225 | sub new_tmp_file { | ||
| 226 | my $temp_dir = shift; | ||
| 227 | my $data = shift; | ||
| 228 | |||
| 229 | my $path = ""; | ||
| 230 | my $fh; | ||
| 231 | my $i = 0; | ||
| 232 | do { | ||
| 233 | $i < 3 or last; | ||
| 234 | my $name = tmpnam(); | ||
| 235 | $name = (split("/", $name))[-1]; | ||
| 236 | $path = join("/", $temp_dir, $name); | ||
| 237 | $i++; | ||
| 238 | } until($fh = IO::File->new($path, O_RDWR|O_CREAT|O_EXCL)); | ||
| 239 | defined($fh) or return("Couldn't create temporary file: $path"); | ||
| 240 | binmode($fh); | ||
| 241 | $fh->autoflush(1); | ||
| 242 | $data->{values} = [$fh, $path]; | ||
| 243 | return(); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub multipart_data { | ||
| 247 | my $r = shift; | ||
| 248 | my $args = shift; | ||
| 249 | my $data = shift; | ||
| 250 | my $boundary = shift; | ||
| 251 | my $len = shift; | ||
| 252 | my $h = shift; | ||
| 253 | my $buff = shift; | ||
| 254 | |||
| 255 | my ($part, $content) = ($buff, ""); | ||
| 256 | while($r->get_client_block($buff, $len)) { | ||
| 257 | $part .= $buff; | ||
| 258 | if($h) { | ||
| 259 | if($part =~ /\r?\n\r?\n/) { | ||
| 260 | my ($left, $right) = ($`, $'); | ||
| 261 | $left =~ s/[\r\n]+$//; | ||
| 262 | $_[0]++; | ||
| 263 | push(@{$data}, {values => "", headers => {}}); | ||
| 264 | if(&extract_headers($left, $data->[$_[0]]->{'headers'})) { | ||
| 265 | if(my $error = &new_tmp_file($args->{'temp_dir'}, $data->[$_[0]])) { $r->log->warn($error), next; } | ||
| 266 | } | ||
| 267 | $part = $content = $right; | ||
| 268 | $h = 0; | ||
| 269 | } else { next; } | ||
| 270 | } | ||
| 271 | if($part =~ /\r?\n--$boundary\r?\n/) { | ||
| 272 | my ($left, $right) = ($`, $'); | ||
| 273 | &output_data($data->[$_[0]], $left) if($left); | ||
| 274 | &multipart_data($r, $args, $data, $boundary, $len, 1, $right, $_[0]); | ||
| 275 | $part = ""; | ||
| 276 | } | ||
| 277 | if($part) { | ||
| 278 | $content = substr($part, 0, int($len/2)); | ||
| 279 | &output_data($data->[$_[0]], $content) if($content); | ||
| 280 | $part = substr($part, int($len/2)); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | if($h && $part =~ /\r?\n\r?\n/) { | ||
| 284 | my ($left, $right) = ($`, $'); | ||
| 285 | $left =~ s/[\r\n]+$//; | ||
| 286 | $_[0]++; | ||
| 287 | push(@{$data}, {values => "", headers => {}}); | ||
| 288 | if(&extract_headers($left, $data->[$_[0]]->{'headers'})) { | ||
| 289 | if(my $error = &new_tmp_file($args->{'temp_dir'}, $data->[$_[0]])) { $r->log->warn($error), next; } | ||
| 290 | } | ||
| 291 | $part = $right; | ||
| 292 | $h = 0; | ||
| 293 | } | ||
| 294 | if($part =~ /\r?\n--$boundary\r?\n/) { | ||
| 295 | my ($left, $right) = ($`, $'); | ||
| 296 | &output_data($data->[$_[0]], $left) if($left); | ||
| 297 | &multipart_data($r, $args, $data, $boundary, $len, 1, $right, $_[0]); | ||
| 298 | $part = ""; | ||
| 299 | } | ||
| 300 | if($part =~ /\r?\n--$boundary--[\r\n]*/) { | ||
| 301 | my $left = $`; | ||
| 302 | &output_data($data->[$_[0]], $left) if($left); | ||
| 303 | } | ||
| 304 | return(); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub delete { | ||
| 308 | my $self = shift; | ||
| 309 | map { $self->parms->unset($_); } @_; | ||
| 310 | return(); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub delete_all { | ||
| 314 | my $self = shift; | ||
| 315 | $self->parms->clear(); | ||
| 316 | return(); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub param { | ||
| 320 | my $self = shift; | ||
| 321 | |||
| 322 | if(scalar(@_) > 1) { | ||
| 323 | my %hash = @_; | ||
| 324 | while(my ($k, $v) = each(%hash)) { | ||
| 325 | my @transfer = (ref($v) eq "HASH") ? %{$v} : (ref($v) eq "ARRAY") ? @{$v} : ($v); | ||
| 326 | my $first = shift(@transfer) || ""; | ||
| 327 | $self->parms->set($k => $first); | ||
| 328 | map { $self->parms->add($k, $_); } @transfer; | ||
| 329 | } | ||
| 330 | return(); | ||
| 331 | } | ||
| 332 | if(scalar(@_) == 1) { | ||
| 333 | my $k = shift; | ||
| 334 | return($self->parms->get($k)); | ||
| 335 | } | ||
| 336 | return(keys(%{$self->parms})); | ||
| 337 | } | ||
| 338 | |||
| 339 | 1; | ||
| 340 | __END__ | ||