From 5cefa4500f6e1bfb5a0130646fa08c88ea7944c6 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Thu, 5 Sep 2024 10:24:21 +0200 Subject: [PATCH] Remove EXPIRES HTTP header from cache This header conflicts and can cause confusion when 'Cache-Control + max-age' are set. And currently SCG always sets 'max-age' in 'Cache-Control' via 'SetMaxAgeHeaderAfterCacheExchangeMutator'. Closes #3488 --- .../filter/factory/cache/ResponseCacheManager.java | 2 +- ...LocalResponseCacheGatewayFilterFactoryTests.java | 13 +++++++++++-- .../cache/LocalResponseCacheGlobalFilterTests.java | 13 +++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManager.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManager.java index e6f115f192..1a5471f589 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManager.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/cache/ResponseCacheManager.java @@ -81,7 +81,7 @@ public ResponseCacheManager(CacheKeyGenerator cacheKeyGenerator, Cache cache, Du this.ignoreNoCacheUpdate = isSkipNoCacheUpdateActive(requestOptions); this.afterCacheExchangeMutators = List.of(new SetResponseHeadersAfterCacheExchangeMutator(), new SetStatusCodeAfterCacheExchangeMutator(), - new RemoveHeadersAfterCacheExchangeMutator(HttpHeaders.PRAGMA), + new RemoveHeadersAfterCacheExchangeMutator(HttpHeaders.PRAGMA, HttpHeaders.EXPIRES), new SetMaxAgeHeaderAfterCacheExchangeMutator(configuredTimeToLive, Clock.systemDefaultZone(), ignoreNoCacheUpdate), new SetCacheDirectivesByMaxAgeAfterCacheExchangeMutator()); diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactoryTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactoryTests.java index b14be47698..1325e75ecc 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGatewayFilterFactoryTests.java @@ -353,6 +353,15 @@ void shouldNotCacheWhenAuthorizationHeaderIsDifferent() { @Test void shouldNotReturnPragmaHeaderInNonCachedAndCachedResponses() { + shouldNotReturnHeader(HttpHeaders.PRAGMA); + } + + @Test + void shouldNotReturnExpiresHeaderInNonCachedAndCachedResponses() { + shouldNotReturnHeader(HttpHeaders.EXPIRES); + } + + private void shouldNotReturnHeader(String header) { String uri = "/" + UUID.randomUUID() + "/cache/headers"; testClient.get() @@ -360,14 +369,14 @@ void shouldNotReturnPragmaHeaderInNonCachedAndCachedResponses() { .header("Host", "www.localresponsecache.org") .exchange() .expectHeader() - .doesNotExist(HttpHeaders.PRAGMA); + .doesNotExist(header); testClient.get() .uri(uri) .header("Host", "www.localresponsecache.org") .exchange() .expectHeader() - .doesNotExist(HttpHeaders.PRAGMA); + .doesNotExist(header); } void assertNonVaryHeaderInContent(String uri, String varyHeader, String varyHeaderValue, String nonVaryHeader, diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGlobalFilterTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGlobalFilterTests.java index 28d63d0b68..f5f4c6686f 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGlobalFilterTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/cache/LocalResponseCacheGlobalFilterTests.java @@ -126,6 +126,15 @@ void shouldGlobalCacheResponseWhenRouteDoesNotHaveFilter() { @Test void shouldNotReturnPragmaHeaderInNonCachedAndCachedResponses() { + shouldNotReturnHeader(HttpHeaders.PRAGMA); + } + + @Test + void shouldNotReturnExpiresHeaderInNonCachedAndCachedResponses() { + shouldNotReturnHeader(HttpHeaders.EXPIRES); + } + + private void shouldNotReturnHeader(String header) { String uri = "/" + UUID.randomUUID() + "/global-cache/headers"; testClient.get() @@ -133,14 +142,14 @@ void shouldNotReturnPragmaHeaderInNonCachedAndCachedResponses() { .header("Host", "www.localresponsecache.org") .exchange() .expectHeader() - .doesNotExist(HttpHeaders.PRAGMA); + .doesNotExist(header); testClient.get() .uri(uri) .header("Host", "www.localresponsecache.org") .exchange() .expectHeader() - .doesNotExist(HttpHeaders.PRAGMA); + .doesNotExist(header); } @EnableAutoConfiguration