访问完成就保存cookie

pull/2605/head
kunfei 2 years ago
parent 2ea28b6070
commit 8154c001c1
  1. 5
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  2. 45
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -41,7 +41,10 @@ object CookieStore : CookieManager {
*/
override fun getCookie(url: String): String {
val domain = NetworkUtils.getSubDomain(url)
CacheManager.getFromMemory("${domain}_cookie")?.let { return it }
CacheManager.getFromMemory("${domain}_cookie")?.let {
return it
}
val cookieBean = appDb.cookieDao.get(domain)
val cookie = cookieBean?.cookie ?: ""
CacheManager.putMemory(url, cookie)

@ -74,6 +74,7 @@ class AnalyzeUrl(
private var useWebView: Boolean = false
private var webJs: String? = null
private val enabledCookieJar = source?.enabledCookieJar ?: false
private val domain: String
init {
val urlMatcher = paramPattern.matcher(baseUrl)
@ -86,6 +87,7 @@ class AnalyzeUrl(
}
}
initUrl()
domain = NetworkUtils.getSubDomain(source?.getKey() ?: url)
}
/**
@ -368,7 +370,7 @@ class AnalyzeUrl(
}
val concurrentRecord = fetchStart()
try {
setCookie(source?.getKey())
setCookie()
val strResponse: StrResponse
if (this.useWebView && useWebView) {
strResponse = when (method) {
@ -422,6 +424,7 @@ class AnalyzeUrl(
}
return strResponse
} finally {
saveCookie()
fetchEnd(concurrentRecord)
}
}
@ -463,7 +466,7 @@ class AnalyzeUrl(
suspend fun getResponseAwait(): Response {
val concurrentRecord = fetchStart()
try {
setCookie(source?.getKey())
setCookie()
@Suppress("BlockingMethodInNonBlockingContext")
val response = getProxyClient(proxy).newCallResponse(retry) {
addHeaders(headerMap)
@ -486,6 +489,7 @@ class AnalyzeUrl(
}
return response
} finally {
saveCookie()
fetchEnd(concurrentRecord)
}
}
@ -569,21 +573,19 @@ class AnalyzeUrl(
}
/**
*设置cookie 优先级
* 设置cookie 优先级
* urlOption临时cookie > 数据库cookie = okhttp CookieJar保存在内存中的cookie
*@param tag 书源url 缺省为传入的url
*/
private fun setCookie(tag: String?) {
val domain = NetworkUtils.getSubDomain(tag ?: url)
//书源启用保存cookie时 添加内存中的cookie到数据库
if (enabledCookieJar) {
val key = "${domain}_cookieJar"
CacheManager.getFromMemory(key)?.let {
CookieStore.replaceCookie(domain, it)
CacheManager.deleteMemory(key)
private fun setCookie() {
val cookie = kotlin.run {
if (enabledCookieJar) {
val key = "${domain}_cookieJar"
CacheManager.getFromMemory(key)?.let {
return@run it
}
}
CookieStore.getCookie(domain)
}
val cookie = CookieStore.getCookie(domain)
if (cookie.isNotEmpty()) {
val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "")
@ -594,11 +596,26 @@ class AnalyzeUrl(
}
}
/**
* 保存cookie
*
*/
private fun saveCookie() {
//书源启用保存cookie时 添加内存中的cookie到数据库
if (enabledCookieJar) {
val key = "${domain}_cookieJar"
CacheManager.getFromMemory(key)?.let {
CookieStore.replaceCookie(domain, it)
CacheManager.deleteMemory(key)
}
}
}
/**
*获取处理过阅读定义的urlOption和cookie的GlideUrl
*/
fun getGlideUrl(): GlideUrl {
setCookie(source?.getKey())
setCookie()
return GlideUrl(url, GlideHeaders(headerMap))
}

Loading…
Cancel
Save