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:

<!--#exec cgi="/path_to/increment" -->

Execution is allowed by this line in an .htaccess file in the directory /path_to/:

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

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
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
** 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?**

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

. 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.

Here’s the code before compiling:

#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:

headertext = "Content-type: text/html

"
shell headertext

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

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:

print "Content-type: text/html"
print ""
end

The directory where the executable is stored is treated differently now with this .htaccess:

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)