add url encode / secure isipv4

This commit is contained in:
Geoffray Levasseur-Brandin
2025-06-19 14:31:19 +02:00
parent 9d528a6491
commit 4be2e5ea87

View File

@@ -41,6 +41,7 @@ isipv4()
{ {
# Set up local variables # Set up local variables
local ip=$1 local ip=$1
[[ -z $ip ]] && return 1
# Start with a regex format test # Start with a regex format test
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
@@ -83,5 +84,24 @@ isipv6()
} }
export -f isipv6 export -f isipv6
# ------------------------------------------------------------------------------
# Encode a string so it can be used as a URL parameter
# ------------------------------------------------------------------------------
urlencode() {
local LANG=C
local str="$*"
local length="${#str}"
for (( i = 0; i < length; i++ )); do
local c="${str:i:1}"
case "$c" in
[a-zA-Z0-9.~_-]) printf "$c" ;;
' ') printf '+' ;;
*) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;;
esac
done
}
export -f urlencode
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# EOF # EOF