Created
March 20, 2012 03:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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