Dynamic Documents (CGI)

CS 3000 -- Lab Assignment #10

Locating the CGI Directory

Observe the following interaction in which someone locates and examines the directory used by an http server for running CGI programs.



www.cs> ps -ax|grep http
  446  ??  Ss     1:18.94 /usr/sbin/httpd
10091  ??  S      0:00.34 /usr/sbin/httpd
10092  ??  S      0:00.24 /usr/sbin/httpd
10094  ??  S      0:00.30 /usr/sbin/httpd
10095  ??  S      0:00.25 /usr/sbin/httpd
10096  ??  S      0:00.24 /usr/sbin/httpd
10363  ??  S      0:00.01 /usr/sbin/httpd
10380  ??  S      0:00.01 /usr/sbin/httpd
10381  ??  S      0:00.02 /usr/sbin/httpd
10382  ??  S      0:00.00 /usr/sbin/httpd
 9940  p4  S+     0:00.01 /usr/local/bin/less httpd.conf
10384 std  R+     0:00.00 grep http

www.cs> ls -d /etc/http*
/etc/httpd/

www.cs> cd /etc/httpd

www.cs> ls
./                        httpd.conf.02.10.19       magic
../                       httpd.conf.1.35           magic.default
access.conf               httpd.conf.OSX2           mime.types
access.conf.default       httpd.conf.applesaved     mime.types.default
httpd.conf                httpd.conf.bak            srm.conf
httpd.conf.02.06.18       httpd.conf.default        srm.conf.default
httpd.conf.02.06.23       httpd.conf.dist           users/

www.cs> grep ScriptAlias httpd.conf
    # ScriptAlias: This controls which directories contain server scripts.
    # ScriptAliases are essentially the same as Aliases, except that
    # The same rules about trailing "/" apply to ScriptAlias directives as to
    ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"
    # "/Library/WebServer/CGI-Executables" should be changed to whatever your !
    # ScriptAliased directories, uncomment the following lines.

www.cs> cd /Library/WebServer/CGI-Executables/

www.cs> ls
./         count-cgi* ip-cgi*    printenv*
../        date-cgi*  ipaddrs    test-cgi*



What it all means

The web server is the program /usr/sbin/httpd. The output of the ps command above shows that there are several copies of the server running. The copy with process id number (PID) 446 is the parent server (mother). The others are child processes or threads created by the mother process. (I don't know exactly what their role is -- probably persistent service threads.)

The file /etc/httpd/httpd.conf is the configuration file for the web server. One of the lines I grep'd out of the config file was:

ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"

The meaning of the line is that when

"/cgi-bin/"

appears in a URL that is sent to this server, it is meant to be an alias for

"/Library/WebServer/CGI-Executables/"

It also means that the server will assume that all the files in /Library/WebServer/CGI-Executables/ are CGI scripts or programs. If all the permissions and the other configuration settings allow it, The http server will execute the program when you give it a URL of this form:

http://www.cs.csustan.edu/cgi-bin/[[program-name]]

The output of the program is supposed to be an HTML page, plain text, an image, or something like that -- something that an http client can accept, interpret, and display. The server will pass the output of the program to the client that made the request for the URL.

(For more information about CGI and the Apache Web Server, go here: http://httpd.apache.org/docs/howto/cgi.html. )

For example, the smallest of the scripts in /Library/WebServer/CGI-Executables/ is "date-cgi." Here is the content of the script:



#!/bin/sh
#
# CGI script that prints the date and time at which it was run
#
# Output the document header followed by a blank line

echo Content-type: text/plain
echo

# Output the date

echo
echo
echo
echo
echo
echo Welcome to WWW.CS.CSUSTAN.EDU!
echo
echo The local date and time is:
echo
echo `date`



The "ScriptAlias" directive in the httpd.conf file sets things up so that you can receive the output of the script if you use the URL:

http://www.cs.csustan.edu/cgi-bin/date-cgi

Try it: Click on the URL above and then hit the back button.

Try running the rest of the scripts. Comer's examples are here along with two that came with the web server. You can use the links below to look at the scripts and then run them to see what they do. (Remember you can get back here if you hit the back button enough times.)

  1. Contents of count-cgi (This script generates HTML)
    go to the URL http://www.cs.csustan.edu/cgi-bin/count-cgi

  2. Contents of date-cgi (This is the same as the example above)
    go to the URL http://www.cs.csustan.edu/cgi-bin/date-cgi

  3. Contents of ip-cgi (This script keeps track of where you connect from)
    go to the URL http://www.cs.csustan.edu/cgi-bin/ip-cgi

  4. Contents of printenv (This is a perl program that displays information in HTML form about the client, server, and request.)
    go to the URL http://www.cs.csustan.edu/cgi-bin/printenv

  5. Contents of test-cgi (This script is very simple but what it does is very similar to what the perl program does.)
    go to the URL http://www.cs.csustan.edu/cgi-bin/test-cgi

Using the Query String

A CGI program may create any web page whatever that the client might want to see. A great advantage of doing things this way is that the program can create something custom-tailored to the parameters that the client entered.

For example, if the client is shopping on-line the CGI program can generate a page to show the client slippers and pajamas -- or hats and coats -- whatever the client is interested in buying.

To see how this works, just click on the URL below to execute the CGI program with a non-empty query string, read the display, and then hit the back button to come back here. When you see the display, notice that the query string we sent to the server is in the display. This means that the server was able to see the query string and send it back to us.

http://www.cs.csustan.edu/cgi-bin/printenv?SHOW+ME+SLIPPERS+AND+HATS.

If we had really been online shopping the CGI script could have generated a page with slippers and hats for sale, and sent that to us. The query string it received from us contained enough information to let it know what we were interested is seeing.

Try this URL for a similar test with a different query string:

http://www.cs.csustan.edu/cgi-bin/printenv?SHOW+ME+BOTTLES+BRUSHES+AND+BANGLES

To Turn In:

Now just transform the example CGI script into a viable competitor of amazon.com, and e-mail me another million dollars. :-)