web server on bash, nc, awk and sed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#!/bin/sh rm /tmp/f mkfifo /tmp/f #runloop while true do nc -l 8080 < /tmp/f | ( #read request read line #parse request args=$(echo -n "$line" | awk '{print $2}' | sed s~'?'~' '~ | awk '{print $2}') page=$(echo -n "$line" | awk '{print $2}' | sed s~'?'~' '~ | awk '{print $1}' | sed s~'/'~''~) user=$(echo -n "$args" | sed s~','~"\n"~ | sed s~'='~' '~ | awk '{if($1 == "user") print $2}') #some logic if [ "$page" != 'index.sh' ] then conttype="application/octet-stream" len=$(stat -c%s $page) echo "HTTP/1.0 200 OK" echo "Server: MadnessBASH" echo "Content-Length: $len" echo "Content-Type: $conttype" echo "" cat $page else conttype="text/html" if [ -z "$user" ] then data=`cat login.htm` else data=`cat index.htm` data=$(echo -n "$data" | sed s~_user_~"$user"~) fi len=`echo $data | wc -c` echo "HTTP/1.0 200 OK" echo "Server: MadnessBASH" echo "Content-Length: $len" echo "Content-Type: $conttype" echo "" echo $data fi ) > /tmp/f done |
pages content(without doctypes etc.)
login.htm:
1 2 3 4 5 6 7 8 9 10 |
<html> <body> <p>Hello! Who are you?</p> <form action=""> <label>name:</label> <input type="text" name="user"></input> <input type="submit" value="login"></input> </form> </body> </html> |
index.htm:
1 2 3 4 5 6 7 |
<html> <body> <p>It works!</p> <p>Hello, _user_</p> <p><a href="/srv.sh">download source</a></p> </body> </html> |