You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Octavian Rasnita <or...@fcc.ro> on 2005/03/20 08:52:49 UTC

HTTP headers

Hi,

I have tried the following script under Windows 2000, mod_perl 2, Perl
5.8.4:

use strict;
use CGI;

my $q = new CGI;

print "Content-type: text/html\n\nTestare\n";
$q->redirect("http://localhost/");

This should print the Content-type: text/html header, then the word
"testare" in the body, then the word "Location: http://localhost/", but it
doesn't happen this way.

The page is redirected to http://localhost/ and after the redirection
header, "Content-type: text/html" followed by "testare" are still printed.

I think this may have something to do with the fact that CGI.pm is loaded
before anything by mod_perl, and who knows why, the method $q->redirect() is
used before other print statements.

Please tell me how can I avoid this, and print the headers in order?

Thank you.

PS. I have seen that if I use print "Location: http://localhost/\n\n";
instead of using the CGI method, it doesn't happen what I said.

Teddy


Re: HTTP headers

Posted by Tuomo Salo <ta...@almamedia.fi>.
Octavian Rasnita wrote:

> print "Content-type: text/html\n\nTestare\n";
> $q->redirect("http://localhost/");
> 
> This should print the Content-type: text/html header, then the word
> "testare" in the body, then the word "Location: http://localhost/", but it
> doesn't happen this way.

I think this is a "feature" of CGI.pm.

If you call CGI->redirect inside mod_perl, then CGI.pm does an
optimization for you:

sub header
     ...
     if ($MOD_PERL and not $nph) {
         my $r = Apache->request;
         $r->send_cgi_header($header);
         return '';
     }

If I am not mistaken, this send_cgi_header-way "overtakes" the
printed-and-parsed headers on their way to the browser, and gets
there first.

   -bass

> The page is redirected to http://localhost/ and after the redirection
> header, "Content-type: text/html" followed by "testare" are still printed.
> 
> I think this may have something to do with the fact that CGI.pm is loaded
> before anything by mod_perl, and who knows why, the method $q->redirect() is
> used before other print statements.
> 
> Please tell me how can I avoid this, and print the headers in order?

You could try either
1) using $q->header() instead of printing the content-type by hand, or
2) running in nph-mode (in which case you probably want to use
    $q->header anyway, since printing full HTTP headers by hand can
    be a bit tedious.)

Hope this helps,

   -bass

Re: HTTP headers

Posted by David Dick <da...@iprimus.com.au>.
Octavian Rasnita wrote:
> Hi,
> 
> I have tried the following script under Windows 2000, mod_perl 2, Perl
> 5.8.4:
> 
> use strict;
> use CGI;
> 
> my $q = new CGI;
> 
> print "Content-type: text/html\n\nTestare\n";
> $q->redirect("http://localhost/");
> 

try

use CGI();
use strict;
use warnings;

my $q = new CGI;

print $q->redirect("http://localhost/");

instead.

uru
-Dave