Lecture Notes for Client-Server Interfaces

29 March 2001 - Fun with CGI


A CGI Script

cs date
Thu Mar 29 15:52:49 EST 2001

cs uname -a
SunOS cslab16.cs.monmouth.edu 5.8 Generic sun4u sparc SUNW,Ultra-5_10

cs pwd 
/export/opt/cs-537/p3/thttpd/thttpd-2.20b

cs cat cgi-bin/printenv        
#!/bin/sh

date=date -u +%a, %d %b %Y %H:%M:%S %Z'

cat << EOF
Content-type: text/plain
Expires: $date

CGI printenv

EOF

echo Date:'
date
echo
echo 'Id:'
id
echo
echo 'Env:'
printenv
echo
if [ "$CONTENT_LENGTH" != "" ] ; then
    if [ "$CONTENT_LENGTH" -ne 0 ] ; then
        echo 'Input:'
        echo
        dd bs=1 count=$CONTENT_LENGTH
        echo
    fi
fi

cs 


Running the Server

*cs thttpd -p 1537 -D

 ---

*cl telnet cslab16 1537
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*GET /cgi-bin/printenv HTTP/1.0
*
 HTTP/1.0 403 Forbidden
 Server: thttpd/2.20b 10oct00
 Content-type: text/html
 Date: Thu, 29 Mar 2001 20:56:38 GMT
 Last-modified: Thu, 29 Mar 2001 20:56:38 GMT
 Accept-Ranges: bytes
 Connection: close

 <HTML><HEAD><TITLE>403 Forbidden</TITLE></HEAD>
 <BODY BGCOLOR="#cc9999"><H2>403 Forbidden</H2>
 The requested URL '/cgi-bin/printenv' resolves<br>
 to a file which is marked executable but is not <br>
 a CGI file; retrieving it is forbidden.
 <HR>
 <ADDRESS>
 <A HREF="http://www.acme.com/software/thttpd/">
 thttpd/2.20b 10oct00</A></ADDRESS>
 </BODY></HTML>
 Connection closed by foreign host.

 cl 


Running the Server Correctly

*cs thttpd -p 1537 -c '/cgi-bin/**' -D

--

*cl telnet cslab16 1537
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*GET /cgi-bin/printenv HTTP/1.0
*
 HTTP/1.0 200 OK
 Content-type: text/plain
 Expires: Thu, 29 Mar 2001 21:01:02 GMT

 CGI printenv

 Date:
 Thu Mar 29 16:01:02 EST 2001

 Id:
 uid=890(rclayton) gid=51(faculty)

 Env:
 AUTH_TYPE=Basic
 CGI_PATTERN=cgi-bin/**
 GATEWAY_INTERFACE=CGI/1.1
 PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin
 REMOTE_ADDR=10.1.12.4
 REQUEST_METHOD=GET
 SCRIPT_NAME=/cgi-bin/printenv
 SERVER_NAME=cslab16.cs.monmouth.edu
 SERVER_PORT=1537
 SERVER_PROTOCOL=HTTP/1.0
 SERVER_SOFTWARE=thttpd/2.20b 10oct00
 TZ=US/Eastern

 Connection closed by foreign host.

 cl 


Passing Information via a GET

*cl telnet cslab16 1537
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*GET /cgi-bin/printenv?name=clayton&flavor=butterscotch HTTP/1.0
*
 HTTP/1.0 200 OK
 Content-type: text/plain
 Expires: Thu, 29 Mar 2001 21:05:20 GMT

 CGI printenv

 Date:
 Thu Mar 29 16:05:20 EST 2001

 Id:
 uid=890(rclayton) gid=51(faculty)

 Env:
 AUTH_TYPE=Basic
 CGI_PATTERN=cgi-bin/**
 GATEWAY_INTERFACE=CGI/1.1
 PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin
 QUERY_STRING=name=clayton&flavor=butterscotch
 REMOTE_ADDR=10.1.12.4
 REQUEST_METHOD=GET
 SCRIPT_NAME=/cgi-bin/printenv
 SERVER_NAME=cslab16.cs.monmouth.edu
 SERVER_PORT=1537
 SERVER_PROTOCOL=HTTP/1.0
 SERVER_SOFTWARE=thttpd/2.20b 10oct00
 TZ=US/Eastern

 Connection closed by foreign host.

 cl 


Passing Information by POST

*cl telnet cslab16 1537
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*POST /cgi-bin/printenv HTTP/1.0
*Content-Length: 5
*
 HTTP/1.0 200 OK
 Content-type: text/plain
 Expires: Thu, 29 Mar 2001 22:14:44 GMT

 CGI printenv

 Date:
 Thu Mar 29 17:14:44 EST 2001

 Id:
 uid=890(rclayton) gid=51(faculty)

 Env:
 AUTH_TYPE=Basic
 CGI_PATTERN=cgi-bin/**
 CONTENT_LENGTH=5
 GATEWAY_INTERFACE=CGI/1.1
 PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin
 REMOTE_ADDR=10.1.12.4
 REQUEST_METHOD=POST
 SCRIPT_NAME=/cgi-bin/printenv
 SERVER_NAME=cslab16.cs.monmouth.edu
 SERVER_PORT=1537
 SERVER_PROTOCOL=HTTP/1.0
 SERVER_SOFTWARE=thttpd/2.20b 10oct00
 TZ=US/Eastern

 Input:

*Hello
 Hello5+0 records in
 5+0 records out

 Connection closed by foreign host.

 cl 


Passing Information by Content-Type

*cl telnet cslab16 1537
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*GET /cgi-bin/printenv HTTP/1.0
*Content-Type: text/plain ; name=clayton color=green
*
 HTTP/1.0 200 OK
 Content-type: text/plain
 Expires: Thu, 29 Mar 2001 22:18:18 GMT

 CGI printenv

 Date:
 Thu Mar 29 17:18:18 EST 2001

 Id:
 uid=890(rclayton) gid=51(faculty)

 Env:
 AUTH_TYPE=Basic
 CGI_PATTERN=cgi-bin/**
 CONTENT_TYPE=text/plain ; name=clayton color=green
 GATEWAY_INTERFACE=CGI/1.1
 PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin
 REMOTE_ADDR=10.1.12.4
 REQUEST_METHOD=GET
 SCRIPT_NAME=/cgi-bin/printenv
 SERVER_NAME=cslab16.cs.monmouth.edu
 SERVER_PORT=1537
 SERVER_PROTOCOL=HTTP/1.0
 SERVER_SOFTWARE=thttpd/2.20b 10oct00
 TZ=US/Eastern

 Connection closed by foreign host.

 cl 


Not Passing Information by Header Lines

*cl telnet cslab16 1537                  
 Trying 10.1.13.25...
 Connected to cslab16.cs.monmouth.edu.
 Escape character is '^]'.
*GET /cgi-bin/printenv HTTP/1.0
*From: joe blow
*
 HTTP/1.0 200 OK
 Content-type: text/plain
 Expires: Thu, 29 Mar 2001 22:22:31 GMT

 CGI printenv

 Date:
 Thu Mar 29 17:22:31 EST 2001

 Id:
 uid=890(rclayton) gid=51(faculty)

 Env:
 AUTH_TYPE=Basic
 CGI_PATTERN=cgi-bin/**
 GATEWAY_INTERFACE=CGI/1.1
 PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin
 REMOTE_ADDR=10.1.12.4
 REQUEST_METHOD=GET
 SCRIPT_NAME=/cgi-bin/printenv
 SERVER_NAME=cslab16.cs.monmouth.edu
 SERVER_PORT=1537
 SERVER_PROTOCOL=HTTP/1.0
 SERVER_SOFTWARE=thttpd/2.20b 10oct00
 TZ=US/Eastern

 Connection closed by foreign host.

 cl 


This page last modified on 10 April 2001.