|
|
@@ -2,6 +2,7 @@ package io.legado.app.help.http
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
import android.net.http.SslError
|
|
|
+import android.os.Build
|
|
|
import android.os.Handler
|
|
|
import android.os.Looper
|
|
|
import android.util.AndroidRuntimeException
|
|
|
@@ -20,6 +21,9 @@ import kotlinx.coroutines.Dispatchers.IO
|
|
|
import kotlinx.coroutines.Runnable
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import kotlinx.coroutines.withTimeout
|
|
|
+import okhttp3.Protocol
|
|
|
+import okhttp3.Request
|
|
|
+import okhttp3.Response
|
|
|
import org.apache.commons.text.StringEscapeUtils
|
|
|
import splitties.init.appCtx
|
|
|
import java.lang.ref.WeakReference
|
|
|
@@ -143,7 +147,20 @@ class BackstageWebView(
|
|
|
|
|
|
private inner class HtmlWebViewClient : WebViewClient() {
|
|
|
|
|
|
- var runnable: EvalJsRunnable? = null
|
|
|
+ private var runnable: EvalJsRunnable? = null
|
|
|
+ private var isRedirect = false
|
|
|
+
|
|
|
+ override fun shouldOverrideUrlLoading(
|
|
|
+ view: WebView,
|
|
|
+ request: WebResourceRequest
|
|
|
+ ): Boolean {
|
|
|
+ isRedirect = isRedirect || if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ request.isRedirect
|
|
|
+ } else {
|
|
|
+ request.url.toString() != view.url
|
|
|
+ }
|
|
|
+ return super.shouldOverrideUrlLoading(view, request)
|
|
|
+ }
|
|
|
|
|
|
override fun onPageFinished(view: WebView, url: String) {
|
|
|
setCookie(url)
|
|
|
@@ -181,7 +198,7 @@ class BackstageWebView(
|
|
|
val content = StringEscapeUtils.unescapeJson(result)
|
|
|
.replace(quoteRegex, "")
|
|
|
try {
|
|
|
- val response = StrResponse(url, content)
|
|
|
+ val response = buildStrResponse(content)
|
|
|
callback?.onResult(response)
|
|
|
} catch (e: Exception) {
|
|
|
callback?.onError(e)
|
|
|
@@ -201,6 +218,27 @@ class BackstageWebView(
|
|
|
retry++
|
|
|
mHandler.postDelayed(this@EvalJsRunnable, 1000)
|
|
|
}
|
|
|
+
|
|
|
+ private fun buildStrResponse(content: String): StrResponse {
|
|
|
+ if (!isRedirect) {
|
|
|
+ return StrResponse(url, content)
|
|
|
+ }
|
|
|
+ val originUrl = this@BackstageWebView.url ?: url
|
|
|
+ val originResponse = Response.Builder()
|
|
|
+ .code(302)
|
|
|
+ .request(Request.Builder().url(originUrl).build())
|
|
|
+ .protocol(Protocol.HTTP_1_1)
|
|
|
+ .message("Found")
|
|
|
+ .build()
|
|
|
+ val response = Response.Builder()
|
|
|
+ .code(200)
|
|
|
+ .request(Request.Builder().url(url).build())
|
|
|
+ .protocol(Protocol.HTTP_1_1)
|
|
|
+ .message("OK")
|
|
|
+ .priorResponse(originResponse)
|
|
|
+ .build()
|
|
|
+ return StrResponse(response, content)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|