CGI Throughout the internet, there are forms in which you fill out, click enter, and the data is sent to somebody else. How does this happen? This is done by programs called "Common Gateway Interface" programs or CGI scripts. In order to understand how this works. You have to first understand how the internet works normally. Normally, the WWW client, such as Netscape or Lynx, requests a document from a WWW server, and displays it on your computer. If that document contains a link, and you click on it, the WWW client will then go off and get that document, and show that on your computer. Let's say you have a WWW client running on your computer, Computer A, and interacting with two servers, an HTTP server running on Computer B, and an HTTP server running on Computer C. The client running on Computer A gets a document, stored in a file named docu1.html, from the HTTP server running on Computer B. This document contains a link to another document, docu2.html, stored on Computer C. If you click on that link, the client retrieves the file from the HTTP server running on Computer C, and displays it on Computer A. The HyperText Transfer Protocol defines communication between the client and the HTTP server. The following example shows what a HTTP exchange between a Lynx client and an HTTP server running on Computer C might look like as the client fetches docu2.html. GET /docu2.html HTTP/1.0 Accept: www/source Accept: text/html Accept: image/gif User-Agent: Lynx/2.2 libwww/2.14 from: Aristotle2@goplay.com *a blank line* The "GET" request indicates while file the client wants, and announces that it is using HTTP version 1.0 to communicate. The client also lists the Multipurpose Internet Extension (MIME) types it will accept in return, and identifies itself as a Lynx client. The server then responds by sending the following. HTTP/1.0 200 OK Date: Wednesday, 02-Sep-98 23:04:12 GMT Server: NCSA/1.1 MIME-version: 1.0 Last-modified: Monday, 15-Nov-93 23:33:16 GMT Content-length: 2345 *a blank line*
return header and body The program is stored in a file named "date", in a folder called "scripts". When you click on the link that points to this script, the Web client will generate a HTTP request that looks like the following. GET /scripts/date HTTP/1.0 Accept: www/source Accept: text/html Accept: image/gif User-Agent: Lynx/2.2 libwww/2.14 From: Aristotle2@goplay.com *a blank line* When the script runs, it will generate an HTTP response that would look like this. HTTP/1.0 Server: MacHTTP MIME-Version: 1.0 Content-type: text/html *a blank line*
The addition of forms to the internet required various modifications. A set of tags was added to HTML to direct the WWW client to display the form to be filled out, and then forward the collected data to the HTTP server specified in the form. Servers were modified so that they could then start the CGI program specified in the form, and pass the collected data to that program, which could, in turn, prepare a response, and return a WWW document to the user. Let's say you're on Computer A. The Web client running on Computer A acquires a form from some Web server running on Computer B. It displays the form, you enter data, and the client sends the entered information to the HTTP server running on Computer C. There, the data is handed off to a CGI program which prepares a document, and sends it to the client on Computer A. The client then displays the document.