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

Hi,

Is there a way to determine the size of a parameter of a textarea or filefield in CGI.pm? For example, given this form.
use CGI qw/:standard/; # snip textarea( -name => 'some_name', -rows => 10, -columns => 50, -wrap => 'physical' ); #or filefield(-name=>'upload_file', -size=>60)
We would like to know the size of:
param('some_name'); # of text area and param('upload_file') # of file field
Besides, cgi-lib.pl has a method called "maxdata" as a way to limit the size of the uploaded/inserted file.
I was wondering if CGI.pm also has this functionality.

Regards,
Edward

Replies are listed 'Best First'.
Re: To know parameter size in CGI.pm
by derby (Abbot) on May 20, 2006 at 14:17 UTC

    monkfan, the size limits on HTML text fields are just suggestions to the browser ... the limits can be easily avoided (manual submission via LWP). If you control the form rendering, you control the processing so you can easily truncate the data for those that bypass your forms.

    As for maxdata, CGI's POST_MAX is equivalant (but there are server settings which can make this figure not as large as you want).

    -derby
      ... the limits can be easily avoided
      Please correct me if I misunderstood your answer. The reason I want to know the size is to abort the web submission process when the input/upload is too large.

      So my worry is not that the input is too small, on the contrary I want to avoid data input of certain size, be it file_upload or text_field input.

      Regards,
      Edward

        The reason I want to know the size is to abort the web submission process when the input/upload is too large

        Right ... the client does not send that data back to you on a per-field basis. You can abort if all the data is too large but per field, you need to actually read the param before you can determine if it's too big for your needs.

        To limit for all POST data, follow the example from CGI:

        use CGI qw/:standard/; use CGI::Carp ’fatalsToBrowser’; $CGI::POST_MAX=1024 * 100; # max 100K posts
        An attempt to send a POST larger than $POST_MAX bytes will cause param() to return an empty CGI parameter list. You can test for this event by checking cgi_error(), either after you create the CGI object or, if you are using the function-oriented interface, call <param()> for the first time. If the POST was intercepted, then cgi_error() will return the message "413 POST too large".

        -derby
Re: To know parameter size in CGI.pm
by CountZero (Bishop) on May 20, 2006 at 14:50 UTC
    As far as I know none of the popular webservers have yet implemented a "crystal ball" module which can predict the size of a param-field before it is uploaded.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: To know parameter size in CGI.pm
by ww (Archbishop) on May 20, 2006 at 14:31 UTC
    Too long for here, but (online) see Ex3-2, p48 et seq, in the Mouse book, CGI Programming, O'Reilly.