]> git.plutz.net Git - shcgi/commitdiff
initial commit
authorpaul <paul@plutz.net>
Sat, 13 Jun 2015 13:45:25 +0000 (13:45 +0000)
committerpaul <paul@plutz.net>
Sat, 13 Jun 2015 13:45:25 +0000 (13:45 +0000)
svn path=/trunk/; revision=1

action.sh [new file with mode: 0755]
cgi.sh [new file with mode: 0755]
index.sh [new file with mode: 0755]
page.sh [new file with mode: 0755]

diff --git a/action.sh b/action.sh
new file mode 100755 (executable)
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 <http://www.gnu.org/licenses/>. 
+
+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 (executable)
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 <http://www.gnu.org/licenses/>. 
+
+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 (executable)
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 <http://www.gnu.org/licenses/>. 
+
+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" <<EOF
+Options                +ExecCGI
+AddHandler     cgi-script .cgi
+DirectoryIndex index.cgi
+EOF
+[ -f "$_DATA/.htaccess" ] || die "no htaccess file present and unable to create one"
+
+. "$_EXEC/shcgi/cgi.sh"
+
+cgi_get
+
+[ -x "$_EXEC/constants.sh" ] && . "$_EXEC/constants.sh"
+
+if [ -n "$_GET[\"action\"]" ]; then
+  . "$_EXEC/shcgi/action.sh"
+elif [ -n "$_GET[\"export\"]" ]; then
+  . "$_EXEC/shcgi/export.sh"
+else
+  . "$_EXEC/shcgi/page.sh"
+fi
diff --git a/page.sh b/page.sh
new file mode 100755 (executable)
index 0000000..fce83fb
--- /dev/null
+++ b/page.sh
@@ -0,0 +1,34 @@
+#!/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 <http://www.gnu.org/licenses/>. 
+
+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