]> git.plutz.net Git - cgilite/blob - cgi.sh
allow dot in parameter keys
[cgilite] / cgi.sh
1 #!/bin/zsh
2
3 # Copyright 2014,2015 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 declare -A _GET
21 declare -A _POST
22 declare -A _REF
23 declare -A _COOKIE
24
25 [ -z "$HTTP_REFERER" ] && HTTP_REFERER="./"
26
27 # parse HTTP GET string
28 debug "== CGI DATA: GET =="
29 printf '%s\n' "$QUERY_STRING" |tr '&' '\n' |while read query; do
30   key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
31   val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
32   _GET[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
33   debug "_GET[$key] => $val"
34 done
35
36 if [ "$REQUEST_METHOD" = POST ]; then
37   # parse HTTP POST string
38   debug "== CGI DATA: POST =="
39   sed -u 1q |tr '&' '\n' |while read query; do
40     key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
41     val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
42     value="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g;')")"
43     n=''
44     if [ -n "${_POST[$key$n]+x}" ]; then
45       n=0
46       while [ -n "${_POST[$key$n]+x}" ]; do n=$(($n + 1)); done
47     fi
48     _POST[$key$n]="$value"
49     debug "_POST[$key$n] => $value"
50   done
51 fi
52
53 cgi_refdata() { # Parse GET data from referer
54   debug "== CGI DATA: REFERER =="
55   printf '%s\n' "${HTTP_REFERER#*\?}" |tr '&' '\n' |while read query; do
56     key="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
57     val="$(printf %s "$query" |sed -r 's:^([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
58     _REF[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
59     debug "_REF[$key] => $val"
60   done
61 }
62
63 cgi_cookie() { # Parse GET data from referer
64   debug "== CGI DATA: COOKIE =="
65   printf '%s\n' "$HTTP_COOKIE" |tr ';' '\n' |while read query; do
66     key="$(printf %s "$query" |sed -r 's:^ *([\.a-zA-Z0-9_-]+)=(.*)$:\1:')"
67     val="$(printf %s "$query" |sed -r 's:^ *([\.a-zA-Z0-9_-]+)=(.*)$:\2:')"
68     _COOKIE[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
69     debug "_COOKIE[$key] => $val"
70   done
71 }
72
73 urlsafe(){
74   printf %s "$*" \
75   | sed 's;%;%25;g;
76          s;\?;%3f;g;
77          s;#;%23;g;
78          s;<;%3c;g;
79          s;>;%3e;g;
80          s;&;%26;g;
81          s;";%22;g;
82          s;'\'';%27;g;'
83 }
84
85 htmlsafe(){
86   printf %s "$*" \
87   | sed 's;<;\&lt\;;g;
88          s;>;\&gt\;;g;
89          s;&;\&amp\;;g;
90          s;";\&quot\;;g;
91          s;'\'';\&apos\;;g;'
92 }
93
94 redirect(){
95   printf '%s\n\n' "Location: $*"
96   exit 0
97 }
98
99 set_cookie(){
100   case "$1" in
101     session|0)  expire='';;
102     ''|default) expire="$(LANG=C date -d "+ 1 week" +'%a, %d %b %Y %T %Z')";;
103     *)          expire="$(LANG=C date -d "$1" +'%a, %d %b %Y %T %Z' 2>&-)";;
104   esac
105   cookie="$2"
106   
107   printf 'Set-Cookie: %s' "$cookie"
108   [ -n "$expire" ] && printf '; Expires=%s' "$expire" 
109   [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
110   printf '\n'
111 }