Re: accessing the entire stream

Torsten Förtsch <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.