|
Posted by Elie W on November 10, 2004, 10:19 pm
Please log in for more thread options
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 ;
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.
|
|
Posted by John Bokma on November 10, 2004, 8:58 pm
Please log in for more thread options
Elie W wrote:
> $finalHTML = eval ;
>
> 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?
my $finalHTML = eval ...
if ( $@ ) {
} else {
$finalHTML = $query->header(....) . $finalHTML;
And remove the header from webify.
Also use strict, use warnings, I guess you don't
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
|
|
Posted by Gunnar Hjalmarsson on November 10, 2004, 9:58 pm
Please log in for more thread options Elie W wrote:
> 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.
I'm not able to explain the 'magic' when you use CGI.pm's header method
together with mod_perl, but why wouldn't it be as clean to simply wait
with calling $query->header until you are about to print to STDOUT?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
| Similar Threads | Posted | | http request headers | October 1, 2004, 12:47 pm |
| Parse tcpdump for HTTP Request Response Headers | July 29, 2007, 2:10 pm |
| Net::Analysis Parse tcpdump for HTTP Request/Response Headers | July 29, 2007, 6:41 am |
| CGI.pm and ModPerl::Registry | December 21, 2007, 11:20 pm |
| modperl and apache handlers | December 28, 2004, 5:38 am |
| CPAN conflict issue - CGI.pm vs ModPerl::Registry | September 25, 2006, 12:42 pm |
| XML::Twig parseurl with input Headers/XML | January 16, 2005, 10:58 pm |
| Premature Ending of script headers | August 17, 2005, 4:53 pm |
| Problem with CPAN and tar file headers | May 16, 2007, 4:57 am |
| HTML::TableExtract with headers constraint, exluding right-most column | May 15, 2005, 3:31 pm |
|