WWW: Beyond the Basics

12. Common Gateway Interface

12.1. Introduction

The Common Gateway Interface (CGI) is a specification that allows World Wide Web (WWW) pages to be created dynamically by applications launched by an HTTP server. Essentially, the CGI allows people to write programs that generate HTML code, which can then be viewed by a World Wide Web browser. Such programs are often called CGI applications, CGI-apps, CGI-bins, or simply CGIs. Initially, the CGI specification was written to allow people to access remote applications using the World Wide Web. But, as the World Wide Web is now widely considered to be the "universal front-end," the focus for using the Common Gateway Interface has shifted slightly, from the remote application to the appearance of the HTML pages that CGI applications produce.

A World Wide Web server has a set of legal URLs that it makes available to WWW clients. Every legal URL that a server provides corresponds to a file on that server. Normally, those files are images, HTML files, or other static documents. When a server is asked to supply such a static document, it simply returns the document's contents. However, when a client asks for a URL that points to a CGI application, the following things happen:

  1. The WWW client sends a request to the HTTP server.
    This request gives the name and location of the CGI application, and can provide parameters to the application (See Section 12.2.3).
  2. The HTTP server starts the application.
    The server provides some extra information to the CGI application, including any parameters passed by the client.
  3. When the CGI application terminates, all output produced by the application is returned to the client.
    Some additional information is added to the HTTP header produced by the CGI (see Object Header lines in HTTP) before the output is returned.

The client treats the output from the CGI-app the same as it would if the data had come from a static file. So, if a CGI-app produces HTML, it will appear the same, when viewed by the client, as if the HTML had been stored statically. A CGI application isn't limited to creating HTML pages, though -- it can produce any type of data, including images and sound files.

Sometimes, however, the output from a CGI application is secondary to the CGI-app's side effects (see Section 12.2.4). CGI applications can be written to perform other actions on the server in addition to producing HTML pages. If a CGI-app is constructed correctly, it can act as a gateway to other applications (in fact, this is why the Common Gateway Interface is named as it is). For example: there are many telephone directory services available on the World Wide Web, such as Four11 Directory Services. These services maintain large databases which contain publicly listed names, addresses, and telephone numbers. Users can access the information in these databases over the World Wide Web through a CGI-based front-end. When a user asks for information on a person, they send a query to a CGI application that acts as an interface to the database. This CGI application accesses the databse and generates a WWW page containing the information requested in the query.

[PREV][NEXT][UP][HOME][VT CS]

Copyright © 1996 J. Patrick Van Metre, All Rights Reserved

J. Patrick Van Metre <vanmetre@csgrad.cs.vt.edu>
Last modified: Sat Oct 26 13:26:04 1996