I am building a very small http server to control a raspberry pi-based device from a browser using this as a starting point: http://paulbuchheit.blogspot.com/2007/04/webserver-in-bash.html
The core of it is:
#!/bin/bash
RESPONSE=/tmp/webresp
[ -p $RESPONSE ] || mkfifo $RESPONSE
while true ; do
( cat $RESPONSE ) | nc -l -p 8080 | (
REQUEST=`while read L && [ " " "<" "$L" ] ; do echo "$L" ; done`
REQ="`echo \"$REQUEST\" | head -n 1`"
echo "[ `date '+%Y-%m-%d %H:%M:%S'` ] $REQ" >>/var/log/http-access.log
if [[ $REQ =~ ^GET\ /a[\ \/\#?] ]]; then
# ...
RESP="<p>You are at A</p><p><a href='/'>Home</a></p>"
elif [[ $REQ =~ ^GET\ /b[\ \/\#?] ]]; then
# ...
RESP="<p>You are at B</p><p><a href='/'>Home</a></p>"
elif [[ $REQ =~ ^GET\ /c[\ \/\#?] ]]; then
# ...
RESP="<p>You are at C</p><p><a href='/'>Home</a></p>"
else
read -r -d '' RESP <<'HTMLDOC'
<h3>Home</h3>
<p><a href='/a'>A</a></p>
<p><a href='/b'>B</a></p>
<p><a href='/c'>C</a></p>
HTMLDOC
fi
cat >$RESPONSE <<EOF
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/html
Server: bash/2.0
Connection: Close
Content-Length: ${#RESP}
$RESP
EOF
)
done
It works great except if I want to run the server on port 80 I have to sudo it. I think it is probably a bad idea to run the server with elevated privileges all the time. How can I un-sudo after it starts listening on port 80? It looks as if the nc command is rerun on each request.
Yes, I know I can run a 'real' lightweight web server but I want to keep the memory footprint as small as possible and I figured bash is already running. Also, I would like to keep the installation limited to adding a few .sh files and running one with a ~/.config/autostart/autorun.desktop
file