Topic: [Shell-Scripting] Simple Webserver
This has been showed in July Meetup by dan_r. Also presentet in the forums here!
As requested, here are some snippets from last night:
A verbose command for downloading the top 3 videos from youtube:
wget http://www.youtube.com/ -O - 2> /dev/null | grep "<a href" | sed 's/.*<a href/<a href/' |cut -f2 -d'"' | grep "popular" | cut -f1 -d'&' | head -n 3 | sed 's/^/http:\/\/youtube.com/' | xargs -i clive {} \;For the web server, here are the contents of a config file that must be added to /etc/xinetd.d/
(The name of the file doesn't matter):
service www
{
port = 80
protocol = tcp
wait = no
user = root
server = /home/dan/uaelug/httpd/httpd
}And here's the example web server in bash script (for educational use only).
#! /bin/bash
docroot="/home/dan/uaelug/httpd"
# First line holds the HTTP request
read request
# Keep swallowing lines until you get a blank one
# Note: HTTP lines are terminated with CR LF, and CR='\r'
while read line; do
test "$line" == $'\r' && break
done
# Trim the request string and log it
url=${request#GET }
url=${url% HTTP/*}
echo $url >> /home/dan/uaelug/httpd/log.txt
# Serve the request if the file exists, otherwise 404
if [ -f "${docroot}/${url}" ]; then
echo -e "HTTP/1.1 200 OK\r
Content-Type: text/html; charset=ISO-8859-1\r
\r"
cat ${docroot}/${url}
echo -e "\r"
else
echo -e "HTTP/1.1 404 Not Found\r
Content-Type: text/html; charset=UTF-8\r
\r"
cat ${docroot}/404.html
echo -e "\r"
fiReference: http://www.debian-administration.org/ar ell_script
Again -> Kudos to dan_r.
