| 1 | #!/bin/bash |
| 2 | #HTTPd.bash (Harvie 2oo9) |
| 3 | #WebServer+Small-INSECURE+Emergency+Filelisting+Mime+Unicode-BASH |
| 4 | #TODO: Security, Dynamic pages in BASH/PHP, Speed |
| 5 | #dependencies: GNU NetCat, Links, sed |
| 6 | |
| 7 | listen_port=8080; |
| 8 | web_root='/home/harvie'; |
| 9 | nc='nc'; links='links'; #binaries |
| 10 | |
| 11 | if [ "$1" == '--serve' ]; then |
| 12 | urldecode() { echo -e $( echo "$@" | sed -e 's/+/ /g;s/%/\\x/g' ); } |
| 13 | |
| 14 | get="$( head -n 1 | cut -d ' ' -f 2 )"; |
| 15 | get="$web_root$(urldecode $get)"; |
| 16 | |
| 17 | if [ -d "$get" ]; then content_type='text/html;charset=utf-8'; |
| 18 | else content_type=$(file -i "$get" | cut -d ' ' -f 2-); fi; |
| 19 | |
| 20 | echo -ne \ |
| 21 | 'HTTP/1.1 200 OK |
| 22 | Server: HTTPd.bash |
| 23 | Content-Type: '"$content_type"' |
| 24 | Connection: close\n\n'; |
| 25 | |
| 26 | if [ -d "$get" ]; then "$links" -source "$get"; else cat "$get"; fi; |
| 27 | exit; |
| 28 | fi; |
| 29 | |
| 30 | listen() { "$nc" -l -p "$listen_port" -e "kill -s USR1 '$$' & '$0' --serve"; } |
| 31 | |
| 32 | trap 'listen &' SIGUSR1; |
| 33 | trap "echo -e '\nHTTPd.bash stoped'; exit" SIGINT; |
| 34 | kill -s USR1 "$$"; |
| 35 | echo -e "HTTPd.bash started\nHarvie 2oo9\nport: $listen_port\nroot: $web_root" |
| 36 | while true; do sleep 0.1; done; |