You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Alex Menendez <am...@hotbacon.com> on 2000/08/21 04:38:53 UTC
cgis and subrequests
Hello, all
I have a module that appends a line of stats sensitive information to
the bottom of html pages or
cgi scripts. the module uses lookup_uri() to generate a subrequest then
calls run() to output the actual contents of the file. the eagle book
says that calling run() on a subrequest should automatically send the
client the appropriate http headers and the document's body. However, I
have found that this is not the case. The following code does not send
http headers for both cgis and html docs. The body stuff is working fine
but the headers are not being sent:
my $uri = $r->uri;
unless(!$r->args) {
$uri = $uri .'?'.$r->args;
}
my $subr = $r->lookup_uri($uri);
if($r->dir_config('is_cgi')) {
$subr->handler('cgi-script');
} else {
$subr->handler('server-parsed');
}
$subr->run();
my $status = $subr->status;
$r->print(&create_img_tag($file,$SCRIPT_ON,$status));
return $status;
any ideas?
BTW> &create_img_tag just returns a string.
-amen
Re: cgis and subrequests
Posted by Alex Menendez <am...@hotbacon.com>.
hey, guys
just for the record....I finally got this thing to work. However, it does
have some bizarre behavior. After compiling the patch,
$subr->run(1) returns headers without calling the send_http_header method.
This holds true for static files and for cgis that only set the content type
header. For cgis that add any additional header like Location, the headers
are not returned unless you explicitly call send_http_header. I know that
our cgi's at most only alter the Location header in addition to content
type...so...I coded around this with the following:
my $subr = $r->lookup_uri($uri);
if($r->dir_config('is_cgi')) {
$subr->handler('cgi-script');
} else {
$subr->handler('server-parsed');
}
$subr->run(1);
if($subr->header_out('Location')) {
$subr->send_http_header;
} else {
$r->print(&create_img_tag($file,$SCRIPT_ON,$status));
}
my $status = $subr->status;
return $status;
if I always called send_http_header, a second set of HTTP headers would be
appended to static documents and to cgis that only change the content type
header.
Just an FYI...
-amen
Doug MacEachern wrote:
> On Mon, 21 Aug 2000, Alex Menendez wrote:
>
> >
> > ok, what about cgi's that generate Location: headers. Am I out of luck
> > here? should I contemplate changing src for ap_run_sub_req
>
> doh, right, headers are not added until run(). no need to change
> ap_run_sub_req, we can support this. with the patch below you can say
> $subr->run(1), where 1 enables send_http_header() within subrequests.
>
> --- src/modules/perl/Apache.xs 2000/08/15 19:36:32 1.103
> +++ src/modules/perl/Apache.xs 2000/08/21 19:38:32
> @@ -1996,10 +2003,15 @@
> "Apache::SubRequest::DESTROY(0x%lx)\n", (unsigned long)r));
>
> int
> -run(r)
> +run(r, allow_send_header=0)
> Apache::SubRequest r
> + int allow_send_header
>
> CODE:
> + if (allow_send_header) {
> + r->assbackwards = 0;
> + }
> +
> RETVAL = run_sub_req(r);
>
> OUTPUT:
Re: cgis and subrequests
Posted by Doug MacEachern <do...@covalent.net>.
On Mon, 21 Aug 2000, Alex Menendez wrote:
>
> ok, what about cgi's that generate Location: headers. Am I out of luck
> here? should I contemplate changing src for ap_run_sub_req
doh, right, headers are not added until run(). no need to change
ap_run_sub_req, we can support this. with the patch below you can say
$subr->run(1), where 1 enables send_http_header() within subrequests.
--- src/modules/perl/Apache.xs 2000/08/15 19:36:32 1.103
+++ src/modules/perl/Apache.xs 2000/08/21 19:38:32
@@ -1996,10 +2003,15 @@
"Apache::SubRequest::DESTROY(0x%lx)\n", (unsigned long)r));
int
-run(r)
+run(r, allow_send_header=0)
Apache::SubRequest r
+ int allow_send_header
CODE:
+ if (allow_send_header) {
+ r->assbackwards = 0;
+ }
+
RETVAL = run_sub_req(r);
OUTPUT:
Re: cgis and subrequests
Posted by Alex Menendez <am...@hotbacon.com>.
ok, what about cgi's that generate Location: headers. Am I out of luck
here? should I contemplate changing src for ap_run_sub_req
thanx,
-amen
On Mon, 21 Aug 2000, Doug MacEachern wrote:
> whoops. yeah, $subr->send_http_header won't work for the same reason
> send_http_header() calls within run() do not. you'll just need to
> propagate subrequest info like so:
>
> my $subr = $r->lookup_uri($uri);
>
> $r->send_http_header($subr->content_type);
>
> $subr->run;
>
>
Re: cgis and subrequests
Posted by Doug MacEachern <do...@covalent.net>.
whoops. yeah, $subr->send_http_header won't work for the same reason
send_http_header() calls within run() do not. you'll just need to
propagate subrequest info like so:
my $subr = $r->lookup_uri($uri);
$r->send_http_header($subr->content_type);
$subr->run;
Re: cgis and subrequests
Posted by Alex Menendez <am...@hotbacon.com>.
hmmmm,
I have tried what you suggested and it is still not generating the
headers. I have tried calling the send_http_header function both before
and after the handler switch and it still is not working. I think it is
bug but I am not sure. At any rate, here is some of the module code and
the http.conf directives:
my $uri = $r->uri;
unless(!$r->args) {
$uri = $uri .'?'.$r->args;
}
my $subr = $r->lookup_uri($uri);
if($r->dir_config('is_cgi')) {
$subr->handler('cgi-script');
} else {
$subr->handler('server-parsed');
}
$subr->send_http_header;
$subr->run();
my $status = $subr->status;
$subr->print(&create_img_tag($file,$SCRIPT_ON,$status));
return $status;
<Files ~ "\.(html|htm|shtml|fhtml)$">
SetHandler perl-script
PerlHandler Macromedia::AddStatImage
</Files>
<Files ~ "\.(cgi|pl|sh)$">
SetHandler perl-script
PerlHandler Macromedia::AddStatImage
PerlSetVar is_cgi 1
</Files>
any ideas?
-amen
On Mon, 21 Aug 2000, Doug MacEachern wrote:
> On Sun, 20 Aug 2000, Alex Menendez wrote:
>
> > unfortunately, I am not entirely sure what a cgi is going to output all the
> > time. The cgi might try to do a redirect using the Location field. Therefore,
> > I don't think I can simply send_http_headers as type text/html...
>
> right,
>
> > > my $lookup = $r->lookup_uri( $uri );
> > > $r->send_http_header( 'text/html' );
>
> so just change that line to:
> $lookup->send_http_header;
>
> and you'll get the content-type and headers generated by the subrequest.
> if there's a bug, it's in the book, subrequest->run() is not supposed to
> include headers in the output.
>
Re: cgis and subrequests
Posted by Doug MacEachern <do...@covalent.net>.
On Sun, 20 Aug 2000, Alex Menendez wrote:
> unfortunately, I am not entirely sure what a cgi is going to output all the
> time. The cgi might try to do a redirect using the Location field. Therefore,
> I don't think I can simply send_http_headers as type text/html...
right,
> > my $lookup = $r->lookup_uri( $uri );
> > $r->send_http_header( 'text/html' );
so just change that line to:
$lookup->send_http_header;
and you'll get the content-type and headers generated by the subrequest.
if there's a bug, it's in the book, subrequest->run() is not supposed to
include headers in the output.
Re: cgis and subrequests
Posted by Alex Menendez <am...@hotbacon.com>.
unfortunately, I am not entirely sure what a cgi is going to output all the
time. The cgi might try to do a redirect using the Location field. Therefore,
I don't think I can simply send_http_headers as type text/html...
thanx, though
-amen
Rick Myers wrote:
> On Aug 20, 2000 at 19:38:53 -0700, Alex Menendez twiddled the keys to say:
> > cgi scripts. the module uses lookup_uri() to generate a subrequest then
> > calls run() to output the actual contents of the file. the eagle book
> > says that calling run() on a subrequest should automatically send the
> > client the appropriate http headers and the document's body. However, I
> > have found that this is not the case. The following code does not send
> > http headers for both cgis and html docs. The body stuff is working fine
> > but the headers are not being sent:
> >
> > my $uri = $r->uri;
> > unless(!$r->args) {
> > $uri = $uri .'?'.$r->args;
> > }
> > my $subr = $r->lookup_uri($uri);
> > if($r->dir_config('is_cgi')) {
> > $subr->handler('cgi-script');
> > } else {
> > $subr->handler('server-parsed');
> > }
> > $subr->run();
> > my $status = $subr->status;
> > $r->print(&create_img_tag($file,$SCRIPT_ON,$status));
> > return $status;
> >
> > any ideas?
>
> Yes. run() no longer sends headers (as far as I know). I don't know when
> it was changed, but it pre-dates my experience. I've had the following
> working just fine for close to a year now (or maybe my sense of time is
> warped :).
>
> my $lookup = $r->lookup_uri( $uri );
> $r->send_http_header( 'text/html' );
> my $status = $lookup->run;
> $r->status( $status );
>
> Rick Myers rik@sumthin.nu
> ----------------------------------------------------
> The Feynman Problem 1) Write down the problem.
> Solving Algorithm 2) Think real hard.
> 3) Write down the answer.
Re: cgis and subrequests
Posted by Rick Myers <ri...@sumthin.nu>.
On Aug 20, 2000 at 19:38:53 -0700, Alex Menendez twiddled the keys to say:
> cgi scripts. the module uses lookup_uri() to generate a subrequest then
> calls run() to output the actual contents of the file. the eagle book
> says that calling run() on a subrequest should automatically send the
> client the appropriate http headers and the document's body. However, I
> have found that this is not the case. The following code does not send
> http headers for both cgis and html docs. The body stuff is working fine
> but the headers are not being sent:
>
> my $uri = $r->uri;
> unless(!$r->args) {
> $uri = $uri .'?'.$r->args;
> }
> my $subr = $r->lookup_uri($uri);
> if($r->dir_config('is_cgi')) {
> $subr->handler('cgi-script');
> } else {
> $subr->handler('server-parsed');
> }
> $subr->run();
> my $status = $subr->status;
> $r->print(&create_img_tag($file,$SCRIPT_ON,$status));
> return $status;
>
> any ideas?
Yes. run() no longer sends headers (as far as I know). I don't know when
it was changed, but it pre-dates my experience. I've had the following
working just fine for close to a year now (or maybe my sense of time is
warped :).
my $lookup = $r->lookup_uri( $uri );
$r->send_http_header( 'text/html' );
my $status = $lookup->run;
$r->status( $status );
Rick Myers rik@sumthin.nu
----------------------------------------------------
The Feynman Problem 1) Write down the problem.
Solving Algorithm 2) Think real hard.
3) Write down the answer.