CGI Data File format

The CGI data file has lots of information which is useful for your program run. It is in the Windows profile file format (normally used for .INI files), so you can use GetPrivateProfileString() to retrieve the relevant strings. There are up to 7 sections: [CGI], [Accept], [System], [Extra Headers], [Form Literal], [Form External] and [Form Huge]. Remember that you can call GetPrivateProfileString() with a NULL lpszEntry to enumerate all the keys within a section.

The [CGI] section contains all the Common Gateway Interface variables. Note that for DOS executables, these are passed as environment variables to the program. The Logical Path and Query String are the most immediately useful variables, as discussed earlier. I found that both DOS and Windows programs did not receive any of the authentication variables.

[Accept] lists the MIME types that the user's browser can accept, for example audio/wav=Yes. I presume that the entry */*=YES at the end of list provided by httpd means that you can send any type. However, note carefully that you have to set up httpd's MIME types file in order to be able to send different file types successfully.

The [System] section has a copy of the Output File path and the Content File path.

[Extra Headers] lists extra information that was provided with the request, eg User Agent=whatever.

Form results

The other sections of the CGI data file contain the values sent from the form, assuming you used the POST method. If a "key=value" string is less than 255 characters long then it is decoded and put directly in the [Form Literal] section. If it is a bit larger than this, or if there are any control characters, then it is decoded into an external temporary file; in which case the [Form External] section lists "key=pathname length". Finally if the string is more then 65535 bytes long then it is not decoded; however the [Form Huge] section lists "key=offset length" giving the offset of the raw value string in the Content File, together with its length. Make sure that you read the external and content files in binary mode.

In the example code, the GetFormValue() searches for a key in one of these sections. It always returns a huge pointer to retrieved value (even if the string is small). ReadBuf() is used to read data from a file - this copes with reading huge amounts of data. For [Form Huge] keys, DecodeValue() is called to decode the raw value string. For debugging purposes, EnumerateFormKeys() simply lists the available keys as HTML to the output file. EnumerateKeys() lists the Query String and Logical Path variables and calls EnumerateFormKeys() for each of the three [form] sections.

Output File format

One of the command line parameters to your program gives the Output File name. You should write your results here in the required format. There should be one or more special header lines, following by a blank line and then optionally the data.

To return HTML, you would write just one header line containing "Content-type: text/html"; the data would be the HTML that you want displayed. Optionally you can include a Status: special header, but httpd assumes a "200 OK" status if it is not given.

If your special header line is "Location: url" then the given URL is displayed instead.

Finally, you can return HTTP data directly to the browser, though this is not for the faint- hearted. You may have to begin your executable's name with "nph-" to make httpd realise that it should not package the data. Here is some example direct data:

HTTP/1.0 200 OK
Date: Wednesday, 26-Oct-94, 14:05:30 GMT
Server: NCSA/V1.3Pre/MSWin
Content-type: text/html
Last-modified: Wednesday, 26-Oct-94, 14:05:30 GMT
Content-length: 1123

<HTML>
...
</HTML>
You should be able to return other data types apart from HTML, but my brief attempts to do so have not worked.
Return to Web Forms