From fcb3e2096bd811a910c686bbfe181708878ba8b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 18 Jul 2018 04:31:33 +0200 Subject: [PATCH] huge speed gain for GET() and POST() --- cgilite.sh | 56 ++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/cgilite.sh b/cgilite.sh index e1b3480..996fe9d 100755 --- a/cgilite.sh +++ b/cgilite.sh @@ -37,17 +37,18 @@ HEADER(){ fi } +HEX_DECODE=' + s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X; + # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf + s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; + s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; + s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; + s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; + s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; +' + HEX_DECODE(){ - printf "$(printf %s "$1" \ - | sed -r ' - s;\\;\\\\;g; :x; s;%([^0-9A-F]);\\045\1;g; tx; - # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf - s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; - s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; - s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; - s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; - s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; - ')" + printf "$(printf %s "$1" |sed -r "$HEX_DECODE")" } if [ -z "$REQUEST_METHOD" ]; then @@ -103,35 +104,28 @@ fi [ -n "${DEBUG+x}" ] && env cgilite_count(){ - case $1 in - GET) printf %s "&${QUERY_STRING}";; - POST) printf %s "&${cgilite_post}";; - REF) printf %s "&${HTTP_REFERER#*\?}";; - esac \ - | grep -Eo '&'"$2"'=[^&]*' \ + printf %s "&$1" \ + | grep -oE '&'"$2"'=[^&]*' \ | wc -l } cgilite_value(){ - HEX_DECODE "$( - case $1 in - GET) printf %s "&${QUERY_STRING}";; - POST) printf %s "&${cgilite_post}";; - REF) printf %s "&${HTTP_REFERER#*\?}";; - esac \ - | grep -Eo '&'"$2"'=[^&]*' \ - | sed -rn "${3:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}' - )" + local str="&$1" name="$2" cnt="${3:-1}" + while [ $cnt -gt 0 ]; do + str=${str#*&${name}=} + cnt=$((cnt - 1)) + done + printf "$(printf %s "${str%%&*}" |sed -r 's;\+; ;g;'"$HEX_DECODE")" } -GET(){ cgilite_value GET $@; } -GET_COUNT(){ cgilite_count GET $1; } +GET(){ cgilite_value "${QUERY_STRING}" $@; } +GET_COUNT(){ cgilite_count "${QUERY_STRING}" $1; } -POST(){ cgilite_value POST $@; } -POST_COUNT(){ cgilite_count POST $1; } +POST(){ cgilite_value "${cgilite_post}" $@; } +POST_COUNT(){ cgilite_count "${cgilite_post}" $1; } -REF(){ cgilite_value REF $@; } -REF_COUNT(){ cgilite_count REF $1; } +REF(){ cgilite_value "${HTTP_REFERER#*\?}" $@; } +REF_COUNT(){ cgilite_count "${HTTP_REFERER#*\?}" $1; } COOKIE(){ HEX_DECODE "$( -- 2.39.2