]> git.plutz.net Git - serve0/commitdiff
Merge commit 'b982ad7206b71b6a429a68588d71c4e201ec5296'
authorPaul Hänsch <paul@plutz.net>
Tue, 9 Jun 2020 18:12:33 +0000 (20:12 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 9 Jun 2020 18:12:33 +0000 (20:12 +0200)
1  2 
cgilite/cgilite.sh

diff --combined cgilite/cgilite.sh
index 86ad28073018c9a2626877e74426c0b2d4a95914,f03f74732165e4d672418d3718ecc228db4ac53d..f03f74732165e4d672418d3718ecc228db4ac53d
@@@ -187,35 -187,48 +187,48 @@@ COOKIE()
  }
  
  HTML(){
-   # HTML Entity Coding
-   # Prints UTF-8 string as decimal Unicode Code Points
-   # Useful for escaping user input for use in HTML text and attributes
-   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
-   | hexdump -ve '/1 "%03o\n"' \
-   | while read n; do
-     case $n in
-       # bitbanging octal UTF-8 chains into singular 7 digit octal numbers
-       [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do
-       2??)    printf '%s' ${n#2};; # tail fragment, append 6 bit
-       3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start
-       34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start
-       35?) printf '01%s' ${n#35};; # 3 octet chain start, high
-       36?) printf '%s' ${n#36};;   # 4 octet (21 bit) chain start
+   # Escape HTML cahracters
+   # 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;";;
+       \<*) out="${out}&lt;";;
+       \>*) out="${out}&gt;";;
+       \"*) out="${out}&quot;";;
+       \'*) out="${out}&#x27;";;
+       \[*) out="${out}&#x5B;";;
+       \]*) out="${out}&#x5D;";;
+       "${BR}"*) out="${out}&#x0A;";;
+       *) out="${out}${str%"${str#?}"}";;
      esac
-   done \
-   | sed -E 's;.{7};&\n;g;' \
-   | while read n; do
-     printf '&#%d;' $((0$n))
+     str="${str#?}"
    done
+   printf %s "$out"
  }
  
  URL(){
-   # Code every character in URL escape hex format
-   # except alphanumeric ascii
-   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
-   | hexdump -v -e '/1 ",%02X"' \
-   | sed 's;,;%;g; s;%2F;/;g;'
+   # Escape pathes, so they can be used in link tags and HTTP Headers
+   local str out
+   [ $# -eq 0 ] && str="$(cat)" || str="$*"
+   while [ "$str" ]; do
+     case $str in
+       \&*) out="${out}%26";;
+       \"*) out="${out}%22";;
+       \'*) out="${out}%27";;
+       \?*) out="${out}%3F";;
+       \#*) out="${out}%23";;
+       \[*) out="${out}%5B";;
+       \]*) out="${out}%5D";;
+       \ *) out="${out}%20";;
+       "${BR}"*) out="${out}%0A";;
+       %*) out="${out}%25";;
+       *) out="${out}${str%"${str#?}"}";;
+     esac
+     str="${str#?}"
+   done
+   printf %s "$out"
  }
  
  SET_COOKIE(){