Getting nc to behave turned out to be the most difficult part. It won't exit until both ends of the connection are closed. Correction, making blogger not mangle this code was the most difficult part.
#!/bin/bash
# web.sh -- http://localhost:9000/hello?world
RESP=/tmp/webresp
[ -p $RESP ] || mkfifo $RESP
while true ; do
( cat $RESP ) | nc -l -p 9000 | (
REQ=`while read L && [ " " "<" "$L" ] ; do echo "$L" ; done`
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $REQ" | head -1
cat >$RESP <<EOF
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/plain
Server: bash/2.0
Connection: Close
Content-Length: ${#REQ}
$REQ
EOF
)
done
# web.sh -- http://localhost:9000/hello?world
RESP=/tmp/webresp
[ -p $RESP ] || mkfifo $RESP
while true ; do
( cat $RESP ) | nc -l -p 9000 | (
REQ=`while read L && [ " " "<" "$L" ] ; do echo "$L" ; done`
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $REQ" | head -1
cat >$RESP <<EOF
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/plain
Server: bash/2.0
Connection: Close
Content-Length: ${#REQ}
$REQ
EOF
)
done
Update: Fixed script so that it also work in Linux, where tr lacks the -u option.
It should be like this to behave with both gnu and bsd utils:
ReplyDelete( cat $RESP ) | nc -l 9000 | sed -ue 's/\r//g' | (
Thanks abbot, but OSX sed doesn't support -u. I found a way to get rid of tr instead.
ReplyDeletewell that wasted a bit of my day.
ReplyDeletehow about a version that takes a document root and serves html files:
#!/bin/bash
# pass in document root as first arg
# web.sh ~/Sites/test
DOC_ROOT=$1
RESP=/tmp/webresp
[ -p $RESP ] || mkfifo $RESP
while true ; do
( cat $RESP ) | nc -l -p 9000 | tr -ud '\r' | (
REQ=`while read L && [ -n "$L" ] ; do echo "$L" ; done`
SERV_PATH=`echo "$REQ" | head -1 | cut -f2 -d" " `
FULL_PATH="$DOC_ROOT$SERV_PATH"
echo $FULL_PATH
if [ -f "$FULL_PATH" ]; then
RESP_BODY=`cat $FULL_PATH`
RESP_CODE="200 OK"
else
RESP_BODY="not found<br/><br/>$REQ"
RESP_CODE="404 NOT FOUND"
fi
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $REQ" | head -1
cat >$RESP <<EOF
HTTP/1.0 $RESP_CODE
Cache-Control: private
Content-Type: text/html
Server: bash/2.0
Connection: Close
Content-Length: ${#RESP_BODY}
$RESP_BODY
EOF
)
done
Another web server written in bash: http://sourceforge.net/projects/mws/
ReplyDeleteThis both intrigues me and scares me...
ReplyDeleteDoes the HTTP spec. say the time must be GMT rather than your local timezone, e.g. "TZ=GMT date ...".
ReplyDeleteCheers, Ralph Corderoy.