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

Hi Monks,
I get stuck in figuring out, how I can limit the file upload size within Mason.

I've got the following Mason code :

<%perl> my $apr = Apache::Request->instance($r,POST_MAX => 1); my $status = $apr->parse; if ($status) { print "Too big"; }else{ print "OK" </%perl>
Like in the documentation of Apache::Request I create a new instance of the magic $r object, and limit this size to 1 byte for testing.

When I call this mason dokument from within a form and upload a file bigger then 1byte I always get a OK back!?

Sure I can get the filesize of $apr with the methodes $apr->tempname and the stat this.

But as I understand the documentation, there must be a possibility to do this with the parse function.

Can anyone bring some light in my $r->darkness :-)


--the good, the bad and the physi--

Replies are listed 'Best First'.
Re: Mason upload limit size
by Fletch (Bishop) on Feb 13, 2004 at 03:24 UTC

    Glancing through the XS source, it returns HTTP_REQUEST_ENTITY_TOO_LARGE which has a value of 413 (and thus is true for perl). What you want to do is make sure it's returning OK instead.

    <%perl> my $apr = Apache::Request->instance( $r, POST_MAX => 1 ); my $status = $apr->parse(); if( $status != Apache::Constants::OK ) { print "Too big"; } else { print "Just right."; } </%perl>

    Update: As a somewhat related aside, if you're using HTML::Mason::ApacheHandler the $r global should already be upgraded to an Apache::Request instance for you by default (or you can explicitly request this by passing args_method => "mod_perl", but that's the default setting; the other option is to set it to CGI to get a instance).

    Another Update: Looking into what HTML::Mason::ApacheHandler does, it appears it already calls Apache::Request->instance() with no extra arguments. This might explain your problem (i.e. the instance is created and possibly already parsed before your size limitation is seen). If that's the case you might need to subclass HTML::Mason::ApacheHandler to override the prepare_request() method yourself.

    package My::ApacheHandler; use base 'HTML::Mason::ApacheHandler'; sub prepare_request { my $self = shift; my $r = shift; Apache::Request->instance( $r, POST_MAX => 1 ); return $self->SUPER::prepare_request( $r, @_ ); } 1;
      Thanks Fletch for your explanation.
      Yes I use mod_perl and so I should might be use the last way you mentioned. But that will mean, I can't limit different upload forms to different upload size. Or I have to put each upload form into an own directory and use different My::ApacheHandler for each of them.
      Sounds to be to complicated :-)

      Maybe I use CGI for the upload stuff. This will also save me for memory problems, which mod_perl maybe will cause by uploading big files ...


      --the good, the bad and the physi--

        No, you can vary the limit but you just have to make your My::ApacheHandler a little bit fancier.

        package My::ApacheHandler; use base 'HTML::Mason::ApacheHandler'; sub prepare_request { my $self = shift; my $r = shift; my @args; ## <Location /foo> ## PerlSetVar MAH_POST_MAX 1024 ## </Location> ## <Location /bar> ## PerlSetVar MAH_POST_MAX none ## </Location> my $post_max = $r->dir_config( 'MAH_POST_MAX' ); unless( $post_max =~ /^none/ ) { push @args, POST_MAX => $post_max; } Apache::Request->instance( $r, @args ); return $self->SUPER::prepare_request( $r, @_ ); } 1;