]> git.plutz.net Git - cgilite/blobdiff - cgilite.sh
bugfix: allow empty query string
[cgilite] / cgilite.sh
index 384ec1907f5bd7aa31dc41caec103896a1aaba4b..86ad28073018c9a2626877e74426c0b2d4a95914 100755 (executable)
@@ -27,18 +27,21 @@ BR='
 '
 cgilite_timeout=2
 
-PATH(){
-  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
-  | sed -E 's;^.*$;/&/;; s;/+;/;g;
-            :X;
-            s;^/\.\./;/;; s;/\./;/;g;
-            tX;
-            s;/[^/]+/\.\./;/;;
-            tX;
-            s;^(/.*)/$;\1;'
+PATH(){ 
+  local str seg out
+  [ $# -eq 0 ] && str="$(cat)" || str="$*"
+  while [ "$str" ]; do
+    seg=${str%%/*}; str="${str#*/}"
+    case $seg in
+      ..) out="${out%/}"; out="${out%/*}/";;
+    .|'') out="${out%/}/";;
+       *) out="${out%/}/${seg}";;
+    esac;
+    [ "$seg" = "$str" ] && break
+  done
+  [ "${str}" -a "${out}" ] && printf %s "$out" || printf %s/ "${out%/}"
 }
 
-
 HEX_DECODE='
   s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X;
   # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
@@ -74,7 +77,9 @@ if [ -z "$REQUEST_METHOD" ]; then
 
     SERVER_PROTOCOL="${SERVER_PROTOCOL%${CR}}"
     PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}" |PATH)"
-    QUERY_STRING="${REQUEST_URI#*\?}"
+    [ "${REQUEST_URI}" = "${REQUEST_URI#*\?}" ] \
+    && QUERY_STRING='' \
+    || QUERY_STRING="${REQUEST_URI#*\?}"
     cgilite_headers=''; while read -r hl; do
       hl="${hl%${CR}}"; [ "$hl" ] || break
       case $hl in
@@ -92,11 +97,16 @@ if [ -z "$REQUEST_METHOD" ]; then
     # Without Content-Length header, connection will terminate after
     # script.
 
-    cgilite_status='200 OK'; cgilite_response=''; cgilite_cl="Connection: close${CR}";
+    cgilite_status='200 OK'; cgilite_response=''; cgilite_cl="Connection: close${CR}${BR}";
     . "$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%s\n\r\n' \
+      Status:*)
+        cgilite_status="${l#Status: }";;
+      Content-Length:*)
+        cgilite_cl=""
+        cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
+      Connection:*)
+        cgilite_cl="${l}${BR}";;
+      $CR) printf '%s %s\r\n%s%s\r\n' \
              'HTTP/1.1' "${cgilite_status%${CR}}" \
              "${cgilite_response}${cgilite_response:+${BR}}" "${cgilite_cl}"
            cat || kill $$