From 63753a4fabb4e5402b40e7abe089870e699972a8 Mon Sep 17 00:00:00 2001 From: ge Date: Sat, 14 May 2022 21:08:08 +0300 Subject: [PATCH] fix: Pass test file:///path/with/may/sub/dir/s --- src/lib/uri.sh | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/lib/uri.sh b/src/lib/uri.sh index 805e2cf..b5911e2 100644 --- a/src/lib/uri.sh +++ b/src/lib/uri.sh @@ -33,35 +33,47 @@ parse_uri() { # query # fragment - local uri="$1" + local uri + local authority + local userinfo + local host + local ipv6_hostname + + uri="$1" # * Get scheme - scheme="$(<<< "$uri" cut -d ':' -f 1)" + scheme="${uri%%:*}" # * Get authority component # The absence of two slashes (//) after the scheme means that the path # component follows, not the authority. if [[ "$uri" =~ ${scheme}:[^//] ]]; then - local authority= + # Example (empty authority): scheme:/path + authority= + elif [[ "$uri" =~ ${scheme}:/// ]]; then + # Example (empty authority): scheme:///path + authority= else - # Fix 'scheme://host' test failure + # Exapmle: scheme://authority/path + + # Fix 'scheme://authority' test failure # Correctly detect authority component if empty path is set if <<< "$uri" sed 's/\/\///g' | grep / &>/dev/null; then # URI contain non empty path component - local authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)(?=/)')" + authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)(?=/)')" || true else - # If URI don/t contain path starts with "/" - local authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)$')" + # If URI does not contain path starts with "/" + authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)$')" || true fi fi # * Get host and userinfo components if authority is set. if [[ "$authority" =~ @ ]]; then - local userinfo="$(<<< "$authority" cut -d '@' -f 1)" - local host="$(<<< "$authority" cut -d '@' -f 2)" + userinfo="$(<<< "$authority" cut -d '@' -f 1)" || true + host="$(<<< "$authority" cut -d '@' -f 2)" || true else - local userinfo= - local host="$authority" + userinfo= + host="$authority" fi # * Get fragment @@ -72,19 +84,19 @@ parse_uri() { # * Get path if [[ "$uri" =~ ^${scheme}:// ]]; then - path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")" - path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")" + path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")" || true + path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")" || true # Dirty hack for 'schema://host:~/path' if [[ "$uri" =~ :~/ ]]; then path="~${path}" fi else # For non authority component URI - path="$(<<< "$uri" sed "s/${scheme}://g")" + path="$(<<< "$uri" sed "s/${scheme}://g")" || true fi # * Get hostname and port - local ipv6_hostname="$(<<< "$host" grep -Po '\[(.*)\]')" + ipv6_hostname="$(<<< "$host" grep -Po '\[(.*)\]')" || true if [ "$ipv6_hostname" ]; then hostname="$ipv6_hostname" port="$(<<< "$host" awk -F ']:' '{print $2}')"