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.
No comments:
Post a Comment