Skip to content

Commit 45b27a8

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 (cherry picked from commit b732251)
1 parent 0f71f58 commit 45b27a8

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

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

Lines changed: 3 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.
@@ -166,8 +166,8 @@ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletRespo
166166
int responseStatusCode, InputStream inputStream) {
167167

168168
String method = request.getMethod();
169-
if (responseStatusCode >= 200 && responseStatusCode < 300 &&
170-
(HttpMethod.GET.matches(method) || HttpMethod.HEAD.matches(method))) {
169+
if (responseStatusCode >= 200 && responseStatusCode < 300
170+
&& HttpMethod.GET.matches(method)) {
171171

172172
String cacheControl = null;
173173
if (servlet3Present) {

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)