http headers with CGI.pm and mod_perl (bug?)

From: Elie W (welie_at_yahoo.com)
Date: 11/10/04


Date: Wed, 10 Nov 2004 21:19:30 +0200

I have a question about printing http headers with CGI.pm running on an
Apache server with mod_perl. What happens if I need to change the http
header after I already called $query->header? Is there a way to prevent
CGI from sending the header to mod_perl?

According to the documentation of CGI, to print a header I would do:

print $query->header;

My assumption is, that $query->header; returns a string, so I could do:
$header=$query->header;

However, a quick look under the hood shows that when mod_perl is running
on the server, $query->header does not actually return anything, rather
it uses a call to mod_perl to print the header.

This being the case, I have the following problem, where I need to
reprint an http header.

I have a subroutine called webify which is supposed to take some text
and wrap it up in html. In the subroutine some other routines may be
called to massage the text a bit. Becuase it is possible that those
routines may fail, I put the call to webify inside an eval. If anything
fails, I want to default to printing the plain text.

$finalHTML = eval {webify(text)};

if ($@){
    do alternate....
    $query->header(-type=>'text/plain'); #or whatever the type for plain
text is
}

In the routine I do:
sub webify {
....
$output=$query->header(-type=>'text/html');
$output.=$query->h1(getUser($user));
....
}

Suppose getUser dies, and therefore the "do alternate...." above is
executed. Well, the call to $query->header in webify() has already sent
the header to mod_perl which has printed out the header already and I
believe the 2nd call to $query->header, in the alrenate code, actually
won't do anything.

So what to do? Obviously I can code around it but its not as clean. Is
this the way things are supposed to happen? Is this a bug? Is there a
way to disable sending the header to mod_perl?

Thanks,
Elie.



Relevant Pages

  • Re: ad else is wrong ????
    ... Okay, I headed after the error - and after some reading, moved the entire ... ASP portion into the which took care of these problems. ... > Header Error ... > The HTTP headers are already written to the client browser. ...
    (microsoft.public.scripting.vbscript)
  • Re: associating subdomains to .net sockets
    ... subdomains/multiple hostnames for same IP address are enabled ... > using a "Host Header" tag in the HTTP headers. ...
    (microsoft.public.dotnet.general)
  • Re: AxShDocVw.AxWebBrowser.Navigate and object headers
    ... Http headers are just string name/value pairs. ... If you can serialize ... header from the Request, or the body, etc. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlHttpRequest not loading latest version of xml
    ... >>> Sending proper HTTP headers is good also, but the browser may choose to ... sending identifying HTTPs header _and_ change the URI would not ... > That doesn't mean the browser is going to follow that advice. ... You set a header, you hope the browser honors it and some don't. ...
    (comp.lang.javascript)
  • Re: [PHP] header("Location: page.php") not redirecting
    ... would be if there was some output before the header(). ... Replace your header call with an echo. ... > when in the HTTP headers instead of the content. ... > PHP Security Handbook ...
    (php.general)