From: Paul Hänsch Date: Tue, 9 Jun 2020 18:12:33 +0000 (+0200) Subject: Squashed 'cgilite/' changes from 5b013b6..b8cec22 X-Git-Url: http://git.plutz.net/?p=serve0;a=commitdiff_plain;h=b982ad7206b71b6a429a68588d71c4e201ec5296 Squashed 'cgilite/' changes from 5b013b6..b8cec22 b8cec22 more escapes for use in html-sh and HTTP headers 4a73c71 limit escaping to necessary characters, more readable output, much faster escaping functions git-subtree-dir: cgilite git-subtree-split: b8cec2270b5917e96f57d72c4d1ad96471cad3d3 --- diff --git a/cgilite.sh b/cgilite.sh index 86ad280..f03f747 100755 --- a/cgilite.sh +++ b/cgilite.sh @@ -187,35 +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}&";; + \<*) out="${out}<";; + \>*) out="${out}>";; + \"*) out="${out}"";; + \'*) out="${out}'";; + \[*) out="${out}[";; + \]*) out="${out}]";; + "${BR}"*) out="${out} ";; + *) 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(){