checkNotModified needs to consider HEAD as well

Issue: SPR-11317
(cherry picked from commit 17cc63e)
This commit is contained in:
Juergen Hoeller 2014-01-15 23:06:47 +01:00
parent 68b1eb1aba
commit 1f60738431
2 changed files with 63 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -47,6 +47,8 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
private static final String METHOD_GET = "GET"; private static final String METHOD_GET = "GET";
private static final String METHOD_HEAD = "HEAD";
private HttpServletResponse response; private HttpServletResponse response;
@ -155,7 +157,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
long ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE); long ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE);
this.notModified = (ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000)); this.notModified = (ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000));
if (this.response != null) { if (this.response != null) {
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) { if (this.notModified && supportsNotModifiedStatus()) {
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} }
else { else {
@ -172,7 +174,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
String ifNoneMatch = getRequest().getHeader(HEADER_IF_NONE_MATCH); String ifNoneMatch = getRequest().getHeader(HEADER_IF_NONE_MATCH);
this.notModified = eTag.equals(ifNoneMatch); this.notModified = eTag.equals(ifNoneMatch);
if (this.response != null) { if (this.response != null) {
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) { if (this.notModified && supportsNotModifiedStatus()) {
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} }
else { else {
@ -181,7 +183,11 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
} }
} }
return this.notModified; return this.notModified;
}
private boolean supportsNotModifiedStatus() {
String method = getRequest().getMethod();
return (METHOD_GET.equals(method) || METHOD_HEAD.equals(method));
} }
public boolean isNotModified() { public boolean isNotModified() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,6 +37,7 @@ import static org.junit.Assert.*;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Markus Malkusch
* @since 26.07.2006 * @since 26.07.2006
*/ */
public class ServletWebRequestTests { public class ServletWebRequestTests {
@ -116,7 +117,7 @@ public class ServletWebRequestTests {
} }
@Test @Test
public void checkNotModifiedTimeStamp() { public void checkNotModifiedTimeStampForGET() {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
servletRequest.setMethod("GET"); servletRequest.setMethod("GET");
servletRequest.addHeader("If-Modified-Since", currentTime); servletRequest.addHeader("If-Modified-Since", currentTime);
@ -127,7 +128,7 @@ public class ServletWebRequestTests {
} }
@Test @Test
public void checkModifiedTimeStamp() { public void checkModifiedTimeStampForGET() {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60); long oneMinuteAgo = currentTime - (1000 * 60);
servletRequest.setMethod("GET"); servletRequest.setMethod("GET");
@ -140,7 +141,7 @@ public class ServletWebRequestTests {
} }
@Test @Test
public void checkNotModifiedETag() { public void checkNotModifiedETagForGET() {
String eTag = "\"Foo\""; String eTag = "\"Foo\"";
servletRequest.setMethod("GET"); servletRequest.setMethod("GET");
servletRequest.addHeader("If-None-Match", eTag ); servletRequest.addHeader("If-None-Match", eTag );
@ -151,7 +152,7 @@ public class ServletWebRequestTests {
} }
@Test @Test
public void checkModifiedETag() { public void checkModifiedETagForGET() {
String currentETag = "\"Foo\""; String currentETag = "\"Foo\"";
String oldEtag = "Bar"; String oldEtag = "Bar";
servletRequest.setMethod("GET"); servletRequest.setMethod("GET");
@ -163,4 +164,52 @@ public class ServletWebRequestTests {
assertEquals(currentETag, servletResponse.getHeader("ETag")); assertEquals(currentETag, servletResponse.getHeader("ETag"));
} }
@Test
public void checkNotModifiedTimeStampForHEAD() {
long currentTime = new Date().getTime();
servletRequest.setMethod("HEAD");
servletRequest.addHeader("If-Modified-Since", currentTime);
request.checkNotModified(currentTime);
assertEquals(304, servletResponse.getStatus());
}
@Test
public void checkModifiedTimeStampForHEAD() {
long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60);
servletRequest.setMethod("HEAD");
servletRequest.addHeader("If-Modified-Since", oneMinuteAgo);
request.checkNotModified(currentTime);
assertEquals(200, servletResponse.getStatus());
assertEquals(""+currentTime, servletResponse.getHeader("Last-Modified"));
}
@Test
public void checkNotModifiedETagForHEAD() {
String eTag = "\"Foo\"";
servletRequest.setMethod("HEAD");
servletRequest.addHeader("If-None-Match", eTag );
request.checkNotModified(eTag);
assertEquals(304, servletResponse.getStatus());
}
@Test
public void checkModifiedETagForHEAD() {
String currentETag = "\"Foo\"";
String oldEtag = "Bar";
servletRequest.setMethod("HEAD");
servletRequest.addHeader("If-None-Match", oldEtag);
request.checkNotModified(currentETag);
assertEquals(200, servletResponse.getStatus());
assertEquals(currentETag, servletResponse.getHeader("ETag"));
}
} }