Revert "Ktor CIO Client"

This reverts commit 67c529ffe9.
This commit is contained in:
tarsin committed 2023-02-13 17:28:28 +08:00
1 parent 8b4b060425
commit b2aa6552b1
3 files changed
+26 -51

No files matched your search

+1 -1
View File
@@ -192,7 +192,7 @@ dependencies {
// implementation 'io.coil-kt:coil:2.2.2'
implementation("com.github.Ehviewer-Overhauled.coil:coil-compose:53302f2")
implementation("io.ktor:ktor-client-cio:2.2.3")
implementation("io.ktor:ktor-client-okhttp:2.2.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-cbor:1.5.0-RC")
implementation("org.jsoup:jsoup:1.15.3")
@@ -56,8 +56,7 @@ import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore
import eu.kanade.tachiyomi.ui.reader.setting.ReaderPreferences
import eu.kanade.tachiyomi.util.lang.launchIO
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.cookies.HttpCookies
import io.ktor.client.engine.okhttp.OkHttp
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -276,25 +275,6 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
@JvmStatic
val ehProxySelector by lazy { EhProxySelector() }
private val trustManager by lazy {
try {
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
)
trustManagerFactory.init(null as KeyStore?)
val trustManagers = trustManagerFactory.trustManagers
check(!(trustManagers.size != 1 || trustManagers[0] !is X509TrustManager)) {
"Unexpected default trust managers:" + Arrays.toString(
trustManagers
)
}
trustManagers[0] as X509TrustManager
} catch (e: Exception) {
e.printStackTrace()
EhX509TrustManager
}
}
@JvmStatic
val okHttpClient by lazy {
val builder = OkHttpClient.Builder()
@@ -303,6 +283,24 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
.proxySelector(ehProxySelector)
if (Settings.getDF()) {
var trustManager: X509TrustManager
try {
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
)
trustManagerFactory.init(null as KeyStore?)
val trustManagers = trustManagerFactory.trustManagers
check(!(trustManagers.size != 1 || trustManagers[0] !is X509TrustManager)) {
"Unexpected default trust managers:" + Arrays.toString(
trustManagers
)
}
trustManager = trustManagers[0] as X509TrustManager
} catch (e: Exception) {
e.printStackTrace()
trustManager = EhX509TrustManager
}
builder.sslSocketFactory(EhSSLSocketFactory, trustManager)
builder.proxy(Proxy.NO_PROXY)
}
@@ -310,15 +308,9 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
}
val ktorClient by lazy {
HttpClient(CIO) {
HttpClient(OkHttp) {
engine {
https {
serverName = "0.0.0.0".takeIf { Settings.getDF() }
trustManager = EhX509TrustManager
}
}
install(HttpCookies) {
storage = EhCookieStore
preconfigured = okHttpClient
}
}
}
@@ -18,21 +18,14 @@ package com.hippo.ehviewer.client
import com.hippo.ehviewer.EhApplication
import com.hippo.network.CookieDatabase
import com.hippo.network.CookieSet
import io.ktor.client.plugins.cookies.CookiesStorage
import io.ktor.http.Url
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import java.util.Collections
import java.util.regex.Pattern
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
typealias KtorCookie = io.ktor.http.Cookie
object EhCookieStore : CookieJar, CookiesStorage {
object EhCookieStore : CookieJar {
private val db: CookieDatabase = CookieDatabase(EhApplication.application, "okhttp3-cookie.db")
private val map: MutableMap<String, CookieSet> = db.allCookies
@@ -183,19 +176,9 @@ object EhCookieStore : CookieJar, CookiesStorage {
db.clear()
}
override suspend fun addCookie(requestUrl: Url, cookie: io.ktor.http.Cookie) {}
// This should never be closed
override fun close() {}
private val okhttpCookieToKtorCookieMap = hashMapOf<Cookie, KtorCookie>()
override suspend fun get(requestUrl: Url): List<io.ktor.http.Cookie> {
return loadForRequest(requestUrl.toString().toHttpUrl()).map {
okhttpCookieToKtorCookieMap[it] ?: KtorCookie(
it.name,
it.value
).apply { okhttpCookieToKtorCookieMap[it] = this }
}
@Synchronized
fun close() {
db.close()
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {