[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: accessing the entire stream
From:       Torsten =?iso-8859-1?q?F=F6rtsch?= <torsten.foertsch () gmx ! net>
Date:       2010-06-17 9:12:34
Message-ID: 201006171112.34968.torsten.foertsch () gmx ! net
[Download RAW message or body]

On Thursday 17 June 2010 10:47:39 Chris Datfung wrote:
> I want to manipulate various href links in the server response. I read in
> the server response as follows:
> 
> sub handler
> {
>   my $f = shift;
>   unless ($f->ctx){
>     while ($f->read(my $Buffer, BUFF_LEN)) {
>       if ($Buffer =~ m/Logout/){
>         $Buffer =~ s/href="(.*?\?.*?)"/href="$1\&NewParameter=$Value"/g;
>         $f->print($Buffer);
>     }
>     return Apache2::Const::OK;
>    }
>   }
> }
> 
> The BUFF_LEN constant is set to 1024. I want to run the regex against the
> entire server response. I tried adding a $CompleteBuffer .= $Buffer inside
> the while loop and moving the $f->print($Buffer) statement out of that
>  loop, but still ended up with multiple copies of the same text within
>  $Buffer. What is the correct what to access the entire server response in
>  a single variable? Any pointers to examples of this are greatly
>  appreciated.
> 
Here is what I do sometimes:

sub Filter : method FilterRequestHandler {
  my ($I, $f, $bb)=@_;

  my $ctx=$f->ctx;

  unless( $ctx ) {
    unless( $f->r->status==Apache2::Const::HTTP_OK ) {
      $f->remove;
      return Apache2::Const::DECLINED;
    }
    $f->ctx($ctx=[]);
    $f->r->headers_out->unset('Content-Length');
  }

  return Apache2::Const::OK
    unless ModPerl2::Tools::Filter::read_bb($bb, $ctx);

  # If the code reaches this point @$ctx contains all the chunks of output
  # from the original response. That is
  #   join '', @$ctx
  # is the response.

Now, you can process the response and then create one or more content buckets 
plus 1 eos bucket and pass all of them to the next filter:

  my $ba=$f->c->bucket_alloc;
  $bb->cleanup;			# reuse the input brigade

  $bb->insert_tail(APR::Bucket->new($ba, $_)) for (@content);
  $bb->insert_tail(APR::Bucket::eos_create($ba));
  $rc=$f->next->pass_brigade($bb);

  return $rc==APR::Const::SUCCESS ? Apache2::Const::OK : $rc;
}

See my 2 ˘ to the conversation back on Apr/15 2010 about "accessing 
environment variables set by other modules". You were the initiator of the 
thread.

Torsten Förtsch

-- 
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic