From 45b27a85f4b749d821452f18490d24a023fc5b2a Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 20 Feb 2017 11:58:22 +0100 Subject: [PATCH] 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) --- .../springframework/web/filter/ShallowEtagHeaderFilter.java | 6 +++--- .../web/filter/ShallowEtagHeaderFilterTests.java | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java index c94791a576..50f061af73 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -166,8 +166,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { int responseStatusCode, InputStream inputStream) { String method = request.getMethod(); - if (responseStatusCode >= 200 && responseStatusCode < 300 && - (HttpMethod.GET.matches(method) || HttpMethod.HEAD.matches(method))) { + if (responseStatusCode >= 200 && responseStatusCode < 300 + && HttpMethod.GET.matches(method)) { String cacheControl = null; if (servlet3Present) { diff --git a/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java index 893501b23e..d5961ce112 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java @@ -46,6 +46,9 @@ public class ShallowEtagHeaderFilterTests { assertTrue(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); assertFalse(filter.isEligibleForEtag(request, response, 300, StreamUtils.emptyInput())); + request = new MockHttpServletRequest("HEAD", "/hotels"); + assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput())); + request = new MockHttpServletRequest("POST", "/hotels"); assertFalse(filter.isEligibleForEtag(request, response, 200, StreamUtils.emptyInput()));