Fix PersistentCookieStore for domains with an underscore (#989)

* Fix PersistentCookieStore for domains with an underscore

* Missed one uri
This commit is contained in:
AeonLucid
2024-07-28 21:57:30 +02:00
committed by GitHub
parent d05ed0a56c
commit 25a62e33a1

View File

@@ -8,6 +8,7 @@ import okio.withLock
import java.net.CookieStore import java.net.CookieStore
import java.net.HttpCookie import java.net.HttpCookie
import java.net.URI import java.net.URI
import java.net.URL
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.milliseconds
@@ -45,10 +46,8 @@ class PersistentCookieStore(context: Context) : CookieStore {
cookies: List<Cookie>, cookies: List<Cookie>,
) { ) {
lock.withLock { lock.withLock {
val uri = url.toUri()
// Append or replace the cookies for this domain. // Append or replace the cookies for this domain.
val cookiesForDomain = cookieMap[uri.host].orEmpty().toMutableList() val cookiesForDomain = cookieMap[url.host].orEmpty().toMutableList()
for (cookie in cookies) { for (cookie in cookies) {
// Find a cookie with the same name. Replace it if found, otherwise add a new one. // Find a cookie with the same name. Replace it if found, otherwise add a new one.
val pos = cookiesForDomain.indexOfFirst { it.name == cookie.name } val pos = cookiesForDomain.indexOfFirst { it.name == cookie.name }
@@ -58,9 +57,9 @@ class PersistentCookieStore(context: Context) : CookieStore {
cookiesForDomain[pos] = cookie cookiesForDomain[pos] = cookie
} }
} }
cookieMap[uri.host] = cookiesForDomain cookieMap[url.host] = cookiesForDomain
saveToDisk(uri) saveToDisk(url.toUrl())
} }
} }
@@ -74,19 +73,22 @@ class PersistentCookieStore(context: Context) : CookieStore {
} }
fun remove(uri: URI) { fun remove(uri: URI) {
val url = uri.toURL()
lock.withLock { lock.withLock {
prefs.edit().remove(uri.host).apply() prefs.edit().remove(url.host).apply()
cookieMap.remove(uri.host) cookieMap.remove(url.host)
} }
} }
override fun get(uri: URI): List<HttpCookie> = override fun get(uri: URI): List<HttpCookie> {
get(uri.host).map { val url = uri.toURL()
return get(url.host).map {
it.toHttpCookie() it.toHttpCookie()
} }
}
fun get(url: HttpUrl): List<Cookie> { fun get(url: HttpUrl): List<Cookie> {
return get(url.toUri().host ?: return emptyList()) return get(url.host)
} }
override fun add( override fun add(
@@ -95,10 +97,11 @@ class PersistentCookieStore(context: Context) : CookieStore {
) { ) {
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
val uri = uri ?: URI("http://" + cookie.domain.removePrefix(".")) val uri = uri ?: URI("http://" + cookie.domain.removePrefix("."))
val url = uri.toURL()
lock.withLock { lock.withLock {
val cookies = cookieMap[uri.host] val cookies = cookieMap[url.host]
cookieMap[uri.host] = cookies.orEmpty() + cookie.toCookie(uri) cookieMap[url.host] = cookies.orEmpty() + cookie.toCookie(uri)
saveToDisk(uri) saveToDisk(url)
} }
} }
@@ -122,8 +125,9 @@ class PersistentCookieStore(context: Context) : CookieStore {
): Boolean { ): Boolean {
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
val uri = uri ?: URI("http://" + cookie.domain.removePrefix(".")) val uri = uri ?: URI("http://" + cookie.domain.removePrefix("."))
val url = uri.toURL()
return lock.withLock { return lock.withLock {
val cookies = cookieMap[uri.host].orEmpty() val cookies = cookieMap[url.host].orEmpty()
val index = val index =
cookies.indexOfFirst { cookies.indexOfFirst {
it.name == cookie.name && it.name == cookie.name &&
@@ -132,8 +136,8 @@ class PersistentCookieStore(context: Context) : CookieStore {
if (index >= 0) { if (index >= 0) {
val newList = cookies.toMutableList() val newList = cookies.toMutableList()
newList.removeAt(index) newList.removeAt(index)
cookieMap[uri.host] = newList.toList() cookieMap[url.host] = newList.toList()
saveToDisk(uri) saveToDisk(url)
true true
} else { } else {
false false
@@ -145,17 +149,17 @@ class PersistentCookieStore(context: Context) : CookieStore {
return cookieMap[url].orEmpty().filter { !it.hasExpired() } return cookieMap[url].orEmpty().filter { !it.hasExpired() }
} }
private fun saveToDisk(uri: URI) { private fun saveToDisk(url: URL) {
// Get cookies to be stored in disk // Get cookies to be stored in disk
val newValues = val newValues =
cookieMap[uri.host] cookieMap[url.host]
.orEmpty() .orEmpty()
.asSequence() .asSequence()
.filter { it.persistent && !it.hasExpired() } .filter { it.persistent && !it.hasExpired() }
.map(Cookie::toString) .map(Cookie::toString)
.toSet() .toSet()
prefs.edit().putStringSet(uri.host, newValues).apply() prefs.edit().putStringSet(url.host, newValues).apply()
} }
private fun Cookie.hasExpired() = System.currentTimeMillis() >= expiresAt private fun Cookie.hasExpired() = System.currentTimeMillis() >= expiresAt
@@ -164,7 +168,7 @@ class PersistentCookieStore(context: Context) : CookieStore {
Cookie.Builder() Cookie.Builder()
.name(name) .name(name)
.value(value) .value(value)
.domain(uri.host) .domain(uri.toURL().host)
.path(path ?: "/") .path(path ?: "/")
.let { .let {
if (maxAge != -1L) { if (maxAge != -1L) {