Skip to content
Open
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 @@ -222,7 +222,7 @@ private class FileCacheStorage(
val varyKeysCount = channel.readInt()
val varyKeys = buildMap {
for (j in 0 until varyKeysCount) {
val key = channel.readUTF8Line()!!
val key = channel.readUTF8Line()!!.lowercase()
val value = channel.readUTF8Line()!!
put(key, value)
}
Expand Down
3 changes: 2 additions & 1 deletion ktor-client/ktor-client-core/jvm/test/FileStorageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class FileStorageTest {
val storage = FileStorage(tempDirectory)

storage.store(Url("http://example.com"), data())
storage.store(Url("http://example.com"), data(mapOf("key" to "value")))
// Use an uppercase key to test case insensitivity
storage.store(Url("http://example.com"), data(mapOf("Key" to "value")))

assertNotNull(storage.find(Url("http://example.com"), mapOf("key" to "value")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.test.base.*
import io.ktor.http.*
import io.ktor.util.date.GMTDate
import io.ktor.utils.io.core.toByteArray
import java.nio.file.Files
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -134,4 +136,46 @@ class FileCacheTest : ClientLoader() {
assertEquals("abc", second.bodyAsText())
}
}

@Test
fun testUpgradeOldCacheVersionWithCaseSensitiveVary() = clientTests {
val file = Files.createTempDirectory("cache-test-public-upgrade").toFile()
val publicStorage = FileStorage(file)
config {
install(HttpCache) {
publicStorage(publicStorage)
}
}

test { client ->
val url = Url("$TEST_SERVER/cache/vary-header-not-modified")

// Prepopulate the cache with an entry that uses a capitalized "Accept-Language" vary key.
val now = GMTDate()
publicStorage.store(
url,
CachedResponseData(
url = url,
statusCode = HttpStatusCode.OK,
requestTime = now,
responseTime = now,
version = HttpProtocolVersion.HTTP_1_1,
expires = now,
headers = headers {
append(HttpHeaders.Vary, HttpHeaders.AcceptLanguage)
},
varyKeys = mapOf(
HttpHeaders.AcceptLanguage to "en-US"
),
body = "OK".toByteArray(),
)
)

val response = client.get(url) {
header(HttpHeaders.AcceptLanguage, "en-US")
}.bodyAsText()

assertEquals("OK", response)
}
}
}
5 changes: 5 additions & 0 deletions ktor-test-server/src/main/kotlin/test/server/tests/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ internal fun Application.cacheTestServer() {
else -> error("Should not be invoked")
}
}

get("/vary-header-not-modified") {
call.response.header(HttpHeaders.Vary, HttpHeaders.AcceptLanguage)
call.respond(HttpStatusCode.NotModified)
}
}
}
}