开发者

How to modify post request content using mod_perl

How to modify post request content using mod_perl's filter/handler?

I can read request content in PerlResponseHandler but how do I "attach" modified content back into request?

Also, I don't want to do this in PerlResponseHandler as I want requested resource to handle response generation part.

开发者_如何学CAny help will be appreciated.

Thanks.


if you add use Apache2::RequestIO and from my ($r) = @_; you can do a $r->print();

a PerlResponseHandler can not modify request data, but even if it could what would be the point. Only a PerlInputFilterHandler can do that as it filters input before it gets to a response.

The only thing after a response handler is the Output Filters, Log Handler, and the Cleanup Handler.

package MyFilter;

use strict;

use base qw(Apache::Filter);
use Apache2::Const qw(OK);

sub handler: FilterRequestHandler {
  my ($f) = @_;
  while($f->read(my $buf, 1024)) {
    # do something with $buf
    $f->print($buf);
  }
  return OK;
}

1;

Important to know, that you will get data in chunks. When you read you may or may not get the whole posted in a single call.


This code also works -

  package MyApache2::Test7;

  use strict;
  use warnings;

  use base qw(Apache2::Filter);

  use Apache2::Connection ();
  use APR::Brigade ();
  use APR::Bucket ();

  use Apache2::Const -compile => 'OK';
  use APR::Const     -compile => ':common';

use Apache2::Log ();

  sub handler : FilterRequestHandler {

Apache2::ServerRec->log_error("f*** starts");


      my ($f, $bb, $mode, $block, $readbytes) = @_; 
      my $c = $f->c;
      my $bb_ctx = APR::Brigade->new($c->pool, $c->bucket_alloc);
      my $rv = $f->next->get_brigade($bb_ctx, $mode, $block, $readbytes);
      return $rv unless $rv == APR::Const::SUCCESS;

      while (!$bb_ctx->is_empty) {
          my $b = $bb_ctx->first;

          if ($b->is_eos) {
              $bb->insert_tail($b);
              last;
          }

          my $len = $b->read(my $data);

Apache2::ServerRec->log_error($len);

          #$b = APR::Bucket->new($bb->bucket_alloc, lc $data) if $len;

          #$b->remove;
          #$bb->insert_tail($b);

Apache2::ServerRec->log_error($data);

Apache2::ServerRec->log_error("f*** ends");

$bb_ctx->cleanup;

      }

      return Apache2::Const::OK;
  }

  1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜