Re: HTTP headers

[prev] [thread] [next] [Date index for 2005/03/21]

From: Tuomo Salo
Subject: Re: HTTP headers
Date: 13:14 on 21 Mar 2005
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

HTTP headers
Octavian Rasnita 07:52 on 20 Mar 2005

Re: HTTP headers
David Dick 07:56 on 20 Mar 2005

Re: HTTP headers
Tuomo Salo 13:14 on 21 Mar 2005

Generated at 15:10 on 27 Mar 2005 by mariachi v0.52