保存Cookie时以二级域名为url

pull/337/head
AndyBernie 4 years ago
parent df7af4617b
commit c23173f668
  1. 8
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  2. 16
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 8
      app/src/main/java/io/legado/app/utils/NetworkUtils.kt

@ -6,13 +6,13 @@ import com.franmontiel.persistentcookiejar.persistence.SerializableCookie
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.Cookie import io.legado.app.data.entities.Cookie
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.NetworkUtils
object CookieStore : CookiePersistor { object CookieStore : CookiePersistor {
fun setCookie(url: String, cookie: String?) { fun setCookie(url: String, cookie: String?) {
Coroutine.async { Coroutine.async {
val cookieBean = Cookie(url, cookie ?: "") val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "")
App.db.cookieDao().insert(cookieBean) App.db.cookieDao().insert(cookieBean)
} }
} }
@ -33,12 +33,12 @@ object CookieStore : CookiePersistor {
} }
fun getCookie(url: String): String { fun getCookie(url: String): String {
val cookieBean = App.db.cookieDao().get(url) val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url))
return cookieBean?.cookie ?: "" return cookieBean?.cookie ?: ""
} }
fun removeCookie(url: String) { fun removeCookie(url: String) {
App.db.cookieDao().delete(url) App.db.cookieDao().delete(NetworkUtils.getSubDomain(url))
} }
private fun cookieToMap(cookie: String): MutableMap<String, String> { private fun cookieToMap(cookie: String): MutableMap<String, String> {

@ -348,21 +348,9 @@ class AnalyzeUrl(
@Throws(Exception::class) @Throws(Exception::class)
fun getImageBytes(tag: String): ByteArray? { fun getImageBytes(tag: String): ByteArray? {
//资源为本站的资源,保留cookie
//图片盗链的不保留当前的cookie,可由js生成图片源站的cookie
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
NetworkUtils.getBaseUrl(url)?.let { if (cookie.isNotEmpty()) {
val regex: Any headerMap["Cookie"] += cookie
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
}
}
} }
headerMap[UA_NAME] = headerMap[UA_NAME] ?: userAgent headerMap[UA_NAME] = headerMap[UA_NAME] ?: userAgent
return if (fieldMap.isEmpty()) { return if (fieldMap.isEmpty()) {

@ -98,6 +98,14 @@ object NetworkUtils {
} else url.substring(0, index) } 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. * Get local Ip address.
*/ */

Loading…
Cancel
Save