Skip to content

Commit

Permalink
KTOR-7172 Fix IndexOutOfBoundsException in HttpCache (#4509)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte authored Nov 28, 2024
1 parent f3ac483 commit e9404c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/test/server/tests/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ internal fun Application.cacheTestServer() {
call.response.header(HttpHeaders.CacheControl, "max-age=${Long.MAX_VALUE}000")
call.respond(HttpStatusCode.OK)
}
get("/invalid-max-age") {
call.response.header(HttpHeaders.CacheControl, "max-age: 120")
call.respond(HttpStatusCode.OK)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal fun HttpResponse.cacheExpires(isShared: Boolean, fallback: () -> GMTDat

val maxAge = cacheControl.firstOrNull { it.value.startsWith(maxAgeKey) }
?.value?.split("=")
?.get(1)?.toLongOrNull()
?.getOrNull(1)?.toLongOrNull()

if (maxAge != null) {
return requestTime + maxAge * 1000L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,21 @@ class CacheTest : ClientLoader() {
}
}

@Test
fun testInvalidMaxAge() = clientTests {
config {
install(HttpCache)
}

test { client ->
val url = Url("$TEST_SERVER/cache/invalid-max-age")

client.get(url).apply {
assertEquals(HttpStatusCode.OK, status)
}
}
}

/**
* Does delay and ensures that the [GMTDate] measurements report at least
* the specified number of [milliseconds].
Expand Down

0 comments on commit e9404c5

Please sign in to comment.