Testing your own CGIs

Unfortunately, we don't allow just anyone to install their own CGI programs or scripts. So this presents a little bit of a problem for people who want to learn how to write or test CGI programs.

Until we decide on what tack to take:

  • A CGI wrapper running on the main server
  • A separate CGI server
  • Or other...
  • you'll have to use a mini server.

    Setting up a personal mini server

    The mini server is in /usr/local/www/support/tinyhttpd.pl and is only slightly different from tinyhttpd.pl by Olaf Titz.

    What directories you'll need

    You'll need (if you don't have them already) a ~/public_html and ~/public_html/cgi-bin directory.
    	mkdir ~/public_html
    	mkdir ~/public_html/cgi-bin
    	chmod 711 ~/public_html ~/public_html/cgi-bin
    

    The ~/public_html directory contains your standard HTML files and is also accessible via http://www.csi.uottawa.ca/~username (substitute your username for username).

    The ~public_html/cgi-bin directory contains your CGI files (programs and scripts) and (currently) will only be accessible via your personal mini server.

    You'll only need to do the chmod line if you also want your files accessible by the departmental WWW server.

    Starting the mini server

    You need to select a machine and a port for your server. For this example, we'll say that you want to use port 1234 on grda:
    	rsh -n grda /usr/local/www/support/tinyhttpd.pl 1234
    
    That's all there is to it! When you've finished testing, ctrl-c to kill the process.

    The mini server appends accesses to the file httpd.log in your home directory. You might want to clear out this file from time to time.

    Errors

    Unfortunately, you can get a few error messages:
    1. Permission denied.
      
      The most likely cause for this error is that you don't have an entry in ~/.rhosts Correct this by typing this:
      	echo `hostname` $user >> ~/.rhosts
      
    2. bind: Address already in use at /usr/local/www/support/tinyhttpd.pl line 54.
      
      The most likely cause for this error is that another process is already using port 1234. Select another port and try again.
    3. bind: Permission denied at /usr/local/www/support/tinyhttpd.pl line 54.
      
      You are most likely trying to use a port number less than 1024. Use a port number greater than 1024.

    Automating the whole thing

    It'd be really nice if all this could be automated. The following script expects two parameters, a machine name and a starting port number. It will try to run a server on the given machine and port, and will modify your ~/.rhosts file or the port number until the server runs. It also creates a gateway.html file in your ~/public_html directory that contains the correct link for your mini server.

    Probably, somewhere in your ~/public_html/index.html file you'd have a link something like this:

    	Access to <a href=gateway.html>my local server</a>.
    

    
    #!/bin/sh
    #
    case $# in
    	2)	host=$1; port=$2;;
    	*)	echo "format $0 <host> <port>"; exit 1;;
    esac
    
    errors=0
    HOSTNAME=`hostname | cut -d. -f1`
    
    while [ $errors -lt 10 ]; do
    cat - <<EOF > $HOME/public_html/gateway.html
    	<html><head><title>Mini server gateway</title></head>
    	<body>click <a href=http://$host:$port/>here</a></body> 
    	for your <b>gatewayed</b> files.
    EOF
    chmod 644 $HOME/public_html/gateway.html
    	echo "trying port $port on $host"
    	case "`rsh -n $host /usr/local/www/support/tinyhttpd.pl $port 2>&1`" in
    	[Pp]"ermission denied"*) 
    		echo not in $HOME/.rhosts
    		mv $HOME/.rhosts $HOME/.rhosts.orig
    		sed -e "/^$HOSTNAME\>/d" $HOME/.rhosts.orig > $HOME/.rhosts
    		echo $HOSTNAME $LOGNAME >> $HOME/.rhosts
    		echo $HOSTNAME.csi.uottawa.ca $LOGNAME >> $HOME/.rhosts
    		echo $HOSTNAME.site.uottawa.ca $LOGNAME >> $HOME/.rhosts
    		sync
    		errors=`expr $errors + 1`
    		;;
    	"bind: Permission denied"*)
    		echo "invalid port $port"
    		port=1234
    		errors=`expr $errors + 1`
    		;;
    	"bind: Address already in use"*)
    		echo "port $port already used"
    		port=`expr $port + 1`
    		errors=`expr $errors + 1`
    		;;
    	*)	
    		echo "we're done $?"
    		exit 0
    		;;
    	esac
    done
    
    echo "too many errors, exiting"
    exit 0
    
    

    What addresses should you use for your CGIs

    A URL in your HTML files for your CGIs should look something like this:
    	<a href=/cgi-bin/url>mycgi</a>
    

    Another alternative

    ``But,'' you whine ``it still doesn't work like the real server!''

    Then, perhaps, your only alternative is to run a real server. Here's how...


    webmaster@csi.uottawa.ca

    Last update 1999/06/18