Skip to content

Commit aed7848

Browse files
committed
Fix parsing errors on older Android versions
1 parent f33588e commit aed7848

File tree

1 file changed

+14
-4
lines changed
  • rpc/src/main/kotlin/org/equeim/tremotesf/torrentfile/rpc/requests

1 file changed

+14
-4
lines changed

rpc/src/main/kotlin/org/equeim/tremotesf/torrentfile/rpc/requests/TorrentsList.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package org.equeim.tremotesf.torrentfile.rpc.requests
66

7-
import android.net.Uri
87
import kotlinx.serialization.Contextual
98
import kotlinx.serialization.ExperimentalSerializationApi
109
import kotlinx.serialization.KSerializer
@@ -29,6 +28,9 @@ import org.equeim.tremotesf.torrentfile.rpc.RpcClient
2928
import org.equeim.tremotesf.torrentfile.rpc.RpcRequestContext
3029
import org.equeim.tremotesf.torrentfile.rpc.RpcRequestError
3130
import org.threeten.bp.Instant
31+
import timber.log.Timber
32+
import java.net.URI
33+
import java.net.URISyntaxException
3234
import java.util.concurrent.ConcurrentHashMap
3335
import kotlin.time.Duration
3436

@@ -180,10 +182,18 @@ private object TrackerSitesSerializer : JsonTransformingSerializer<List<String>>
180182
return JsonArray(element.jsonArray.map { arrayElement ->
181183
val announceUrl = arrayElement.jsonObject.getValue("announce").jsonPrimitive
182184
cache.getOrPut(announceUrl) {
183-
// We can't convert announceUrl to HttpUtl directly since announce URLs may have udp:// scheme
185+
// We can't convert announceUrl to HttpUrl directly since announce URLs may have udp:// scheme
184186
// and HttpUrl supports only http:// and https://
185-
// Extract host using Uri and construct fake HttpUrl using http:// scheme instead
186-
Uri.parse(announceUrl.content).host?.let { host ->
187+
// Extract host using URI and construct fake HttpUrl using http:// scheme instead
188+
// android.net.Uri incorrectly parses URL wit IPv6 addresses on older Android versions,
189+
// so we use java.net.URI which works
190+
val uri = try {
191+
URI(announceUrl.content)
192+
} catch (e: URISyntaxException) {
193+
Timber.e(e, "Failed to parse URI from ${announceUrl.content}")
194+
null
195+
}
196+
uri?.host?.let { host ->
187197
JsonPrimitive(HttpUrl.Builder().scheme("http").host(host).build().topPrivateDomain() ?: host)
188198
} ?: announceUrl
189199
}

0 commit comments

Comments
 (0)