From a50f55fe627ae87524467ee83aa5b7f5c2a88a9c Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sat, 30 Dec 2023 21:22:51 +0100 Subject: [PATCH] [quote/lighttpdquot] New function to quote strings for pasting in a lighttpd config file --- lib/quote/lighttpdquot.sh | 16 +++++ spec/quote/lighttpdquot.spec | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 lib/quote/lighttpdquot.sh create mode 100644 spec/quote/lighttpdquot.spec diff --git a/lib/quote/lighttpdquot.sh b/lib/quote/lighttpdquot.sh new file mode 100644 index 0000000..0bdbe58 --- /dev/null +++ b/lib/quote/lighttpdquot.sh @@ -0,0 +1,16 @@ +# Quotes all arguments into a single lighttpd config-style string. +# +# Usage: +# - lighttpdquot foo bar baz +# - lighttpdquot 'foo bar baz' + +sed \ + -e ':a' -e '$!N' -e '$!b a' \ + -e 's/\\/\\\\/g' \ + -e 's/\n/\\n/g' \ + -e 's/"/\\"/g' \ + -e 's/\r/\\r/g' \ + -e 's/ /\\t/g' \ + -e '$s/^/e"/' -e '$s/$/"/' <1 LFs + lfstr="line 1${LF}line 2${LF}line 3" + + When run command lighttpdquot "${lfstr}" + + The status should be success + The stdout should equal 'e"line 1\nline 2\nline 3"' + The stderr should equal '' + End + + It "escapes carriage returns" + # use >1 CRs to check if the sed command uses /g + crstr="before${CR}mid${CR}after" + + When run command lighttpdquot "${crstr}" + + The status should be success + The stdout should equal 'e"before\rmid\rafter"' + The stderr should equal '' + End + + It "escapes tabs" + # use >1 tabs to check if the sed command uses /g + htstr="col 1${HT}col 2${HT}col 3" + + When run command lighttpdquot "${htstr}" + + The status should be success + The stdout should equal 'e"col 1\tcol 2\tcol 3"' + The stderr should equal '' + End + + It "escapes backslashes" + # use >1 backslashes to check if the sed command uses /g + bsstr="a${BS}b${BS}c" + + When run command lighttpdquot "${bsstr}" + + The status should be success + The stdout should equal 'e"a\\b\\c"' + The stderr should equal '' + End + + Context # long strings + Parameters:dynamic + l=2048 + + while test $((l*=2)) -le 100000 + do + %data $l + done + End + + It "quotes a $1 bytes long string" + randstr=$(random_string $(($1)) '[:alnum:]') + + When run command lighttpdquot "${randstr}" + + The status should be success + The stdout should equal "e\"${randstr}\"" + The stderr should equal '' + End + End +End