From db3c98fe729ef6b00beba8d605bc002a7b8d1712 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 25 Dec 2022 00:24:53 -0500 Subject: [PATCH] Update OkHttp --- .../kanade/tachiyomi/network/NetworkHelper.kt | 4 - .../network/interceptor/Http103Interceptor.kt | 110 ------------------ gradle/libs.versions.toml | 2 +- 3 files changed, 1 insertion(+), 115 deletions(-) delete mode 100644 core/src/main/java/eu/kanade/tachiyomi/network/interceptor/Http103Interceptor.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 adf77d07e3..d81b666d6b 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.network import android.content.Context import eu.kanade.tachiyomi.network.interceptor.CloudflareInterceptor -import eu.kanade.tachiyomi.network.interceptor.Http103Interceptor import eu.kanade.tachiyomi.network.interceptor.UserAgentInterceptor import okhttp3.Cache import okhttp3.OkHttpClient @@ -21,7 +20,6 @@ class NetworkHelper(context: Context) { val cookieManager = AndroidCookieJar() private val userAgentInterceptor by lazy { UserAgentInterceptor() } - private val http103Interceptor by lazy { Http103Interceptor(context) } private val cloudflareInterceptor by lazy { CloudflareInterceptor(context) } private val baseClientBuilder: OkHttpClient.Builder @@ -31,9 +29,7 @@ class NetworkHelper(context: Context) { .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .callTimeout(2, TimeUnit.MINUTES) - .fastFallback(true) .addInterceptor(userAgentInterceptor) - .addNetworkInterceptor(http103Interceptor) if (preferences.verboseLogging().get()) { val httpLoggingInterceptor = HttpLoggingInterceptor().apply { diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/Http103Interceptor.kt b/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/Http103Interceptor.kt deleted file mode 100644 index 4d71705fcb..0000000000 --- a/core/src/main/java/eu/kanade/tachiyomi/network/interceptor/Http103Interceptor.kt +++ /dev/null @@ -1,110 +0,0 @@ -package eu.kanade.tachiyomi.network.interceptor - -import android.annotation.SuppressLint -import android.content.Context -import android.webkit.JavascriptInterface -import android.webkit.WebView -import androidx.core.content.ContextCompat -import eu.kanade.tachiyomi.util.system.WebViewClientCompat -import eu.kanade.tachiyomi.util.system.logcat -import okhttp3.Interceptor -import okhttp3.MediaType.Companion.toMediaType -import okhttp3.Protocol -import okhttp3.Request -import okhttp3.Response -import okhttp3.ResponseBody.Companion.toResponseBody -import java.io.IOException -import java.util.concurrent.CountDownLatch - -// TODO: Remove when OkHttp can handle HTTP 103 responses -class Http103Interceptor(context: Context) : WebViewInterceptor(context) { - - private val executor = ContextCompat.getMainExecutor(context) - - override fun shouldIntercept(response: Response): Boolean { - return response.code == 103 - } - - override fun intercept(chain: Interceptor.Chain, request: Request, response: Response): Response { - logcat { "Proceeding with WebView for request $request" } - try { - return proceedWithWebView(request, response) - } catch (e: Exception) { - throw IOException(e) - } - } - - @SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface") - private fun proceedWithWebView(originalRequest: Request, originalResponse: Response): Response { - // We need to lock this thread until the WebView loads the page, because - // OkHttp doesn't support asynchronous interceptors. - val latch = CountDownLatch(1) - - val jsInterface = JsInterface(latch) - - var webview: WebView? = null - - var exception: Exception? = null - - val requestUrl = originalRequest.url.toString() - val headers = parseHeaders(originalRequest.headers) - - executor.execute { - webview = createWebView(originalRequest) - webview?.addJavascriptInterface(jsInterface, "android") - - webview?.webViewClient = object : WebViewClientCompat() { - override fun onPageFinished(view: WebView, url: String) { - view.evaluateJavascript(jsScript) {} - } - - override fun onReceivedErrorCompat( - view: WebView, - errorCode: Int, - description: String?, - failingUrl: String, - isMainFrame: Boolean, - ) { - if (isMainFrame) { - exception = Exception("Error $errorCode - $description") - latch.countDown() - } - } - } - - webview?.loadUrl(requestUrl, headers) - } - - latch.awaitFor30Seconds() - - executor.execute { - webview?.run { - stopLoading() - destroy() - } - } - - exception?.let { throw it } - - val responseHtml = jsInterface.responseHtml ?: throw Exception("Couldn't fetch site through webview") - - return originalResponse.newBuilder() - .code(200) - .protocol(Protocol.HTTP_1_1) - .message("OK") - .body(responseHtml.toResponseBody(htmlMediaType)) - .build() - } -} - -internal class JsInterface(private val latch: CountDownLatch, var responseHtml: String? = null) { - @Suppress("UNUSED") - @JavascriptInterface - fun passPayload(passedPayload: String) { - responseHtml = passedPayload - latch.countDown() - } -} - -private const val jsScript = "window.android.passPayload(document.querySelector('html').outerHTML)" -private val htmlMediaType = "text/html".toMediaType() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 149a929155..5d97fd207b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,6 @@ [versions] aboutlib_version = "10.5.2" -okhttp_version = "5.0.0-alpha.10" +okhttp_version = "5.0.0-alpha.11" coil_version = "2.2.2" shizuku_version = "12.2.0" sqlite = "2.3.0-rc01"