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/glide/ImageLoader.kt b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt index 625086c0a..4b0616d95 100644 --- a/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt @@ -7,7 +7,6 @@ import android.net.Uri import androidx.annotation.DrawableRes import com.bumptech.glide.RequestBuilder import io.legado.app.constant.AppPattern.dataUriRegex -import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isContentScheme import java.io.File @@ -21,19 +20,11 @@ object ImageLoader { fun load(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).load(path) - dataUriRegex.find(path) != null -> { - //glide内部已经实现dataUri解析 - GlideApp.with(context).load(path) - } + dataUriRegex.find(path) != null -> GlideApp.with(context).load(path) path.isAbsUrl() -> GlideApp.with(context).load(path) path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path)) else -> kotlin.runCatching { - val file = File(path) - if (file.exists()) { - GlideApp.with(context).load(file) - } else { - GlideApp.with(context).load(path) - } + GlideApp.with(context).load(File(path)) }.getOrElse { GlideApp.with(context).load(path) } @@ -43,8 +34,7 @@ object ImageLoader { fun loadBitmap(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).asBitmap().load(path) - path.isAbsUrl() -> GlideApp.with(context).asBitmap() - .load(AnalyzeUrl(path).getGlideUrl()) + path.isAbsUrl() -> GlideApp.with(context).asBitmap().load(path) path.isContentScheme() -> GlideApp.with(context).asBitmap().load(Uri.parse(path)) else -> kotlin.runCatching { GlideApp.with(context).asBitmap().load(File(path)) @@ -57,7 +47,7 @@ object ImageLoader { fun loadFile(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).asFile().load(path) - path.isAbsUrl() -> GlideApp.with(context).asFile().load(AnalyzeUrl(path).getGlideUrl()) + path.isAbsUrl() -> GlideApp.with(context).asFile().load(path) path.isContentScheme() -> GlideApp.with(context).asFile().load(Uri.parse(path)) else -> kotlin.runCatching { GlideApp.with(context).asFile().load(File(path)) diff --git a/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt b/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt index b8cd1ba9a..df822a618 100644 --- a/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt +++ b/app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt @@ -4,6 +4,8 @@ import com.bumptech.glide.load.Option import com.bumptech.glide.load.Options import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.ModelLoader +import io.legado.app.model.analyzeRule.AnalyzeUrl + import java.io.InputStream object OkHttpModelLoader : ModelLoader { @@ -17,7 +19,8 @@ object OkHttpModelLoader : ModelLoader { height: Int, options: Options ): ModelLoader.LoadData { - return ModelLoader.LoadData(model, OkHttpStreamFetcher(model, options)) + val modelWithHeader = AnalyzeUrl(model.toString()).getGlideUrl() + return ModelLoader.LoadData(modelWithHeader, OkHttpStreamFetcher(modelWithHeader, options)) } override fun handles(model: GlideUrl): Boolean { 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/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index cc0d64a39..dbf8f9b98 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 @@ -518,22 +518,28 @@ class AnalyzeUrl( } } + /** + *设置cookie urlOption的优先级大于书源保存的cookie + *@param tag 书源url 缺省为传入的url + */ private fun setCookie(tag: String?) { - if (tag != null) { - val cookie = CookieStore.getCookie(tag) - if (cookie.isNotEmpty()) { - val cookieMap = CookieStore.cookieToMap(cookie) - val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") - cookieMap.putAll(customCookieMap) - val newCookie = CookieStore.mapToCookie(cookieMap) - newCookie?.let { - headerMap.put("Cookie", it) - } + val cookie = CookieStore.getCookie(tag ?: url) + if (cookie.isNotEmpty()) { + val cookieMap = CookieStore.cookieToMap(cookie) + val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") + cookieMap.putAll(customCookieMap) + val newCookie = CookieStore.mapToCookie(cookieMap) + newCookie?.let { + headerMap.put("Cookie", it) } } } + /** + *获取处理过阅读定义的urlOption和cookie的GlideUrl + */ fun getGlideUrl(): GlideUrl { + setCookie(source?.getKey()) val headers = LazyHeaders.Builder() headerMap.forEach { (key, value) -> headers.addHeader(key, value) 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) }