From db3634f8598fc34bab57092008d83336fcbae07c Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 20 Dec 2010 16:32:58 +0000 Subject: [PATCH] SPR-7706 - 304 responses should not have non-0 Content-Length --- .../servlet/resource/ResourceHttpRequestHandler.java | 4 ++-- .../web/filter/ShallowEtagHeaderFilter.java | 11 +++++++---- .../web/filter/ShallowEtagHeaderFilterTests.java | 12 +++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 07fe31be8c..396ed89b31 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -121,11 +121,11 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H } // header phase - setHeaders(response, resource, mediaType); if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) { logger.debug("Resource not modified - returning 304"); return; } + setHeaders(response, resource, mediaType); // content phase if (METHOD_HEAD.equals(request.getMethod())) { diff --git a/org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java b/org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java index 07269002c4..422f947098 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java +++ b/org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -69,7 +69,6 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { if (logger.isTraceEnabled()) { logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304"); } - response.setContentLength(0); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { @@ -89,8 +88,8 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { } private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException { - response.setContentLength(body.length); if (body.length > 0) { + response.setContentLength(body.length); FileCopyUtils.copy(body, response.getOutputStream()); } } @@ -113,7 +112,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { /** * Generate the ETag header value from the given response body byte array. *

The default implementation generates an MD5 hash. - * @param bytes the response bdoy as byte array + * @param bytes the response body as byte array * @return the ETag header value * @see org.springframework.util.DigestUtils */ @@ -168,6 +167,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { this.statusCode = sc; } + @Override + public void setContentLength(int len) { + } + @Override public ServletOutputStream getOutputStream() { return this.outputStream; diff --git a/org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java b/org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java index b591329113..9edb23600d 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -23,7 +23,6 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; -import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; @@ -31,6 +30,8 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.util.FileCopyUtils; +import static org.junit.Assert.*; + public class ShallowEtagHeaderFilterTests { private ShallowEtagHeaderFilter filter; @@ -87,6 +88,7 @@ public class ShallowEtagHeaderFilterTests { assertEquals("Invalid request passed", request, filterRequest); byte[] responseBody = "Hello World".getBytes("UTF-8"); FileCopyUtils.copy(responseBody, filterResponse.getOutputStream()); + filterResponse.setContentLength(responseBody.length); } }; @@ -94,7 +96,7 @@ public class ShallowEtagHeaderFilterTests { assertEquals("Invalid status", 304, response.getStatus()); assertEquals("Invalid ETag header", "\"0b10a8db164e0754105b7a99be72e3fe5\"", response.getHeader("ETag")); - assertEquals("Invalid Content-Length header", 0, response.getContentLength()); + assertFalse("Response has Content-Length header", response.containsHeader("Content-Length")); assertArrayEquals("Invalid content", new byte[0], response.getContentAsByteArray()); } @@ -120,7 +122,7 @@ public class ShallowEtagHeaderFilterTests { assertEquals("Invalid status", 304, response.getStatus()); assertEquals("Invalid ETag header", "\"0b10a8db164e0754105b7a99be72e3fe5\"", response.getHeader("ETag")); - assertEquals("Invalid Content-Length header", 0, response.getContentLength()); + assertFalse("Response has Content-Length header", response.containsHeader("Content-Length")); assertArrayEquals("Invalid content", new byte[0], response.getContentAsByteArray()); }