Search code examples
chttp-headerscgiurl-routinglighttpd

How do I get headers from a LightTPD CGI app?


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?


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);
    }