Skip to content

Commit b732251

Browse files
committed
Ignore HEAD requests in ShallowEtagHeaderFilter
Prior to this commit, the `ShallowEtagHeaderFilter` could participate in the response and set its ETag/Content-Length headers, even for HEAD requests. Since the response body is empty, the filter implementation would set a `"Content-Length: 0"`. The RFC states that responses to HEAD requests should exhibit identical response headers to GET (with the possible exception of payload related headers such as Content-Length. With this commit, `ShallowEtagHeaderFilter` now ignores HEAD requests since the proper values may be set already for payload related headers by the handler. The filter has no way to generate a proper ETag value nor calculate the content length without the actual body. Issue: SPR-15261
1 parent 598d9a4 commit b732251

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.PrintWriter;
22+
2223
import javax.servlet.FilterChain;
2324
import javax.servlet.ServletException;
2425
import javax.servlet.ServletOutputStream;
@@ -168,8 +169,8 @@ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletRespo
168169
int responseStatusCode, InputStream inputStream) {
169170

170171
String method = request.getMethod();
171-
if (responseStatusCode >= 200 && responseStatusCode < 300 &&
172-
(HttpMethod.GET.matches(method) || HttpMethod.HEAD.matches(method))) {
172+
if (responseStatusCode >= 200 && responseStatusCode < 300
173+
&& HttpMethod.GET.matches(method)) {
173174

174175
String cacheControl = response.getHeader(HEADER_CACHE_CONTROL);
175176
if (cacheControl == null || !cacheControl.contains(DIRECTIVE_NO_STORE)) {

spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public void isEligibleForEtag() {
4646
assertTrue(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput()));
4747
assertFalse(filter.isEligibleForEtag(request, response, 300, StreamUtils.emptyInput()));
4848

49+
request = new MockHttpServletRequest("HEAD", "/hotels");
50+
assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput()));
51+
4952
request = new MockHttpServletRequest("POST", "/hotels");
5053
assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput()));
5154

0 commit comments

Comments
 (0)