Skip to content

Instantly share code, notes, and snippets.

@tomdwaggy
Created September 2, 2016 05:15
Show Gist options
  • Save tomdwaggy/79c2fd9df0314fcd5c6d3770f2355b76 to your computer and use it in GitHub Desktop.
Save tomdwaggy/79c2fd9df0314fcd5c6d3770f2355b76 to your computer and use it in GitHub Desktop.
#!/bin/ksh
# Decodes an URL-string
# an URL encoding has "+" instead of spaces
# and no special characters but "%HEX"
urlDec() {
value=${*//+/%20} # replace +-spaces by %20 (hex)
for part in ${value//%/ \\x}; do # split at % prepend \x for printf
printf "%b%s" "${part:0:4}" "${part:4}" # output decoded char
done
}
# For all given query strings
# parse them an set shell variables
setQueryVars() {
vars=${*//\*/%2A} # escape * as %2A
for var in ${vars//\&/ }; do # split at &
value=$(urlDec "${var#*=}") # decode value after =
value=${value//\\/\\\\} # change \ to \\ for later
eval "CGI_${var%=*}=\"${value//\"/\\\"}\"" # evaluate assignment
done
}
# HTML Encoding of a text
# quotes "&" to "&amp;", "<" to "&lt;", etc.
htmlEnc() {
tmp=${*//&/&amp;}
tmp=${tmp//</&lt;}
tmp=${tmp//>/&gt;}
echo "${tmp//\"/&quot;}"
}
# Execute the evaluation
# set all variables for both, POST and GET data
setQueryVars $QUERY_STRING $(</dev/stdin)
@tomdwaggy
Copy link
Author

  • Removed the local qualifiers.
  • Changed the for var in ${vars//&/ }; do for ksh escaping behavior.
  • Removed function before the function names.
  • Added call to setQueryVars at bottom, just source the file and all the ${CGI_*} variables should be available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment