+++ /dev/null
-#!/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="$(printf %s "${_GET[action]}" |egrep '^[a-zA-Z0-9_-]+$')"
-ACTION="${_EXEC}/actions/${ACTION}.sh"
-
-if [ -x "$ACTION" ]; then
- debug "trying to execute $ACTION"
- . $ACTION
-else
- debug "unable to execute $ACTION"
- printf "Location: ?p=error\n\n"
-fi
-
#!/bin/zsh
-# Copyright 2014, 2015 Paul Hänsch
+# Copyright 2014 - 2016 Paul Hänsch
#
# This file is part of shcgi.
#
_EXEC="${real%/shcgi/index.cgi}" #execution directory
. "$_EXEC/shcgi/misc.sh"
+
# put debug options in the local.opts file
. "$_EXEC/shcgi/debug.sh"
[ -r "$_DATA/local.opts" ] && . "$_DATA/local.opts"
[ -x "$_EXEC/globals.sh" ] && . "$_EXEC/globals.sh"
-if [ -n "${_GET[action]}" ]; then
- . "$_EXEC/shcgi/action.sh"
+ PAGE=$(validate "${PAGE:-${_GET[page]}}" '[a-zA-Z0-9_-]+' '')
+ACTION=$(validate "${ACTION:-${_GET[action]}}" '[a-zA-Z0-9_-]+' '')
+STATIC=$(validate "${STATIC:-${_GET[static]}}" '[^\.]+' '')
+
+if [ -n "$PAGE" -a -x "${_EXEC}/pages/${PAGE}.sh" ]; then
+ . "$_EXEC/shcgi/page.sh"
+elif [ -n "$ACTION" -a -x "${_EXEC}/actions/${ACTION}.sh" ]; then
+ . "${_EXEC}/actions/${ACTION}.sh"
+elif [ -n "$STATIC" -a -e "${_EXEC}/static/${STATIC}" ]; then
+ . "$_EXEC/shcgi/static.sh"
else
+ printf 'HTTP/1.1 404 Not Found\r\n'
+ PAGE=error
. "$_EXEC/shcgi/page.sh"
fi
validate(){
# print value if value matches regex; otherwise print default
value="$1"
- regex="$(printf %s\\n "$2" \
- | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;'
- )" # ^^ escape only unescaped slash characters for later insertion
+ regex="$(printf %s\\n "$2" | sed -r ':X;s;(^|[^\\])((\\\\)*)/;\1\2\\/;g;tX')"
+ # ^^ escape only unescaped slash characters for later insertion
default="$3"
printf %s\\n "${value}" \
- | sed -rn "2q; /^(${regex})\$/{p;q}; a${default}"
+ | sed -rn "2q; /^(${regex})\$/{p;q}; a${default}
+ "
}
invalidate(){
# print default if value matches regex; otherwise print value
value="$1"
- regex="$(printf %s\\n "$2" \
- | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;'
- )" # ^^ escape only unescaped slash characters for later insertion
+ regex="$(printf %s\\n "$2" | sed -r ':X;s;(^|[^\\])((\\\\)*)/;\1\2\\/;g;tX')"
+ # ^^ escape only unescaped slash characters for later insertion
default="$3"
printf %s\\n "${value}" \
- | sed -rn "2q; /^(${regex})\$/{bd}; p;q; :d;a${default}"
+ | sed -rn "2q; /^(${regex})\$/{bX}; p;q; :X;a${default}
+ "
}
declare -A item_name
printf "Content-Type: text/html;charset=utf-8\n\n"
-PAGE=$(validate "${PAGE:-${_GET[page]}}" '[a-zA-Z0-9_-]+' error)
-[ -x "${_EXEC}/pages/${PAGE}.sh" ] || PAGE="error"
-
-[ -z "$NAVIGATION" ] && NAVIGATION=($(printf %s\\n "${_EXEC}"/pages/*.sh |sed -r 's;^.*/([^/]*)\.sh$;\1;'))
+[ -z "$NAVIGATION" ] \
+&& NAVIGATION=($(printf %s\\n "${_EXEC}"/pages/*.sh |sed -r 's;^.*/([^/]*)\.sh$;\1;'))
CSS="${CSS:-${_EXEC}/templates/${PAGE}.css.sh}"
BODY="${BODY:-${_EXEC}/templates/${PAGE}.html.sh}"
--- /dev/null
+#!/bin/zsh
+
+# Copyright 2016 Paul Hänsch
+#
+# This file is part of shcgi.
+#
+# shcgi 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.
+#
+# shcgi 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 shcgi. If not, see <http://www.gnu.org/licenses/>.
+
+
+unset length date file
+file="$_EXEC/static/$STATIC"
+date="$(stat -c %Y "$file")"
+
+if [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then
+ printf 'HTTP/1.1 403 Forbidden\n\n'
+elif [ "$date" = "$HTTP_IF_NONE_MATCH" ]; then
+ printf 'HTTP/1.1 304 Not Modified\n\n'
+else
+ length="$(stat -c %s "$file")"
+ magic="$(file -bi "$file")"
+
+ printf 'Etag: %s\r\n' "$date"
+ printf 'Content-Type: %s\r\n' "${magic:-all/all}"
+ printf 'Content-Length: %s\r\n' "$length"
+ printf '\r\n'
+
+ cat "$file"
+fi
+