Created
September 2, 2016 05:15
-
-
Save tomdwaggy/79c2fd9df0314fcd5c6d3770f2355b76 to your computer and use it in GitHub Desktop.
KSH URI Decode Functions - From BASH snippet here: https://marc.waeckerlin.org/computer/blog/parsing_of_query_string_in_bash_cgi_scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 "&", "<" to "<", etc. | |
htmlEnc() { | |
tmp=${*//&/&} | |
tmp=${tmp//</<} | |
tmp=${tmp//>/>} | |
echo "${tmp//\"/"}" | |
} | |
# Execute the evaluation | |
# set all variables for both, POST and GET data | |
setQueryVars $QUERY_STRING $(</dev/stdin) |
Author
tomdwaggy
commented
Sep 2, 2016
- 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