From 15fe8279e6342dcd5cd52d59052c77d2159bff6b Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 23 Mar 2016 16:18:15 +0100 Subject: [PATCH] Reset Expires HTTP header when caching configured Just like SPR-13252 addressed this issue for the "Pragma" header, this issue resets the HTTP 1.0 "Expires" header. When such a header has been set (by a filter, for example) and HTTP caching has been configured at the WebContentGenerator, this header value is reset to "". In this case, "Cache-Control" and "Expires" might have inconsistent values and we consider that the HTTP caching configuration should take precedence. Depending on the servlet container chosen to deploy the application, this might result in empty "" header values or no header set at all. Issue: SPR-14053 --- .../web/servlet/support/WebContentGenerator.java | 8 ++++++++ .../servlet/mvc/WebContentInterceptorTests.java | 15 +++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java index 68b6573a4d..c774d62ec4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java @@ -421,6 +421,10 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { // Reset HTTP 1.0 Pragma header if present response.setHeader(HEADER_PRAGMA, ""); } + if (response.containsHeader(HEADER_EXPIRES)) { + // Reset HTTP 1.0 Expires header if present + response.setHeader(HEADER_EXPIRES, ""); + } } } @@ -545,6 +549,10 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { // HTTP 1.0 header response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L); } + else if (response.containsHeader(HEADER_EXPIRES)) { + // Reset HTTP 1.0 Expires header if present + response.setHeader(HEADER_EXPIRES, ""); + } if (this.useCacheControlHeader) { // HTTP 1.1 header diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java index a269583f9d..06a844e13a 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java @@ -16,6 +16,7 @@ package org.springframework.web.servlet.mvc; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; import java.util.Properties; @@ -104,20 +105,21 @@ public class WebContentInterceptorTests { assertThat(cacheControlHeaders, Matchers.emptyIterable()); } - // SPR-13252 + // SPR-13252, SPR-14053 @Test public void cachingConfigAndPragmaHeader() throws Exception { WebContentInterceptor interceptor = new WebContentInterceptor(); interceptor.setCacheSeconds(10); response.setHeader("Pragma", "no-cache"); + response.setHeader("Expires", "0"); interceptor.preHandle(request, response, null); - Iterable pragmaHeaders = response.getHeaders("Pragma"); - assertThat(pragmaHeaders, Matchers.contains("")); + assertThat(response.getHeader("Pragma"), is("")); + assertThat(response.getHeader("Expires"), is("")); } - // SPR-13252 + // SPR-13252, SPR-14053 @SuppressWarnings("deprecation") @Test public void http10CachingConfigAndPragmaHeader() throws Exception { @@ -125,11 +127,12 @@ public class WebContentInterceptorTests { interceptor.setCacheSeconds(10); interceptor.setAlwaysMustRevalidate(true); response.setHeader("Pragma", "no-cache"); + response.setHeader("Expires", "0"); interceptor.preHandle(request, response, null); - Iterable pragmaHeaders = response.getHeaders("Pragma"); - assertThat(pragmaHeaders, Matchers.contains("")); + assertThat(response.getHeader("Pragma"), is("")); + assertThat(response.getHeader("Expires"), is("")); } @SuppressWarnings("deprecation")