forked from github/szkolny
[API] Fix detecting session cookies. Remove expired cookies.
This commit is contained in:
parent
db00566ebf
commit
21c00bbe53
@ -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()
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user