pull/274/head
gedoor 4 years ago
parent 6ec41d10e2
commit ced13d2544
  1. 6
      app/src/main/java/io/legado/app/data/dao/CookieDao.kt
  2. 35
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  3. 4
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt

@ -9,6 +9,9 @@ interface CookieDao {
@Query("SELECT * FROM cookies Where url = :url")
fun get(url: String): Cookie?
@Query("select * from cookies where url like '%|%'")
fun getOkHttpCookies(): List<Cookie>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg cookie: Cookie)
@ -17,4 +20,7 @@ interface CookieDao {
@Query("delete from cookies where url = :url")
fun delete(url: String)
@Query("delete from cookies where url like '%|%'")
fun deleteOkHttp()
}

@ -1,12 +1,14 @@
package io.legado.app.help.http
import android.text.TextUtils
import com.franmontiel.persistentcookiejar.persistence.CookiePersistor
import com.franmontiel.persistentcookiejar.persistence.SerializableCookie
import io.legado.app.App
import io.legado.app.data.entities.Cookie
import io.legado.app.help.coroutine.Coroutine
object CookieStore {
object CookieStore : CookiePersistor {
fun setCookie(url: String, cookie: String?) {
Coroutine.async {
@ -76,5 +78,36 @@ object CookieStore {
return builder.deleteCharAt(builder.lastIndexOf(";")).toString()
}
override fun loadAll(): MutableList<okhttp3.Cookie> {
val cookies = arrayListOf<okhttp3.Cookie>()
App.db.cookieDao().getOkHttpCookies().forEach {
val serializedCookie = it.cookie
SerializableCookie().decode(serializedCookie)?.let { ck ->
cookies.add(ck)
}
}
return cookies
}
override fun saveAll(cookies: MutableCollection<okhttp3.Cookie>?) {
val mCookies = arrayListOf<Cookie>()
cookies?.forEach {
mCookies.add(Cookie(createCookieKey(it), SerializableCookie().encode(it)))
}
App.db.cookieDao().insert(*mCookies.toTypedArray())
}
override fun removeAll(cookies: MutableCollection<okhttp3.Cookie>?) {
cookies?.forEach {
App.db.cookieDao().delete(createCookieKey(it))
}
}
override fun clear() {
App.db.cookieDao().deleteOkHttp()
}
private fun createCookieKey(cookie: okhttp3.Cookie): String {
return (if (cookie.secure()) "https" else "http") + "://" + cookie.domain() + cookie.path() + "|" + cookie.name()
}
}

@ -2,8 +2,6 @@ package io.legado.app.help.http
import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import io.legado.app.App
import io.legado.app.constant.AppConst
import io.legado.app.help.http.api.HttpGetApi
import io.legado.app.utils.NetworkUtils
@ -22,7 +20,7 @@ object HttpHelper {
val client: OkHttpClient by lazy {
val cookieJar =
PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(App.INSTANCE))
PersistentCookieJar(SetCookieCache(), CookieStore)
val specs = arrayListOf(
ConnectionSpec.MODERN_TLS,

Loading…
Cancel
Save