From ec09fbd8358fdb526286037f347400245497e846 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Wed, 5 Jan 2022 19:48:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96getSubDomain=EF=BC=9A?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E4=B8=BAip=E6=97=B6=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/NetworkUtils.kt | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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 ece587df0..6e3c0d8c5 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -144,10 +144,24 @@ object NetworkUtils { } else url.substring(0, index) } + /** + * 获取二级域名,供cookie保存和读取 + * + * 1.2.3.4 => 1.2.3.4 + * www.example.com => example.com + * www.example.com.cn => example.com.cn + * 未实现www.content.example.com => example.com + */ fun getSubDomain(url: String?): String { val baseUrl = getBaseUrl(url) ?: return "" + //baseUrl为IPv4时,直接返回 + //IPv6暂时不考虑支持 + if (isIPv4Address(baseUrl)) { + return baseUrl + } + //td do:利用https://github.com/publicsuffix/list,https://github.com/Kevin-Sangeelee/PublicSuffixList实现二级域名返回 return if (baseUrl.indexOf(".") == baseUrl.lastIndexOf(".")) { - baseUrl.substring(baseUrl.lastIndexOf("/") + 1) + baseUrl } else baseUrl.substring(baseUrl.indexOf(".") + 1) }