diff --git a/core/network/build.gradle.kts b/core/network/build.gradle.kts index 2829c2164..1f3f0b5b6 100644 --- a/core/network/build.gradle.kts +++ b/core/network/build.gradle.kts @@ -43,6 +43,7 @@ kotlin { implementation(libs.ktxml) implementation(libs.kermit) implementation(libs.crashkios.bugsnag) + api(libs.korlibs.string) } commonTest.dependencies { implementation(libs.kotlin.test) } diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt index 1be972f31..b484d478c 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt @@ -32,9 +32,9 @@ import io.ktor.http.HttpStatusCode import io.ktor.http.URLBuilder import io.ktor.http.URLProtocol import io.ktor.http.Url -import io.ktor.http.charset import io.ktor.http.contentType -import io.ktor.utils.io.charsets.Charsets +import korlibs.io.lang.Charset +import korlibs.io.lang.Charsets import me.tatarka.inject.annotations.Inject @Inject @@ -99,12 +99,10 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe } } else { val content = response.bodyAsChannel() - val feedPayload = - feedParser.parse( - feedUrl = url, - content = content, - charset = response.charset() ?: Charsets.UTF_8 - ) + val responseCharset = response.contentType()?.parameter("charset") + val charset = Charset.forName(responseCharset ?: Charsets.UTF8.name) + + val feedPayload = feedParser.parse(feedUrl = url, content = content, charset = charset) FeedFetchResult.Success(feedPayload) } diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt index 139571807..6ce446fde 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt @@ -24,9 +24,10 @@ import io.ktor.http.URLBuilder import io.ktor.http.URLProtocol import io.ktor.http.set import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.charsets.Charset +import io.ktor.utils.io.charsets.decode import io.ktor.utils.io.core.String import io.ktor.utils.io.core.readBytes +import korlibs.io.lang.Charset import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext import kotlinx.coroutines.runBlocking @@ -156,7 +157,8 @@ private fun ByteReadChannel.toCharIterator( if (this@toCharIterator.isClosedForRead) return false val packet = runBlocking(context) { this@toCharIterator.readRemaining(DEFAULT_BUFFER_SIZE) } - currentBuffer = String(bytes = packet.readBytes(), charset = charset) + currentBuffer = buildString { charset.decode(this, packet.readBytes()) } + packet.release() currentIndex = 0 return currentBuffer.isNotEmpty() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index defb693a5..2a5306c64 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -50,6 +50,7 @@ bugsnag = "6.6.1" crashkios-bugsnag = "0.8.6" kermit = "2.0.4" reorderable = "2.2.0" +korlibs = "6.0.0" [libraries] compose_runtime = { module = "org.jetbrains.compose.runtime:runtime", version.ref = "compose" } @@ -120,6 +121,7 @@ crashkios-bugsnag = { module = "co.touchlab.crashkios:bugsnag", version.ref = "c kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } kermit-bugsnag = { module = "co.touchlab:kermit-bugsnag", version.ref = "kermit" } reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" } +korlibs-string = { module = "com.soywiz:korlibs-string", version.ref = "korlibs" } [plugins] android_application = { id = "com.android.application", version.ref = "android_gradle_plugin" }