Bash webserver

Serves webpages with .jpeg images more mime types could easily be added (anyway it does seem to work even if improper mime types are used for some images)

Copy this to a file called server (or whatever) and run with
‘server start &’, to run as a daemon, gee its almost as good as apache 😉

to stop use ‘server stop’

NOTE: do not run as a proper webserver as its extremely insecure, would be a good idea to run a chroot jail

Also dont run it while you’ve got other instances of netcat open as it will kill them.

case $1 in start) while(( x != 1)); do nc -l -p 80 -c "$0 serve" 2> /dev/null; done ;;stop) killall ${0:2} && killall nc;; serve)read b;var_name="111";while [ ${#var_name} != "1" ]; do read -t 1 var_name; done; b=${b:4:${#b}-13}; b=${b:1:${#b}}; if [ ${#b} = "1" ]; then b="index.htm"; fi; if [ -f $b ]; then ext=${b#*.}; case "$ext" in jpeg) cont="image/jpeg";; *) cont="text/html" ;; esac; echo -e "HTTP/1.1 200 OK\nContent-Type: $cont\nContent-Length:`ls -l $b | awk '{ print $5 }'`\n"; dd if=$b 2> /dev/null;killall nc 2> /dev/null; else echo -e "HTTP/1.1 404 Not Found\n\n404"; fi ;; esac


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: