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

View File

@ -30,10 +30,20 @@ class DumbCookieJar(
private val sessionCookies = mutableSetOf<DumbCookie>() private val sessionCookies = mutableSetOf<DumbCookie>()
init { init {
val toRemove = mutableListOf<String>()
prefs.all.forEach { (key, value) -> prefs.all.forEach { (key, value) ->
if (value !is String) if (value !is String)
return@forEach 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) { 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>) { override fun saveFromResponse(url: HttpUrl, cookies: MutableList<Cookie>) {
@ -114,4 +131,11 @@ class DumbCookieJar(
} }
delete(*toRemove.toTypedArray()) delete(*toRemove.toTypedArray())
} }
fun clearAllDomains() {
sessionCookies.clear()
prefs.edit {
clear()
}
}
} }

View File

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

View File

@ -107,6 +107,14 @@
android:text="Reset event types" android:text="Reset event types"
android:textAllCaps="false" /> 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 <TextView
android:id="@+id/cookies" android:id="@+id/cookies"
android:layout_width="match_parent" android:layout_width="match_parent"