]> git.plutz.net Git - cgilite/blob - index.cgi
021c516c38849188473d88581406b3762d7d5be6
[cgilite] / index.cgi
1 #!/bin/zsh
2
3 # Copyright 2014 - 2016 Paul Hänsch
4 #
5 # This file is part of shcgi.
6
7 # shcgi is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # shcgi is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with shcgi.  If not, see <http://www.gnu.org/licenses/>. 
19
20 export LC_ALL=C LANG=C
21
22 # this program is supposed to be symlinked into a http root directory
23 # we will use the http root as object storage (data directory) and call sub
24 # programs from the directory in which the real executable resides
25 # therefore we need to identify the code and data directories _EXEC and _DATA
26 call="$0"
27 real="$(readlink -f $call)"
28 export _DATA="$(dirname "$call")"  #storage directory
29 export _EXEC="${real%/shcgi/index.cgi}"  #execution directory
30
31 . "$_EXEC/shcgi/misc.sh"
32
33 if [ "$1" = '--server' -o  "$1" = '--inetd' -o "$1" = '--ncat' ]; then
34   . "$_EXEC/shcgi/server.sh" $@
35 else
36   HTTP_format(){ cat }
37 fi
38
39 # put debug options in the local.opts file
40 . "$_EXEC/shcgi/debug.sh"
41 [ -r "$_DATA/local.opts" ] && . "$_DATA/local.opts"
42
43 . "$_EXEC/shcgi/cgi.sh"
44
45 [ -x "$_EXEC/globals.sh" ] && . "$_EXEC/globals.sh"
46
47   PAGE=$(validate   "${PAGE:-${_GET[page]}}"   '[a-zA-Z0-9_-]+' '')
48 ACTION=$(validate "${ACTION:-${_GET[action]}}" '[a-zA-Z0-9_-]+' '')
49 STATIC=$(invalidate "${STATIC:-${_GET[static]}}" '(^|.*/)\.\./.*' '')
50
51 if [ -n "$STATIC" -a -e "${_EXEC}/static/${STATIC}" ]; then
52   . "$_EXEC/shcgi/static.sh" "$_EXEC/static/$STATIC"
53 elif [ -n "$ACTION" -a -x "${_EXEC}/actions/${ACTION}.sh" ]; then
54   . "${_EXEC}/actions/${ACTION}.sh"
55 elif   [ -n "$PAGE"   -a -x "${_EXEC}/pages/${PAGE}.sh" ]; then
56   . "$_EXEC/shcgi/page.sh"
57 else
58   printf 'HTTP/1.1 404 Not Found\r\n'
59   PAGE=error
60   . "$_EXEC/shcgi/page.sh"
61 fi |HTTP_format $@