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* .............etc. Let's say you're doing a form rather than a normal document. The HTTP URL will identify a program or script rather than a HTML document. The program will be executed when you click on that link. Let's say you're on Computer A, looking at a document on Computer B, and you click on a link to a file stored on Computer C, except that link is for a CGI program instead of an HTML document. The file is stored on Computer C in such a way that the HTTP server on Computer C can tell that the file contains a program that is to be run instead of a document that is to be sent to the client to be displayed. When the program is run, the program creates an HTML document which is then sent to the client, which displays the document as it would any other HTML document. Such programs are called Common Gateway Interface or CGI scripts. They can be written in normal programming languages, such as Basic, Pascal, C, etc., or in special scripting languages, such as Perl, TCL, etc. Usually, these programs are stored in a directory called "cgi-bin". Here is a simple AppleScript program that can be run by a MacHTTP server when it receives a request for the file containing the script. When it runs, this program builds an HTML document containing the current time, and returns the document to the WWW client that requested it. set crlf to (ASCII character 13) & (ASCII character 10) set header to "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf set header to header & "MIME-Version: 1.0" & crlf & "Content-type: text/html" set header to header & crlf & crlf & "Server Script" set body to "

The time is:

" & (current date) & "

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* Server Script

The time is

September 2, 1998 3:30 pm

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.