Modify error output from searchapi.cgi.

This commit is contained in:
Darren 'Tadgy' Austin 2023-10-13 22:38:18 +01:00
commit 6379a864b7

View file

@ -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/#+(+)}"