From 6aed45f5608c96189443e0241e60b87ead4ff5f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 28 Oct 2026 17:47:33 +0100 Subject: [PATCH] Squashed 'cgilite/' changes from 417cc42..2390146 2390146 automatically discard stderr when running via inetd ca8a3d6 general improved performance for POST/GET, performance switch for large HTML escapes b233ce0 improved attribute parsing in headlines 221c2b7 avoid null characters in server secret, remove debug output git-subtree-dir: cgilite git-subtree-split: 2390146d94760232970fc8609ede4e398be50a92 --- cgilite.sh | 56 +++++++++++++++++++++++++++++++++++++--------------- markdown.awk | 8 +++++--- session.sh | 7 ++++--- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/cgilite.sh b/cgilite.sh index ac23f40..0d12260 100755 --- a/cgilite.sh +++ b/cgilite.sh @@ -3,7 +3,7 @@ # This is CGIlite. # A collection of posix shell functions for writing CGI scripts. -# Copyright 2017 - 2023, 2025 Paul Hänsch +# Copyright 2017 - 2023, 2025 - 2026 Paul Hänsch # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -52,6 +52,11 @@ _DATA="${_DATA:-${PWD:-.}}" _EXEC="${_EXEC%/}" _DATA="${_DATA%/}" _BASE="${_BASE%/}" # Do not set DEBUG here, because it will increase log level +# discard stderr when running via inetd (relies on undocumented env var?) +if [ -z "$DEBUG" -a -n "$inetd_dummy" ]; then + exec 2>/dev/null +fi + export _EXEC _DATA _BASE # Carriage Return and Line Break characters for convenience @@ -224,10 +229,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 +299,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}&"; str="${str#?}";; - \<*) out="${out}<"; str="${str#?}";; - \>*) out="${out}>"; str="${str#?}";; - \"*) out="${out}""; str="${str#?}";; - \'*) out="${out}'"; str="${str#?}";; - \[*) out="${out}["; str="${str#?}";; - \]*) out="${out}]"; str="${str#?}";; - "${CR}"*) out="${out} "; str="${str#?}";; - "${BR}"*) out="${out} "; 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}&"; str="${str#?}";; + \<*) out="${out}<"; str="${str#?}";; + \>*) out="${out}>"; str="${str#?}";; + \"*) out="${out}""; str="${str#?}";; + \'*) out="${out}'"; str="${str#?}";; + \[*) out="${out}["; str="${str#?}";; + \]*) out="${out}]"; str="${str#?}";; + "${CR}"*) out="${out} "; str="${str#?}";; + "${BR}"*) out="${out} "; 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;&;\&\;;g; + s;<;\<\;;g; + s;>;\>\;;g; + s;";\"\;;g; + s;\[;\[\;;g; + s;\];\]\;;g; + s;\r;\ \;;g; + s;\n;\ \;;g; + s;'\'';\'\;;g; + ' } URL(){ diff --git a/markdown.awk b/markdown.awk index d1f6616..b60285e 100755 --- a/markdown.awk +++ b/markdown.awk @@ -650,7 +650,8 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, # First Order Heading H1 + Attrib } else if ( match( block, "^([^\n]+)([ \t]*"rattr")\n===+(\n|$)" ) ) { len = RLENGTH; text = attrib = block; - sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text); + sub("([ \t]*"rattr")\n===+(\n.*)?$", "", text); + sub(/\n===+(\n.*)?$/, "", attrib); attrib = _attr(attrib); ret = ret headline(1, text, attrib) ; block = substr( block, len + 1 ); @@ -667,7 +668,8 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, # Second Order Heading H2 + Attrib } else if ( match( block, "^([^\n]+)([ \t]*"rattr")\n---+(\n|$)" ) ) { len = RLENGTH; text = attrib = block; - sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text); + sub("([ \t]*"rattr")\n---+(\n.*)?$", "", text); + sub(/\n---+(\n.*)?$/, "", attrib); attrib = _attr(attrib); ret = ret headline(2, text, attrib) ; block = substr( block, len + 1); @@ -686,7 +688,7 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, len = RLENGTH; text = attrib = substr(block, 1, len); match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1; text = substr(text, n + 1); sub(/^[ \t]*/, "", text); - sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text); + sub("[ \t]*#*([ \t]*"rattr")(\n.*)?$", "", text); attrib = _attr(attrib); diff --git a/session.sh b/session.sh index c3a44e8..a29cbf4 100755 --- a/session.sh +++ b/session.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright 2018 - 2022 Paul Hänsch +# Copyright 2018 - 2022, 2026 Paul Hänsch # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -45,7 +45,8 @@ fi server_key(){ IDFILE="${IDFILE:-${_DATA:-.}/serverkey}" if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then - dd count=1 bs=512 if=/dev/urandom \ + tr -d \\0 &- } @@ -116,7 +117,7 @@ update_session(){ new_session(){ local sid time sig - debug "Setting up new session" + # debug "Setting up new session" sid="$(randomid)" time=$(( $_DATE + $SESSION_TIMEOUT )) sig="$(session_mac "$sid" "$time")" -- 2.47.3