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

Premature end of script headers

by gszabo (Novice)
on Sep 30, 2015 at 07:04 UTC ( [id://1143390]=perlquestion: print w/replies, xml ) Need Help??

gszabo has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I have installed an IPAM system (Gestioip)to my server ( Debian7, apache 2.2 ) it starts with an .sh script, it downloads the modules, copy the files, made config files.
The script downloads perl modules, and create an apache config. opening it in a browser generates this error in apache.log
Tue Sep 29 09:44:05 2015 error client 10.10.10.2 Premature end of script headers: index.cgi
Permissions of files, directories, the perl script is okay, i installed the program in virtual machine succesfuly in similar enviroment.
How can i debug this error?
Thanks for your help

Replies are listed 'Best First'.
Re: Premature end of script headers
by dsheroh (Monsignor) on Sep 30, 2015 at 07:23 UTC
    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.
      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.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        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
Re: Premature end of script headers
by Discipulus (Canon) on Sep 30, 2015 at 07:41 UTC
    A complete header is needed to have a succesfull http response. You must analyze what your cgi script sends back and inspect headers to check they are complete and valid. If some error occurs (MySQL connection?) before headers are emitted you can have such errors too. You can test the cgi in command line (bypassing the server completely), see Testing out a CGI script using the command line

    Might be that the server put some hands in header composition and the results is bad. In this case you can, empirically speaking, try to play with no-parse-header CGI mode and, eventually modifying the cgi code, tell the script to produce a complete and valid header.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      update
      The script is index.cgi, copied by the install.sh script. the script is okay, i can run it in a virtual debian with similar settings ( apache, perl version )
      Checked apache logs and found this line only nothing else.
      Tue Sep 29 08:55:54 2015] [info] [client 10.10.10.2] Connection closed + to child 3 with standard shutdown (server my.test.gestio.hu:443) [Tue Sep 29 08:55:56 2015] [info] [client 10.10.10.2] Connection to ch +ild 4 established (server my.test.gestio.hu:443) [Tue Sep 29 08:55:56 2015] [info] Seeding PRNG with 656 bytes of entro +py [Tue Sep 29 08:55:56 2015] [info] Initial (No.1) HTTPS request receive +d for child 4 (server my.test.gestio.hu:443) [Tue Sep 29 08:55:56 2015] [error] [client 10.10.10.2] Premature end o +f script headers: index.cgi [Tue Sep 29 08:55:56 2015] [info] [client 10.10.10.2] Connection close +d to child 4 with standard shutdown (server my.test.gestio.hu:443) [Tue Sep 29 09:43:56 2015] [info] [client 10.10.10.2] Connection to ch +ild 5 established (server my.test.gestio.hu:443) [Tue Sep 29 09:43:56 2015] [info] Seeding PRNG with 656 bytes of entro +py [Tue Sep 29 09:43:56 2015] [info] Initial (No.1) HTTPS request receive +d for child 5 (server my.test.gestio.hu:443) [Tue Sep 29 09:43:56 2015] [info] [client 10.10.10.2] Connection close +d to child 5 with standard shutdown (server my.test.gestio.hu:443) [Tue Sep 29 09:44:00 2015] [info] [client 10.10.10.2] Connection to ch +ild 9 established (server my.test.gestio.hu:443) [Tue Sep 29 09:44:00 2015] [info] Seeding PRNG with 656 bytes of entro +py [Tue Sep 29 09:44:05 2015] [info] Initial (No.1) HTTPS request receive +d for child 9 (server my.test.gestio.hu:443) [Tue Sep 29 09:44:05 2015] [error] [client 10.10.10.2] Premature end o +f script headers: index.cgi [Tue Sep 29 09:44:05 2015] [info] [client 10.10.10.2] Connection close +d to child 9 with standard shutdown (server my.test.gestio.hu:443)
      I try to run another .cgi perl script, but get the same error always.
      i do not use fasrcgi or suexec, i have read they could made similar errors.
      I have a goog running system as well with this script, so i just need to figure out the difference what cause this error.

        work through the check lists

Re: Premature end of script headers
by Anonymous Monk on Sep 30, 2015 at 07:11 UTC
Re: Premature end of script headers
by gszabo (Novice) on Sep 30, 2015 at 10:22 UTC
    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
Re: Premature end of script headers
by gszabo (Novice) on Oct 02, 2015 at 07:50 UTC
    Thank you for the help guys, finaly i solved it. That was the problem: http://blog.gmane.org/gmane.comp.apache.mod-log-sql/month=20080701

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found