From 3b81668b2bbe9183f9746d3155d52a4ff76902bf Mon Sep 17 00:00:00 2001 From: ge Date: Tue, 17 May 2022 01:47:12 +0300 Subject: [PATCH] feat: Code improvements --- src/lib/uri.sh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/lib/uri.sh b/src/lib/uri.sh index 784c5b4..1367ea0 100644 --- a/src/lib/uri.sh +++ b/src/lib/uri.sh @@ -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 }