Skip to content

Commit

Permalink
Merge pull request #21 from blmayer/fix-url
Browse files Browse the repository at this point in the history
Update URL parsing
  • Loading branch information
blmayer authored Jun 30, 2022
2 parents 0642554 + 6869c05 commit 8d1ff8d
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions astro
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,27 @@ getprevious() {
# Returns the complete url scheme with gemini defaults
# Parameters: url
parseurl() {
# shellcheck disable=SC2154
# Credits: https://stackoverflow.com/a/6174447/7618649
[ "$debug" ] && echo "Parsing: $1" >&2 && sleep 2
IFS='|' read -r proto hostport path rest << EOF
$(echo "$1" | sed -E 's@^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?@\2\|\4\|\5\|\7@g')
EOF

[ "$debug" ] && echo "Parsed URL: proto: $proto host: $hostport path: $path" >&2 && sleep 2
if [ "$oldhost" ] && [ "$(echo "$hostport" | sed 's/ //g')" = "" ]
then
hostport="$oldhost"
elif [ "$hostport" = "" ]
then
hostport="${path%%/}"
path=
proto="$(echo "$1" | grep :// | sed -e 's,^\(.*://\).*,\1,g')"
if [ "$proto" ]
then
url="$(echo "$1" | sed -e "s@$proto@@g")"
else
url="$1"
fi

IFS=':' read -r host port << EOF
$hostport
EOF

# Check path continuation
if [ "$oldhost" = "$host" ]
then
: "${oldpath:=}"
case "$path" in
"/"*) ;;
*) path="$oldpath$path" ;;
esac
fi
[ "$debug" ] && echo "Response: proto: ${proto:-gemini} host: $host port: ${port:-1965} path: ${path#/}" >&2 && sleep 2
[ "$debug" ] && echo "url: $url" >&2 && sleep 2

proto="$(echo "$proto" | sed -e 's,:\?//,,g')"
user="$(echo "$url" | grep @ | cut -d@ -f1)"
hostport="$(echo "$url" | sed -e "s/$user@//g" | cut -d/ -f1)"
host="$(echo "$hostport" | sed -e 's,:.*,,g')"
port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
path="$(echo "$url" | grep / | cut -d/ -f2-)"

[ "$oldhost" ] && [ ! "$path" ] && path="$host" && host="$oldhost"
[ "$oldhost" ] && [ ! "$host" ] && host="$oldhost"
[ "$debug" ] && echo "Parsed: proto: ${proto:-gemini} host: $host port: ${port:-1965} path: ${path#/}" >&2 && sleep 2
echo "${proto:-gemini}" "$host" "${port:-1965}" "${path#/}" "$rest"
return 0
}
Expand All @@ -102,7 +93,7 @@ fetch() {
[ "$debug" ] && echo "Requesting $1://$2:$3/$4$5" >&2
printf '\033]2;%s\007' "astro: $2/$4"

echo "$1://$2:$3/$4$5" >> "$histfile"
echo "$1 $2 $3 $4 $5" >> "$histfile"

clear
certfile=""
Expand Down Expand Up @@ -161,11 +152,12 @@ EOF
44)
return 7
;;
51)
51*)
echo "Page not found!" >&2
url="$(getprevious)"
# shellcheck disable=SC2046
fetch $(parseurl "$url")
[ "$debug" ] && echo "Previous page: $url" >&2 && sleep 2
# shellcheck disable=SC2086
fetch $url
return 0
;;
52)
Expand All @@ -188,8 +180,8 @@ EOF
url="$(getprevious)"

# word splitting here is intentional
# shellcheck disable=SC2046
fetch $(parseurl "$url")
# shellcheck disable=SC2086
fetch $url
else
fetch "$1" "$2" "$3" "$4" "$5"
fi
Expand Down Expand Up @@ -277,9 +269,13 @@ EOF
read -r i <&1
url="$(sed "${i}q;d" "$linksfile" | cut -d' ' -f1)"
;;
52) url="$(getprevious)" ;;
52)
# shellcheck disable=SC2046
fetch $(getprevious)
return
;;
53) url="$homepage"; shift $# ;;
54)
54)
echo "Enter description: (optional)"
read -r desc <&1
url="$1://$2:$3/$4"
Expand All @@ -295,6 +291,13 @@ EOF
read -r proto host port path << EOF
$(oldhost="$2" oldpath="$4" parseurl "$url")
EOF
if [ ! "$proto" = "gemini" ]
then
echo "Only gemini links are supported."
echo "Type a key to continue."
read -r i <&1
proto="$1"; host="$2"; port="$3"; path="$4"
fi
fetch "$proto" "$host" "$port" "$path"
}
}
Expand Down Expand Up @@ -357,7 +360,7 @@ fi
LESSKEY="$confighome/astro/less.keys"

# Restore terminal
trap 'tput rmcup && rm $histfile $linksfile; exit' EXIT INT HUP
trap 'tput rmcup && rm $histfile $linksfile 2&> /dev/null; exit' EXIT INT HUP

# This is the final binary form, to save space, it corresponds to:
# o (49): go to a URL
Expand Down

0 comments on commit 8d1ff8d

Please sign in to comment.