Skip to content

Commit

Permalink
VERSION 4. urlencode added.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColasNahaboo committed Apr 16, 2020
1 parent 8ef2e24 commit ffae547
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 179 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ E.g: `source /usr/local/bin/cgibashopts` or
- **-n** can be given to ignore and discard any requests to upload files. This is recommended if you do not expect files to be uploaded, as it can save some computing load if some attacker try to upload fake files, but not mandatory. It also does not defines the variable `$CGIBASHOPTS_DIR` nor the function `cgibashopts_clean`, and do not use trap. **Note:** This is only available in versions 3 and above. Example of use : `. cgibashopts -n`
- The variable `CGIBASHOPTS_VERSION` holds the version number (an integer) of the cgibashopts libray used, versions being listed at the end of this page in *History of changes*..
- Misc goodies:
- A bash function `urldecode` is provided that takes a string in parameter and outputs its decoded version, transforming `+` in spaces and `%XX` in the character of hexadecimal ascii code XX (e.g %41 becomes A), and removing carriage returns
- Two handy bash functions are provided:
- `urldecode` that takes a string in parameter and outputs its decoded version, transforming `+` in spaces and `%XX` in the character of hexadecimal ascii code XX (e.g %41 becomes A), and removing carriage returns.
- `urlencode` that performs the reverse operation. Both are faster than the binary linux commands.
- two variables `$nl` and `$cr` hold a newline and a carriage return character
- An alternate way to get the variables values is via the `param` function. This is just a convenience function compatible with [bashlib](http://bashlib.sourceforge.net/) for people (or scripts) used to it.
- `param` without argument outputs the value of `FORMS`
Expand All @@ -53,6 +55,7 @@ E.g: `source /usr/local/bin/cgibashopts` or
A test suite is provided, it can be run by `./RUN-ALL-TESTS`, for more details see the README.md in directory `tests`

## History of changes
- 2020-04-16 Version 4: urlencode goodie function added
- 2020-04-04 Some cosmetic changes in this doc and the tests (test-suite dir renamed as tests), but no changes to cgibashopts code itself, so no version number increase.
- 2020-03-27 Version 3: -n option added to disable file uploads
- 2018-10-09 Version 2: fix, spaces in parameter values could be seen as +
Expand Down
14 changes: 13 additions & 1 deletion cgibashopts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See https://github.com/ColasNahaboo/cgibashopts
# Uses the CGI env variables REQUEST_METHOD CONTENT_TYPE QUERY_STRING

export CGIBASHOPTS_VERSION=3
export CGIBASHOPTS_VERSION=4
cr=$'\r'
nl=$'\n'
export FORMS=
Expand Down Expand Up @@ -54,6 +54,18 @@ urldecode() {
echo "$r"
}

# the reverse of urldecode above
urlencode() {
local length="${#1}" i c
for (( i = 0; i < length; i++ )); do
c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
}

if [ "${REQUEST_METHOD:-}" = POST ]; then
if [[ ${CONTENT_TYPE:-} =~ ^multipart/form-data\;[[:space:]]*boundary=([^\;]+) ]]; then
sep="--${BASH_REMATCH[1]}"
Expand Down
Loading

0 comments on commit ffae547

Please sign in to comment.