CGI Operation

CGI program response and Name/Value pairs

Provides more detail about CGI program response and name value pairs during CGI operation. Data sent from forms, as you can see from the example on the previous page, has the following characteristics:

  • Information is sent in the form of a name and value pair.
  • Each name/value pair begins with an & sign.
  • The name/value pair are seperated by the equals (=) sign between the name and the value.
  • They are URL encoded.

URL encoding changes some characters to placeholders and substitutes a hexadecimal ASCII value for some characters. To process the data in the server follow this sequence:

  1. Get the data from the proper variable depending on whether the POST or GET method was used.
  2. Change any placeholders to their correct values (Change any + signs to spaces).
  3. Split each group of name value pairs into an array of strings.
  4. Convert hexadecimal values back to ASCII character equivalents.
  5. Evaluate the respective names and values.

See the "Poll Receiving" section of the Perl manual in this CGI section for an example of this process.

Responding to the Client

Any output to be returned to the client web browser can be sent to the standard output of the CGI program as in the following line:

print "Content-type: text/html", "\n\n<P>This will appear on the clients' web browser.</P>"; # MIME header.

Two blank lines must be placed after the parsed header.

The CGI application must send a parsed header to it's standard output which is intercepted by the web server. From the parsed header, the web server will create the required non-parsed HTTP response header to be sent to the client. The parsed header must contain a server directive which may include one or more of:

  • Content-type - Indicates the MIME type of the data being sent back to the client.
  • Location - The URL the client web browser should be directed to.
  • Status - A HTTP status code. Common codes are 200 (OK) and 404 (not found).

The CGI application may also send a HTTP response header. The server creates a non-parsed header from the CGI parsed information and sends it to the client. The following is a valid non-parsed header: "

HTTP/1.0 200 OK
Server: Netscape-Communications/3.0
Content-type: text/html

Using the client to call a CGI application

Methods for a client to call a CGI application:

  • An action attribute in an HTML FORM tag specifies the CGI application to run when the user presses the FORM submit button. As described in the introduction, the POST or GET methods may be used to send the data. The data is sent as part of the standard input or as the QUERY_STRING if POST or GET are used respectively.
  • Server-side includes. The Form of a SSI statement as found on an HTML page:

    <!--#exec cgi="/cgi-bin/"-->

    The output from the server side include script is plcaed into the HTML page at the location where the SSI call was made. See the SSI manual for maor information on server side includes.

  • An HTML IMG tag may be used to indirectly call the CGI application with a line like the following:

    <IMG SRC="/cgi-bin/image">

    It is expected that the CGI program will generate an image to be sent back to the client web browser. See the "Printing a File" section in the Perl manual for an example about how to do this.

  • Client side script programs embedded into the HTML page such as JavaScript may be used to call a CGI application program.