w.sh (2503 octets)


#!/bin/sh

cd `dirname $0`

. ./commun-httpd

date >> $LOG
echo $ROOT >> $LOG

while read ligne
do
    ligne=`echo $ligne | tr -d '\015'`
    [ -z "$ligne" ] && echo >> $LOG && exit 0
    set -- $ligne
    reqtype=$1

    file=`echo $ROOT | sed -e "s/\/*$//"`/`echo $2 | sed "s/^\///"`
    [ -d "$file" ] && file="$file/index.html"

    if [ "$reqtype" = "GET" ]
    then
        if [ -r $file ] && [ -f $file ]
        then
            echo "GET $file" >> $LOG
            echo "HTTP/1.1 200 OK"
            echo "Content-Length: `wc -c < $file`"
            cat << EOF            
Date: `date`
Server: minhttpd, v$VERSION
Connection: close
EOF

Type=`file $file | cut -d" " -f2 `

            case "$Type" in
                "GIF")  echo "Content-Type: image/gif"
                        text=""
                        echo
                        cat $file
                        ;;
                "JPEG") echo "Content-Type: image/jpeg"
                        text=""
                        echo
                        cat $file
                        ;;
                "PNG")  echo "Content-Type: image/png"
                        text=""
                        echo
                        cat $file
                        ;;
                *)      echo "Content-Type: text/html"
                        echo
                        if echo $file | grep "\.\.\/" > /dev/null 2>&1
                        then
                            cat ./error400.html
                            echo "What are you trying to do ?!?"
                            echo "GET $file"
                            echo "Error : $file" >> $ERRLOG
                            exit 1
                        fi
                        /bin/sh exec.mod $file
                        ;;
            esac
#        [ -f $FOOTER -a "$text" ] && sed -e "s/\(.*\)VERSION\(.*\)HOSTNAME\(.*\)/\1$VERSION\2`hostname`\3/" \
#                          -e "s/HOSTNAME/`hostname`/" < $FOOTER
        else # try to get an unvalid file
            echo "404 $file" >> $ERRLOG
            cat ./error404.html
#    [ -f $FOOTER -a "$text" ] && sed -e "s/VERSION/$VERSION/" \
#                          -e "s/HOSTNAME/`hostname`/" < $FOOTER
        fi
        [ -f $FOOTER -a "$text" ] && sed -e \
            "s/\(.*\)VERSION\(.*\)HOSTNAME\(.*\)/\1$VERSION\2`hostname`\3/" \
            < $FOOTER
    else # not a GET request -> log this request
        echo "$ligne" >> $LOG
    fi
done

echo "Serious problem !" >&2
exit 1