From bf28d30e4caa89c94a81e9e7b17647980e571954 Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 9 Jan 2022 22:18:36 +0100 Subject: [PATCH] MainActivity: use / as empty path in URLs As per the spec, clients should rewrite empty paths of absolute URLs to "/" instead. Indeed, why the server would return something other than a redirection for "" to "/"? --- .../java/dev/lowrespalmtree/comet/MainActivity.kt | 12 ++++++++++++ .../main/java/dev/lowrespalmtree/comet/UriUtils.kt | 1 + 2 files changed, 13 insertions(+) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt index 2546b31..a6b29fd 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/MainActivity.kt @@ -75,6 +75,16 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen { openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null) } + /** + * Open an URL. + * + * This function can be called after the user entered an URL in the app bar, clicked on a link, + * whatever. To make the user's life a bit easier, this function also makes a few guesses: + * - If the URL is not absolute, make it so from a base URL (e.g. the current URL) or assume + * the user only typed a hostname without scheme and use a utility function to make it + * absolute. + * - If it's an absolute Gemini URL with an empty path, use "/" instead as per the spec. + */ private fun openUrl(url: String, base: String? = null, redirections: Int = 0) { if (redirections >= 5) { alert("Too many redirections.") @@ -84,6 +94,8 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen { var uri = Uri.parse(url) if (!uri.isAbsolute) { uri = if (!base.isNullOrEmpty()) joinUrls(base, url) else toGeminiUri(uri) + } else if (uri.scheme == "gemini" && uri.path.isNullOrEmpty()) { + uri = uri.buildUpon().path("/").build() } when (uri.scheme) { diff --git a/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt b/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt index 3f971da..7e667f6 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/UriUtils.kt @@ -15,6 +15,7 @@ fun toGeminiUri(uri: Uri): Uri = Uri.Builder() .scheme("gemini") .authority(uri.path) + .path("/") .query(uri.query) .fragment(uri.fragment) .build()