Simple fix on url normalization

This commit is contained in:
Vitor Pamplona
2025-08-30 10:59:53 -04:00
parent 4c43399cce
commit 4c34c6b8e3

View File

@@ -27,7 +27,7 @@ import org.czeal.rfc3986.URIReference
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
sealed interface NormalizationResult { sealed interface NormalizationResult {
class Sucess( class Success(
val url: NormalizedRelayUrl, val url: NormalizedRelayUrl,
) : NormalizationResult ) : NormalizationResult
@@ -157,28 +157,16 @@ class RelayUrlNormalizer {
} }
fun normalize(url: String): NormalizedRelayUrl { fun normalize(url: String): NormalizedRelayUrl {
normalizedUrls[url]?.let { val result = normalizeOrNull(url)
return when (it) { return result ?: throw IllegalArgumentException("Invalid Relay Url: $url")
is NormalizationResult.Sucess -> it.url
else -> throw IllegalArgumentException("Invalid Url: $url")
}
}
return try {
val fixed = fix(url) ?: return NormalizedRelayUrl(url)
val normalized = norm(fixed)
normalizedUrls.put(url, NormalizationResult.Sucess(normalized))
normalized
} catch (e: Exception) {
NormalizedRelayUrl(url)
}
} }
fun normalizeOrNull(url: String): NormalizedRelayUrl? { fun normalizeOrNull(url: String): NormalizedRelayUrl? {
if (url.isEmpty()) return null if (url.isEmpty()) return null
// happy path when the url has been fixed already
normalizedUrls[url]?.let { normalizedUrls[url]?.let {
return when (it) { return when (it) {
is NormalizationResult.Sucess -> it.url is NormalizationResult.Success -> it.url
else -> null else -> null
} }
} }
@@ -187,8 +175,8 @@ class RelayUrlNormalizer {
val fixed = fix(url) val fixed = fix(url)
if (fixed != null) { if (fixed != null) {
val normalized = norm(fixed) val normalized = norm(fixed)
normalizedUrls.put(url, NormalizationResult.Sucess(normalized)) normalizedUrls.put(url, NormalizationResult.Success(normalized))
return normalized normalized
} else { } else {
Log.w("NormalizedRelayUrl", "Rejected Error $url") Log.w("NormalizedRelayUrl", "Rejected Error $url")
normalizedUrls.put(url, NormalizationResult.Error) normalizedUrls.put(url, NormalizationResult.Error)