Search code examples
ccgiquery-stringfastcgilighttpd

FastCGI and Query Strings (working in C)


Just a quick question about using FastCGI. I've been programming for about a month so far and I'm finding it very difficult to wrap my head around FastCGI.

Basically here are my questions.

  1. How exactly can I set my Query String?

  2. Is it even something I can "set" from a programming standpoint?

  3. If it is something I have to set, where would I do it?

Code:

 while(FCGI_Accept() >= 0) {

    if (getenv("QUERY_STRING") != NULL)
    {
        strcpy(query_string, getenv("QUERY_STRING"));

        if (query_count == 0)
        {
            printf("Content-type: text/html\r\n\r\n");
            printf("Query string has no data pairs!\n");
            continue;
        }
    }
    else
    {
        printf("Content-type: text/html\r\n\r\n");
        printf("Query string does not exist!\n");
        continue;
    }
}

The following code always returns NULL for getenv(QUERY_STRING).


Solution

  • All the form variables are passed from browser to http server in query string(for GET http request not for POST) A html page will typically have html form with various form variables eg

    <HTML>
    
     <BODY>
      <FORM METHOD=GET ACTION="http://localhost:8888/login.cgi">
      <input name="ID" type="text">
        <input name=submit type="submit" value="Submit">
    
      </FORM>
     </BODY>
    </HTML>
    

    The user will fillup form and submit typically by clicking on submit button(which is just another form variable), browser sends GET request to webserver identified by URL as http://localhost:8888/login.cgi?ID=aa&submit=Submit where localhost:8888 identifies the ip and port where webserver is running, /login.cgi tells web server which cgi is going to serve the given http request,query string is the text that follows ? ie ID=aa&submit=Submit.

    Webserver or (library provided by webserver bind into CGI) parses http request data including query string,http request headers and passes them as environment variables to cgi program. Query string is passed in env variable QUERY_STRING. The CGI programs are supposed to use these values in its application logic