3 # Copyright 2014 - 2016 Paul Hänsch
5 # This file is part of shcgi.
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.
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.
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/>.
20 unset _GET _POST _REF _COOKIE
26 [ -z "$HTTP_REFERER" ] && HTTP_REFERER="./"
28 # parse HTTP GET string
29 debug "== CGI DATA: GET =="
30 printf '%s\n' "$QUERY_STRING" |tr '&' '\n' |while read query; do
31 key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
32 val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
33 _GET[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
34 debug "_GET[$key] => $val"
37 if [ "$REQUEST_METHOD" = POST -a "${HTTP_CONTENT_LENGTH:=$CONTENT_LENGTH}" -gt 0 ]; then
38 # parse HTTP POST string
39 debug "== CGI DATA: POST =="
40 head -c "$HTTP_CONTENT_LENGTH" \
41 | sed -un 's;&;\n;g; p; q' \
42 | while read query; do
43 key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
44 val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
45 value="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g;')")"
47 if [ -n "${_POST[$key$n]+x}" ]; then
49 while [ -n "${_POST[$key$n]+x}" ]; do n=$(($n + 1)); done
51 _POST[$key$n]="$value"
52 debug "_POST[$key$n] => $value"
56 cgi_refdata() { # Parse GET data from referer
57 debug "== CGI DATA: REFERER =="
58 printf '%s\n' "${HTTP_REFERER#*\?}" |tr '&' '\n' |while read query; do
59 key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
60 val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
61 _REF[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
62 debug "_REF[$key] => $val"
66 cgi_cookie() { # Parse GET data from referer
67 debug "== CGI DATA: COOKIE =="
68 printf '%s\n' "$HTTP_COOKIE" |tr ';' '\n' |while read query; do
69 key="$(printf %s "$query" |sed -r 's:^ *([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
70 val="$(printf %s "$query" |sed -r 's:^ *([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
71 _COOKIE[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
72 debug "_COOKIE[$key] => $val"
77 # escape HTML code from string
89 # Code every character in URL escape hex format
90 # except alphanumeric ascii
93 | hexdump -v -e '/1 ",%02X"' \
95 | sed 's;%30;0;g; s;%31;1;g; s;%32;2;g; s;%33;3;g; s;%34;4;g; s;%35;5;g;
96 s;%36;6;g; s;%37;7;g; s;%38;8;g; s;%39;9;g;
97 s;%41;A;g; s;%42;B;g; s;%43;C;g; s;%44;D;g; s;%45;E;g; s;%46;F;g;
98 s;%47;G;g; s;%48;H;g; s;%49;I;g; s;%4A;J;g; s;%4B;K;g; s;%4C;L;g;
99 s;%4D;M;g; s;%4E;N;g; s;%4F;O;g; s;%50;P;g; s;%51;Q;g; s;%52;R;g;
100 s;%53;S;g; s;%54;T;g; s;%55;U;g; s;%56;V;g; s;%57;W;g; s;%58;X;g;
101 s;%59;Y;g; s;%5A;Z;g;
102 s;%61;a;g; s;%62;b;g; s;%63;c;g; s;%64;d;g; s;%65;e;g; s;%66;f;g;
103 s;%67;g;g; s;%68;h;g; s;%69;i;g; s;%6A;j;g; s;%6B;k;g; s;%6C;l;g;
104 s;%6D;m;g; s;%6E;n;g; s;%6F;o;g; s;%70;p;g; s;%71;q;g; s;%72;r;g;
105 s;%73;s;g; s;%74;t;g; s;%75;u;g; s;%76;v;g; s;%77;w;g; s;%78;x;g;
106 s;%79;y;g; s;%7A;z;g;'
110 # Code every character in HTML escape hex format
111 # except alphanumerig ascii
114 | iconv -f utf-8 -t utf32le \
115 | hexdump -v -e '/4 "&#x%02X;"' \
116 | sed 's;0\;;0;g; s;1\;;1;g; s;2\;;2;g; s;3\;;3;g; s;4\;;4;g; s;5\;;5;g;
117 s;6\;;6;g; s;7\;;7;g; s;8\;;8;g; s;9\;;9;g;
118 s;A\;;A;g; s;B\;;B;g; s;C\;;C;g; s;D\;;D;g; s;E\;;E;g; s;F\;;F;g;
119 s;G\;;G;g; s;H\;;H;g; s;I\;;I;g; s;J\;;J;g; s;K\;;K;g; s;L\;;L;g;
120 s;M\;;M;g; s;N\;;N;g; s;O\;;O;g; s;P\;;P;g; s;Q\;;Q;g; s;R\;;R;g;
121 s;S\;;S;g; s;T\;;T;g; s;U\;;U;g; s;V\;;V;g; s;W\;;W;g; s;X\;;X;g;
122 s;Y\;;Y;g; s;Z\;;Z;g;
123 s;a\;;a;g; s;b\;;b;g; s;c\;;c;g; s;d\;;d;g; s;e\;;e;g; s;f\;;f;g;
124 s;g\;;g;g; s;h\;;h;g; s;i\;;i;g; s;j\;;j;g; s;k\;;k;g; s;l\;;l;g;
125 s;m\;;m;g; s;n\;;n;g; s;o\;;o;g; s;p\;;p;g; s;q\;;q;g; s;r\;;r;g;
126 s;s\;;s;g; s;t\;;t;g; s;u\;;u;g; s;v\;;v;g; s;w\;;w;g; s;x\;;x;g;
127 s;y\;;y;g; s;z\;;z;g;'
131 printf '%s\r\n\r\n' "Location: $*"
137 session|0) expire='';;
138 ''|default) expire="$(LANG=C date -d "+ 1 week" +'%a, %d %b %Y %T %Z')";;
139 *) expire="$(LANG=C date -d "$1" +'%a, %d %b %Y %T %Z' 2>&-)";;
143 printf 'Set-Cookie: %s' "$cookie"
144 [ -n "$expire" ] && printf '; Expires=%s' "$expire"
145 [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"