commit 8d1ff8d883585968e7d85f54f2f2e60a413cfdf2
parent 0642554dcced914c0dc0b319514ba3602e9b3015
Author: Brian Mayer <bleemayer@gmail.com>
Date: Wed, 29 Jun 2022 22:10:11 -0300
Merge pull request #21 from blmayer/fix-url
Update URL parsing
Diffstat:
M | astro | | | 77 | ++++++++++++++++++++++++++++++++++++++++------------------------------------- |
1 file changed, 40 insertions(+), 37 deletions(-)
diff --git a/astro b/astro
@@ -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
}
@@ -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=""
@@ -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)
@@ -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
@@ -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"
@@ -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"
}
}
@@ -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