Fix Cronet not loading cookieJar on first request
This commit is contained in:
@@ -59,19 +59,18 @@ object CookieManager {
|
|||||||
val cookie = CookieStore.getCookie(domain)
|
val cookie = CookieStore.getCookie(domain)
|
||||||
val requestCookie = request.header("Cookie")
|
val requestCookie = request.header("Cookie")
|
||||||
|
|
||||||
mergeCookies(requestCookie, cookie)?.let { newCookie ->
|
val newCookie = mergeCookies(requestCookie, cookie) ?: return request
|
||||||
kotlin.runCatching {
|
|
||||||
return request.newBuilder()
|
kotlin.runCatching {
|
||||||
.header("Cookie", newCookie)
|
return request.newBuilder()
|
||||||
.build()
|
.header("Cookie", newCookie)
|
||||||
}.onFailure {
|
.build()
|
||||||
CookieStore.removeCookie(url)
|
}.onFailure {
|
||||||
AppLog.put(
|
CookieStore.removeCookie(url)
|
||||||
"设置cookie出错,已清除cookie $domain cookie:$newCookie\n${it.localizedMessage}",
|
val msg = "设置cookie出错,已清除cookie $domain cookie:$newCookie\n$it"
|
||||||
it
|
AppLog.put(msg, it)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,15 @@ package io.legado.app.lib.cronet
|
|||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.annotation.Keep
|
import androidx.annotation.Keep
|
||||||
|
import io.legado.app.help.http.CookieManager
|
||||||
|
import io.legado.app.help.http.CookieManager.cookieJarHeader
|
||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import okhttp3.*
|
import okhttp3.Call
|
||||||
|
import okhttp3.CookieJar
|
||||||
|
import okhttp3.HttpUrl
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.Response
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@Keep
|
@Keep
|
||||||
@@ -28,7 +35,12 @@ class CronetInterceptor(private val cookieJar: CookieJar) : Interceptor {
|
|||||||
builder.removeHeader("Keep-Alive")
|
builder.removeHeader("Keep-Alive")
|
||||||
builder.removeHeader("Accept-Encoding")
|
builder.removeHeader("Accept-Encoding")
|
||||||
|
|
||||||
val newReq = builder.build()
|
var newReq = builder.build()
|
||||||
|
|
||||||
|
if (newReq.header(cookieJarHeader) != null) {
|
||||||
|
newReq = CookieManager.loadRequest(newReq)
|
||||||
|
}
|
||||||
|
|
||||||
return proceedWithCronet(newReq, chain.call(), chain.readTimeoutMillis())!!
|
return proceedWithCronet(newReq, chain.call(), chain.readTimeoutMillis())!!
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
cronetException = e
|
cronetException = e
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class AnalyzeUrl(
|
|||||||
private var retry: Int = 0
|
private var retry: Int = 0
|
||||||
private var useWebView: Boolean = false
|
private var useWebView: Boolean = false
|
||||||
private var webJs: String? = null
|
private var webJs: String? = null
|
||||||
private val enabledCookieJar = source?.enabledCookieJar ?: false
|
private val enabledCookieJar = source?.enabledCookieJar == true
|
||||||
private val domain: String
|
private val domain: String
|
||||||
private var webViewDelayTime: Long = 0
|
private var webViewDelayTime: Long = 0
|
||||||
private val concurrentRateLimiter = ConcurrentRateLimiter(source)
|
private val concurrentRateLimiter = ConcurrentRateLimiter(source)
|
||||||
@@ -551,6 +551,8 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
if (enabledCookieJar) {
|
if (enabledCookieJar) {
|
||||||
headerMap[CookieManager.cookieJarHeader] = "1"
|
headerMap[CookieManager.cookieJarHeader] = "1"
|
||||||
|
} else {
|
||||||
|
headerMap.remove(CookieManager.cookieJarHeader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user