fix(getSubDomain):二级域名带http/https

This commit is contained in:
Xwite
2022-04-14 07:40:23 +08:00
parent 589cf05a2d
commit c6cd4bb138
3 changed files with 13 additions and 3 deletions

View File

@@ -15,6 +15,7 @@
* 修复解码正文图片报错,添加解码日志
* js文档java.toast java.longToast
* cookie保存策略更改若登录失效请重新登录
**2022/04/12**

View File

@@ -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 ?: ""

View File

@@ -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)
}