From d6c4af89c4a2df213f06ed4c3d714a2608117afb Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Fri, 12 Jan 2024 07:22:31 +0800 Subject: [PATCH] Fix extension interceptors receiving compressed responses (#10388) --- .../kanade/tachiyomi/network/NetworkHelper.kt | 4 +++- .../interceptor/IgnoreGzipInterceptor.kt | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/eu/kanade/tachiyomi/network/interceptor/IgnoreGzipInterceptor.kt diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt b/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt index f8f1a6395c..5516ed748a 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.network import android.content.Context import eu.kanade.tachiyomi.network.interceptor.CloudflareInterceptor +import eu.kanade.tachiyomi.network.interceptor.IgnoreGzipInterceptor import eu.kanade.tachiyomi.network.interceptor.UncaughtExceptionInterceptor import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor import okhttp3.Cache @@ -30,9 +31,10 @@ class NetworkHelper( maxSize = 5L * 1024 * 1024, // 5 MiB ), ) - .addInterceptor(BrotliInterceptor) .addInterceptor(UncaughtExceptionInterceptor()) .addInterceptor(UserAgentInterceptor(::defaultUserAgentProvider)) + .addNetworkInterceptor(IgnoreGzipInterceptor()) + .addNetworkInterceptor(BrotliInterceptor) if (preferences.verboseLogging().get()) { val httpLoggingInterceptor = HttpLoggingInterceptor().apply { diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/IgnoreGzipInterceptor.kt b/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/IgnoreGzipInterceptor.kt new file mode 100644 index 0000000000..f1331a5766 --- /dev/null +++ b/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/IgnoreGzipInterceptor.kt @@ -0,0 +1,21 @@ +package eu.kanade.tachiyomi.network.interceptor + +import okhttp3.Interceptor +import okhttp3.Response + +/** + * To use [okhttp3.brotli.BrotliInterceptor] as a network interceptor, + * add [IgnoreGzipInterceptor] right before it. + * + * This nullifies the transparent gzip of [okhttp3.internal.http.BridgeInterceptor] + * so gzip and Brotli are explicitly handled by the [okhttp3.brotli.BrotliInterceptor]. + */ +class IgnoreGzipInterceptor : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { + var request = chain.request() + if (request.header("Accept-Encoding") == "gzip") { + request = request.newBuilder().removeHeader("Accept-Encoding").build() + } + return chain.proceed(request) + } +}