slitaz-dev-tools rev 63

Add TazTPD - A tiny Web server (3,5 Kb) all in shell script
author Christophe Lincoln <pankso@slitaz.org>
date Wed Apr 06 02:09:31 2011 +0200 (2011-04-06)
parents 8d54319f6ebc
children ba3d9fbe6d89
files taztpd/AUTHORS taztpd/COPYING taztpd/Makefile taztpd/README taztpd/taztpd taztpd/taztpd.cgi taztpd/taztpd.conf taztpd/taztpd.css taztpd/taztpd.html
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/taztpd/AUTHORS	Wed Apr 06 02:09:31 2011 +0200
     1.3 @@ -0,0 +1,1 @@
     1.4 +Christophe Lincoln <pankso@slitaz.org>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/taztpd/COPYING	Wed Apr 06 02:09:31 2011 +0200
     2.3 @@ -0,0 +1,20 @@
     2.4 +TazTPD Copyright License
     2.5 +===============================================================================
     2.6 +
     2.7 +
     2.8 +Copyright (c)  2011  SliTaz GNU/Linux
     2.9 +
    2.10 +
    2.11 +TazTPD is free software; you can redistribute it and/or modify
    2.12 +it under the terms of the GNU General Public License as published by
    2.13 +the Free Software Foundation; either version 3 of the License, or
    2.14 +(at your option) any later version.
    2.15 +
    2.16 +TazTPD is distributed in the hope that it will be useful, but WITHOUT
    2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.18 +FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.19 +for more details.
    2.20 +
    2.21 +You should have received a copy of the GNU General Public License
    2.22 +along with TazTPD; if not, write to the Free Software Foundation, Inc.,
    2.23 +51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/taztpd/Makefile	Wed Apr 06 02:09:31 2011 +0200
     3.3 @@ -0,0 +1,29 @@
     3.4 +# Makefile for Tank tools.
     3.5 +# Check the README for more information.
     3.6 +#
     3.7 +PREFIX?=/usr
     3.8 +SYSCONFDIR?=/etc
     3.9 +LOCALSTATDIR?=/usr/share
    3.10 +DOCDIR?=$(LOCALSTATDIR)/doc
    3.11 +DESTDIR?=
    3.12 +	
    3.13 +all:
    3.14 +
    3.15 +install:
    3.16 +	mkdir -p $(DESTDIR)$(PREFIX)/bin \
    3.17 +		$(DESTDIR)$(SYSCONFDIR)/slitaz \
    3.18 +		$(DESTDIR)$(LOCALSTATDIR)/taztpd \
    3.19 +		$(DESTDIR)$(DOCDIR)/taztpd
    3.20 +	cp -f taztpd $(DESTDIR)$(PREFIX)/bin
    3.21 +	cp -f taztpd.conf $(DESTDIR)$(SYSCONFDIR)/slitaz
    3.22 +	cp -f *.css *.html *.cgi $(DESTDIR)$(LOCALSTATDIR)/taztpd
    3.23 +	cp -f README $(DESTDIR)$(DOCDIR)/taztpd
    3.24 +
    3.25 +uninstall:
    3.26 +	rm -rf $(DESTDIR)$(PREFIX)/bin/taztpd \
    3.27 +		$(DESTDIR)$(SYSCONFDIR)/slitaz/taztpd.conf \
    3.28 +		$(DESTDIR)$(LOCALSTATDIR)/taztpd \
    3.29 +		$(DESTDIR)$(DOCDIR)/taztpd
    3.30 +
    3.31 +clean:
    3.32 +	rm -rf $(DESTDIR) _pkg install
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/taztpd/README	Wed Apr 06 02:09:31 2011 +0200
     4.3 @@ -0,0 +1,34 @@
     4.4 +TazTPD - SliTaz micro HTTP Web Server
     4.5 +================================================================================
     4.6 +
     4.7 +
     4.8 +TazTPD is a very small Web Server all in Shell script who can be run in user
     4.9 +space and so without root access. TazTPD is not designed to be used in a 
    4.10 +production envirinment but for fun, for testing, to easily and on demand serve
    4.11 +static files or dynamic content with CGI scripts.
    4.12 +
    4.13 +The goal is to keep TazTPD core as small and response as possible. Everything
    4.14 +is possible, we may have better directory listing and several plugins depending
    4.15 +on users feedbacks, requests and our free time.
    4.16 +
    4.17 +TazTPD is also a nice base to create small or custom servers dedicated to
    4.18 +a special task such as streaming audio and with a very small footprint. By
    4.19 +the way, TazTPD output some valid xHTML 5 documents with a clean generated
    4.20 +code.
    4.21 +
    4.22 +Under the hood... This small (actuall 3Kb) Web Server will be part of all
    4.23 +SliTaz installation and dedicated to the the user! Yes it can do many
    4.24 +things with it CGI capacity and the so famous QUERY_STRING without talking
    4.25 +about it is all SHell, Ash compatible, like all other SliTaz tools and utility.
    4.26 +So the idea is here now the code must follow, but we will have a full Tazpkg
    4.27 +web based graphical interface.
    4.28 +
    4.29 +
    4.30 +Quick notes
    4.31 +-----------
    4.32 +
    4.33 +	* CGI scripts must be executable: chmod 0777 *.cgi
    4.34 +	* xHTML and text CGI scripts works well
    4.35 +	* TazTPD can run in user mode throught nc and from inetd
    4.36 +	  --> www stream tcp nowait nobody /usr/bin/taztpd taztpd
    4.37 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/taztpd/taztpd	Wed Apr 06 02:09:31 2011 +0200
     5.3 @@ -0,0 +1,188 @@
     5.4 +#!/bin/sh
     5.5 +#
     5.6 +# TazTPD - The SliTaz micro HTTP Web Server all in SHell script
     5.7 +# 
     5.8 +# (C) 2011 SliTaz GNU/Linux - GNU gpl v2
     5.9 +#
    5.10 +# AUTHOR: Christophe Lincoln
    5.11 +#
    5.12 +
    5.13 +# Personnal configuration overwrite system wide config.
    5.14 +[ -f "/etc/slitaz/taztpd.conf" ] && . /etc/slitaz/taztpd.conf
    5.15 +[ -f "taztpd.conf" ] && . taztpd.conf
    5.16 +
    5.17 +# Web Server functions
    5.18 +
    5.19 +# Output standardized header for valid requests
    5.20 +http_header() {
    5.21 +	cat << EOT
    5.22 +HTTP/1.1 200 OK
    5.23 +EOT
    5.24 +}
    5.25 +
    5.26 +html_header() {
    5.27 +	cat << EOT
    5.28 +Content-Type: text/html
    5.29 +
    5.30 +EOT
    5.31 +}
    5.32 +
    5.33 +text_header() {
    5.34 +	cat << EOT
    5.35 +Content-Type: text/plain
    5.36 +
    5.37 +EOT
    5.38 +}
    5.39 +
    5.40 +# List all files in a directory
    5.41 +directory_listing() {
    5.42 +	cat << EOT
    5.43 +<!DOCTYPE html>
    5.44 +<html xmlns="http://www.w3.org/1999/xhtml">
    5.45 +<head>
    5.46 +	<title>Index of $url</title>
    5.47 +	<meta charset="utf-8" />
    5.48 +	`css_style`
    5.49 +</head>
    5.50 +EOT
    5.51 +	echo -e "<h1>$PAGE_HEADING $url</h1>"
    5.52 +	echo "<ul>"
    5.53 +	if [ "$url" != "/" ]; then
    5.54 +		echo "	<li><a href=\"../\">$PARENT_DIR/</a></li>"
    5.55 +	fi
    5.56 +	(cd ${SERVER_ROOT}$url && ls -p | while read line
    5.57 +	do
    5.58 +		echo "	<li><a href=\"$line\">$line</a></li>"
    5.59 +	done)
    5.60 +	echo -e "</ul>"
    5.61 +	taztpd_footer
    5.62 +	echo -e "</body>\n</html>"
    5.63 +}
    5.64 +
    5.65 +# Handled by an external CSS file
    5.66 +css_style() {
    5.67 +	echo '<style type="text/css">'
    5.68 +	cat $SERVER_CSS
    5.69 +	echo '</style>'
    5.70 +}
    5.71 +
    5.72 +taztpd_footer() {
    5.73 +	echo "<div id=\"footer\">$SERVER_NAME</div>"
    5.74 +}
    5.75 +
    5.76 +# Handle file type by extension
    5.77 +handle_filetype() {
    5.78 +	case $file in
    5.79 +		# Check for HTML first for fast anser (most requests)
    5.80 +		*.html|*.htm) type="text/html";;
    5.81 +		*.css) type="text/css" ;;
    5.82 +		*.xml) type="text/xml" ;;
    5.83 +		*.jpg|*.jpeg) type="image/jpeg" ;;
    5.84 +		*.png) type="image/png" ;;
    5.85 +		*.tar.gz) "application/x-tgz" ;;
    5.86 +		*.tazpkg) "application/x-tazpkg" ;;
    5.87 +		*)
    5.88 +			# Default to plain text document
    5.89 +			type=text/plain ;;
    5.90 +	esac
    5.91 +	cat << EOT
    5.92 +Content-Type: $type
    5.93 +
    5.94 +EOT
    5.95 +}
    5.96 +
    5.97 +# Server main function
    5.98 +read_request() {
    5.99 +	# Record the HTTP request
   5.100 +	read request
   5.101 +	while /bin/true; do
   5.102 +		read header
   5.103 +		[ "$header" == $'\r' ] && break;
   5.104 +	done
   5.105 +	# Extract URL from the request string
   5.106 +	url="${request#GET }"
   5.107 +	url="${url% HTTP/*}"
   5.108 +	query="${url#*\?}"
   5.109 +	url="${url%%\?*}"
   5.110 +	# Handle CGI scripts
   5.111 +	if [ "$query" != "$url" -a -x "$file" ]; then
   5.112 +		export QUERY_STRING="$query"
   5.113 +		http_header
   5.114 +		exec "$file"		
   5.115 +		echo -e "\r"
   5.116 +		exit 0
   5.117 +	fi
   5.118 +	# Locate the wanted file
   5.119 +	file="${SERVER_ROOT}$url"
   5.120 +	# First try to display requested page
   5.121 +	if [ -f "$file" ]; then
   5.122 +		http_header
   5.123 +		handle_filetype
   5.124 +		cat "$file"
   5.125 +		echo -e "\r" && exit 0
   5.126 +	fi
   5.127 +	# Requested URL may be a directory
   5.128 +	if [ -d "$file" ]; then
   5.129 +		http_header
   5.130 +		if [ -f "$file/index.html" ]; then
   5.131 +			file=$file/index.html
   5.132 +			echo -e "Content-Type: text/html\r"
   5.133 +			echo -e "\r"
   5.134 +			cat "$file"
   5.135 +			echo -e "\r" && exit 0
   5.136 +		fi
   5.137 +		html_header
   5.138 +		directory_listing
   5.139 +		echo -e "\r"
   5.140 +	# 404 error
   5.141 +	else
   5.142 +		cat << EOT
   5.143 +HTTP/1.1 404 Not Found
   5.144 +Content-Type: text/html
   5.145 +
   5.146 +<!DOCTYPE html>
   5.147 +<html xmlns="http://www.w3.org/1999/xhtml">
   5.148 +<head>
   5.149 +	<title>404 Not Found</title>
   5.150 +	<meta charset="utf-8" />
   5.151 +	`css_style`
   5.152 +</head>
   5.153 +<body>
   5.154 +	<h1>404 Not Found</h1>
   5.155 +	<p>$NOT_FOUND</p>
   5.156 +	`taztpd_footer`
   5.157 +</body>
   5.158 +</html>
   5.159 +EOT
   5.160 +		echo -e "\r"
   5.161 +	fi
   5.162 +}
   5.163 +
   5.164 +# Web Server commands
   5.165 +
   5.166 +case $1 in
   5.167 +	status|-s)
   5.168 +		echo ""
   5.169 +		ps | grep taztpd
   5.170 +		echo "" ;;
   5.171 +	dev|-d)
   5.172 +		# Devel mode by keeping the hand
   5.173 +		echo "Starting Web Server on port: $SERVER_PORT (dev mode)"
   5.174 +		while true
   5.175 +		do 
   5.176 +			nc -l -p $SERVER_PORT -e /home/pankso/Public/taztpd/taztpd
   5.177 +		done ;;
   5.178 +	nc|-n)
   5.179 +		# Use nc to listen on a port and execute TazTPD on a request
   5.180 +		echo "Starting Web Server on port: $SERVER_PORT"
   5.181 +		(while true
   5.182 +		do 
   5.183 +			nc -l -p $SERVER_PORT -e /home/pankso/Public/taztpd/taztpd
   5.184 +		done) & ;;
   5.185 +	usage|*help|-u|*-h)
   5.186 +		# Display a short usage
   5.187 +		echo "Usage: `basename $0` [status|dev|nc]" ;;
   5.188 +	*)
   5.189 +		read_request ;;
   5.190 +esac
   5.191 +exit 0	
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/taztpd/taztpd.cgi	Wed Apr 06 02:09:31 2011 +0200
     6.3 @@ -0,0 +1,34 @@
     6.4 +#!/bin/sh
     6.5 +#
     6.6 +# CGI/Shell script example for TazTPD Web Server
     6.7 +#
     6.8 +echo "Content-Type: text/html"
     6.9 +echo ""
    6.10 +
    6.11 +var=${QUERY_STRING#var=}
    6.12 +
    6.13 +# xHTML 5 output
    6.14 +cat << EOT
    6.15 +<!DOCTYPE html>
    6.16 +<html xmlns="http://www.w3.org/1999/xhtml">
    6.17 +<head>
    6.18 +	<title>TazTPD CGI</title>
    6.19 +	<style type="text/css">
    6.20 +		body { padding: 40px 16%; }
    6.21 +		h1 { color: #4d4d4d; border-bottom: 1px dotted #ddd; }
    6.22 +	</style>
    6.23 +<head>
    6.24 +<body>
    6.25 +<h1>TazTPD and CGI</h1>
    6.26 +<p>
    6.27 +	Entered form value: $var
    6.28 +</p>
    6.29 +<form method="get" action="taztpd.cgi">
    6.30 +	<input type="text" name="var" size="32">
    6.31 +</form>
    6.32 +<p>
    6.33 +	`date '+%Y-%m-%d %H:%M'`
    6.34 +</p>
    6.35 +</body>
    6.36 +</html>
    6.37 +EOT
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/taztpd/taztpd.conf	Wed Apr 06 02:09:31 2011 +0200
     7.3 @@ -0,0 +1,15 @@
     7.4 +# taztpd.conf: TazTPD Web Server configuration file
     7.5 +#
     7.6 +
     7.7 +# Path to the files to serve and HTTP port
     7.8 +SERVER_ROOT="$HOME/Public"
     7.9 +SERVER_PORT="8080"
    7.10 +
    7.11 +# Page heading is followed by $url
    7.12 +PAGE_HEADING="Index of"
    7.13 +PARENT_DIR="Parent Directory"
    7.14 +SERVER_NAME="Served by TazTPD (SliTaz GNU/Linux)"
    7.15 +SERVER_CSS="/usr/share/taztpd/taztpd.css"
    7.16 +
    7.17 +# 404 Not Found
    7.18 +NOT_FOUND="The requested URL was not found on the server"
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/taztpd/taztpd.css	Wed Apr 06 02:09:31 2011 +0200
     8.3 @@ -0,0 +1,25 @@
     8.4 +/* CSS style for TazTPD Web Server listing page and 404 error */
     8.5 +
     8.6 +body {
     8.7 +	margin: 0;
     8.8 +	font: 0.9em sans-serif;
     8.9 +}
    8.10 +
    8.11 +h1 {
    8.12 +	color: #4d4d4d;
    8.13 +	margin: 0;
    8.14 +	padding: 5px;
    8.15 +	border-bottom: 1px dotted #ddd;
    8.16 +}
    8.17 +
    8.18 +a { text-decoration: none; color: #215090; }
    8.19 +a:hover { text-decoration: underline; color: blue; }
    8.20 +p { padding: 5px; }
    8.21 +
    8.22 +#footer {
    8.23 +	font-size: 84%;
    8.24 +	padding: 5px;
    8.25 +	width: 100%;
    8.26 +	border-top: 1px dotted #ddd;
    8.27 +	color: #888888;
    8.28 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/taztpd/taztpd.html	Wed Apr 06 02:09:31 2011 +0200
     9.3 @@ -0,0 +1,36 @@
     9.4 +<!DOCTYPE html>
     9.5 +<html xmlns="http://www.w3.org/1999/xhtml">
     9.6 +<head>
     9.7 +	<title>TazTPD Web Server</title>
     9.8 +	<meta charset="utf-8" />
     9.9 +	<style type="text/css">
    9.10 +		body { padding: 40px 16%; }
    9.11 +		h1 { color: #4d4d4d; border-bottom: 1px dotted #ddd; }
    9.12 +		#content { text-align: justify; }
    9.13 +		#footer { font-size: 84%; border-top: 1px solid #d4d4d4;
    9.14 +			padding: 4px 0; }
    9.15 +		#footer, #footer a { color: #888888; }
    9.16 +	</style>
    9.17 +</head>
    9.18 +<body>
    9.19 +
    9.20 +<h1>TazTPD</h1>
    9.21 +
    9.22 +<div id="content">
    9.23 +	<p>
    9.24 +		A Web server in a nutSHell!
    9.25 +	</p>
    9.26 +	<p>
    9.27 +		TazTPD is a very small Web Server all in Shell script who can
    9.28 +		be run in user space. TazTPD is not designed to be used in
    9.29 +		production but for fun, for testing, to easily and on demand
    9.30 +		serve static files or dynamic content with CGI scripts.
    9.31 +	</p>
    9.32 +	<h4>CGI testing</h4>
    9.33 +	<form method="get" action="taztpd.cgi">
    9.34 +		<input type="text" name="var" size="32">
    9.35 +	</form>
    9.36 +</div>
    9.37 +
    9.38 +</body>
    9.39 +</html>