Home > Software design >  Perl upload dies when the file is compressed
Perl upload dies when the file is compressed

Time:07-19

I have a form designed for uploading multiple text files and therefore I have set the mime type in the HTML form to

enctype="multipart/form-data"

I am using the following script

#!/usr/bin/env perl -wT

use strict;
use warnings;
use open qw(:std :encoding(UTF-8));
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = CGI->new();

However, there is a chance that people will upload a gzip file. I would like to protect against it, but when I upload a gzip file the script crashes as soon as the new CGI object is made with this error message in httpd error_log.

...
test.cgi: utf8 "\\xA3" does not map to Unicode at (eval 16) line 5.
test.cgi: utf8 "\\xB5" does not map to Unicode at (eval 16) line 5.
test.cgi: Malformed multipart POST

How am I meant to enable uploading multiple plain text files and not die when someone uploads a gzip file?

CodePudding user response:

:std affects STDIN, STDOUT and STDERR, and STDIN is used to receive information from the web server.

Either remove the encoding layer after adding it

use open qw( :std :encoding(UTF-8) );
BEGIN { binmode( STDIN ); }

or avoid adding it in the first place.

use open qw( :encoding(UTF-8) );
BEGIN {
   binmode( STDIN );
   binmode( STDOUT, ':encoding(UTF-8)' );
   binmode( STDERR, ':encoding(UTF-8)' );
}
  • Related