Results 1 to 7 of 7

Thread: Apache error log "Premature end of script headers"

  1. #1
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Apache error log "Premature end of script headers"

    Hi

    Each time a Linux executable named "increment" is called from my web page the the error log records this error:
    [error] [client IP address] Premature end of script headers: increment
    The executable is not a script. It's a compiled executable. What it does is open a text file that contains an integer number, increase the integer by one and save the text file (yes, it's a "hit" counter). So it's not meant to send any information back to the originating web page.

    More background:

    The executable (called "increment") is located at /path_to/increment where /path_to/ is relative to the document root. It is called/triggered by this line in the web page:
    Code:
    <!--#exec cgi="/path_to/increment" -->
    Execution is allowed by this line in an .htaccess file in the directory /path_to/:
    Code:
    options +ExecCGI
    The counter executes properly, faithfully recording the page hits.

    What bothers me are the tens of thousands of error messages. Obviously I'm mis-using the ExecCGI thingy/mechanism somehow.

    What should I do to turn off the error messages?

    Thanks
    Swerdna
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Apache error log "Premature end of script headers"

    This error message usually means that the CGI program failed to output the HTTP headers required before the body of the reply, or end of connection. Among other things this header should contain:

    Content-Type: text/html (or whatever)

    and end with two \r\n groups. Then the body follows.

  3. #3
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Re: Apache error log "Premature end of script headers"

    Quote Originally Posted by ken_yap View Post
    This error message usually means that the CGI program failed to output the HTTP headers required before the body of the reply, or end of connection. Among other things this header should contain:

    Content-Type: text/html (or whatever)

    and end with two \r\n groups. Then the body follows.
    Thanks for the reply.

    I am not trying to write anything back to the calling web page. I'm only trying to run a conventional program. It's owned by uid=wwwrun gid=www. It writes to a text file, like a normal program. It doesn't write back to the web page (no stdout). So there are no HTTP headers to send.

    I understand that "exec cgi" creates an expectation of Apache that dynamic content will be returned to the page. And I understand that the error message is caused by me not sending any dynamic content back to be written to the web page.

    So I need to be able either
    [a] to call the executable program some other way (that doesn't create an expectation of dynamic content coming back)
    or
    [b] continue to use use the "exec cgi" method but switch off the error response.

    How can I run a script/executable that deliberately doesn't return dynamic content, without generating an error message?
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Apache error log "Premature end of script headers"

    Even if you don't want to send any content to the client you have to write the headers for the benefit of the webserver. The content begins after the \r\n\r\n. At that point you can close the connection and the client will receive zero bytes of data.

    Do a search for a simple CGI program. Even a shell script can serve as a CGI program, it only has to echo a couple of lines.

  5. #5
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Re: Apache error log "Premature end of script headers"

    Here's the code before compiling:
    Code:
    #include "file.bi"
    dim as integer icount, itest
    dim as string filetest, filename, shelltouchtxt, shellrmtxt
    filename=environ("DOCUMENT_NAME")+".counter"
    filetest = "aa." + filename
    shelltouchtxt = "touch " + filetest
    shellrmtxt = "rm " + filetest
    itest = FileExists(filetest)
    	if itest <> 0 then
    	end
    	end if
    shell shelltouchtxt
    open filename for input Lock Read Write as #1
    input #1, icount
    close #1
    icount=icount+1
    open filename for output Lock Read Write as #1
    write #1, icount
    close #1
    ' now do the pausing
    sleep 500, 1
    shell shellrmtxt
    end
    Do you mean that just before the final statement "end" I should put these lines:
    Code:
    headertext = "Content-type: text/html\r\n\r\n"
    shell headertext
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Apache error log "Premature end of script headers"

    No, it should be the very first thing you output, before anything is sent to stdout.

  7. #7
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Re: Apache error log "Premature end of script headers"

    Sorted now.

    I threw everything out (that had been cobbled together from internet examples) and started again -- so I would end up understanding what it is all about.

    I left the stdout to the very last thing before the "end" statement (because I don't want to "include" anything back on the web page).

    The last statements in the program are now:
    Code:
    print "Content-type: text/html"
    print ""
    end
    The directory where the executable is stored is treated differently now with this .htaccess:
    Code:
    Options +ExecCGI
    AddHandler cgi-script .lnx
    Thus only files with extension .lnx will execute. So of course I renamed the executable from "increment" to "increment.lnx"

    I threw out the old call to an executable from the web page: <!--#exec cgi="/path_to/increment" -->
    And replaced it with the better form: <!--#include virtual="/path_to/increment.lnx" -->

    And the error message disappeared.

    So thanks very much for your help (as always -- wise counsel)
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •