A webserver in a shell script (small source in body)

Eduardo Costa Lisboa eduardo.linux at gmail.com
Fri Mar 17 13:08:47 PST 2006


I was just reading this URL -
http://www.debian-administration.org/articles/371 - and got interested
on the article. Hence, I created a very small one which I will try to
separate in several files later so I can _include_ them as modules.

Take a look:

-- cut here

#!/bin/bash

#httpd.sh
#Software: small script that *tries* to be a webserver
#(based on the article: http://www.debian-administration.org/articles/371)
#How to use: use inetd or netcat: nc -l -p portnumber -e httpd.sh
#Author: Eduardo Costa Lisboa - elisboa at superig.com.br
#Date: 17/mar/2006
#Version: 0.20060317

while true
do
   read header
   [ "$header" == $'\r' ] && break;
   request=$request$header
   [ ! -z "$(echo $header | grep GET)" ] && GET=$(echo $header | awk {'print $3'
})
   [ ! -z "$(echo $header | grep GET)" ] && FILE=$(echo $header | awk {'print $2
'})
done

FILE="$(echo $FILE | cut -c2-)"
if [ -e "$FILE" ]
then
   FILE0="yes"
else
   FILE0="no"
fi

if [ -r "$FILE" ]
then
   FILE1="yes"
else
   FILE1="no"
fi

FILE2=$(file -bi "$FILE")

echo -e "HTTP/1.1 200 OK\rContent-type: text/plain"

cat<<EOF

Greeting: Hello World!
Date: $(date)

Request content:
$request

Identifying...
HTTP version: $GET
File required: $FILE
File exists: $FILE0
File is readable: $FILE1
File type is: $FILE2
EOF

-- cut here

For now, it only reads the header and interprets some of it's lines.


--
Eduardo Costa Lisboa



More information about the lfs-chat mailing list