3 # Copyright 2014,2015 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/>.
25 [ -z "$HTTP_REFERER" ] && HTTP_REFERER="./"
27 cgi_get() { # 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"
37 cgi_post() { # 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 if [ -n "${_POST[$key]}" ]; then
45 while [ -n "${_POST[$key$n]}" ]; do n=$(($n + 1)); done
46 _POST[$key$n]="$value"
50 debug "_POST[$key] => $value"
54 cgi_refdata() { # Parse GET data from referer
55 debug "== CGI DATA: REFERER =="
56 printf '%s\n' "$HTTP_REFERER" |cut -d'?' -f2- |tr '&' '\n' |while read query; do
57 key="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]+)=(.*)$:\1:')"
58 val="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]+)=(.*)$:\2:')"
59 _REF[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
60 debug "_REF[$key] => $val"
64 cgi_cookie() { # Parse GET data from referer
65 debug "== CGI DATA: COOKIE =="
66 printf '%s\n' "$HTTP_COOKIE" |tr ';' '\n' |while read query; do
67 key="$(printf %s "$query" |sed -r 's:^ *([a-zA-Z0-9_-]+)=(.*)$:\1:')"
68 val="$(printf %s "$query" |sed -r 's:^ *([a-zA-Z0-9_-]+)=(.*)$:\2:')"
69 _COOKIE[$key]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")"
70 debug "_COOKIE[$key] => $val"