From 6379a864b7b4f6fb115ddb05e749765e63047fe6 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Fri, 13 Oct 2023 22:38:18 +0100 Subject: [PATCH] Modify error output from searchapi.cgi. --- cgi-bin/searchapi.cgi | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cgi-bin/searchapi.cgi b/cgi-bin/searchapi.cgi index 55e5232..19e9436 100755 --- a/cgi-bin/searchapi.cgi +++ b/cgi-bin/searchapi.cgi @@ -13,9 +13,13 @@ shopt -s extglob # Output an error in processing. die() { - # $1 = The error message to output. - printf "%s: %s\\n" "Status" "1" - printf "%s: %s\\n" "Error" "$1" + # $1 = The error code to output. + # 0 = Success. + # 1 = Temporary error (eg, max concurrent). + # 2 = Perminant error. + # $2 = The error message to output (if any). + printf "%s: %s\\n" "Status" "${1:-2}" + [[ -n "$2" ]] && printf "%s: %s\\n" "Error" "$2" exit 1 } @@ -27,11 +31,11 @@ printf "%s: %s\\n\\n" "Expires" "1" # Limit the number of concurrent searches to avoid DoS. if (( $(lsof -t "$LOCATE_DB" | wc -l) > (MAX_CONCURRENT - 1) )); then - die "Too many concurrent searches" + die "1" "Too many concurrent searches" else # Lock the DB for the lsof count above. # shellcheck disable=SC2015 - exec 9<"$LOCATE_DB" && flock -s -E 10 -w 2 9 || die "Too many concurrent searches" + exec 9<"$LOCATE_DB" && flock -s -E 10 -w 2 9 || die "1" "Too many concurrent searches" # shellcheck disable=SC2154 while read -r -d '&' QUERY; do @@ -43,7 +47,7 @@ else VALUE="${QUERY#*=}" # Check the key is valid as a variable name. - [[ ! "$KEY" =~ ^[[:digit:]_]*[[:alnum:]_]*$ ]] && die "Invalid query - don't try to be clever :)" + [[ ! "$KEY" =~ ^[[:digit:]_]*[[:alnum:]_]*$ ]] && die "2" "Invalid query - don't try to be clever :)" # Remove spaces from beginning and end of value. : "${VALUE/#+(+)}"