As you have seen previously, the Common Gateway Interface (CGI) provides a mechanism that allows a Web browser to provide information to a server, as part of a request for a document. In addition, a Web document can specify that a Web server should run a CGI script and program as part of a response for a document. One simple example of a cgi script is what-i-know-script.cgi, as shown below:
#!/usr/bin/csh /usr/local/bin/scm -q << ! (load "what-i-know-script.ss") !As explained in a previous lab, the primary purpose of this script is to run scm Scheme and load the Scheme program ~walker/public_html/cgi-bin/what-i-know-script.ss .
As you can see, the CGI script and program receives and prints information received from your browser. On our system, using SCM Scheme, the particular mechanism through which this information is transferred is a non-standard, predefined procedure named getenv, which takes a string as its argument and returns another string as its value.
Five strings that can be used as arguments are found in the what-i-know-script.ss program:
In outline, this full interaction works in several steps:
We now look at each of these steps in somewhat more detail. The html document /~walker/cgi-bin/fac-directory.html contains a special formatting element, called a form which sets up the blocks for data input and the buttons for responses and which specifies what action should accompany the user's typing.
When Henry is entered into the First Name box and Walker is entered into the Last Name box, then when the user clicks the submit button, a request is generated to the Web server -- as shown in step B above.
The fac-directory.cgi script loads program fac-directory.ss. Using SCM Scheme, fac-directory.cgi may recover this information using the procedure call
(getenv "QUERY_STRING")This call returns the string firstname=Henry&lastname=Walker, just as the previous calls to getenv returned other environmental variables. More generally, the procedure call (getenv "QUERY_STRING") looks at the URL that the browser used to activate the CGI program. If that URL just ends in .cgi, then (getenv "QUERY_STRING") returns #f. However, if there is a question mark after the .cgi, and a string of characters after that, then (getenv "QUERY_STRING") returns the characters in this additional string. The user can supply almost any kind of information to the CGI program through a query string, and the CGI program recovers it by decoding and parsing that string.
http://www.math.grin.edu/~yourUsername/cgi-bin/what-i-know-script.cgi?IlikePOPCORN!the program should include the following text in its output
IlikePOPCORN!
In CGI programming, a query string usually consists of a sequence of equations separated by ampersands, with some attribute on the left-hand side of each equation and the value of that attribute on the right-hand side. For instance, in our form example, the query string had the form firstname=Henry&lastname=Walker
Because the user often wants to supply attribute values that contain spaces, slashes, question marks, or other special characters that would wreak havoc if attached to URLs, CGI requires that such characters be encoded. The conventional encoding is to replace each space with a plus sign and each special character with a sequence of three characters beginning with a percent sign. The CGI program is expected to decode the strings recovered from the query string. This is usually done with the help of some ``library routine'' -- a procedure that someone else has written. In this lab, you'll find it convenient to use the extract-attributes procedure in http://www.walker.cs.grinnell.edu/public_html/cgi-bin/cgi-utilities.scm. This Scheme code, written by John Stone, takes a query string as argument and returns a list of pairs, with the car in each pair being a fully decoded attribute and the corresponding cdr being its fully decoded value:
(extract-attributes "firstname=Henry&lastname=Walker") ===> (("firstname" . "Henry") ("lastname" . "Walker"))In this form of an association list, a program can extract specific information for key values with the assoc procedure, as discussed in our previous lab on pairs and association lists.
Other processing for program fac-directory.ss combines ideas of processing files and generating html documents, as described in previous labs.
Work to be turned in:
For steps 3, 4, 8, 11, and 12, only a printout of the relevant files are needed. You need not show a run of the program (as any run will show up in your Web browser rather than in a window that you can record).
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153.sp00/lab-cgi-programming.html