Fix absolute url resolving

master
Koitharu 1 year ago
parent 6abcdd8d4b
commit 8481fadbd0
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -79,7 +79,7 @@ public fun Element.attrAsAbsoluteUrlOrNull(attributeKey: String): String? {
if (attr.isEmpty() || attr.startsWith("data:")) { if (attr.isEmpty() || attr.startsWith("data:")) {
return null return null
} }
return (baseUri().toHttpUrlOrNull()?.newBuilder(attr) ?: return null).toString() return (baseUri().toHttpUrlOrNull()?.resolve(attr) ?: return null).toString()
} }
/** /**

@ -12,6 +12,9 @@ import org.jsoup.nodes.Document
import org.koitharu.kotatsu.parsers.InternalParsersApi import org.koitharu.kotatsu.parsers.InternalParsersApi
import java.text.DateFormat 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 * Parse [Response] body as html document using Jsoup
* @see [parseJson] * @see [parseJson]
@ -76,10 +79,10 @@ public fun String.toRelativeUrl(domain: String): String {
* @return an absolute url with [domain] if this is relative * @return an absolute url with [domain] if this is relative
*/ */
public fun String.toAbsoluteUrl(domain: String): String = when { public fun String.toAbsoluteUrl(domain: String): String = when {
this.startsWith("//") -> "https:$this" startsWith("//") -> "$SCHEME_HTTPS:$this"
this.startsWith('/') -> "https://$domain$this" startsWith('/') -> "$SCHEME_HTTPS://$domain$this"
this.startsWith("https://") -> this REGEX_SCHEME_PREFIX.containsMatchIn(this) -> this
else -> "https://$domain/$this" else -> "$SCHEME_HTTPS://$domain/$this"
} }
public fun concatUrl(host: String, path: String): String { public fun concatUrl(host: String, path: String): String {

Loading…
Cancel
Save