CGI error 'Premature end of script headers'

Hi all,

I’m having a problem tying to get CGI to work. I am using Neko, a nice open source programming language, with which I produce an executable. You’re probably not familiar but the syntax is very easy:


class Main {
        static function main() {
                var fi = File.stdin();
                var str = '' + Sys.environment();
                var w = File.write("M:\\\\Apache2.2\\\\htdocs\\\	est.txt", true);
                w.writeString(str);
                w.close();
               
                var fo = File.stdout();
                fo.writeString("Content-type: text/html\\r\
\\r\
");
                fo.writeString("Hello world");
                fo.close();
        }
}

I compiled it to a index.exe, and then renamed it to index.cgi. Then I copied this file to the /htdocs directory of Apache and made sure I enabled cgi by specifying ‘Options +ExecCGI’ in the htaccess file.

When I tested the file by visiting http://localhost/index.cgi I got a ‘500 Internal Server Error’. The error logs revealed the following line: ‘[Sat Apr 10 10:43:28 2010] [error] [client 127.0.0.1] Premature end of script headers: index.cgi’

After consulting Google, I found out that the error would probably mean index.cgi doesn’t output the correct headers, but I’m pretty sure it does. Also, it should write a test.txt file if the file gets executed at all, because it happens before generating any output. However, no file is created which indicates that index.cgi doesn’t get run at all.

When I open a command prompt and execute the index.cgi manually, the test.txt file is created and the following (correct) output is shown:


Content-type: text/html

Hello world

Here are the relevant settings of my Apache config file:


..
<IfModule dir_module>
    DirectoryIndex index.cgi index.html
</IfModule>
..
<IfModule mime_module>
    AddHandler cgi-script .cgi
</IfModule>
...

I always thought you could use any executable file for CGI as long as it produces the correct output to stdout. Am I wrong at this? I have double checked all my settings and don’t understand what is wrong otherwise.

What could be wrong?

Regards,
Bas

Bas,

I thought that a cgi file needed to be a pl script, not an executable file. That said, I’m not surprised that you’re throwing an error. Executable (.exe) file? Won’t that only work on a WinDoze box, not a 'nix box? Okay, I don’t know but I’d recommend that you rework the PERL script for your “executable.”

Regards,

DK

I made a little perl script with a single ‘system’ call which, in turn, executes the executable. It seems to work: the environment variables get delivered to the executable as well as the stdin/stdout streams.

Thanks for your help!