# HG changeset patch # User Christophe Lincoln # Date 1302048571 -7200 # Node ID 7c1765f8e1e6906e8b2e02b80c29fa52707d845e # Parent 8d54319f6ebcc2b7f4a0efbf23a09e0439e9044b Add TazTPD - A tiny Web server (3,5 Kb) all in shell script diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/AUTHORS --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/AUTHORS Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,1 @@ +Christophe Lincoln diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/COPYING --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/COPYING Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,20 @@ +TazTPD Copyright License +=============================================================================== + + +Copyright (c) 2011 SliTaz GNU/Linux + + +TazTPD is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or +(at your option) any later version. + +TazTPD is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with TazTPD; if not, write to the Free Software Foundation, Inc., +51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/Makefile Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,29 @@ +# Makefile for Tank tools. +# Check the README for more information. +# +PREFIX?=/usr +SYSCONFDIR?=/etc +LOCALSTATDIR?=/usr/share +DOCDIR?=$(LOCALSTATDIR)/doc +DESTDIR?= + +all: + +install: + mkdir -p $(DESTDIR)$(PREFIX)/bin \ + $(DESTDIR)$(SYSCONFDIR)/slitaz \ + $(DESTDIR)$(LOCALSTATDIR)/taztpd \ + $(DESTDIR)$(DOCDIR)/taztpd + cp -f taztpd $(DESTDIR)$(PREFIX)/bin + cp -f taztpd.conf $(DESTDIR)$(SYSCONFDIR)/slitaz + cp -f *.css *.html *.cgi $(DESTDIR)$(LOCALSTATDIR)/taztpd + cp -f README $(DESTDIR)$(DOCDIR)/taztpd + +uninstall: + rm -rf $(DESTDIR)$(PREFIX)/bin/taztpd \ + $(DESTDIR)$(SYSCONFDIR)/slitaz/taztpd.conf \ + $(DESTDIR)$(LOCALSTATDIR)/taztpd \ + $(DESTDIR)$(DOCDIR)/taztpd + +clean: + rm -rf $(DESTDIR) _pkg install diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/README Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,34 @@ +TazTPD - SliTaz micro HTTP Web Server +================================================================================ + + +TazTPD is a very small Web Server all in Shell script who can be run in user +space and so without root access. TazTPD is not designed to be used in a +production envirinment but for fun, for testing, to easily and on demand serve +static files or dynamic content with CGI scripts. + +The goal is to keep TazTPD core as small and response as possible. Everything +is possible, we may have better directory listing and several plugins depending +on users feedbacks, requests and our free time. + +TazTPD is also a nice base to create small or custom servers dedicated to +a special task such as streaming audio and with a very small footprint. By +the way, TazTPD output some valid xHTML 5 documents with a clean generated +code. + +Under the hood... This small (actuall 3Kb) Web Server will be part of all +SliTaz installation and dedicated to the the user! Yes it can do many +things with it CGI capacity and the so famous QUERY_STRING without talking +about it is all SHell, Ash compatible, like all other SliTaz tools and utility. +So the idea is here now the code must follow, but we will have a full Tazpkg +web based graphical interface. + + +Quick notes +----------- + + * CGI scripts must be executable: chmod 0777 *.cgi + * xHTML and text CGI scripts works well + * TazTPD can run in user mode throught nc and from inetd + --> www stream tcp nowait nobody /usr/bin/taztpd taztpd + diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/taztpd --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/taztpd Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,188 @@ +#!/bin/sh +# +# TazTPD - The SliTaz micro HTTP Web Server all in SHell script +# +# (C) 2011 SliTaz GNU/Linux - GNU gpl v2 +# +# AUTHOR: Christophe Lincoln +# + +# Personnal configuration overwrite system wide config. +[ -f "/etc/slitaz/taztpd.conf" ] && . /etc/slitaz/taztpd.conf +[ -f "taztpd.conf" ] && . taztpd.conf + +# Web Server functions + +# Output standardized header for valid requests +http_header() { + cat << EOT +HTTP/1.1 200 OK +EOT +} + +html_header() { + cat << EOT +Content-Type: text/html + +EOT +} + +text_header() { + cat << EOT +Content-Type: text/plain + +EOT +} + +# List all files in a directory +directory_listing() { + cat << EOT + + + + Index of $url + + `css_style` + +EOT + echo -e "

$PAGE_HEADING $url

" + echo "" + taztpd_footer + echo -e "\n" +} + +# Handled by an external CSS file +css_style() { + echo '' +} + +taztpd_footer() { + echo "
$SERVER_NAME
" +} + +# Handle file type by extension +handle_filetype() { + case $file in + # Check for HTML first for fast anser (most requests) + *.html|*.htm) type="text/html";; + *.css) type="text/css" ;; + *.xml) type="text/xml" ;; + *.jpg|*.jpeg) type="image/jpeg" ;; + *.png) type="image/png" ;; + *.tar.gz) "application/x-tgz" ;; + *.tazpkg) "application/x-tazpkg" ;; + *) + # Default to plain text document + type=text/plain ;; + esac + cat << EOT +Content-Type: $type + +EOT +} + +# Server main function +read_request() { + # Record the HTTP request + read request + while /bin/true; do + read header + [ "$header" == $'\r' ] && break; + done + # Extract URL from the request string + url="${request#GET }" + url="${url% HTTP/*}" + query="${url#*\?}" + url="${url%%\?*}" + # Handle CGI scripts + if [ "$query" != "$url" -a -x "$file" ]; then + export QUERY_STRING="$query" + http_header + exec "$file" + echo -e "\r" + exit 0 + fi + # Locate the wanted file + file="${SERVER_ROOT}$url" + # First try to display requested page + if [ -f "$file" ]; then + http_header + handle_filetype + cat "$file" + echo -e "\r" && exit 0 + fi + # Requested URL may be a directory + if [ -d "$file" ]; then + http_header + if [ -f "$file/index.html" ]; then + file=$file/index.html + echo -e "Content-Type: text/html\r" + echo -e "\r" + cat "$file" + echo -e "\r" && exit 0 + fi + html_header + directory_listing + echo -e "\r" + # 404 error + else + cat << EOT +HTTP/1.1 404 Not Found +Content-Type: text/html + + + + + 404 Not Found + + `css_style` + + +

404 Not Found

+

$NOT_FOUND

+ `taztpd_footer` + + +EOT + echo -e "\r" + fi +} + +# Web Server commands + +case $1 in + status|-s) + echo "" + ps | grep taztpd + echo "" ;; + dev|-d) + # Devel mode by keeping the hand + echo "Starting Web Server on port: $SERVER_PORT (dev mode)" + while true + do + nc -l -p $SERVER_PORT -e /home/pankso/Public/taztpd/taztpd + done ;; + nc|-n) + # Use nc to listen on a port and execute TazTPD on a request + echo "Starting Web Server on port: $SERVER_PORT" + (while true + do + nc -l -p $SERVER_PORT -e /home/pankso/Public/taztpd/taztpd + done) & ;; + usage|*help|-u|*-h) + # Display a short usage + echo "Usage: `basename $0` [status|dev|nc]" ;; + *) + read_request ;; +esac +exit 0 diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/taztpd.cgi --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/taztpd.cgi Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,34 @@ +#!/bin/sh +# +# CGI/Shell script example for TazTPD Web Server +# +echo "Content-Type: text/html" +echo "" + +var=${QUERY_STRING#var=} + +# xHTML 5 output +cat << EOT + + + + TazTPD CGI + + + +

TazTPD and CGI

+

+ Entered form value: $var +

+
+ +
+

+ `date '+%Y-%m-%d %H:%M'` +

+ + +EOT diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/taztpd.conf --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/taztpd.conf Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,15 @@ +# taztpd.conf: TazTPD Web Server configuration file +# + +# Path to the files to serve and HTTP port +SERVER_ROOT="$HOME/Public" +SERVER_PORT="8080" + +# Page heading is followed by $url +PAGE_HEADING="Index of" +PARENT_DIR="Parent Directory" +SERVER_NAME="Served by TazTPD (SliTaz GNU/Linux)" +SERVER_CSS="/usr/share/taztpd/taztpd.css" + +# 404 Not Found +NOT_FOUND="The requested URL was not found on the server" diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/taztpd.css --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/taztpd.css Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,25 @@ +/* CSS style for TazTPD Web Server listing page and 404 error */ + +body { + margin: 0; + font: 0.9em sans-serif; +} + +h1 { + color: #4d4d4d; + margin: 0; + padding: 5px; + border-bottom: 1px dotted #ddd; +} + +a { text-decoration: none; color: #215090; } +a:hover { text-decoration: underline; color: blue; } +p { padding: 5px; } + +#footer { + font-size: 84%; + padding: 5px; + width: 100%; + border-top: 1px dotted #ddd; + color: #888888; +} diff -r 8d54319f6ebc -r 7c1765f8e1e6 taztpd/taztpd.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/taztpd/taztpd.html Wed Apr 06 02:09:31 2011 +0200 @@ -0,0 +1,36 @@ + + + + TazTPD Web Server + + + + + +

TazTPD

+ +
+

+ A Web server in a nutSHell! +

+

+ TazTPD is a very small Web Server all in Shell script who can + be run in user space. TazTPD is not designed to be used in + production but for fun, for testing, to easily and on demand + serve static files or dynamic content with CGI scripts. +

+

CGI testing

+
+ +
+
+ + +