Friday, April 13, 2007

Webserver in bash

And not using perl or any of that fancy stuff. It's the inane things that keep me awake at night.

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

Update: Fixed script so that it also work in Linux, where tr lacks the -u option.

6 comments:

  1. It should be like this to behave with both gnu and bsd utils:
    ( cat $RESP ) | nc -l 9000 | sed -ue 's/\r//g' | (

    ReplyDelete
  2. Thanks abbot, but OSX sed doesn't support -u. I found a way to get rid of tr instead.

    ReplyDelete
  3. well that wasted a bit of my day.
    how 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

    ReplyDelete
  4. Another web server written in bash: http://sourceforge.net/projects/mws/

    ReplyDelete
  5. This both intrigues me and scares me...

    ReplyDelete
  6. Does the HTTP spec. say the time must be GMT rather than your local timezone, e.g. "TZ=GMT date ...".

    Cheers, Ralph Corderoy.

    ReplyDelete

Note: Only a member of this blog may post a comment.