?
Solved

robust upload file without CGI.pm

Posted on 2006-10-25
12
Medium Priority
?
898 Views
Last Modified: 2013-12-25
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"

mailadd@hotmail.com
-----------------------------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.
0
Comment
Question by:Traltixx
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 17808962
could you use CGI::Minimal;
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 17809209
Can you show the code you use that produced the output you posted?
0
 
LVL 2

Author Comment

by:Traltixx
ID: 17809224
well, I cant use the CGI module at all, so no, I cant use CGI::Minimal.
And code that produced that?
The perl snippet is:
print "\n\nStandard Input:\n";
if ($ENV{'REQUEST_METHOD'}  eq "POST") {
      read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
      print $in;
      }
and the HTML snippet is something like:

<FORM ACTION="upload.cgi" METHOD="post" ENCTYPE="multipart/form-data">
  File to Upload: <INPUT TYPE="file" NAME="filex">
     <br><br>
      Your Email Address: <INPUT TYPE="text" NAME="email_address">
       <br><br>
        <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form">
       </FORM>

0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
LVL 85

Expert Comment

by:ozo
ID: 17809244
the CGI::Minimal module is not the CGI module
0
 
LVL 2

Author Comment

by:Traltixx
ID: 17809843
it isnt? is there an example of how to use/upload files with this?
0
 
LVL 2

Author Comment

by:Traltixx
ID: 17809893
OK, i've had a look and it seems that CGI::minimal is a whole module alltogether. I suppose I should change my question to, not using CGI.pm or any downloaded modules (because i cant download them).
Is there another way of doing this?
Thanks alot again!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17811854
you have to parse the HTTP header *and* the POST data line by line yourself.
From the header you need to get the value of the Content-Type and extract the boundary string.
Then you need to parse the POST data and search the boundary  string followed by a
  Content-Disposition: form-data; name="whatever"
wher "whatever" is your variable name from the HTML form.
While parsing you need to count the bytes of the data you ignore, the remaining ones 'til the value of the Content-Length *or* #til next boundary string is your file's data.

Simple, isn't it?
You better go with a redy-to-use module. Or go and get one and steal the code from there, if you like the hard way ;-)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17814754
Why can't you download them?  Is this a homework/assignment question?
0
 
LVL 2

Author Comment

by:Traltixx
ID: 17816347
I cant download them because its a personal restraint (i.e. im trying to understand CGI more by relying less on modules and doing everything standalone).
That is why i was wondering if anyone knew a quick way of parsing the POST data bymyself since if I did it, i would have missed some things due to the nature of my test cases.
Anyway, it seems that there is a more minimal module than CGI::minimal, namely cgi-lib.pl ( http://cgi-lib.berkeley.edu/#intro )
thanks for the info anyway for cgi::minimal
0
 
LVL 48

Expert Comment

by:Tintin
ID: 17816828
cgi-lib is a not a particularly good example to study as it is so out of date and doesn't conform to all the latest standards.  It was good in its day when there wasn't any other choice.

If you really want to learn more about CGI, I'd suggest studying the CGI spec

http://www.w3.org/CGI/

and studying the code of CGI::Minimal or CGI (admittedly, it's not particularly easy code to read as it uses many tricks to be as efficient and broad based as possible)
0
 
LVL 8

Accepted Solution

by:
Perl_Diver earned 1050 total points
ID: 17817438
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.
 
0
 
LVL 2

Author Comment

by:Traltixx
ID: 17817451
yes, cgi-lib.pl is rather out of date, but it seems to be fine.
Also thanks for the snippet (although it doesnt work really well with uploading file but i found what i was looking for).
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question