From 8481fadbd025a640387aacba6aa7a3c3d93b2068 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 16 Jan 2025 08:51:48 +0200 Subject: [PATCH] Fix absolute url resolving --- .../kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt | 2 +- .../kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt index a0d7aa07..47f38e1b 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Jsoup.kt @@ -79,7 +79,7 @@ public fun Element.attrAsAbsoluteUrlOrNull(attributeKey: String): String? { if (attr.isEmpty() || attr.startsWith("data:")) { return null } - return (baseUri().toHttpUrlOrNull()?.newBuilder(attr) ?: return null).toString() + return (baseUri().toHttpUrlOrNull()?.resolve(attr) ?: return null).toString() } /** diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt index ac7b4f77..1e96b1aa 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt @@ -12,6 +12,9 @@ import org.jsoup.nodes.Document import org.koitharu.kotatsu.parsers.InternalParsersApi import java.text.DateFormat +private val REGEX_SCHEME_PREFIX = Regex("^\\w{2,6}://", RegexOption.IGNORE_CASE) +private const val SCHEME_HTTPS = "https" + /** * Parse [Response] body as html document using Jsoup * @see [parseJson] @@ -76,10 +79,10 @@ public fun String.toRelativeUrl(domain: String): String { * @return an absolute url with [domain] if this is relative */ public fun String.toAbsoluteUrl(domain: String): String = when { - this.startsWith("//") -> "https:$this" - this.startsWith('/') -> "https://$domain$this" - this.startsWith("https://") -> this - else -> "https://$domain/$this" + startsWith("//") -> "$SCHEME_HTTPS:$this" + startsWith('/') -> "$SCHEME_HTTPS://$domain$this" + REGEX_SCHEME_PREFIX.containsMatchIn(this) -> this + else -> "$SCHEME_HTTPS://$domain/$this" } public fun concatUrl(host: String, path: String): String {