accessing the entire stream

accessing the entire stream

am 17.06.2010 10:47:39 von Chris Datfung

--0016e6db61e91719d7048935e4b7
Content-Type: text/plain; charset=ISO-8859-1

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.

Thanks,
Chris

--0016e6db61e91719d7048935e4b7
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I want to manipulate various href links in the server=
response. I read in the server response as follows:

v>
sub handler
{
=A0 my $f =3D shift;
=A0 unless ($f-=
>ctx){

=A0 =A0 while ($f->read(my $Buffer, BUFF_LEN)) {
=A0 =A0 =
=A0 if ($Buffer =3D~ m/Logout/){
=A0 =A0 =A0 =A0 $Buffer =3D~ s/h=
ref=3D"(.*?\?.*?)"/href=3D"$1\&NewParameter=3D$Value&quo=
t;/g;
=A0 =A0 =A0 =A0 $f->print($Buffer);

=A0 =A0 }
=A0 =A0 return Apache2::Const::OK;
=A0 =A0}
=A0 } iv>
}

The BUFF_LEN constant is set to 1024. I =
want to run the regex against the entire server response. I tried adding a =
$CompleteBuffer .=3D $Buffer inside the while loop and moving the $f->pr=
int($Buffer) statement out of that loop, but still ended up with multiple c=
opies of the same text within $Buffer. What is the correct what to access t=
he entire server response in a single variable? Any pointers to examples of=
this are greatly appreciated.


Thanks,
Chris


--0016e6db61e91719d7048935e4b7--

Re: accessing the entire stream

am 17.06.2010 11:12:34 von torsten.foertsch

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:
>=20
> sub handler
> {
> my $f =3D shift;
> unless ($f->ctx){
> while ($f->read(my $Buffer, BUFF_LEN)) {
> if ($Buffer =3D~ m/Logout/){
> $Buffer =3D~ s/href=3D"(.*?\?.*?)"/href=3D"$1\&NewParameter=3D$Va=
lue"/g;
> $f->print($Buffer);
> }
> return Apache2::Const::OK;
> }
> }
> }
>=20
> The BUFF_LEN constant is set to 1024. I want to run the regex against the
> entire server response. I tried adding a $CompleteBuffer .=3D $Buffer ins=
ide
> 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.
>=20
Here is what I do sometimes:

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

my $ctx=3D$f->ctx;

unless( $ctx ) {
unless( $f->r->status==Apache2::Const::HTTP_OK ) {
$f->remove;
return Apache2::Const::DECLINED;
}
$f->ctx($ctx=3D[]);
$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 bucke=
ts=20
plus 1 eos bucket and pass all of them to the next filter:

my $ba=3D$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=3D$f->next->pass_brigade($bb);

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

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

Torsten Förtsch

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

Like fantasy? http://kabatinte.net