Question

I have this simple CGI script (which I compile):

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Content-Type: text/plain\n\nHello world!\n");
    for(register unsigned short i=argc; i>0;) {
        --i; printf("argv[%d]=%s ", i, argv[i]);
    }
}

But I can't figure out how to get any of the header information.

Here are the differences I've made to my default lighttpd.conf:

server.modules = ("mod_access", "mod_accesslog", "mod_alias",
                  "mod_cgi", "mod_compress",  "mod_status", )

server.port    = 8000

#### CGI module
$HTTP["url"]   =~ "/cgi-bin/" {
    cgi.assign =  ( "" => "" )
}
cgi.assign     =  ( ".cgi"  => "")

How do I get header information and arguments?

BTW: Is this the most efficient way of serving compiled C code?

Was it helpful?

Solution

Both @Stefan's links are broken, so decided to write the answer I found from research:

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]) {
    printf("Content-Type: text/plain\n\nHello world!\n");
    const char *method_str = getenv("REQUEST_METHOD");
    for(register unsigned short i=argc; i>0;) {
        --i; printf("argv[%d]=%s ", i, argv[i]);
    }
    printf("REQUEST_METHOD = %s", method_str);
}

OTHER TIPS

Just read how CGI works. FastCGI is a variant that keeps the backend process running for multiple requests and doesn't involve forking on every request. There are many similar protocols (SCGI, uwsgi, ...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top