kunfei 4 лет назад
Родитель
Сommit
45ffb0ef21

+ 4 - 1
app/src/main/java/io/legado/app/help/http/CookieStore.kt

@@ -41,7 +41,10 @@ object CookieStore : CookieManager {
      */
      */
     override fun getCookie(url: String): String {
     override fun getCookie(url: String): String {
         val domain = NetworkUtils.getSubDomain(url)
         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 cookieBean = appDb.cookieDao.get(domain)
         val cookie = cookieBean?.cookie ?: ""
         val cookie = cookieBean?.cookie ?: ""
         CacheManager.putMemory(url, cookie)
         CacheManager.putMemory(url, cookie)

+ 31 - 14
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@@ -74,6 +74,7 @@ class AnalyzeUrl(
     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 ?: false
+    private val domain: String
 
 
     init {
     init {
         val urlMatcher = paramPattern.matcher(baseUrl)
         val urlMatcher = paramPattern.matcher(baseUrl)
@@ -86,6 +87,7 @@ class AnalyzeUrl(
             }
             }
         }
         }
         initUrl()
         initUrl()
+        domain = NetworkUtils.getSubDomain(source?.getKey() ?: url)
     }
     }
 
 
     /**
     /**
@@ -368,7 +370,7 @@ class AnalyzeUrl(
         }
         }
         val concurrentRecord = fetchStart()
         val concurrentRecord = fetchStart()
         try {
         try {
-            setCookie(source?.getKey())
+            setCookie()
             val strResponse: StrResponse
             val strResponse: StrResponse
             if (this.useWebView && useWebView) {
             if (this.useWebView && useWebView) {
                 strResponse = when (method) {
                 strResponse = when (method) {
@@ -422,6 +424,7 @@ class AnalyzeUrl(
             }
             }
             return strResponse
             return strResponse
         } finally {
         } finally {
+            saveCookie()
             fetchEnd(concurrentRecord)
             fetchEnd(concurrentRecord)
         }
         }
     }
     }
@@ -463,7 +466,7 @@ class AnalyzeUrl(
     suspend fun getResponseAwait(): Response {
     suspend fun getResponseAwait(): Response {
         val concurrentRecord = fetchStart()
         val concurrentRecord = fetchStart()
         try {
         try {
-            setCookie(source?.getKey())
+            setCookie()
             @Suppress("BlockingMethodInNonBlockingContext")
             @Suppress("BlockingMethodInNonBlockingContext")
             val response = getProxyClient(proxy).newCallResponse(retry) {
             val response = getProxyClient(proxy).newCallResponse(retry) {
                 addHeaders(headerMap)
                 addHeaders(headerMap)
@@ -486,6 +489,7 @@ class AnalyzeUrl(
             }
             }
             return response
             return response
         } finally {
         } finally {
+            saveCookie()
             fetchEnd(concurrentRecord)
             fetchEnd(concurrentRecord)
         }
         }
     }
     }
@@ -569,21 +573,19 @@ class AnalyzeUrl(
     }
     }
 
 
     /**
     /**
-     *设置cookie 优先级
+     * 设置cookie 优先级
      * urlOption临时cookie > 数据库cookie = okhttp CookieJar保存在内存中的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()) {
         if (cookie.isNotEmpty()) {
             val cookieMap = CookieStore.cookieToMap(cookie)
             val cookieMap = CookieStore.cookieToMap(cookie)
             val customCookieMap = CookieStore.cookieToMap(headerMap["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
      *获取处理过阅读定义的urlOption和cookie的GlideUrl
      */
      */
     fun getGlideUrl(): GlideUrl {
     fun getGlideUrl(): GlideUrl {
-        setCookie(source?.getKey())
+        setCookie()
         return GlideUrl(url, GlideHeaders(headerMap))
         return GlideUrl(url, GlideHeaders(headerMap))
     }
     }