Standard CGI


OmniHTTPd has the ability to run both Standard CGI and Windows CGI scripts. Scripts must be placed in their respective directories so that the server can determine how to correctly execute the script. Use the administration tool to define these directories. Do not alias to these directories as an alias definition will override a CGI directory definition.

In addition, scripts must be placed in the base of the CGI directory. For example, if you have /cgi-bin defined as c:\httpd\cgi-bin, scripts must be placed in c:\httpd\cgi-bin. You cannot put them in a child directory like c:\httpd\cgi-bin\child\dir. This, however, does not prevent scripts from storing their own information in child directories.

This restriction allows OmniHTTPd to efficiently parse path information without the access overhead of attempting each possible path.

If you get errors when launching standard CGI scripts or not all environment variable are present, you are running out of DOS environment space. To fix this, add the following lines to your SYSTEM.INI:

[NonWindowsApp]
CommandEnvSize=8192

How do I get information from the server?

Each time a client requests the URL corresponding to your CGI program, the server will execute it in real-time. The output of your program will go more or less directly to the client.

A common misconception about CGI is that you can send command-line options and arguments to your program, such as

     command% myprog -qa blorf

CGI uses the command line for other purposes and thus this is not directly possible. Instead, CGI uses environment variables to send your program its parameters. The two major environment variables you will use for this purpose are:

How do I send my document back to the client?

I have found that the most common error in beginners' CGI programs is not properly formatting the output so the server can understand it.

CGI programs can return a myriad of document types. They can send back an image to the client, and HTML document, a plaintext document, or perhaps even an audio clip. They can also return references to other documents. The client must know what kind of document you're sending it so it can present it accordingly. In order for the client to know this, your CGI program must tell the server what type of document it is returning.

In order to tell the server what kind of document you are sending back, whether it be a full document or a reference to one, CGI requires you to place a short header on your output. This header is ASCII text, consisting of lines separated by either linefeeds or carriage returns (or both) followed by a single blank line. The output body then follows in whatever native format.

Advanced usage: If you would like to output headers such as Expires or Content-encoding, you can if your server is compatible with CGI/1.1. Just output them along with Location or Content-type and they will be sent back to the client.

Decoding FORMs with CGI

If you are unfamiliar with forms or how to write them, we suggest you look at this guide to fill-out forms. They're just plain HTML, and pretty easy to do.

Where do I get the form data from?

As you now know, there are two methods which can be used to access your forms. These methods are GET and POST. Depending on which method you used, you will receive the encoded results of the form in a different way.

How do I decode the form data?

When you write a form, each of your input items has a NAME tag. When the user places data in these items in the form, that information is encoded into the form data. The value each of the input items is given by the user is called the value.

Form data is a stream of name=value pairs separated by the & character. Each name=value pair is URL encoded, i.e. spaces are changed into plusses and some characters are encoded into hexadecimal.

Because others have been presented with this problem as well, there are already a number of programs which will do this decoding for you. The following are links into the CGI archive, clicking on them will retrieve the software package being referred to.

The basic procedure is to split the data by the ampersands. Then, for each name=value pair you get for this, you should URL decode the name, and then the value, and then do what you like with them.

Writing Secure CGI Scripts

Any time that a program is interacting with a networked client, there is the possibility of that client attacking the program to gain unauthorized access. Even the most innocent looking script can be very dangerous to the integrity of your system.

With that in mind, we would like to present a few guidelines to making sure your program does not come under attack.

For a more comprehensive summary of security and the World-Wide Web, see the WWW Security FAQ.


Back to the Extending OmniHTTPd functionality page

Copyright © 1996 Omnicron Technologies Corporation


Casa de Bender