You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Morten Bøgeskov <mo...@bogeskov.dk> on 2005/09/21 12:44:16 UTC

->lookup_uri(...), ->run() & Headers

Hello...

 I'm trying to wrap an entire website in a perl handler. And then use
the $r->lookup_uri() to access some files in the domain.

httpd.conf:---------------------------8<--------------------------------------
<VirtualHost *>
  PerlAccessHandler   FOO_Access
  PerlResponseHandler FOO_Response
</VirtualHost>
FOO_Access.pm-------------------------8<--------------------------------------
package FOO_Access;

use Apache2::Const -compile => qw(:common);
use Apache2::RequestRec();

sub handler : method {
  my($class, $r) = @_;
  $r->handler('modperl');
  return Apache2::Const::OK;
}

1;
FOO_Response.pm:----------------------8<--------------------------------------
package FOO_Response;

use Apache2::Const -compile => qw(:common);
use Apache2::SubRequest ();
use Apache2::RequestRec();

sub handler : method {
  my($class, $r) = @_;
  my $subr = $r->lookup_uri('/index.php?a=1&b=2');
  return $subr->run();
}

1;
index.php:----------------------------8<--------------------------------------
<?php
if($_GET['set_cookie']) {
  Header('Set-Cookie: ' . $_GET['set_cookie']);
}
Header("Last-Modified: " . gmdate('D, d M Y H:i:s T', time()));
Header("Pragma: no-cache");
Header("Expires: 0");
Header("Cache-Control: no-cache, must-revalidate, max_age=0");
Header("Content-Type: text/html; charset=iso-8859-1");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>foo</title>
  </head>
--------------------------------------8<--------------------------------------

My strategy is to set the 'modperl' handler (for PerlResponseHandler) in
the PerlAccessHandler resulting in the modperl handler is not set when
using 'lookup_uri', providing access to a php script.

And this works like a charm (the content on the php-script is parsed and
executed), except for the headers. I don't get any headers generated
from the php-script. Is there any way of getting them through to the
user?

--------------------------------------8<--------------------------------------
$ echo "GET / HTTP/1.0\nHost: localhost\n" | nc localhost 8000 
Server: Apache/2.0.54 (Debian GNU/Linux) mod_perl/2.0.1 Perl/v5.8.4
Connection: close
Content-Type: httpd/unix-directory

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>foo</title>
  </head>
--------------------------------------8<--------------------------------------

Does anybody have any idea how to get a hold of the headers?


-- 
  Morten Bøgeskov (email: morten@bogeskov.dk)

- But I do think that any model which fundamentally prevents people getting
  something they want is going to fail - D. Adams.     (on RIAA vs napster)


Re: ->lookup_uri(...), ->run() & Headers

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> And this works like a charm (the content on the php-script is parsed and
> executed), except for the headers. I don't get any headers generated
> from the php-script. Is there any way of getting them through to the
> user?

oooh!

sorry, I totally misread what you were trying to do - print headers from
within the subrequest, not have mod_perl or apache send the headers for you.
 gotcha now.

ok, then I think you're right - this probably won't work.  the only thing I
can think of is setting

  PerlOptions -ParseHeaders

in httpd.conf and see if that makes a difference.  basically you're looking
for the equivalent of php nph scripts at this point.

however, I have an alternative for you.  if you're just using your mod_perl
PerlResponseHandler to dispatch to php you could just use

  $r->internal_redirect("/index.php?a=1&b=2")

and let php pick up the script naturally.

HTH

--Geoff

Re: ->lookup_uri(...), ->run() & Headers

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> I've now tried any combination of:
> ( $r | $subr ) ->assbackwards( 0 | 1 );

blarg.  yes, that was supposed to be $subr->assbackwards(), not
$r->assbackwards.

> The only one with any effect was: $r->assbackwards(1); which resulted in
> no headers at all. Not quite what I was hoping for ;o)

hmph, that's odd.

the issue is that you want the subrequest to go directly to the client, with
headers, right?  $subr->assbackwards(0) ought to do that.  can you check the
result of

  my $ab = $subr->assbackwards;

?  I'd hope it's set to 1.

for the record and archives, this is exactly what

  $sub->run(1);   # sets sub->assbackwards(0);

did in mod_perl 1.0.  in mp2 we require you to call $sub->assbackwards()
yourself.

> 
> But thanks anyway.
> 
> My gut feeling (nothing to go by, by the way) is that I somehow need to
> tell apache2 that, it shouldn't strip the headers when running a
> subrequest.

yes, that's what the assbackwards flag is for :)

> 
> Maby I should just go back to apache1.3 & mod_perl, that does what I
> want it to.

well, if you like.  but we can probably make this work, too.  after all, it
ought to work, unless httpd 2.0 prohibits it, which IIRC it doesn't.  but
it's been a while since I looked at that code.

> 
> Or maby, the problem is that in
> "xs/Apache2/SubRequest/Apache2__SubRequest.h" "mpxs_ap_run_sub_req" runs
> "modperl_wbucket_flush" to make sure all that is printed before ->run()
> is outputed, however this might (I havn't diged deep enough into the
> code yet) send the headers, and the run the script forcing apache to
> discart them.

headers are handled by a separate filter.  maybe that's where the issue is,
though.  IIRC, I was having lots of issues using subrequests due to that
flush call, but I'm not sure what it was (since it was 2 years ago).  you
might want to check the dev@perl.apache.org archives.

--Geoff

Re: ->lookup_uri(...), ->run() & Headers

Posted by Morten Bøgeskov <mo...@bogeskov.dk>.
On Wed, 2005-09-21 at 07:34 -0400, Geoffrey Young wrote:
> > sub handler : method {
> >   my($class, $r) = @_;
> >   my $subr = $r->lookup_uri('/index.php?a=1&b=2');
> 
>     $r->assbackwards(0);
> 
> >   return $subr->run();
> > }
> 
> > And this works like a charm (the content on the php-script is parsed and
> > executed), except for the headers. I don't get any headers generated
> > from the php-script. Is there any way of getting them through to the
> > user?
> 
> try the above.

I've now tried any combination of:
( $r | $subr ) ->assbackwards( 0 | 1 );
The only one with any effect was: $r->assbackwards(1); which resulted in
no headers at all. Not quite what I was hoping for ;o)

But thanks anyway.

My gut feeling (nothing to go by, by the way) is that I somehow need to
tell apache2 that, it shouldn't strip the headers when running a
subrequest.

Maby I should just go back to apache1.3 & mod_perl, that does what I
want it to.

Or maby, the problem is that in
"xs/Apache2/SubRequest/Apache2__SubRequest.h" "mpxs_ap_run_sub_req" runs
"modperl_wbucket_flush" to make sure all that is printed before ->run()
is outputed, however this might (I havn't diged deep enough into the
code yet) send the headers, and the run the script forcing apache to
discart them.


-- 
  Morten Bøgeskov (email: morten@bogeskov.dk)

Gallway's law of libraries : There ARE no answers, only crossreferences


Re: ->lookup_uri(...), ->run() & Headers

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> sub handler : method {
>   my($class, $r) = @_;
>   my $subr = $r->lookup_uri('/index.php?a=1&b=2');

    $r->assbackwards(0);

>   return $subr->run();
> }

> And this works like a charm (the content on the php-script is parsed and
> executed), except for the headers. I don't get any headers generated
> from the php-script. Is there any way of getting them through to the
> user?

try the above.

--Geoff