fix: Pass test file:///path/with/may/sub/dir/s
This commit is contained in:
parent
7cff116df3
commit
63753a4fab
@ -33,35 +33,47 @@ parse_uri() {
|
|||||||
# query
|
# query
|
||||||
# fragment
|
# fragment
|
||||||
|
|
||||||
local uri="$1"
|
local uri
|
||||||
|
local authority
|
||||||
|
local userinfo
|
||||||
|
local host
|
||||||
|
local ipv6_hostname
|
||||||
|
|
||||||
|
uri="$1"
|
||||||
|
|
||||||
# * Get scheme
|
# * Get scheme
|
||||||
scheme="$(<<< "$uri" cut -d ':' -f 1)"
|
scheme="${uri%%:*}"
|
||||||
|
|
||||||
# * Get authority component
|
# * Get authority component
|
||||||
# The absence of two slashes (//) after the scheme means that the path
|
# The absence of two slashes (//) after the scheme means that the path
|
||||||
# component follows, not the authority.
|
# component follows, not the authority.
|
||||||
if [[ "$uri" =~ ${scheme}:[^//] ]]; then
|
if [[ "$uri" =~ ${scheme}:[^//] ]]; then
|
||||||
local authority=
|
# Example (empty authority): scheme:/path
|
||||||
|
authority=
|
||||||
|
elif [[ "$uri" =~ ${scheme}:/// ]]; then
|
||||||
|
# Example (empty authority): scheme:///path
|
||||||
|
authority=
|
||||||
else
|
else
|
||||||
# Fix 'scheme://host' test failure
|
# Exapmle: scheme://authority/path
|
||||||
|
|
||||||
|
# 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" sed 's/\/\///g' | grep / &>/dev/null; then
|
||||||
# URI contain non empty path component
|
# URI contain non empty path component
|
||||||
local authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)(?=/)')"
|
authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)(?=/)')" || true
|
||||||
else
|
else
|
||||||
# If URI don/t contain path starts with "/"
|
# If URI does not contain path starts with "/"
|
||||||
local authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)$')"
|
authority="$(<<< "$uri" grep -Po '(?<=//)(.[^/]+)$')" || true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# * Get host and userinfo components if authority is set.
|
# * Get host and userinfo components if authority is set.
|
||||||
if [[ "$authority" =~ @ ]]; then
|
if [[ "$authority" =~ @ ]]; then
|
||||||
local userinfo="$(<<< "$authority" cut -d '@' -f 1)"
|
userinfo="$(<<< "$authority" cut -d '@' -f 1)" || true
|
||||||
local host="$(<<< "$authority" cut -d '@' -f 2)"
|
host="$(<<< "$authority" cut -d '@' -f 2)" || true
|
||||||
else
|
else
|
||||||
local userinfo=
|
userinfo=
|
||||||
local host="$authority"
|
host="$authority"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# * Get fragment
|
# * Get fragment
|
||||||
@ -72,19 +84,19 @@ parse_uri() {
|
|||||||
|
|
||||||
# * Get path
|
# * Get path
|
||||||
if [[ "$uri" =~ ^${scheme}:// ]]; then
|
if [[ "$uri" =~ ^${scheme}:// ]]; then
|
||||||
path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")"
|
path="$(<<< "$uri" sed "s/${scheme}:\/\/${authority//\[/\\[}//g")" || true
|
||||||
path="$(<<< "$path" sed "s/\?${query}//g;s/#${fragment}//g")"
|
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
|
||||||
else
|
else
|
||||||
# For non authority component URI
|
# For non authority component URI
|
||||||
path="$(<<< "$uri" sed "s/${scheme}://g")"
|
path="$(<<< "$uri" sed "s/${scheme}://g")" || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# * Get hostname and port
|
# * Get hostname and port
|
||||||
local ipv6_hostname="$(<<< "$host" grep -Po '\[(.*)\]')"
|
ipv6_hostname="$(<<< "$host" grep -Po '\[(.*)\]')" || true
|
||||||
if [ "$ipv6_hostname" ]; then
|
if [ "$ipv6_hostname" ]; then
|
||||||
hostname="$ipv6_hostname"
|
hostname="$ipv6_hostname"
|
||||||
port="$(<<< "$host" awk -F ']:' '{print $2}')"
|
port="$(<<< "$host" awk -F ']:' '{print $2}')"
|
||||||
|
Loading…
Reference in New Issue
Block a user