SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Code:
    class Main {
            static function main() {
                    var fi = File.stdin();
                    var str = '' + Sys.environment();
                    var w = File.write("M:\\Apache2.2\\htdocs\\test.txt", true);
                    w.writeString(str);
                    w.close();
                   
                    var fo = File.stdout();
                    fo.writeString("Content-type: text/html\r\n\r\n");
                    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:
    Code:
    Content-type: text/html
    
    Hello world
    Here are the relevant settings of my Apache config file:
    Code:
    ..
    <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

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,679
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

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