Skip to content

Commit

Permalink
Ignore HEAD requests in ShallowEtagHeaderFilter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel committed Feb 20, 2017
1 parent 598d9a4 commit b732251
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
@@ -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.
Expand All @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
Expand Down Expand Up @@ -168,8 +169,8 @@ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletRespo
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 = response.getHeader(HEADER_CACHE_CONTROL);
if (cacheControl == null || !cacheControl.contains(DIRECTIVE_NO_STORE)) {
Expand Down
Expand Up @@ -46,6 +46,9 @@ public void isEligibleForEtag() {
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()));

Expand Down

0 comments on commit b732251

Please sign in to comment.