From bf55924a6aa4d7431e6e8a353eeab8feccbbf0d5 Mon Sep 17 00:00:00 2001 From: rsinukov Date: Wed, 14 Dec 2022 16:22:55 +0100 Subject: [PATCH] KTOR-3757 Allow specifying custom CacheControl directives --- ktor-http/api/ktor-http.api | 4 ++-- .../common/src/io/ktor/http/CacheControl.kt | 2 +- .../cachingheaders/CacheControlMerge.kt | 12 ++++++++--- .../server/plugins/CachingHeadersTest.kt | 20 +++++++++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/ktor-http/api/ktor-http.api b/ktor-http/api/ktor-http.api index 998d5f8675a..b85b9026733 100644 --- a/ktor-http/api/ktor-http.api +++ b/ktor-http/api/ktor-http.api @@ -6,8 +6,8 @@ public final class io/ktor/http/BadContentTypeFormatException : java/lang/Except public fun (Ljava/lang/String;)V } -public abstract class io/ktor/http/CacheControl { - public synthetic fun (Lio/ktor/http/CacheControl$Visibility;Lkotlin/jvm/internal/DefaultConstructorMarker;)V +public class io/ktor/http/CacheControl { + public fun (Lio/ktor/http/CacheControl$Visibility;)V public final fun getVisibility ()Lio/ktor/http/CacheControl$Visibility; } diff --git a/ktor-http/common/src/io/ktor/http/CacheControl.kt b/ktor-http/common/src/io/ktor/http/CacheControl.kt index 337855481d0..548aa156681 100644 --- a/ktor-http/common/src/io/ktor/http/CacheControl.kt +++ b/ktor-http/common/src/io/ktor/http/CacheControl.kt @@ -9,7 +9,7 @@ package io.ktor.http * * @param visibility specifies an optional visibility such as private or public */ -public sealed class CacheControl(public val visibility: Visibility?) { +public open class CacheControl(public val visibility: Visibility?) { /** * Controls caching by proxies diff --git a/ktor-server/ktor-server-plugins/ktor-server-caching-headers/jvmAndNix/src/io/ktor/server/plugins/cachingheaders/CacheControlMerge.kt b/ktor-server/ktor-server-plugins/ktor-server-caching-headers/jvmAndNix/src/io/ktor/server/plugins/cachingheaders/CacheControlMerge.kt index 80712f66331..05067f6a904 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-caching-headers/jvmAndNix/src/io/ktor/server/plugins/cachingheaders/CacheControlMerge.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-caching-headers/jvmAndNix/src/io/ktor/server/plugins/cachingheaders/CacheControlMerge.kt @@ -12,8 +12,8 @@ import io.ktor.http.* * Currently, visibility is pinned to the expiration directive, * then to the first no-cache, * then to the no-store directive. - * The RFC doesn't state, where a visibility modifier should be so we can place it at any place - * so there is nothing behind the rule beyond, therefore could be changed. + * The RFC doesn't state where a visibility modifier should be, so we can place it at any place + * so there is nothing behind the rule beyond, therefore, could be changed. * * Only one visibility specifier is kept. * @@ -26,7 +26,7 @@ import io.ktor.http.* * * Revalidation directives are collected as well. * Currently, revalidation directives are tied to max age by-design. - * This is not fair according to RFC so will be changed in the future. + * This is not fair, according to RFC, so it will be changed in the future. */ internal fun List.mergeCacheControlDirectives(): List { if (size < 2) return this @@ -50,6 +50,12 @@ internal fun List.mergeCacheControlDirectives(): List CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private)) } + options { _, _ -> CachingOptions(Immutable) } + } + }, + test = { response -> + assertEquals( + "no-cache, private, no-store, immutable", + response.headers[HttpHeaders.CacheControl] + ) + } + ) + @Test fun testSetInCall() = testApplication { install(CachingHeaders)