From c6cd4bb138d52c5a54617790bc24efc992c00cfa Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Thu, 14 Apr 2022 07:40:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(getSubDomain):=E4=BA=8C=E7=BA=A7=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E5=B8=A6http/https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + app/src/main/java/io/legado/app/help/http/CookieStore.kt | 6 ++++++ app/src/main/java/io/legado/app/utils/NetworkUtils.kt | 9 ++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index fbd1ccd18..9db901086 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -15,6 +15,7 @@ * 修复解码正文图片报错,添加解码日志 * js文档:java.toast java.longToast +* cookie保存策略更改,若登录失效请重新登录 **2022/04/12** 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 e9f9b4dc6..0da6c0d97 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 @@ -10,6 +10,9 @@ import io.legado.app.utils.NetworkUtils object CookieStore : CookieManager { + /** + *保存cookie到数据库,会自动识别url的二级域名 + */ override fun setCookie(url: String, cookie: String?) { val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") appDb.cookieDao.insert(cookieBean) @@ -30,6 +33,9 @@ object CookieStore : CookieManager { } } + /** + *获取url所属的二级域名的cookie + */ override fun getCookie(url: String): String { val cookieBean = appDb.cookieDao.get(NetworkUtils.getSubDomain(url)) return cookieBean?.cookie ?: "" 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 241dce047..c78ed87b4 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -130,6 +130,8 @@ object NetworkUtils { */ fun getAbsoluteURL(baseURL: URL?, relativePath: String): String { if (baseURL == null) return relativePath + if (relativePath.isAbsUrl()) return relativePath + if (relativePath.matches(AppPattern.dataUriRegex)) return relativePath if (relativePath.startsWith("javascript")) return "" var relativeUrl = relativePath try { @@ -156,7 +158,7 @@ object NetworkUtils { } /** - * 获取域名,供cookie保存和读取 + * 获取域名,供cookie保存和读取,处理失败返回传入的url * http://1.2.3.4 => 1.2.3.4 * https://www.example.com => example.com * http://www.biquge.com.cn => biquge.com.cn @@ -167,10 +169,11 @@ object NetworkUtils { return kotlin.runCatching { val mURL = URL(baseUrl) val host: String = mURL.host + //mURL.scheme https/http //判断是否为ip - if (isIPAddress(host)) return baseUrl + if (isIPAddress(host)) return host //PublicSuffixDatabase处理域名 - PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) ?: baseUrl + PublicSuffixDatabase.get().getEffectiveTldPlusOne(host) ?: host }.getOrDefault(baseUrl) }