Example CGI Script

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.
yokuts.csustan.edu> fortune -a
Economics is extremely useful as a form of employment for economists.
                -- John Kenneth Galbraith

yokuts.csustan.edu> ps -ax |grep http
  193 ?  SW    0:01  (httpd)
15261 ?  S     0:02 /usr/local/etc/httpd/httpd -d /usr/local/etc/httpd
16358 ?  S     0:02 /usr/local/etc/httpd/httpd -d /usr/local/etc/httpd
16382 ?  S     0:02 /usr/local/etc/httpd/httpd -d /usr/local/etc/httpd
17082 ?  S     0:01 /usr/local/etc/httpd/httpd -d /usr/local/etc/httpd
21611 p1 S     0:00 grep http
yokuts.csustan.edu> cd /usr/local/etc 
yokuts.csustan.edu> ls
./                gopherd*          gopherls@         uncache*
../               gopherd.conf      httpd/            visudo*
gindexd@          gopherd.conf.old  httpd.csustan/    visudo.11.6.95*
gn*               gopherd.old*      mkcache*
yokuts.csustan.edu> cd httpd
yokuts.csustan.edu> ls
./                   conf/                httpd.yahi.7.28.97*
../                  conf.yahi.7.28.97/   icons/
Makefile             graphics/            logs/
README               htdocs/              src/
cgi-bin/             htdocs.5.22.97/      support/
cgi-src/             httpd*               web.old.docs/

REMARK:

Notice the directory cgi-bin listed above. This is a special directory known to the http server. If a URL references any file in this directory, the http server will assume that the file is a program. Assuming that all the permissons and configuration settings allow it, The http server will execute the program.

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.

The configuration of httpd on the local host (yokuts) stipulates that /usr/local/etc/httpd is the root directory. Therefore we use the relative path cgi-bin in a URL to access /usr/local/etc/httpd/cgi-bin.
yokuts.csustan.edu> ls cgi-bin
./                finger*           kgustavs.cgi@     uptime*
../               fortune*          mail*             vankirk.cgi/
BBcogs.cgi@       hello.pl@         nph-test-cgi*     vmlaker.cgi@
BBcs.cgi@         jnetniss.cgi/     rouss.cgi/        w3-msql*
archie*           john.cgi/         test-cgi*         wais.pl*
calendar*         julie.cgi@        test-cgi.tcl*
date*             julieCS1000.cgi@  test-env*
yokuts.csustan.edu> ls cgi-bin/john.cgi
./         ../        test-cgi*
yokuts.csustan.edu> file cgi-bin/john.cgi
cgi-bin/john.cgi:       symbolic link to /Net/ishi/clients/Users/staff/john/public_html/cgi

REMARK:

The output of the file command above shows that /usr/local/etc/httpd/cgi-bin/john.cgi is a symbolic link to /Net/ishi/clients/Users/staff/john/public_html/cgi, which is inside john's home directory.

This is a security feature as well as a convenience feature.

There is a single directory where all client-accessible cgi programs are kept. The permissions and other attributes of the programs can be monitored here with relative ease.

On the other hand, user john may conveniently add more cgi programs merely by making additions to his own directory.
yokuts.csustan.edu> cat cgi-bin/john.cgi/test-cgi
#!/bin/sh

echo Content-type: text/plain
echo

echo CGI/1.0 test script report:
echo

echo argc is $#. argv is "$*".
echo

echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = "$PATH_INFO"
echo PATH_TRANSLATED = "$PATH_TRANSLATED"
echo SCRIPT_NAME = "$SCRIPT_NAME"
echo QUERY_STRING = "$QUERY_STRING"
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo AUTH_TYPE = $AUTH_TYPE
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH
finger
yokuts.csustan.edu> 

Contents of the CGI Program

In this case the CGI program is just a shell script that (1) tells what parameters were passed from the client, (2) tells the settings of various environment variables, and (3) runs the finger command on the server host machine.

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.

Executing CGI Programs

When ready, just click here on this URL to execute the cgi program: http://yokuts.csustan.edu/cgi-bin/john.cgi/test-cgi?SHOW+ME+SLIPPERS+AND+HATS.

You will need to click the "back button" to return here to this document after you have finished looking at the output of the cgi command.

Try this URL for executing a similar test cgi program on another server: http://www.cs.csustan.edu/cgi-bin/printenv?SHOW+ME+BOTTLES+BRUSHES+AND+BANGLES.

Once again, you will need to click the "back button" to return here to this document after you have finished looking at the output of the cgi command.

To Turn In:

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