Browse Source

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
pull/1011/head
Brian Clozel 9 years ago
parent
commit
15fe8279e6
  1. 8
      spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java
  2. 15
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java

8
spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java

@ -421,6 +421,10 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { @@ -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 { @@ -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

15
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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<String> 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 { @@ -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<String> pragmaHeaders = response.getHeaders("Pragma");
assertThat(pragmaHeaders, Matchers.contains(""));
assertThat(response.getHeader("Pragma"), is(""));
assertThat(response.getHeader("Expires"), is(""));
}
@SuppressWarnings("deprecation")

Loading…
Cancel
Save