gorillaman has asked for the wisdom of the Perl Monks concerning the following question:

When a browser uploads a file to my server, via a cgi script, the script counts the bytes of the upload and stops accepting data when it reaches the specified maximum file size.

The script tells them, "File too big" and then closes the stdin.

The issue is that even if I exit the while(<STDIN>) loop and print a message to the screen, the user's browser still thinks the server is accepting the upload, and continues sending the entire file. The result is, the browser hangs as if it was sending this huge file.

Even printing a message doesn't seem to tell the browser to stop uploading.

Is there a reliable way to inform the browser to stop uploading? The server stopped accepeting data, so the browser needs to know! thanks...

Replies are listed 'Best First'.
Re: STDIN and Browsers
by ccn (Vicar) on Aug 18, 2004 at 21:15 UTC

    First of all a browser sends CONTENT_LENGTH header to a server, so you can do not read anything if it's value too big for you. Anyway if you deside to stop sending you can just close STDIN, or exit.

    You could use CGI and it will do it for you if you set up $CGI::POST_MAX variable

    An example:

    print <<"HTML" and exit if $ENV{CONTENT_LENGTH} > 10000; Content-type: text/plain Error: the file is too big HTML

    upd: misprint fixed

Re: STDIN and Browsers
by dave_the_m (Monsignor) on Aug 18, 2004 at 21:14 UTC
    A pure guess this, but how about doing close STDIN?

    Dave.

      Unfortunately, that isnt the issue, as stdin does get closed. The script clearly exits, but the browser won't display the data sent to it until it thinks it uploaded the entire file.
Re: STDIN and Browsers
by Joost (Canon) on Aug 19, 2004 at 09:54 UTC
    Personally, I'd just set $CGI::POST_MAX, to reject big requests. In fact, you could probably set $CGI::POST_MAX to something a bit higher than the maximum file size (to allow for some additional data, like filename and other user input) and then check the file size after the upload succeeds.

    From the docs:

    $CGI::POST_MAX If set to a non-negative integer, this variable puts a ceil +ing on the size of POSTings, in bytes. If CGI.pm detects a POST t +hat is greater than the ceiling, it will immediately exit with an +error message. This value will affect both ordinary POSTs and mu +ltipart POSTs, meaning that it limits the maximum size of file uplo +ads as well. You should set this to a reasonably high value, such + as 1 megabyte.

    Make sure you set it at the top of your script (before using any CGI.pm functions)

    Joost