[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    [Fwd: reading multipart forms]
From:       Gerd Kortemeyer <korte () lite ! msu ! edu>
Date:       1999-05-31 21:37:57
[Download RAW message or body]

[Attachment #3 (message/rfc822)]

This is a multi-part message in MIME format.

Here's something from an old CGI script that I wrote to process file uploads
via "multipart":

-----

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# See what's in there: multipart or normal data:
if ($buffer =~ 'Content-Disposition:\s*form-data') {
    @entries = split (/Content-Disposition:\s*form-data;\s*name="/,$buffer);
    $i=1;
    while ($entries[$i] ne '') {
       @lines=split(/\n/,$entries[$i]);
       $lines[0] =~ s/".*//;
       $name=$lines[0];
       $j=2;
       @contentlines=();
       until ($lines[$j] =~ '---------------------') {
	   $contentlines[$j-2]=$lines[$j];
           $j++;
       }
       $value=join("\n",@contentlines);
       chop($value);
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

       # Stop people from using subshells to execute commands
       $value =~ s/~!/ ~!/g; 
  
       # Uncomment for debugging purposes
       # print "Setting $name to $value<P>";

       $FORM{$name} = $value;
       $i++;
    }  
}

-----

Maybe you can modify that for mod_perl - it's a bit dirty, but worked great.

Independently of "multipart", I had problems reading POST data into mod_perl
modules, the $r->content would somehow not do the trick (could be me, though).
I ended up writing this:
-----
    my $buffer;
    $r->read($buffer,$r->header_in('Content-length'));
    my @pairs=split(/&/,$buffer);
    my $pair; my $name; my $value; my %FORM;
    foreach $pair (@pairs) {
       ($name,$value) = split(/=/,$pair);
       $FORM{$name}=$value;
    } 
-----

Combining that with the first piece of code might do the trick.

- Gerd.

Jesse Erlbaum wrote:
> 
> Maybe a silly question --
> 
> I'm trying to read the contents of a POST which contains a CGI-uploaded part,
> via multipart/form-data.  For standard POST requests, I am used to the
> content() method, as documented in the Apache POD:
> 
>        $r->content
>            The $r->content method will return the entity body
>            read from the client, but only if the request content
>            type is application/x-www-form-urlencoded.  When
>            called in a scalar context, the entire string is
>            returned.  When called in a list context, a list of
>            parsed key => value pairs are returned.  *NOTE*: you
>            can only ask for this once, as the entire body is read
>            from the client.
> 
> The POD seems to suggest that only the old-style "x-www-form-urlencoded" data,
> and not "multipart" are supported.  How can I deal with multipart forms via
> mod_perl?
> 
> Thanx!
> 
> -Jesse-
> 
> --
> 
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>   Jesse Erlbaum ........... mailto:jesse@vm.com
>   Lead Programmer/IT Director .. Vanguard Media
>   data: 212.242.7685...voice: 212.242.5317 x115
> +-+-+-+-+-+-+- http://www.vm.com/ +-+-+-+-+-+-+-+
["korte.vcf" (text/x-vcard)]
["korte.vcf" (text/x-vcard)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic