Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-7104 Fix saving caches with different vary header #4673

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class UnlimitedCacheStorage : HttpCacheStorage() {
override fun find(url: Url, varyKeys: Map<String, String>): HttpCacheEntry? {
val data = store.computeIfAbsent(url) { ConcurrentSet() }
return data.find {
varyKeys.all { (key, value) -> it.varyKeys[key] == value }
varyKeys.all { (key, value) -> it.varyKeys[key] == value } && varyKeys.size == it.varyKeys.size
}
}

Expand All @@ -45,7 +45,7 @@ internal class UnlimitedStorage : CacheStorage {
override suspend fun find(url: Url, varyKeys: Map<String, String>): CachedResponseData? {
val data = store.computeIfAbsent(url) { ConcurrentSet() }
return data.find {
varyKeys.all { (key, value) -> it.varyKeys[key] == value }
varyKeys.all { (key, value) -> it.varyKeys[key] == value } && varyKeys.size == it.varyKeys.size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class CachingCacheStorage(
}
val data = store.getValue(url)
return data.find {
varyKeys.all { (key, value) -> it.varyKeys[key] == value }
varyKeys.all { (key, value) -> it.varyKeys[key] == value } && varyKeys.size == it.varyKeys.size
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ private class FileCacheStorage(
override suspend fun find(url: Url, varyKeys: Map<String, String>): CachedResponseData? {
val data = readCache(key(url))
return data.find {
varyKeys.all { (key, value) -> it.varyKeys[key] == value }
varyKeys.all { (key, value) -> it.varyKeys[key] == value } && varyKeys.size == it.varyKeys.size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private class InMemoryCacheStorage : CacheStorage {
findCalledCount++
val cache = store.computeIfAbsent(url) { mutableSetOf() }
return cache.find {
varyKeys.all { (key, value) -> it.varyKeys[key] == value }
varyKeys.all { (key, value) -> it.varyKeys[key] == value } && varyKeys.size == it.varyKeys.size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,28 @@ class CacheTest : ClientLoader() {
}
}

@Test
fun testDifferentVaryHeaders() = clientTests {
val storage = CacheStorage.Unlimited()
config {
install(HttpCache) {
publicStorage(storage)
}
}

test { client ->
client.get("$TEST_SERVER/cache/different-vary") {
header("200", "true")
header("Set-Vary", "X-Requested-With,Accept-Encoding")
}
assertFailsWith<InvalidCacheStateException> {
client.get("$TEST_SERVER/cache/different-vary") {
header("Set-Vary", "X-Requested-With")
}
}
}
}

/**
* Does delay and ensures that the [GMTDate] measurements report at least
* the specified number of [milliseconds].
Expand Down
11 changes: 10 additions & 1 deletion ktor-test-server/src/main/kotlin/test/server/tests/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal fun Application.cacheTestServer() {
get("/etag-304") {
if (call.request.header("If-None-Match") == "My-ETAG") {
call.response.header("Etag", "My-ETAG")
call.response.header("Vary", "Origin")
call.response.header("Vary", "Origin, Accept-Encoding")
call.respond(HttpStatusCode.NotModified)
return@get
}
Expand Down Expand Up @@ -118,6 +118,15 @@ internal fun Application.cacheTestServer() {
call.response.header(HttpHeaders.CacheControl, "max-age: 120")
call.respond(HttpStatusCode.OK)
}
get("/different-vary") {
if (call.request.headers.contains("200")) {
call.response.header("Vary", "X-Requested-With,Accept-Encoding")
call.respond(HttpStatusCode.OK)
} else {
call.response.header("Vary", "X-Requested-With")
call.respond(HttpStatusCode.NotModified)
}
}
}
}
}