#!/bin/sh -
# sample web server.

PATH=/bin:/usr/bin
htdocdir=/usr/tmp/htdocs

showheader()
{
  echo "HTTP/1.0 200 OK"
  echo "MIME-Version: 1.0"
  echo "Server: simple-httpd/1.0"
  echo "Content-Type: $ct"
  echo ""
}

showerr()
{
  ct="text/plain"
  showheader
  echo "ERROR: $1 contents not found!"
}

setcontenttype()
{
  ext=`basename $1 | sed -e 's/\.\([^.]*\)$/ \1/' | sed -e 's/[^ ]* //'`
  case $ext in
  html|htm)
    ct="text/html"
    ;;
  gif)
    ct="image/gif"
    ;;
  jpeg|jpg)
    ct="image/jpeg"
    ;;
  txt|*)
    ct="text/plain"
    ;;
  esac
}

read request url version

a=http://`hostname`/
file=`echo $url | sed -e "s,$a,,"`
setcontenttype $file

if echo $file | grep http: >/dev/null ; then
  showerr $url
else
  if [ -f $htdocdir/$file ]; then
    showheader
    cat $htdocdir/$file
  else
    showerr $url
  fi
fi
