fix: Pass test file:///path/with/may/sub/dir/s

This commit is contained in:
ge 2022-05-14 21:08:08 +03:00
parent 7cff116df3
commit 63753a4fab

View File

@ -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}')"