Skip to content

Commit

Permalink
Refactor percent_decode as inverse of percent_encode
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Scavo committed Jan 14, 2017
1 parent de7e7d8 commit 838d078
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions lib/http_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ get_header_value () {
}

#######################################################################
# This function percent-encodes all characters except the "Unreserved
# Characters" defined in section 2.3 of RFC 3986.
# This function percent-encodes all characters in its string argument
# except the "Unreserved Characters" defined in section 2.3 of RFC 3986.
#
# See: https://gist.github.com/cdown/1163649
# https://en.wikipedia.org/wiki/Percent-encoding
Expand Down Expand Up @@ -523,7 +523,9 @@ percent_encode () {
}

#######################################################################
# This function percent-decodes all percent-encoded characters.
# This function is the inverse of the percent_encode function, that
# is, it percent-decodes all percent-encoded characters in its string
# argument.
#######################################################################
percent_decode () {
# percent_decode <string>
Expand All @@ -534,15 +536,7 @@ percent_decode () {
return 2
fi

local encoded_string

# the percent_encode function doesn't convert spaces to
# plus signs (+) but the input string to the percent_decode
# function may have been encoded in some other manner so we
# first decode plus signs (+) into spaces
encoded_string="${1//+/ }"

printf '%b' "${encoded_string//%/\\x}"
printf '%b' "${1//%/\\x}"
}

# DEPRECATED: Use percent_encode and percent_decode instead.
Expand Down

0 comments on commit 838d078

Please sign in to comment.