Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Premature end of script headers

by dsheroh (Monsignor)
on Sep 30, 2015 at 07:23 UTC ( [id://1143392]=note: print w/replies, xml ) Need Help??


in reply to Premature end of script headers

The one thing that a CGI script absolutely must do is emit a header block which contains a Content-Type header and ends with a blank line. If your code fails to do so, it triggers the "premature end of script headers" error.

So that's the problem. How do you debug it?

I'd start by looking at the apache logs a little more, to see if there are any obvious Perl/shell errors immediately before or after the "premature end of script headers" line. If there are no Perl/shell errors there, then the program ran successfully (or at least it thought it did) and exited cleanly, but the output didn't contain the required header block, so I'd run the code from the command line to verify that:

  1. The output includes the line Content-Type: text/html (or whatever the appropriate content type might be) and possibly some other headers
  2. The headers are followed by a blank line, even if they are the only output
  3. There are no non-header lines prior to the blank line
  4. The output described above is sent to STDOUT, not STDERR. (This is unlikely to be a problem in practice, since I would have already seen the output in the apache log if it was sent to STDERR. Still doesn't hurt to double-check, though.)
Actually, re-reading the question, it sounds like you may have an installation script which was intended for command-line use, and you're running it with CGI instead. If that's the case, then there's a good chance it ran just fine and the only problem is that it didn't bother to print the HTTP headers, since it didn't expect to be run in an environment where they're needed.

Replies are listed 'Best First'.
Re^2: Premature end of script headers
by gszabo (Novice) on Sep 30, 2015 at 10:05 UTC
    thank you for the reply.
    I run the script command-line, and found no error.
    perl -c -T /var/www/gestioip/install/index.cgi
    /var/www/gestioip/install/index.cgi syntax OK
      -c doesn't run the script, it just compiles it. Try running it without the option and check the output as described in the previous post.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        perl -d -T /var/www/gestioip/install/index.cgi

        Loading DB routines from perl5db.pl version 1.33
        Editor support available.

        Enter h or `h h' for help, or `man perldebug' for more help.

        main::(/var/www/gestioip/install/index.cgi:27):
        27: my $lang;

        But...
        try to put a test.cgi script in www dir with this :

        #!/bin/sh
        echo "Content-type: text/plain"
        echo
        set

        It generates the same error in apache error.log
        perl -d -T /var/www/gestioip/install/index.cgi

        Loading DB routines from perl5db.pl version 1.33
        Editor support available.

        main::(/var/www/gestioip/install/index.cgi:27):
        27: my $lang;

        But.... I try to put a test.cgi script in www dir with this :

        #!/bin/sh
        echo "Content-type: text/plain"
        echo
        set

        It generates the same error in error.log
      Hi
      It would be ideal if you had updated your post with the /gestioip/install/index.cgi source code, though it is easy to find on the Gestioip page.

      Assuming (based on what you say in Re^4: Premature end of script headers line 27 in your source file) is reffering to the code below we can access from their site ,Its likely there is a config issue which is causing the script to die before it can return the Content-Type Header Tag.

      I feel you must now also look in the logfile called setup.log mentioned in the Gestioip install_guide pdf, for any clues about errors/issues in config or during setup.
      Also im not sure which apache log u are referring to is it the error log or access log ?
      eg: tail -n 5 /var/log/apache~~/error.log
      #!/usr/bin/perl -T -w use strict; #comments till line 27 my $lang; if ( $ENV{'QUERY_STRING'} ) { $ENV{'QUERY_STRING'} =~ /.*lang=(\w{2}).*/; $lang=$1; my $fut_time=gmtime(time()+365*24*3600)." GMT"; my $cookie = "GestioIPLang=$lang; path=/; expires=$fut_time; 0 +"; print "Set-Cookie: " . $cookie . "\n"; } elsif ( $ENV{'HTTP_COOKIE'} ) { $ENV{'HTTP_COOKIE'} =~ /.*GestioIPLang=(\w{2}).*/; $lang=$1; } if ( ! $lang ) { $lang=$ENV{HTTP_ACCEPT_LANGUAGE}; $lang =~ /(^\w{2}).*/; $lang = $1; } my $config; if ( $lang eq "es" ) { $config="./vars_es"; } elsif ( $lang eq "en" ) { $config="./vars_en"; } elsif ( $lang eq "de" ) { $config="./vars_de"; } else { $config="./vars_es"; } open(CONFIG,"<$config") or die "can't open $config: $!"; my %preferences; while (<CONFIG>) { chomp; s/#.*//; s/^\s+//; s/\s+$//; next unless length; my ($var, $value) = split(/\s*=\s*/, $_, 2); $preferences{$var} = $value; } close CONFIG; print <<EOF; Content-type: text/html\n <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> <head><title>$preferences{title}</title>

      The Great Programmer is one who inspires others to code, not just one who writes great code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found