]> git.plutz.net Git - cgilite/blob - server.sh
use proper cgi for response headers, switch cache machanism to timestamps
[cgilite] / server.sh
1 #!/bin/zsh
2
3 # Copyright 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 if [ "$1" = '--server' ]; then
21   shift 1
22   ncat -klc "$call --ncat" $@
23   exit $?
24 fi
25
26 export REMOTE_ADDR="${NCAT_REMOTE_ADDR}"
27 export SERVER_NAME="${NCAT_LOCAL_ADDR}"
28 export SERVER_PORT="${NCAT_LOCAL_PORT}"
29
30 eval $(
31 sed -unr '
32   w/dev/stderr
33   /^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$/{
34     h
35     s;^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;export REQUEST_METHOD='\''\1'\'';p
36     g
37     s;^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;\2;
38     s;'\'';'\''\\'\'''\'';g
39     s;^.*$;export PATH_INFO='\''&'\'';p
40     g
41     s;^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;\3;
42     s;'\'';'\''\\'\'''\'';g
43     s;^.*$;export QUERY_STRING='\''&'\'';p
44     g
45     s;^(GET|HEAD|POST) ([^\?]*)\??(.+)? (HTTP\/[0-9]\.[0-9])\r?$;export SERVER_PROTOCOL='\''\4'\'';p
46     g
47   }
48
49   /^[Pp][Rr][Oo][Xx][Yy]: /d
50
51   /^[a-zA-Z_-]+: .*$/{
52     h
53     s;^[^:]+: (.*)\r$;\1;
54     s;'\'';'\''\\'\'''\'';g
55     s;^.*$;'\''&'\'';
56     x
57     s;: .*$;;
58     y;abcdefghijklmnopqrstuvwxyz-;ABCDEFGHIJKLMNOPQRSTUVWXYZ_;
59     s;^.+$;export HTTP_&=;
60     G
61     s;\n;;
62     p
63   }
64   /^\r?$/q
65 '
66 )
67
68 HTTP_format(){
69   LC_ALL=C sed -rn '
70   :A
71   /^\r?$/!{H;n;bA}
72   
73   g
74   s;^(\r?\n)*;;;
75   s;([^\r])\n;\1\r\n;g;
76   aConnection: close\r\n\r
77   /^Status: *[1-5][0-9][0-9] .*/{s;^Status: *;HTTP/1.1 ;; bX}
78   s;^(.+\n)?Location: .*$;HTTP/1.1 302 Found\r\n&;
79   s;^(.+\n)?Content-Type: .*$;HTTP/1.1 200 OK\r\n&;
80   
81   :X
82   p
83   $q
84   n
85   bX
86   '
87 }
88
89 PATH_INFO="$(invalidate "${PATH_INFO}" '(^|.*/)\.\./.*' '')"
90
91 if [ -z "$QUERY_STRING" -a -f "$_DATA/$PATH_INFO" ]; then
92   . "$_EXEC/shcgi/static.sh" "$_DATA/$PATH_INFO"
93   exit 0
94 fi |HTTP_format