[API] Fix detecting session cookies. Remove expired cookies.

This commit is contained in:
Kuba Szczodrzyński 2023-03-24 21:26:46 +01:00
parent db00566ebf
commit 21c00bbe53
No known key found for this signature in database
GPG Key ID: 43037AC62A600562
4 changed files with 44 additions and 7 deletions

View File

@ -33,7 +33,10 @@ class DumbCookie(var cookie: Cookie) {
cookie = Cookie.Builder()
.name(cookie.name())
.value(cookie.value())
.expiresAt(cookie.expiresAt())
.also {
if (cookie.persistent())
it.expiresAt(cookie.expiresAt())
}
.domain(cookie.domain())
.build()
}
@ -58,8 +61,6 @@ class DumbCookie(var cookie: Cookie) {
return hash
}
fun serialize(): Pair<String, String> {
val key = cookie.domain() + "|" + cookie.name()
return key to cookie.toString()
}
fun serializeKey() = cookie.domain() + "|" + cookie.name()
fun serialize() = serializeKey() to cookie.toString()
}

View File

@ -30,10 +30,20 @@ class DumbCookieJar(
private val sessionCookies = mutableSetOf<DumbCookie>()
init {
val toRemove = mutableListOf<String>()
prefs.all.forEach { (key, value) ->
if (value !is String)
return@forEach
sessionCookies.add(DumbCookie.deserialize(key, value) ?: return@forEach)
val dc = DumbCookie.deserialize(key, value) ?: return@forEach
if (dc.cookie.expiresAt() > System.currentTimeMillis())
sessionCookies.add(dc)
else
toRemove.add(key)
}
prefs.edit {
for (key in toRemove) {
remove(key)
}
}
}
@ -48,7 +58,14 @@ class DumbCookieJar(
}
}
private fun delete(vararg toRemove: DumbCookie) {
sessionCookies.removeAll(toRemove)
sessionCookies.removeAll(toRemove.toSet())
prefs.edit {
for (dc in toRemove) {
val key = dc.serializeKey()
if (prefs.contains(key))
remove(key)
}
}
}
override fun saveFromResponse(url: HttpUrl, cookies: MutableList<Cookie>) {
@ -114,4 +131,11 @@ class DumbCookieJar(
}
delete(*toRemove.toTypedArray())
}
fun clearAllDomains() {
sessionCookies.clear()
prefs.edit {
clear()
}
}
}

View File

@ -179,6 +179,10 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
return@setOnChangeListener true
}
b.clearCookies.onClick {
app.cookieJar.clearAllDomains()
}
val colorSecondary = android.R.attr.textColorSecondary.resolveAttr(activity)
startCoroutineTimer(500L, 300L) {
val text = app.cookieJar.getAllDomains()

View File

@ -107,6 +107,14 @@
android:text="Reset event types"
android:textAllCaps="false" />
<Button
android:id="@+id/clearCookies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Clear all cookies"
android:textAllCaps="false" />
<TextView
android:id="@+id/cookies"
android:layout_width="match_parent"