Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

Access to Request Body

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


tjg at soe

Apr 28, 2008, 11:12 AM

Post #1 of 5 (526 views)
Permalink
Access to Request Body

Hello,

I'm writing a mod_perl logging module, and I can't seem to find anywhere in
the documentation that talks about how I can access the request body
(basically, the POST data) of a request during the logging phase. Is this
not possible?

I'm using mod_perl 2.0 and Apache 2.2 on a CentOS 5.1 box.

Thanks!

Tim Gustafson
SOE Webmaster
UC Santa Cruz
tjg[at]soe.ucsc.edu
(831) 459-5354


adam.prime at utoronto

Apr 28, 2008, 1:46 PM

Post #2 of 5 (500 views)
Permalink
Re: Access to Request Body [In reply to]

Quoting Tim Gustafson <tjg[at]soe.ucsc.edu>:

> Hello,
>
> I'm writing a mod_perl logging module, and I can't seem to find anywhere in
> the documentation that talks about how I can access the request body
> (basically, the POST data) of a request during the logging phase. Is this
> not possible?

do you want access to the unmodified post data, or do you want access
to the content? Apache2::Request parses the POST data, and makes it
available though $r->body. (and possibly $r->upload) If you want the
unparsed body, i'm not sure what you'd have to do offhand.

Adam


tjg at soe

Apr 28, 2008, 2:05 PM

Post #3 of 5 (498 views)
Permalink
RE: Access to Request Body [In reply to]

Hrmm, that doesn't seem to work. In my code, I have:

print $fh $r->body . "\n";

And in my error log, I get:

Can't locate object method "body" via package "Apache2::RequestRec" at
/var/www/lib/Log.pm line 31.

Tim Gustafson
SOE Webmaster
UC Santa Cruz
tjg[at]soe.ucsc.edu
(831) 459-5354


-----Original Message-----
From: adam.prime[at]utoronto.ca [mailto:adam.prime[at]utoronto.ca]
Sent: Monday, April 28, 2008 1:47 PM
To: modperl[at]perl.apache.org
Subject: Re: Access to Request Body

Quoting Tim Gustafson <tjg[at]soe.ucsc.edu>:

> Hello,
>
> I'm writing a mod_perl logging module, and I can't seem to find anywhere
in
> the documentation that talks about how I can access the request body
> (basically, the POST data) of a request during the logging phase. Is this
> not possible?

do you want access to the unmodified post data, or do you want access
to the content? Apache2::Request parses the POST data, and makes it
available though $r->body. (and possibly $r->upload) If you want the
unparsed body, i'm not sure what you'd have to do offhand.

Adam


adam.prime at utoronto

Apr 28, 2008, 2:18 PM

Post #4 of 5 (498 views)
Permalink
RE: Access to Request Body [In reply to]

Quoting Tim Gustafson <tjg[at]soe.ucsc.edu>:

> Hrmm, that doesn't seem to work. In my code, I have:
>
> print $fh $r->body . "\n";
>
> And in my error log, I get:
>
> Can't locate object method "body" via package "Apache2::RequestRec" at
> /var/www/lib/Log.pm line 31.

It's part of Apache2::Request, see the documentation below

http://httpd.apache.org/apreq/docs/libapreq2/group__apreq__xs__request.html#body

It gives you access to the content through an APR::Table derived
class, so you can do stuff like:

my $name = $r->body("name");

or

my $table = $r->body();

It doesn't give you access to the whole, unaltered POST data (afaik).

Adam


torsten.foertsch at gmx

Apr 29, 2008, 12:21 AM

Post #5 of 5 (487 views)
Permalink
Re: Access to Request Body [In reply to]

On Mon 28 Apr 2008, Tim Gustafson wrote:
> I'm writing a mod_perl logging module, and I can't seem to find anywhere in
> the documentation that talks about how I can access the request body
> (basically, the POST data) of a request during the logging phase.  Is this
> not possible?

It is but you'd have to use an input filter. Something like this:

use Apache2::Const -compile=>qw/M_POST OK/;
use APR::Const -compile=>qw/SUCCESS/;
use Apache2::Filter ();
use APR::Bucket ();
use APR::Brigade ();

if( $r->method_number==Apache2::Const::M_POST ) {
my @content;
my $cl=0;
my $rc=$r->add_input_filter( sub {
my ($f, $bb, $mode, $block, $readbytes) = @_;

my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
return $rv unless $rv == APR::Const::SUCCESS;

for (my $b = $bb->first; $b; $b = $bb->next($b)) {
$b->read(my $bdata);
$cl+=length $bdata;
push @content, $bdata;
}

return Apache2::Const::OK;
} );
$r->discard_request_body;
$r->pnotes->{rbody}=\@content;
$r->pnotes->{rbody_length}=$cl;
}

Remember this is just an example not production code. For production you'd
want to put the data chunks into a temporary file / files. This way you'll
get the request body stripped of all transfer-encoding.

Torsten

--
Need professional mod_perl support?
Just hire me: torsten.foertsch[at]gmx.net

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.