3 # Copyright 2017 - 2020 Paul Hänsch
6 # A collection of posix shell functions for writing CGI scripts.
8 # CGIlite is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # CGIlite is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with CGIlite. If not, see <http://www.gnu.org/licenses/>.
21 [ -n "$include_cgilite" ] && return 0
22 # guard set after webserver part
24 # ksh and zsh workaround
25 # set -o posix # ksh, not portable
26 setopt -o OCTAL_ZEROES 2>&-
35 [ $# -eq 0 ] && str="$(cat)" || str="$*"
37 seg=${str%%/*}; str="${str#*/}"
39 ..) out="${out%/}"; out="${out%/*}/";;
40 .|'') out="${out%/}/";;
41 *) out="${out%/}/${seg}";;
43 [ "$seg" = "$str" ] && break
45 [ "${str}" -a "${out}" ] && printf %s "$out" || printf %s/ "${out%/}"
49 s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X;
50 # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
51 s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
52 s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
53 s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
54 s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
55 s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
59 printf -- "$(printf %s "$1" |sed -E "$HEX_DECODE")"
62 if [ -z "$REQUEST_METHOD" ]; then
63 # no webserver variables means we are running via inetd / ncat
64 # so use builtin web server
66 # Use env from inetd as webserver variables
67 REMOTE_ADDR="${TCPREMOTEIP}"
68 SERVER_NAME="${TCPLOCALIP}"
69 SERVER_PORT="${TCPLOCALPORT}"
71 # Wait 2 seconds for request or kill connection through watchdog.
72 # Once Request is received the watchdog will be suspended (killed).
73 # At the end of the loop the watchdog will be restarted to enable
74 # timeout for the subsequent request.
76 (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
77 while read REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL; do
78 [ "${SERVER_PROTOCOL#HTTP/1.[01]${CR}}" ] && break
79 kill $cgilite_watchdog
81 SERVER_PROTOCOL="${SERVER_PROTOCOL%${CR}}"
82 PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}" |PATH)"
83 [ "${REQUEST_URI}" = "${REQUEST_URI#*\?}" ] \
85 || QUERY_STRING="${REQUEST_URI#*\?}"
86 cgilite_headers=''; while read -r hl; do
87 hl="${hl%${CR}}"; [ "$hl" ] || break
89 'Content-Length: '*) CONTENT_LENGTH="${hl#*: }";;
90 'Content-Type: '*) CONTENT_TYPE="${hl#*: }";;
92 cgilite_headers="${cgilite_headers}${hl}${BR}"
95 export REMOTE_ADDR SERVER_NAME SERVER_PORT REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL \
96 PATH_INFO QUERY_STRING CONTENT_TYPE CONTENT_LENGTH
98 # Try to serve multiple requests, provided that script serves a
99 # Content-Length header.
100 # Without Content-Length header, connection will terminate after
103 cgilite_status='200 OK'; cgilite_response=''; cgilite_cl="Connection: close${CR}${BR}";
104 . "$0" | while read -r l; do case $l in
106 cgilite_status="${l#Status: }";;
109 cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
111 cgilite_cl="${l}${BR}";;
112 $CR) printf '%s %s\r\n%s%s\r\n' \
113 'HTTP/1.1' "${cgilite_status%${CR}}" \
114 "${cgilite_response}${cgilite_response:+${BR}}" "${cgilite_cl}"
116 [ "${cgilite_cl#Connection}" = "${cgilite_cl}" ]; exit;;
117 *) cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
118 esac; done || exit 0;
119 (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
121 kill $cgilite_watchdog
127 if [ "${REQUEST_METHOD}" = POST -a "${CONTENT_LENGTH:-0}" -gt 0 -a \
128 "${CONTENT_TYPE}" = "application/x-www-form-urlencoded" ]; then
129 cgilite_post="$(head -c "$CONTENT_LENGTH")"
132 debug(){ [ $# -gt 0 ] && printf '%s\n' "$@" >&2 || tee -a /dev/stderr; }
133 [ "${DEBUG+x}" ] && env >&2
137 | grep -oE '&'"$2"'=[^&]*' \
142 local str="&$1" name="$2" cnt="${3:-1}"
143 while [ $cnt -gt 0 ]; do
144 [ "${str}" = "${str#*&${name}=}" ] && return 1
145 str="${str#*&${name}=}"
148 printf -- "$(printf %s "${str%%&*}" |sed -E 's;\+; ;g;'"$HEX_DECODE")"
153 while [ "${str#*&}" != "${str}" ]; do
155 printf '%s\n' "${str%%=*}"
160 GET(){ cgilite_value "${QUERY_STRING}" $@; }
161 GET_COUNT(){ cgilite_count "${QUERY_STRING}" $1; }
162 GET_KEYS(){ cgilite_keys "${QUERY_STRING}"; }
164 POST(){ cgilite_value "${cgilite_post}" $@; }
165 POST_COUNT(){ cgilite_count "${cgilite_post}" $1; }
166 POST_KEYS(){ cgilite_keys "${cgilite_post}"; }
168 REF(){ cgilite_value "${HTTP_REFERER#*\?}" $@; }
169 REF_COUNT(){ cgilite_count "${HTTP_REFERER#*\?}" $1; }
170 REF_KEYS(){ cgilite_keys "${HTTP_REFERER#*\?}"; }
173 # Read value of header line. Use this instead of
174 # referencing HTTP_* environment variables.
175 if [ -n "${cgilite_headers+x}" ]; then
176 local str="${BR}${cgilite_headers}"
177 [ "${str}" = "${str#*${BR}${1}: }" ] && return 1
178 str="${str#*${BR}${1}: }"
179 printf %s "${str%%${BR}*}"
181 local var="HTTP_$(printf %s "$1" |tr a-z- A-Z-)"
182 eval "[ \"\$$var\" ] && printf %s \"\$$var\" || return 1"
183 # eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\""
190 | grep -oE '(^|; ?)'"$1"'=[^;]*' \
191 | sed -En "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
196 # Escape HTML cahracters
197 # Also escape [, ], and \n for use in html-sh
199 [ $# -eq 0 ] && str="$(cat)" || str="$*"
202 \&*) out="${out}&";;
203 \<*) out="${out}<";;
204 \>*) out="${out}>";;
205 \"*) out="${out}"";;
206 \'*) out="${out}'";;
207 \[*) out="${out}[";;
208 \]*) out="${out}]";;
209 "${CR}"*) out="${out}
";;
210 "${BR}"*) out="${out}
";;
211 *) out="${out}${str%"${str#?}"}";;
219 # Escape pathes, so they can be used in link tags and HTTP Headers
221 [ $# -eq 0 ] && str="$(cat)" || str="$*"
224 \&*) out="${out}%26";;
225 \"*) out="${out}%22";;
226 \'*) out="${out}%27";;
227 \?*) out="${out}%3F";;
228 \#*) out="${out}%23";;
229 \[*) out="${out}%5B";;
230 \]*) out="${out}%5D";;
231 \ *) out="${out}%20";;
232 " "*) out="${out}%09";;
233 "${CR}"*) out="${out}%0D";;
234 "${BR}"*) out="${out}%0A";;
235 %*) out="${out}%25";;
236 *) out="${out}${str%"${str#?}"}";;
244 # Param: session | +seconds | [date]
246 # Param: Path= | Domain= | Secure
249 ''|0|session) expire='';;
250 [+-][0-9]*) expire="$(date -R -d @$(($(date +%s) + $1)))";;
251 *) expire="$(date -R -d "$1")";;
255 printf 'Set-Cookie: %s; HttpOnly; SameSite=Lax' "$cookie"
256 [ -n "$expire" ] && printf '; Expires=%s' "${expire%+????}${expire:+GMT}"
257 [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
262 printf '%s: %s\r\n' \
263 Status "303 See Other" \