From c23173f668592bdc6f9549f8c2f3d615ec1500cf Mon Sep 17 00:00:00 2001 From: AndyBernie Date: Wed, 26 Aug 2020 18:32:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98Cookie=E6=97=B6=E4=BB=A5?= =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E5=9F=9F=E5=90=8D=E4=B8=BAurl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/http/CookieStore.kt | 8 ++++---- .../legado/app/model/analyzeRule/AnalyzeUrl.kt | 16 ++-------------- .../java/io/legado/app/utils/NetworkUtils.kt | 8 ++++++++ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 3f40cfbe0..067435ddb 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -6,13 +6,13 @@ import com.franmontiel.persistentcookiejar.persistence.SerializableCookie import io.legado.app.App import io.legado.app.data.entities.Cookie import io.legado.app.help.coroutine.Coroutine - +import io.legado.app.utils.NetworkUtils object CookieStore : CookiePersistor { fun setCookie(url: String, cookie: String?) { Coroutine.async { - val cookieBean = Cookie(url, cookie ?: "") + val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") App.db.cookieDao().insert(cookieBean) } } @@ -33,12 +33,12 @@ object CookieStore : CookiePersistor { } fun getCookie(url: String): String { - val cookieBean = App.db.cookieDao().get(url) + val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url)) return cookieBean?.cookie ?: "" } fun removeCookie(url: String) { - App.db.cookieDao().delete(url) + App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) } private fun cookieToMap(cookie: String): MutableMap { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 56b1f1c0e..561e3a320 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -348,21 +348,9 @@ class AnalyzeUrl( @Throws(Exception::class) fun getImageBytes(tag: String): ByteArray? { - //资源为本站的资源,保留cookie - //图片盗链的不保留当前的cookie,可由js生成图片源站的cookie val cookie = CookieStore.getCookie(tag) - NetworkUtils.getBaseUrl(url)?.let { - val regex: Any - regex = if (it.lastIndexOf(".") != it.indexOf(".")) { - it.substring(it.indexOf(".") + 1).toRegex() - } else { - it.substring(it.lastIndexOf("/") + 1).toRegex() - } - if (regex.containsMatchIn(tag)) { - if (cookie.isNotEmpty()) { - headerMap["Cookie"] += cookie - } - } + if (cookie.isNotEmpty()) { + headerMap["Cookie"] += cookie } headerMap[UA_NAME] = headerMap[UA_NAME] ?: userAgent return if (fieldMap.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index b706a8d5e..4a3e236cb 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -98,6 +98,14 @@ object NetworkUtils { } else url.substring(0, index) } + fun getSubDomain(url: String?): String { + var baseUrl = getBaseUrl(url) + if (baseUrl == null) return "" + return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { + baseUrl.substring(baseUrl.lastIndexOf("/")+1) + } else baseUrl.substring(baseUrl.indexOf(".")+1) + } + /** * Get local Ip address. */