sub-requests, lookup_uri and that kind of stuff
am 22.12.2008 00:04:30 von aw
Hi.
I'm in the middle of a PerlHandler. To serve the current request
properly, I would need the result (content) from another HTTP request
which I would maself address to this same webserver.
I could do this the hard way by using LWP to just issue a new HTTP
request to localhost and get the content, before I go on with my current
request.
But I figure there must be an easier and more efficient way, no ?
Looking at the explanations for sub-requests, lookup_uri etc.., I get
the impression however that calling these, pretty much terminates the
current request, and replaces it by the one I'm calling.
Which is not what I want.
I want to continue processing the current request but using the response
from my other request.
Any ideas ?
Note (but I think this is irrelevant) : the secondary request I want to
issue is in fact destined to the Tomcat which sits behind this Apache
via a mod_jk re-director. It's like if, before answering the client, I
want to ask Tomcat a question like "if I were, hypothetically, to
request the following URL, what would you answer ?"
A bit like the Sphynx in the famous puzzle.
Re: sub-requests, lookup_uri and that kind of stuff
am 22.12.2008 00:55:43 von torsten.foertsch
On Mon 22 Dec 2008, André Warnier wrote:
> I could do this the hard way by using LWP to just issue a new HTTP
> request to localhost and get the content, before I go on with my
> current request.
>
> But I figure there must be an easier and more efficient way, no ?
>
> Looking at the explanations for sub-requests, lookup_uri etc.., I get
> the impression however that calling these, pretty much terminates the
> current request, and replaces it by the one I'm calling.
> Which is not what I want.
> I want to continue processing the current request but using the
> response from my other request.
Look at what mod_include does to get an impression about subrequests.=20
You can use subrequests in 3 ways:
1) lookup_uri or similar creates the subreq and runs it up to fixup.=20
Then you can use any information the subreq has gathered so far, that=20
means authentication, finfo etc.
2) run it to include the output of the subreq into the current document=20
more or less unmodified. To do that you create the subreq, install the=20
necessary output filters and run() it.
3) run it to process the output in some other way. That can be done for=20
example this way:
my $content=3D'';
my $subr=3D$r->lookup_uri( $uri );
$subr->add_output_filter( sub {
my ($f, $bb) =3D @_;
while (my $e =3D $bb->first) {
$e->read(my $buf);
$content.=3D$buf;
$e->delete;
}
return Apache2::Const::OK;
});
$subr->run;
my $rc=3D$subr->status;
my $ct=3D$subr->content_type;
Now, $content holds the response body, $rc the HTTP status code and $ct=20
the subreq's content type.
In all cases a subreq returns control to the calling request. An=20
internal_redirect on the other hand almost completely takes over the=20
request without return to the calling req.
Torsten
=2D-=20
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net