Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Apache/CGI error: "Premature end of script headers"

by Callum (Chaplain)
on May 18, 2004 at 10:42 UTC ( [id://354216]=perlquestion: print w/replies, xml ) Need Help??

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

This may be OT, as I've got a nagging feeling it's going to end up being an apache problem, if so my appologies.

We moved one of our small websites to a new server a week or so ago, everything seemed hunky-dory at the time, I go away for a week and come back to find that "nothing's changed but none of our CGI scripts work any more" *sigh*.

What we're getting is that loveliest of apache error messages "premature end of script headers" for our perl CGI scripts, I'm running out of things to try, and I'm pretty sure I've covered everything that searching PM turned up that was relevant.

What we have is this:

A trivial test cgi script:

(cgi_test)% cat test.cgi #!/WWW/bin/perl -wT $|=1; print "Content-type: text/html\n\n"; print "hello\n";

which, when called through a browser, errors, leaving the following in the logs:

in http.log: whars0ck.europe.nortel.com - 0502164 [18/May/2004:10:04:28 +0100] "GET + /~callum/cgi_test/test.cgi HTTP/1.1" 500 403 in http-error.log: [Tue May 18 10:04:28 2004] [error] [client 47.165.6.42] Premature end +of script headers: /u/callum/public_html/cgi_test/test.cgi
Error handling is done via bourne shell cgi scripts (in order to produce more user friendly error messages, etc), which work fine:
ErrorDocument 403 /admin/error/403.cgi
Is giving the "500:Server configuration error, whinge to Callum" message that I'd expect it to, and changing the name of the person to complain to dosn't appear to fix the problem :)

The test script works fine on the command line of the webserver:

(logs)% /u/callum/public_html/cgi_test/test.cgi Content-type: text/html hello (logs)%

The http error code 403 Forbidden usually means a permissions problem, but I don't believe that's the case here: the permissions on /WWW/bin/perl are 555, the permissions on test.cgi are 755, everyone has read and execute permissions (ie, ugo+rx) to all directories up to and including cgi_test/

The webserver and the workstation where I'm editing stuff and running the browser are both Unix boxes, and the editing's being done in vi -- so no CRLFs or such nonsense.

The script is being recognised as CGI, in particular the webserver is returning an error code 500, but it dosn't appear to execute it -- change the script so the first thing it does is touch a file in /tmp, works fine on the command line, no result through the browser other than the same errors. There is a handler set up for cgi scripts:

AddHandler cgi-script .cgi .sh
And as mentioned earlier the bourne shell error handling scripts are working fine.

I think I've included everything I've looked at -- does anyone have any suggestions as to what might be going wrong?

Replies are listed 'Best First'.
Re: Apache/CGI error: "Premature end of script headers"
by dave_the_m (Monsignor) on May 18, 2004 at 11:27 UTC
    I'd recommend starting Apache in debug mode (where it runs in the foreground and doesn't fork) using strace/truss -f and see what Apache's doing (or not doing).
      Good suggestion, what I see is:

      apache starts up and does what I'd expect
      it receives a GET for the test script
      it stats test.cgi and says yep
      it looks for and finds an .htaccess file in the path, then authenticates okay
      it forks, does a couple of lwp things, a bunch of memory things, and creates a door
      one error during this, an invalid argument:

      lwp_schedctl(SC_DOOR, 0, 0x00000000) Err#22 EINVAL
      a bunch more lwp stuff, and a little more memory stuff
      then goes:
      chdir("/u/callum/public_html/cgi_test") = 0
      then 7 closes one after another, before:
      door_info(4, 0xEFFFAE70) = 0 door_call(4, 0xEFFFAE58) = 0 door_info(4, 0xEFFF9270) = 0 door_call(4, 0xEFFF9258) = 0 *** process killed ***
      And the child dies

      Then the error handling kicks in:

      Errors are written to logs
      The 500 error handler script is invoked
      lwp, memory, and door stuff, the same EINVAL error
      chdirs to the directory with the error handling script in
      a bunch of closes one after the other
      then it executes the error handling script as it should.

      So, it can stat the script and chdir to the directory, then seems to die for no reason that's evident to me.

      Time I suspect to wade line by line through the truss output I guess.

•Re: Apache/CGI error: "Premature end of script headers"
by merlyn (Sage) on May 18, 2004 at 14:40 UTC
    I'd get away from the Perl part for a second, and try basic CGI functions:
    #!/bin/sh echo content-type: text/plain echo date
    Put this somewhere, and if you don't get the date every time you reload, your problem clearly isn't Perl.

    Your shebang line also looks weirdly weird as well. Are you sure that's the path to Perl?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I've tried something similar in order to confirm what was being seen with the the error handling scripts as I wasn't certain that they would be being invoked in the same way as general CGI, to verify the user it was running as and that it was running each time -- each time I run the datetest.cgi bourne shell script it displays the date and writes it to a file (as the user it's supposed to be running as).

      As for the path to perl,

      (bin)% /WWW/bin/perl -ver This is perl, v5.6.1 built for sun4-solaris Copyright 1987-2001...
      that, plus that it runs the scripts when executed on the command line, makes me pretty sure it's perl :)

      We have a web-server specific perl installation which is only used by the webserver.

Re: Apache/CGI error: "Premature end of script headers"
by itub (Priest) on May 18, 2004 at 15:04 UTC

    Are you using suexec? I think it dies when the path to the CGI doesn't match a whitelist that was included during compilation, or when there's any kind of mismatch in the ownership or permissions of the script.

Re: Apache/CGI error: "Premature end of script headers"
by Callum (Chaplain) on May 18, 2004 at 15:37 UTC
    Partial resolution -- a previously unnoticed error (not appearing in any of the logs) proclaimed "Cannot find ELF", which according to google is a consequence of GNU's strip not play nicely with Solaris.

    So we've now got things working, just need to figure out what went wrong before.

    Thanks for your suggestions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found