]> git.plutz.net Git - cgilite/blob - index.cgi
limit stdin consumption to content length
[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 if [ "$1" = '--ncat' ]; then
23   export REMOTE_ADDR="${NCAT_REMOTE_ADDR}"
24   export SERVER_NAME="${NCAT_LOCAL_ADDR}"
25   export SERVER_PORT="${NCAT_LOCAL_PORT}"
26 else
27   # this program is supposed to be symlinked into a http root directory
28   # we will use the http root as object storage (data directory) and call sub
29   # programs from the directory in which the real executable resides
30   # therefore we need to identify the code and data directories _EXEC and _DATA
31   call="$0"
32   real="$(readlink -f $call)"
33   _DATA="$(dirname "$call")"  #storage directory
34   _EXEC="${real%/shcgi/index.cgi}"  #execution directory
35   
36   . "$_EXEC/shcgi/misc.sh"
37   
38   # put debug options in the local.opts file
39   . "$_EXEC/shcgi/debug.sh"
40   [ -r "$_DATA/local.opts" ] && . "$_DATA/local.opts"
41 fi
42
43 if [ "$1" = '--server' ]; then
44   shift 1
45   ncat -kle "$0 --ncat" $@
46   exit $?
47
48 elif [ "$1" = '--inetd' -o "$1" = '--ncat' ]; then
49   eval $(
50   sed -nr '
51     /^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$/{
52       h
53       s;(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;export REQUEST_METHOD='\''\1'\'';p
54       g
55       s;(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;\2;
56       s;'\'';'\''\\'\'''\'';g
57       s;^.*$;export PATH_INFO='\''&'\'';p
58       g
59       s;(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;\3;
60       s;'\'';'\''\\'\'''\'';g
61       s;^.*$;export QUERY_STRING='\''&'\'';p
62       g
63       s;(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;export SERVER_PROTOCOL='\''\4'\'';p
64       g
65     }
66   
67     /^[Pp][Rr][Oo][Xx][Yy]: /d
68   
69     /^[a-zA-Z_-]+: .*$/{
70       h
71       s;^[^:]+: (.*)\r$;\1;
72       s;'\'';'\''\\'\'''\'';g
73       s;^.*$;'\''&'\'';
74       x
75       s;: .*$;;
76       y;abcdefghijklmnopqrstuvwxyz-;ABCDEFGHIJKLMNOPQRSTUVWXYZ_;
77       s;^.+$;export HTTP_&=;
78       G
79       s;\n;;
80       p
81     }
82     /^\r?$/q
83   '
84   )
85 fi
86
87 . "$_EXEC/shcgi/cgi.sh"
88
89 [ -x "$_EXEC/globals.sh" ] && . "$_EXEC/globals.sh"
90
91   PAGE=$(validate   "${PAGE:-${_GET[page]}}"   '[a-zA-Z0-9_-]+' '')
92 ACTION=$(validate "${ACTION:-${_GET[action]}}" '[a-zA-Z0-9_-]+' '')
93 STATIC=$(invalidate "${STATIC:-${_GET[static]}}" '(^|.*/)\.\./.*' '')
94
95 if [ -n "$STATIC" -a -e "${_EXEC}/static/${STATIC}" ]; then
96   . "$_EXEC/shcgi/static.sh"
97 elif [ -n "$ACTION" -a -x "${_EXEC}/actions/${ACTION}.sh" ]; then
98   . "${_EXEC}/actions/${ACTION}.sh"
99 elif   [ -n "$PAGE"   -a -x "${_EXEC}/pages/${PAGE}.sh" ]; then
100   . "$_EXEC/shcgi/page.sh"
101 else
102   printf 'HTTP/1.1 404 Not Found\r\n'
103   PAGE=error
104   . "$_EXEC/shcgi/page.sh"
105 fi