feat: Code improvements

This commit is contained in:
ge 2022-05-17 01:47:12 +03:00
parent e9c7d3b957
commit 3b81668b2b

View File

@ -58,7 +58,7 @@ parse_uri() {
# Fix 'scheme://authority' test failure
# Correctly detect authority component if empty path is set
if <<< "$uri" sed 's/\/\///g' | grep / &>/dev/null; then
if <<< "${uri//\/\//}" grep / &>/dev/null; then
# URI contain non empty path component
authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)(?=/)')" || true
else
@ -69,8 +69,8 @@ parse_uri() {
# * Get host and userinfo components if authority is set.
if [[ "$authority" =~ @ ]]; then
userinfo="$(<<< "$authority" cut -d '@' -f 1)" || true
host="$(<<< "$authority" cut -d '@' -f 2)" || true
userinfo="${authority%@*}" # Result: username:password
host="${authority#*@}" # Result: hostname:port
else
userinfo=
host="$authority"
@ -88,7 +88,7 @@ parse_uri() {
path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")" || true
# Dirty hack for 'schema://host:~/path'
if [[ "$uri" =~ :~/ ]]; then
path="~${path}"
path="~$path"
fi
elif [[ "$scheme" == "$uri" ]] && [[ "$uri" =~ ^[./] ]]; then
# For URI with only Path component.
@ -97,7 +97,7 @@ parse_uri() {
scheme='file'
else
# For non authority component URI
path="$(<<< "$uri" sed "s/${scheme}://g")" || true
path="${uri//${scheme}:/}"
fi
# * Get hostname and port
@ -106,8 +106,8 @@ parse_uri() {
hostname="$ipv6_hostname"
port="$(<<< "$host" awk -F ']:' '{print $2}')"
elif [[ "$host" =~ : ]]; then
hostname="$(<<< "$host" cut -d ':' -f 1)"
port="$(<<< "$host" cut -d ':' -f 2)"
hostname="${host%:*}"
port="${host#*:}"
# Dirty hack for 'schema://host:~/path'
if [[ "$port" == '~' ]]; then
port=
@ -119,8 +119,8 @@ parse_uri() {
# * Get username and password
if [[ "$userinfo" =~ : ]]; then
username="$(<<< "$userinfo" cut -d ':' -f 1)"
password="$(<<< "$userinfo" cut -d ':' -f 2)"
username="${userinfo%:*}"
password="${userinfo#*:}"
else
username="$userinfo"
password=
@ -141,6 +141,7 @@ parse_uri() {
# Decode URL-encoded password
if [ "$password" ]; then
password="$(<<< "$password" sed 's/+/ /g;s/%/\\\\x/g' | xargs echo -e)"
password="${password//+/ }"
password="$(<<< "${password//%/\\\\x}" xargs echo -e)"
fi
}