On Wed 08 Apr 2009, André Warnier wrote:
> I want to write a mod_perl input filter, which essentially would
> replace some characters from form-posted input on the fly, before
> letting the request go to the real application.
> The requests are POST, with content encoded in one long string,
> URL-encoded. I am thinking of writing this as a FilterRequestHandler
> filter, grabbing the content, splitting it on "&", URL-decoding the
> so-splitted query parameters individually, then in the value
> performing the character substitutions, then re-URL-encoding the
> query parameters, re-joining them with "&", and essentially et voilà.
>
> Basically, the type shown here :
> http://perl.apache.org/docs/2.0/user/handlers/filters.html#toc_Stream
>_oriented_Input_Filters
>
> Is the above a realistic view, or am I missing something essential ?

Remember the request can consist of multiple brigades. Each filter 
invocation gets one brigade. That means you have to be prepared to 
store some context (store the /[^&;]*$/ part in $f->ctx). Don't try to 
read in the whole request. Operate on each part of it.

For example try the following PerlInputFilterHandler:

  sub handler {
    my ($f)=...@_;

    $f->print("\n###########\n");
    my $buf;
    while($f->read($buf, 1024)) {
      $f->print("\n>>>$buf<<<\n");
      undef $buf;
    }
    $f->print("\n:::::::::::\n");
    return Apache2::Const::OK;
  }

with this PerlResponseHandler:

  sub handler {
    my ($r)=...@_;

    $r->content_type('text/plain');
    my $buf;
    while($r->read($buf, 15000)) {
      $r->print($buf);
      undef $buf;
    }

    return Apache2::Const::OK;
  }

and feed it with more than 8000 bytes:

perl -e 'print "x"x10000' | curl -qs -d @- http://localhost/iftest

You'll see multiple blocks wrapped in >>> ... <<< from the filter loop. 
But you'll also see 2 blocks of ######### ... :::::::::: that mark the 
filter invocations.

BTW, the buffer size in the response handler is that large because is 
must be able to store a whole brigade's content due to a modperl bug, 
see "XXX:" comment in modperl_request_read(). So, don't $f->print much 
in one filter invocation.

Torsten

-- 
Need professional mod_perl support?
Just hire me: torsten.foert...@gmx.net

Reply via email to