|
|
Question : robust upload file without CGI.pm
|
|
Hi, Im trying to make a perl-CGI upload script without the CGI.pm module. I know its easier but I want to understand several things. First my upload.html has several fields (and a file input box) and submits via ENCTYPE="multipart/form-data", so when my CGI grabs it all (from POST) via STDIN, I get some strings like:
-----------------------------7d61b51b10426 Content-Disposition: form-data; name="email_address"
[email protected] -----------------------------7d61b51b10426 Content-Disposition: form-data; name="Submit"
Submit Form -----------------------------7d61b51b10426--
I was wondering if anyone has a quick way to parse this? Maybe turn it into hashes or something? Also in this is the binary content of the file I uploaded, so Im also trying to not only get field values, but also upload a file (without the CGI module). Thanks.
|
Answer : robust upload file without CGI.pm
|
|
I'm going to wash my hands after posting this. This is a crude way of parsing the from data into a hash:
my %IN = read_parse();
sub read_parse {
my $buffer; if ($ENV{'REQUEST_METHOD'} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else {die "Invalid request method";}
my @pairs = split(/&/, $buffer); foreach (@pairs) { my($name, $value) = split(/=/, $_); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9]{2})/pack("c", hex($1))/eg; if (defined($IN{$name})){$IN{$name} .= "\0";} $IN{$name} .= $value; } return %IN; }
the above is not recommended but it might give you some insight.
|
|
|
|
|