You are viewing a plain text version of this content. The canonical link for it is here.
Posted to apreq-dev@httpd.apache.org by "Michael Michalowski Web.de" <mi...@web.de> on 2007/08/10 17:50:12 UTC

Apache2::Request UPLOAD_HOOK and mod_perl Handler

hi,

 I've got a few questions about the UPLOAD_HOOK of Apache2::Request
 and the way implementing it. I'm not sure, whether this is the right
 mailinglist or not, so sorry if this is not the right one. ;-)

 I'm trying to write a Perl Class which provides a handler() function
 for mod_perl. something like that:

     package InterRed::UploadLimiter;

     use strict;
     use Digest::MD5 qw(md5_hex);
     use CGI::Cookie;
 
     sub handler
     {
         my $r = shift;
         my $transparent_hook = sub {
         my ($upload, $data, $data_len, $hook_data) = @_;
            warn "$hook_data: got $data_len bytes for " . $upload->name;
         };

         my $req = Apache2::Request->new($r,
            UPLOAD_HOOK => $transparent_hook,
         );
         $req->parse();
        
         return Apache2::Const::OK;
     }
     
     1;
     __END__

 My main goal is to register this handler in the apache and so to
 control user uploads. I want to check the uploaded filesize and
 abort the upload if it's to huge. Then I want to redirect to another
 script (with parameters) or print something out.

 Therefor I tried to register my handler like that in my apache config
 file:

        <IfModule mod_perl.c>
#               PerlFixupHandler        InterRed::UploadLimiter
#               PerlChildInitHandler    InterRed::UploadLimiter
                PerlHandler             InterRed::UploadLimiter
        </IfModule>

 As you can see, I tried different Handler types, because I'm not sure
 which one is right to handle uploads (the upload must not be started
 at this time). I want the handler to control any kind of perl cgi
 script, which sends uploads to the server.

 In case of the PerlHandler I got no error, but handler and hook are not
 fired anyway. Same using PerlChildInitHandler.
 In case of the PerlFixupHandler the client receives a 0 byte answer
 (even if their is no upload submitted). The callback $transparent_hook
 doesn't seem to be called. The handler is called in this case.

 So, what is the right Handler to register the upload hook?
 What am I doing wrong?
 Why does the client receive a 0 byte answer in case of using
 the PerlFixupHandler?
 
greetings,
Michael


Re: Apache2::Request UPLOAD_HOOK and mod_perl Handler

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Michael Michalowski Web.de wrote:
> hi,
>
>  I've got a few questions about the UPLOAD_HOOK of Apache2::Request
>  and the way implementing it. I'm not sure, whether this is the right
>  mailinglist or not, so sorry if this is not the right one. ;-)

Apache2::Request is part of the libapreq package so this is the correct
list, but you could also ask this on the mod_perl list since
Apache2::Request is one of the primary modules for processing request
arguments.

http://perl.apache.org/maillist/modperl.html

>  I'm trying to write a Perl Class which provides a handler() function
>  for mod_perl. something like that:
>
>      package InterRed::UploadLimiter;
>
>      use strict;
>      use Digest::MD5 qw(md5_hex);
>      use CGI::Cookie;
>
>      sub handler
>      {
>          my $r = shift;
>          my $transparent_hook = sub {
>          my ($upload, $data, $data_len, $hook_data) = @_;
>             warn "$hook_data: got $data_len bytes for " . $upload->name;
>          };
>
>          my $req = Apache2::Request->new($r,
>             UPLOAD_HOOK => $transparent_hook,
>          );
>          $req->parse();
>
>          return Apache2::Const::OK;
>      }
>
>      1;
>      __END__
>
...
>  Therefor I tried to register my handler like that in my apache config
>  file:
>
>         <IfModule mod_perl.c>
> #               PerlFixupHandler        InterRed::UploadLimiter
> #               PerlChildInitHandler    InterRed::UploadLimiter
>                 PerlHandler             InterRed::UploadLimiter
>         </IfModule>
>
>  As you can see, I tried different Handler types, because I'm not sure
>  which one is right to handle uploads (the upload must not be started
>  at this time). I want the handler to control any kind of perl cgi
>  script, which sends uploads to the server.

The closest correct handler is the PerlHandler, except that you referenced
Apache2::Request above, so I assume that you are using mod_perl2.   You
need to use PerlResponseHandler there instead of PerlHandler.

  PerlResponseHandler InterRed::UploadLimiter

See this link for a summary of the different handler phases.
http://perl.apache.org/docs/2.0/user/handlers/intro.html#mod_perl_Handlers_Categories

For an example of using the UPLOAD_HOOK, see this article I wrote on
perl.com:

http://www.perl.com/pub/a/2005/05/05/aggregation.html

It may be a bit overkill since you are new to this stuff, but it
demonstrates the necessary configuration and a complete handler for
processing uploaded data.

HTH,

Fred