Search code examples
perlblobcgi

Get blob uploaded data with pure Perl


In Javascript, I am sending a blob using XHR by the following code:

var v=new FormData();
v.append("EFD",new Blob([...Uint8Array...]));

var h=new XMLHttpRequest();
h.setRequestHeader("Content-type","multipart/form-data; charset=utf-8");
h.open("POST","...url...");
h.send(v);

In the server, I have created in Perl the following function, that suppose to implement CGI->param and CGI->upload:

# QS (Query String) receive in argument string for single parameter or array of many required parameters.
# If string been supplied: Return the value of the parameter or undef if missing.
# If array been supplied, a hash will be returned with keys for param names and their corresponding values.
# If the first argument is undef, then return hash with ALL available parameters.
sub QS {
    my $b=$ENV{'QUERY_STRING'};
    if($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN,$b,$ENV{'CONTENT_LENGTH'}) or die "E100";
    }
    my $e=$_[0]; my $t=&AT($e); my $r={}; my @q=split(/&/,$b);
    my %p=(); if($t eq "A") { %p=map { $_=>1 } @{$e}; }
    foreach my $i(@q) {
        my ($k,$s)=split(/=/,$i); $s=~tr/+//; $s=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        if($t eq "") { $r->{$k}=$s; }
        elsif($t eq "A") { if($p{$k}) { $r->{$k}=$s; } }
        elsif($k eq $_[0]) { return $s; }
    }
    return $r;
}

# AT is a function for determining type of an object, and also a quck way to distinguish between just a string and a number.
sub AT {
    if(!defined $_[0]) { return ""; } my $v=ref($_[0]);
    if($v eq "") { return ($_[0]*1 eq $_[0])?"N":"S"; }
    my $k={"ARRAY"=>"A","HASH"=>"H"};
    return $k->{$v}||$_[0]->{_obt}||$v;
}

So in the main program it will be called as:

my $EFD=&FW::QS("EFD"); # FW = The module name where QS and AT are.

When I issuing the POST from the client, the script in the server does not pop-up any errors, and does not terminates - it continues to run and run and run.... Endlessly.... Consuming 100% CPU time and 100% memory - without any explanation.

I have these in the beginning of the script, though:

use strict;
use warnings;
use diagnostics;

but it still behave in such a way that I need to kill the script in order to terminate it...

Anyone know what I did wrong...? No infinite loop here, as far as I know... If I change the Blob to regular classic way of "...url...?EFD=dhglkhserkhgoi" then it works just fine, but I want a Blob....

Thanks a lot


Solution

  • This QS function is only usable for POSTs with an application/x-www-urlencoded body, which yours isn't.