#!/bin/sh

if [ "$1" = "--kinetd" ]; then
	exec <&$2 >&$2
fi

read request url httptype
url=${url/
/}
httptype=${httptype/
/}
# echo "req=|$request| url=|$url| httptype=|$httptype|" >> /tmp/httpd.log
if [ "x$httptype" != "x" ]; then
	line="x"
	while [ -n "$line" ]; do
		read line
		line=${line/
/}
		# echo "$line" >> /tmp/httpd.log
	done
fi
case $url in
/)
	size=`xdpyinfo -display :0| grep dimensions:|head -1|sed -e "s/.*dimensions: *//" -e "s/ pixels.*//"`
	width=`echo $size|sed -e "s/x.*//"`
	height=`echo $size|sed -e "s/.*x//"`
	height=$((height+20))
	ctype="text/html"
	content="
<HTML>
<TITLE>$LOGNAME's desktop</TITLE>
<APPLET CODE=vncviewer.class ARCHIVE=vncviewer.jar WIDTH=$width HEIGHT=$height>
<param name=PORT value=5900>
</APPLET>
</HTML>
"
	;;
*.jar|*.class)
	# Use basename to make sure we have just a filename, not ../../...
	url=`basename "$url"`
	ctype="application/octet-stream"
	cfile="/usr/share/vnc/classes/$url"
	content="FILE"
	;;
esac

if [ "x$httptype" != "x" ]; then
	echo "HTTP/1.0 200 OK"
	echo "Content-Type: $ctype"
	if [ "$content" == "FILE" ]; then
		clen=`wc -c $cfile`
	else
		clen=`echo "$content"|wc -c`
	fi
	echo "Content-Length: $clen"
	echo "Connection: close"
	echo ""
fi

if [ "$request" == "GET" ]; then
	if [ "$content" == "FILE" ]; then
		cat $cfile
	else
		echo "$content"
	fi
fi
exit 0
