From fbfc349b2e6a04e59233d765a06f468854cecf73 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 11 Jun 2022 11:07:36 +0100 Subject: [PATCH] forcing raw response type for the wellknown as some servers may not correctly set the content-type - migrates to the content negotiation api --- dependencies.gradle | 2 ++ matrix/matrix-http-ktor/build.gradle | 3 ++- .../st/matrix/http/ktor/KtorMatrixHttpClientFactory.kt | 10 +++++----- .../app/dapk/st/matrix/auth/internal/AuthRequest.kt | 8 ++++---- .../matrix/auth/internal/FetchWellKnownUseCaseImpl.kt | 3 ++- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 8fa1972..068bc28 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -123,8 +123,10 @@ ext.Dependencies.with { ktorAndroid = "io.ktor:ktor-client-android:${ktorVer}" ktorCore = "io.ktor:ktor-client-core:${ktorVer}" ktorSerialization = "io.ktor:ktor-client-serialization:${ktorVer}" + ktorJson = "io.ktor:ktor-serialization-kotlinx-json:${ktorVer}" ktorLogging = "io.ktor:ktor-client-logging-jvm:${ktorVer}" ktorJava = "io.ktor:ktor-client-java:${ktorVer}" + ktorContentNegotiation = "io.ktor:ktor-client-content-negotiation:${ktorVer}" coil = "io.coil-kt:coil-compose:2.1.0" accompanistSystemuicontroller = "com.google.accompanist:accompanist-systemuicontroller:0.24.9-beta" diff --git a/matrix/matrix-http-ktor/build.gradle b/matrix/matrix-http-ktor/build.gradle index 00105fb..bf47bed 100644 --- a/matrix/matrix-http-ktor/build.gradle +++ b/matrix/matrix-http-ktor/build.gradle @@ -9,5 +9,6 @@ dependencies { implementation Dependencies.mavenCentral.ktorCore implementation Dependencies.mavenCentral.ktorSerialization implementation Dependencies.mavenCentral.ktorLogging - implementation Dependencies.mavenCentral.kotlinSerializationJson + implementation Dependencies.mavenCentral.ktorContentNegotiation + implementation Dependencies.mavenCentral.ktorJson } \ No newline at end of file diff --git a/matrix/matrix-http-ktor/src/main/kotlin/app/dapk/st/matrix/http/ktor/KtorMatrixHttpClientFactory.kt b/matrix/matrix-http-ktor/src/main/kotlin/app/dapk/st/matrix/http/ktor/KtorMatrixHttpClientFactory.kt index e038291..3dcb046 100644 --- a/matrix/matrix-http-ktor/src/main/kotlin/app/dapk/st/matrix/http/ktor/KtorMatrixHttpClientFactory.kt +++ b/matrix/matrix-http-ktor/src/main/kotlin/app/dapk/st/matrix/http/ktor/KtorMatrixHttpClientFactory.kt @@ -4,9 +4,9 @@ import app.dapk.st.matrix.common.CredentialsStore import app.dapk.st.matrix.http.MatrixHttpClient import app.dapk.st.matrix.http.ktor.internal.KtorMatrixHttpClient import io.ktor.client.* -import io.ktor.client.plugins.json.* -import io.ktor.client.plugins.kotlinx.serializer.* +import io.ktor.client.plugins.contentnegotiation.* import io.ktor.client.plugins.logging.* +import io.ktor.serialization.kotlinx.json.* import kotlinx.serialization.json.Json class KtorMatrixHttpClientFactory( @@ -14,10 +14,10 @@ class KtorMatrixHttpClientFactory( private val includeLogging: Boolean, ) : MatrixHttpClient.Factory { - override fun create(json: Json): MatrixHttpClient { + override fun create(jsonInstance: Json): MatrixHttpClient { val client = HttpClient { - install(JsonPlugin) { - serializer = KotlinxSerializer(json) + install(ContentNegotiation) { + json(jsonInstance) } expectSuccess = true if (includeLogging) { diff --git a/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/AuthRequest.kt b/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/AuthRequest.kt index e8d8ea8..ef48a15 100644 --- a/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/AuthRequest.kt +++ b/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/AuthRequest.kt @@ -42,16 +42,16 @@ internal fun registerRequest(userName: String, password: String, baseUrl: String baseUrl = baseUrl, ) -internal fun wellKnownRequest(baseUrl: String) = httpRequest( +internal fun wellKnownRequest(baseUrl: String) = httpRequest( path = ".well-known/matrix/client", method = MatrixHttpClient.Method.GET, baseUrl = baseUrl, authenticated = false, ) -@JvmInline -@Serializable -internal value class RawResponse(val value: String) +typealias RawResponse = ByteArray + +fun RawResponse.readString() = this.toString(Charsets.UTF_8) internal data class Auth( val session: String, diff --git a/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/FetchWellKnownUseCaseImpl.kt b/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/FetchWellKnownUseCaseImpl.kt index 0c4e8d6..cc175fa 100644 --- a/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/FetchWellKnownUseCaseImpl.kt +++ b/matrix/services/auth/src/main/kotlin/app/dapk/st/matrix/auth/internal/FetchWellKnownUseCaseImpl.kt @@ -6,6 +6,7 @@ import io.ktor.http.* import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import java.net.UnknownHostException +import java.nio.charset.Charset internal typealias FetchWellKnownUseCase = suspend (String) -> WellKnownResult @@ -17,7 +18,7 @@ internal class FetchWellKnownUseCaseImpl( override suspend fun invoke(domainUrl: String): WellKnownResult { return runCatching { val rawResponse = httpClient.execute(rawWellKnownRequestForServersWithoutContentTypes(domainUrl)) - json.decodeFromString(ApiWellKnown.serializer(), rawResponse) + json.decodeFromString(ApiWellKnown.serializer(), rawResponse.readString()) } .fold( onSuccess = { WellKnownResult.Success(it) },