]> git.plutz.net Git - cgilite/blobdiff - cgilite.sh
allow empty script headers
[cgilite] / cgilite.sh
index 996fe9daa4befceac6e7461b7e4755e03feebe9c..4c6de8ca6b784a5a361fb5cd8d70208c1a4d701d 100755 (executable)
@@ -22,8 +22,9 @@
 # set -o posix # ksh, not portable
 setopt -o OCTAL_ZEROES 2>&-
 
-BR="$(printf '\n')"
-CR="$(printf '\r')"
+CR="\r"
+BR='
+'
 cgilite_timeout=2
 
 HEADER(){
@@ -31,7 +32,7 @@ HEADER(){
   # referencing HTTP_* environment variables.
   if [ -n "${cgilite_headers+x}" ]; then
     printf %s "$cgilite_headers" \
-    | sed -rn 's;^'"${1}"': ([^\r]+)\r?$;\1;i; tX; d; :X;p;q;'
+    | sed -En 's;^'"${1}"': ([^\r]+)\r?$;\1;i; tX; d; :X;p;q;'
   else
     eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\""
   fi
@@ -48,7 +49,7 @@ HEX_DECODE='
 '
 
 HEX_DECODE(){
-  printf "$(printf %s "$1" |sed -r "$HEX_DECODE")"
+  printf -- "$(printf %s "$1" |sed -E "$HEX_DECODE")"
 }
 
 if [ -z "$REQUEST_METHOD" ]; then
@@ -70,7 +71,7 @@ if [ -z "$REQUEST_METHOD" ]; then
     kill $cgilite_watchdog
     PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}")"
     QUERY_STRING="${REQUEST_URI#*\?}"
-    cgilite_headers="$(sed -u '/^\r\?$/q')"
+    cgilite_headers="$(while read -r hl; do [ "${hl%${CR}}" ] && printf '%s\n' "$hl" || break; done )"
 
     HTTP_CONTENT_LENGTH="$(HEADER Content-Length |grep -xE '[0-9]+')"
 
@@ -86,12 +87,12 @@ if [ -z "$REQUEST_METHOD" ]; then
     . "$0" | while read -r l; do case $l in
       Status:*) cgilite_status="${l#Status: }";;
       Content-Length:*) cgilite_cl="${l}";;
-      $CR) printf '%s %s\r\n%s\n%s\n\r\n' \
+      $CR) printf '%s %s\r\n%s%s\n\r\n' \
              'HTTP/1.1' "${cgilite_status%${CR}}" \
-             "$cgilite_response" "${cgilite_cl}"
-           cat
+             "${cgilite_response}${cgilite_response:+${BR}}" "${cgilite_cl}"
+           cat || kill $$
            [ "${cgilite_cl#Connection}" = "${cgilite_cl}" ]; exit;;
-      *) cgilite_response="$cgilite_response${BR}$l";;
+      *) cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
     esac; done || exit 0;
     (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
   done
@@ -115,23 +116,35 @@ cgilite_value(){
     str=${str#*&${name}=}
     cnt=$((cnt - 1))
   done
-  printf "$(printf %s "${str%%&*}" |sed -r 's;\+; ;g;'"$HEX_DECODE")"
+  printf -- "$(printf %s "${str%%&*}" |sed -E 's;\+; ;g;'"$HEX_DECODE")"
+}
+
+cgilite_keys(){
+  local str="&$1"
+  while [ "${str#*&}" != "${str}" ]; do
+    str="${str#*&}"
+    printf '%s\n' "${str%%=*}"
+  done \
+  | sort -u
 }
 
 GET(){ cgilite_value "${QUERY_STRING}" $@; }
 GET_COUNT(){ cgilite_count "${QUERY_STRING}" $1; }
+GET_KEYS(){ cgilite_keys "${QUERY_STRING}"; }
 
 POST(){ cgilite_value "${cgilite_post}" $@; }
 POST_COUNT(){ cgilite_count "${cgilite_post}" $1; }
+POST_KEYS(){ cgilite_keys "${cgilite_post}"; }
 
 REF(){ cgilite_value "${HTTP_REFERER#*\?}" $@; }
 REF_COUNT(){ cgilite_count "${HTTP_REFERER#*\?}" $1; }
+REF_KEYS(){ cgilite_keys "${HTTP_REFERER#*\?}"; }
 
 COOKIE(){
   HEX_DECODE "$(
     HEADER Cookie \
     | grep -oE '(^|; ?)'"$1"'=[^;]*' \
-    | sed -rn "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
+    | sed -En "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
   )"
 }
 
@@ -152,7 +165,7 @@ HTML(){
       36?) printf '%s' ${n#36};;   # 4 octet (21 bit) chain start
     esac
   done \
-  | sed -r 's;.{7};&\n;g;' \
+  | sed -E 's;.{7};&\n;g;' \
   | while read n; do
     printf '&#%d;' $((0$n))
   done
@@ -169,7 +182,7 @@ URL(){
 
 PATH(){
   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
-  | sed -r 's;^.*$;/&/;; s;/+;/;g;
+  | sed -E 's;^.*$;/&/;; s;/+;/;g;
             :X;
             s;^/\.\./;/;; s;/\./;/;g;
             tX;