Skip to content

Instantly share code, notes, and snippets.

@Radiokot
Last active November 25, 2024 15:06
Show Gist options
  • Save Radiokot/e54c0e9405fd697166ca8416566ef55f to your computer and use it in GitHub Desktop.
Save Radiokot/e54c0e9405fd697166ca8416566ef55f to your computer and use it in GitHub Desktop.
A simple OkHttp CookieJar implementation which stores the cookies in the memory, without a persistence
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
/**
* A simple [CookieJar] which stores the cookies in the memory, without a persistence.
*/
class InMemoryCookieJar : CookieJar {
private val cookieSet = mutableSetOf<StoredCookie>()
override fun loadForRequest(url: HttpUrl): List<Cookie> = synchronized(this) {
cookieSet.removeAll(StoredCookie::isExpired)
return@synchronized cookieSet.mapNotNull { it.takeIf { it.matches(url) }?.cookie }
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
synchronized(this) {
cookies.mapTo(cookieSet, ::StoredCookie)
}
}
private class StoredCookie(val cookie: Cookie) {
val isExpired: Boolean
get() = cookie.expiresAt < System.currentTimeMillis()
fun matches(url: HttpUrl) =
cookie.matches(url)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is StoredCookie) return false
return this.cookie.name == other.cookie.name
&& this.cookie.domain == other.cookie.domain
&& this.cookie.path == other.cookie.path
&& this.cookie.secure == other.cookie.secure
&& this.cookie.hostOnly == other.cookie.hostOnly
}
override fun hashCode(): Int = with(cookie) {
var result = 17
result = 31 * result + name.hashCode()
result = 31 * result + domain.hashCode()
result = 31 * result + path.hashCode()
result = 31 * result + secure.hashCode()
result = 31 * result + hostOnly.hashCode()
return result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment