]> git.plutz.net Git - cgilite/commitdiff
general improved performance for POST/GET, performance switch for large HTML escapes master
authorPaul Hänsch <paul@plutz.net>
Tue, 15 Sep 2026 15:56:45 +0000 (17:56 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 15 Sep 2026 15:56:45 +0000 (17:56 +0200)
cgilite.sh

index ac23f40eb664be2d4fb6d0fc14ae3dff755ace51..f261f9c195c12d8cc53d6b374382a0f46b5e855d 100755 (executable)
@@ -224,10 +224,10 @@ cgilite_count(){
 }
 
 cgilite_value(){
-  local str="&$1" name="$2" cnt="${3:-1}"
+  local ref="&$1" str="&$1" name="$2" cnt="${3:-1}"
   while [ $cnt -gt 0 ]; do
-    [ "${str}" = "${str#*&${name}=}" ] && return 1
     str="${str#*&${name}=}"
+    [ "${str}" = "${ref}" ] && return 1 || ref="${str}"
     cnt=$((cnt - 1))
   done
   HEX_DECODE % "$(printf %s "${str%%&*}" |tr + \  )"
@@ -294,19 +294,38 @@ HTML(){
   # Also escape [, ], and \n for use in html-sh
   local str out
   [ $# -eq 0 ] && str="$(cat)" || str="$*"
-  while [ "$str" ]; do case $str in
-    \&*) out="${out}&amp;";       str="${str#?}";;
-    \<*) out="${out}&lt;";        str="${str#?}";;
-    \>*) out="${out}&gt;";        str="${str#?}";;
-    \"*) out="${out}&quot;";      str="${str#?}";;
-    \'*) out="${out}&#x27;";      str="${str#?}";;
-    \[*) out="${out}&#x5B;";      str="${str#?}";;
-    \]*) out="${out}&#x5D;";      str="${str#?}";;
-    "${CR}"*) out="${out}&#x0D;"; str="${str#?}";;
-    "${BR}"*) out="${out}&#x0A;"; str="${str#?}";;
-    *) out="${out}${str%%[]&<>\"\'${CR}${BR}[]*}"; str="${str#"${str%%[]&<>\"\'${CR}${BR}[]*}"}";;
-  esac; done
-  printf %s "$out"
+  if [ "${#str}" -ge 1000 ]; then
+    HTML_large "$str"
+  else
+    while [ "$str" ]; do case $str in
+      \&*) out="${out}&amp;";       str="${str#?}";;
+      \<*) out="${out}&lt;";        str="${str#?}";;
+      \>*) out="${out}&gt;";        str="${str#?}";;
+      \"*) out="${out}&quot;";      str="${str#?}";;
+      \'*) out="${out}&#x27;";      str="${str#?}";;
+      \[*) out="${out}&#x5B;";      str="${str#?}";;
+      \]*) out="${out}&#x5D;";      str="${str#?}";;
+      "${CR}"*) out="${out}&#x0D;"; str="${str#?}";;
+      "${BR}"*) out="${out}&#x0A;"; str="${str#?}";;
+      *) out="${out}${str%%[]&<>\"\'${CR}${BR}[]*}"; str="${str#"${str%%[]&<>\"\'${CR}${BR}[]*}"}";;
+    esac; done
+    printf %s "$out"
+  fi
+}
+
+HTML_large(){
+  # Better performance for large strings
+  printf %s "$*" | sed -zE '
+    s;&;\&amp\;;g;
+    s;<;\&lt\;;g;
+    s;>;\&gt\;;g;
+    s;";\&quot\;;g;
+    s;\[;\&#x5B\;;g;
+    s;\];\&#x5D\;;g;
+    s;\r;\&#x0D\;;g;
+    s;\n;\&#x0A\;;g;
+    s;'\'';\&#x27\;;g;
+  '
 }
 
 URL(){