Improve Http Client Configuration (#786)

* Improve Http Client Configuration

* Lint
This commit is contained in:
Mitchell Syer
2023-12-08 19:16:38 -05:00
committed by GitHub
parent a2d3fa6e1d
commit 9b27d7ee23
5 changed files with 71 additions and 11 deletions

View File

@@ -16,10 +16,14 @@ package eu.kanade.tachiyomi.network
// import uy.kohesive.injekt.injectLazy
import android.content.Context
import eu.kanade.tachiyomi.network.interceptor.CloudflareInterceptor
import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor
import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor
import mu.KotlinLogging
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.brotli.BrotliInterceptor
import okhttp3.logging.HttpLoggingInterceptor
import java.io.File
import java.net.CookieHandler
import java.net.CookieManager
import java.net.CookiePolicy
@@ -51,8 +55,17 @@ class NetworkHelper(context: Context) {
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.callTimeout(2, TimeUnit.MINUTES)
.cache(
Cache(
directory = File.createTempFile("tachidesk_network_cache", null),
maxSize = 5L * 1024 * 1024, // 5 MiB
),
)
.addInterceptor(BrotliInterceptor)
.addInterceptor(UncaughtExceptionInterceptor())
.addInterceptor(UserAgentInterceptor())
// if (preferences.verboseLogging().get()) {
val httpLoggingInterceptor =
HttpLoggingInterceptor(
object : HttpLoggingInterceptor.Logger {
@@ -65,12 +78,27 @@ class NetworkHelper(context: Context) {
).apply {
level = HttpLoggingInterceptor.Level.BASIC
}
builder.addInterceptor(httpLoggingInterceptor)
builder.addNetworkInterceptor(httpLoggingInterceptor)
// }
// when (preferences.dohProvider()) {
// PREF_DOH_CLOUDFLARE -> builder.dohCloudflare()
// PREF_DOH_GOOGLE -> builder.dohGoogle()
// }
// builder.addInterceptor(
// CloudflareInterceptor(context, cookieJar, ::defaultUserAgentProvider),
// )
// when (preferences.dohProvider().get()) {
// PREF_DOH_CLOUDFLARE -> builder.dohCloudflare()
// PREF_DOH_GOOGLE -> builder.dohGoogle()
// PREF_DOH_ADGUARD -> builder.dohAdGuard()
// PREF_DOH_QUAD9 -> builder.dohQuad9()
// PREF_DOH_ALIDNS -> builder.dohAliDNS()
// PREF_DOH_DNSPOD -> builder.dohDNSPod()
// PREF_DOH_360 -> builder.doh360()
// PREF_DOH_QUAD101 -> builder.dohQuad101()
// PREF_DOH_MULLVAD -> builder.dohMullvad()
// PREF_DOH_CONTROLD -> builder.dohControlD()
// PREF_DOH_NJALLA -> builder.dohNajalla()
// PREF_DOH_SHECAN -> builder.dohShecan()
// }
return builder
}

View File

@@ -0,0 +1,27 @@
package eu.kanade.tachiyomi.network.interceptor
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException
/**
* Catches any uncaught exceptions from later in the chain and rethrows as a non-fatal
* IOException to avoid catastrophic failure.
*
* This should be the first interceptor in the client.
*
* See https://square.github.io/okhttp/4.x/okhttp/okhttp3/-interceptor/
*/
class UncaughtExceptionInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
return try {
chain.proceed(chain.request())
} catch (e: Exception) {
if (e is IOException) {
throw e
} else {
throw IOException(e)
}
}
}
}