Skip to content

Instantly share code, notes, and snippets.

@ariscop
Created March 20, 2012 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariscop/2130583 to your computer and use it in GitHub Desktop.
Save ariscop/2130583 to your computer and use it in GitHub Desktop.
raptor-httpd, you will be eaten by raptors if you ever use this
#!/bin/bash
http-accept() {
read -r REQ <&3
REQ=`echo "$REQ" | tr '\r\n' ' '`
#read -r LINE <&3
#while [ -n "$LINE" ]; do
# read -r LINE <&3
#done
METHOD=`echo "$REQ" | cut -d' ' -f1`
FILENAME="$DIRNAME`echo "$REQ" | cut -d' ' -f2`"
#block unimp methods and ..
if [ "$METHOD" != "GET" ] || [ `echo $FILENAME | grep -F ".."` ]; then
echo "HTTP/1.0 403 Forbidden\n" >&3
echo >&3
echo "$REQ 403"
return
fi
if [ -r "$FILENAME" ] ; then
if [ -f "$FILENAME" ]; then
MIMETYPE=`file --mime-type "$FILENAME" | cut -d' ' -f2`
echo "HTTP/1.0 200 OK" >&3
echo "Content-Type: $MIMETYPE" >&3
echo >&3
echo "$REQ" 200 "$MIMETYPE"
cat "$FILENAME" >&3
elif [ -d "$FILENAME" ]; then
MIMETYPE="text/plain"
echo "HTTP/1.0 200 OK" >&3
echo "Content-Type: $MIMETYPE" >&3
echo >&3
echo "$REQ" 200 "$MIMETYPE" directory listing
echo "Directory listing of " "$FILENAME" >&3
ls -l "$FILENAME" >&3
else
echo "HTTP/1.0 404 File Not Found\n" >&3
echo >&3
echo "$REQ" 404 not found
fi
else
echo "HTTP/1.0 404 File Not Found\n" >&3
echo >&3
echo "$REQ" 404 not found
fi
}
if [[ $MODE == "child" ]] ; then
http-accept
exit
fi
if [ ! -d "$1" ] || [ ! -r "$1" ] ; then
echo invalid directory
else
export DIRNAME=`echo $1 | sed -E s:/?$:/:`
MODE=child socat TCP-LISTEN:8080,reuseaddr,fork EXEC:$0,fdin=3,fdout=3
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment