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