Skip to content

Commit

Permalink
Reset Expires HTTP header when caching configured
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed Mar 23, 2016
1 parent 9b20231 commit 15fe827
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Expand Up @@ -421,6 +421,10 @@ protected final void applyCacheControl(HttpServletResponse response, CacheContro
// 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, "");
}
}
}

Expand Down Expand Up @@ -545,6 +549,10 @@ protected final void cacheForSeconds(HttpServletResponse response, int seconds,
// 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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -104,32 +105,34 @@ public void emptyCacheConfiguration() throws Exception {
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 {
WebContentInterceptor interceptor = new WebContentInterceptor();
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")
Expand Down

0 comments on commit 15fe827

Please sign in to comment.