Browse Source

SPR-7706 - 304 responses should not have non-0 Content-Length

pull/7/head
Arjen Poutsma 14 years ago
parent
commit
db3634f859
  1. 4
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 11
      org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java
  3. 12
      org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java

4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

@ -5,7 +5,7 @@ @@ -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 @@ -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())) {

11
org.springframework.web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

@ -5,7 +5,7 @@ @@ -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 { @@ -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 { @@ -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 { @@ -113,7 +112,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
/**
* Generate the ETag header value from the given response body byte array.
* <p>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 { @@ -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;

12
org.springframework.web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java

@ -1,11 +1,11 @@ @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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());
}

Loading…
Cancel
Save