#!/bin/sh
# Tiny Unix Web Server by Corey Satten, corey @ cac.washington.edu, 7/1/97.
# Latest version at http://staff.washington.edu/corey/
# Invoke from inetd.  Use TCP Wrappers for access control.

export LANG=C LC_ALL=C
cd /web_root_dir || exit 1
# H=fully.qualified.server.name		# needed for lynx 2.5, fixed in 2.6

GET=`/bin/sed -n '/^GET/ { s,^GET  *\([-+a-zA-Z0-9:._/]*\).*,\1,p; q; }'`
L=/tmp/httpd.log; [ -w $L -o ! -f $L ] && echo "`/bin/date` $GET" >> $L

case "$GET" in *..*) GET='';; *) GETD="$GET";; esac
case "$GET" in */);; *)			# fix directory URL not ending with /
    if [ -d ./$GET/ ] ;then if [ -f ./$GETD/index.html ] ;then
	echo HTTP/1.0 302 Found; echo Location: ${H+http://$H}$GETD/; echo; exit
	else GETD=$GET/ ;fi ;fi;;
esac
case "$GETD" in
    */) /bin/cat ./${GETD}index.html 2>&- ||
	/bin/ls -ldtrLF ./${GETD}* 2>&- | /bin/sed '
	    1 { x; s,.*,<HTML> <HEAD> <TITLE> '"$GET"' </TITLE> </HEAD>,p
	           s,.*,<BODY> <H2>Index of '"$GET"'</H2>,p
	           s,.*,<PRE>       Size   Date/Time  Name<HR>,p
		x; }
	    s,.*[!-~]\(  *[0-9][0-9]* [A-Z][a-z][a-z] .[0-9] ..... .*\),     \1,
	    : loop
	    s/ \([0-9][0-9]*\)\([0-9][0-9][0-9][,0-9]* [A-Z].. \)/\1,\2/
	    t loop
	    s,\./*'"$GETD"'\([!-)+-~]*\).*$,<A HREF="'"$GETD"'\1">\1</A>,
	   $s,$,</PRE> </BODY> </HTML>,' ;;
     *) exec /bin/cat ./$GET 2>&- ;;
esac
