From 914662b3dbecfe34126991cfdb2a9049f2a9a29a Mon Sep 17 00:00:00 2001 From: paul Date: Sat, 13 Jun 2015 13:45:25 +0000 Subject: [PATCH 1/1] initial commit svn path=/trunk/; revision=1 --- action.sh | 24 +++++++++++++++++++ cgi.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++ index.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ page.sh | 34 +++++++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100755 action.sh create mode 100755 cgi.sh create mode 100755 index.sh create mode 100755 page.sh diff --git a/action.sh b/action.sh new file mode 100755 index 0000000..0077575 --- /dev/null +++ b/action.sh @@ -0,0 +1,24 @@ +#!/bin/zsh + +# Copyright 2014 Paul Hänsch +# +# This file is part of Serve0. +# +# Serve0 is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Serve0 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Serve0. If not, see . + +ACTION="$(echo "$_GET[\"action\"]" |egrep '^[a-zA-Z0-9_-]+$')" +ACTION="${_EXEC}/actions/${ACTION}.sh" +[ -x "$ACTION" ] || echo -n "Location: http://$HTTP_HOST/?page=error\n\n" + +. $ACTION diff --git a/cgi.sh b/cgi.sh new file mode 100755 index 0000000..c70c4d2 --- /dev/null +++ b/cgi.sh @@ -0,0 +1,58 @@ +#!/bin/zsh + +# Copyright 2014 Paul Hänsch +# +# This file is part of Serve0. +# +# Serve0 is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Serve0 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Serve0. If not, see . + +declare -A _GET +declare -A _POST +declare -A _REF + +cgi_get() { # parse HTTP GET string + echo "$QUERY_STRING" |tr '&' '\n' |while read query; do + key="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + _GET["$key"]="$(echo -e "$(echo "$val" |sed 's:+: :g;s:%:\\x:g')")" + done +} + +cgi_post() { # parse HTTP POST string + sed -u 1q |tr '&' '\n' |while read query; do + key="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + value="$(echo -e "$(echo "$val" |sed 's:+: :g;s:%:\\x:g')")" + if [ -n "$_POST[\"$key\"]" ]; then + n=0 + while [ -n "$_POST[\"$key$n\"]" ]; do n=$(($n + 1)); done + _POST["$key$n"]="$value" + else + _POST["$key"]="$value" + fi + #debug "_POST[$key] => $value" + done +} + +cgi_refdata() { # Parse GET data from referer + echo "$HTTP_REFERER" |cut -d'?' -f2- |tr '&' '\n' |while read query; do + key="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + _REF["$key"]="$(echo -e "$(echo "$val" |sed 's:+: :g;s:%:\\x:g')")" + done +} + +urlsave(){ + echo -E "$*" |sed 's:%:\%25:g;s:\?:\%3F:g;s:&:\%26:g;s:'\'':\%27:g;s: :\%20:g;s:!:\%21:g;s:(:\%28:g;s:):\%29:g;s:":\%22:g;' +} diff --git a/index.sh b/index.sh new file mode 100755 index 0000000..380baef --- /dev/null +++ b/index.sh @@ -0,0 +1,70 @@ +#!/bin/zsh + +# Copyright 2014, 2015 Paul Hänsch +# +# This file is part of Serve0. +# +# Serve0 is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Serve0 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Serve0. If not, see . + +export LC_ALL=C + +# basic functions +die() { + echo "$@" >&2 + exit 1 +} +debug() { #change to false to disable debugging + #true && echo "$*" >>debug + true && [ -n "$*" ] && echo -E "$*" >>/dev/stderr + true && [ -z "$*" ] && tee /dev/stderr +} + +# this program is supposed to be symlinked into a http root directory +# we will use the http root as object storage (data directory) and call sub +# programs from the directory in which the real executable resides +# therefore we need to identify the code and data directories _EXEC and _DATA +call="$0" +real="$(readlink -f $call)" +_EXEC="$(dirname "$real")" #execution directory +_DATA="$(dirname "$call")" #storage directory + +[ -w "$_DATA" ] && [ -d "$_DATA" ] || die "storage directory must be writable" + +# create directories for object storage +for each in "$@"; do + [ ! -e "$_DATA/$each" ] && mkdir "$_DATA/$each" + [ -w "$_DATA/$each" ] && [ -d "$_DATA/$each" ] || die "storage \"$_DATA/$each\" must be a writable directory" +done + +# create htaccess file +[ -f "$_DATA/.htaccess" ] || cat >"$_DATA/.htaccess" <. + +echo -n "Content-Type: text/html;charset=utf-8\n\n" + +PAGE="$(echo "$_GET[\"page\"]" |egrep '^[a-zA-Z0-9_-]+$')" +PAGE="${_EXEC}/pages/${PAGE}.sh" +[ -x "$PAGE" ] || PAGE="${_EXEC}/pages/error.sh" + +NAVIGATION() { + for each in "${_EXEC}"/pages/*.sh; do + link="$(echo "$each" |sed -r "s:^.*/([^/]*)\.sh$:\1:")" + title="$($each title)" + [ -n "$title" ] && echo "/?page=$link $title" + done +} + +. ${_EXEC}/templates/frame.html.sh -- 2.39.2