CGI Scripts

Flow of a CGI Script

An overview of the interactions of clients, servers, and scripts might be helpful. This overview is based on the following example. A company's Web site provides a Web page that gives a short survey to anyone who accesses their site. The survey asks two questions:
  1. Do you prefer to see images on Web pages?
  2. On Web pages with images, do you prefer GIF or JPEG images?
Each person that responds gets added to a database and the current percentages of answers to each question are then returned to the customer's browser.

Now might be a good time to give this sample a try.

What happened when you submitted your answers to the survey? Many things. The above link brought up the HTML document /samples/scripts.htm. This document contained introductory text plus an HTML form. An HTML form consists of special markup that allows for user input to be gathered by the browser and sent back to a script on the Web server.

After the Web document was displayed, you made your survey selections and pressed the "Submit" button. Doing this caused the browser to gather your answers and submit another request to the Web server. This request consisted of a URL that pointed to a specific script (which was listed in part of the special HTML form markup), along with the data gathered from the HTML form.

The Web server then received the script URL and the form data. The server set up some data structures and called the script program. The script made several calls to Application Program Interfaces (APIs) supplied by Web Server/400. These APIs provided the script with the form data and many pieces of data relating to the request. The script wrote out the form data to a database and tallied the current percentages. The script then dynamically created an HTML page. This HTML data was relayed to the Web server through other Web Server/400 APIs. The script program then ended. When the script ended, control was passed back to the Web server to finish the request. The Web server then sent back to the browser all the data dynamically created by the script. The browser then received the Web page consisting of the new survey results and displayed it.

The above steps can be summarized by the following:

  1. Display initial HTML form.
  2. User fills in form and submits the request.
  3. Browser requests the script URL and provides the form data.
  4. Server receives request and prepares script input data.
  5. Server calls script program.
  6. Script receives input from Web Server/400 APIs.
  7. Script performs any useful processing.
  8. Script prepares its output.
  9. Script returns its output to the server through Web Server/400 APIs.
  10. Script program ends.
  11. Server sends back the script output to the browser.
  12. Browser displays new output.

Script Concepts

The sections below cover script creation and the interactions between the Web server, the browser, and a CGI script.

CGI Environment Variables
The request headers sent by the browser are communicated to the script through a set of pre-defined and browser-specific pseudo-environment variables.

Script Inputs
Input to a script is received through environment variables, standard input, or command-line arguments.

Script Outputs
A script can send data and instructions back to the browser through response headers and the object body.

Script APIs
Web Server/400 supplies a set of Application Program Interfaces (APIs) that allow user-written scripts to interact with the Web server to successfully handle an HTTP request.

Creating a Script Program
Some special instructions need to be followed to make your script available to browsers and to make it function properly.