Bomba keypad
[mirrors/Programs.git] / bash / bash-httpd / httpd.bash
CommitLineData
21c4e167
H
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
7listen_port=8080;
8web_root='/home/harvie';
9nc='nc'; links='links'; #binaries
10
11if [ "$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
22Server: HTTPd.bash
23Content-Type: '"$content_type"'
24Connection: close\n\n';
25
26 if [ -d "$get" ]; then "$links" -source "$get"; else cat "$get"; fi;
27 exit;
28fi;
29
30listen() { "$nc" -l -p "$listen_port" -e "kill -s USR1 '$$' & '$0' --serve"; }
31
32trap 'listen &' SIGUSR1;
33trap "echo -e '\nHTTPd.bash stoped'; exit" SIGINT;
34kill -s USR1 "$$";
35echo -e "HTTPd.bash started\nHarvie 2oo9\nport: $listen_port\nroot: $web_root"
36while true; do sleep 0.1; done;
This page took 0.159697 seconds and 4 git commands to generate.