Search code examples
apacheexecutablecgi-binquery-string

Passing CGI arguments to an executable in Apache on Windows


I was under the impression that I could put any old executable program in the cgi-bin directory of Apache and have it be used as a CGI script. Specifically, if I have a C# program

static class TestProg
{
    static void Main(string[] args)
    {
        Console.Write("Content-type: text/plain\r\n\r\n");
        Console.WriteLine("Arguments:");
        foreach (string arg in args)
            Console.WriteLine(arg);
    }
}

and then go to http://example.com/cgi-bin/TestProg?hello=kitty&goodbye=world then the query string hello=kitty&goodbye=world would be passed as the first parameter to main, so my page should look like

Arguments:
hello=kitty&goodbye=world

Unfortunately, none of my query parameters are getting passed; the page loads and just prints Arguments: with nothing following it.

So how do I get my query parameters passed to this program?


Solution

  • Arguments are not passed on the command line - instead, apache sets environment variables before the cgi program is called (http://httpd.apache.org/docs/2.0/howto/cgi.html#behindscenes).

    You can access the environment variable 'QUERY_STRING' which contains the text of the query string.

     String queryString = System.Environment.GetEnvironmentVariable("QUERY_STRING");
    

    You will then need to parse queryString yourself.

    POST data, however, is passed over STDIN, so you will need to use Console.In to process it.