Skip to content

Commit 1f60738

Browse files
committed
checkNotModified needs to consider HEAD as well
Issue: SPR-11317 (cherry picked from commit 17cc63e)
1 parent 68b1eb1 commit 1f60738

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,8 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
4747

4848
private static final String METHOD_GET = "GET";
4949

50+
private static final String METHOD_HEAD = "HEAD";
51+
5052

5153
private HttpServletResponse response;
5254

@@ -155,7 +157,7 @@ public boolean checkNotModified(long lastModifiedTimestamp) {
155157
long ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE);
156158
this.notModified = (ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000));
157159
if (this.response != null) {
158-
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) {
160+
if (this.notModified && supportsNotModifiedStatus()) {
159161
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
160162
}
161163
else {
@@ -172,7 +174,7 @@ public boolean checkNotModified(String eTag) {
172174
String ifNoneMatch = getRequest().getHeader(HEADER_IF_NONE_MATCH);
173175
this.notModified = eTag.equals(ifNoneMatch);
174176
if (this.response != null) {
175-
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) {
177+
if (this.notModified && supportsNotModifiedStatus()) {
176178
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
177179
}
178180
else {
@@ -181,7 +183,11 @@ public boolean checkNotModified(String eTag) {
181183
}
182184
}
183185
return this.notModified;
186+
}
184187

188+
private boolean supportsNotModifiedStatus() {
189+
String method = getRequest().getMethod();
190+
return (METHOD_GET.equals(method) || METHOD_HEAD.equals(method));
185191
}
186192

187193
public boolean isNotModified() {

spring-web/src/test/java/org/springframework/web/context/request/ServletWebRequestTests.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737

3838
/**
3939
* @author Juergen Hoeller
40+
* @author Markus Malkusch
4041
* @since 26.07.2006
4142
*/
4243
public class ServletWebRequestTests {
@@ -116,7 +117,7 @@ public void decoratedNativeRequest() {
116117
}
117118

118119
@Test
119-
public void checkNotModifiedTimeStamp() {
120+
public void checkNotModifiedTimeStampForGET() {
120121
long currentTime = new Date().getTime();
121122
servletRequest.setMethod("GET");
122123
servletRequest.addHeader("If-Modified-Since", currentTime);
@@ -127,7 +128,7 @@ public void checkNotModifiedTimeStamp() {
127128
}
128129

129130
@Test
130-
public void checkModifiedTimeStamp() {
131+
public void checkModifiedTimeStampForGET() {
131132
long currentTime = new Date().getTime();
132133
long oneMinuteAgo = currentTime - (1000 * 60);
133134
servletRequest.setMethod("GET");
@@ -140,7 +141,7 @@ public void checkModifiedTimeStamp() {
140141
}
141142

142143
@Test
143-
public void checkNotModifiedETag() {
144+
public void checkNotModifiedETagForGET() {
144145
String eTag = "\"Foo\"";
145146
servletRequest.setMethod("GET");
146147
servletRequest.addHeader("If-None-Match", eTag );
@@ -151,7 +152,7 @@ public void checkNotModifiedETag() {
151152
}
152153

153154
@Test
154-
public void checkModifiedETag() {
155+
public void checkModifiedETagForGET() {
155156
String currentETag = "\"Foo\"";
156157
String oldEtag = "Bar";
157158
servletRequest.setMethod("GET");
@@ -163,4 +164,52 @@ public void checkModifiedETag() {
163164
assertEquals(currentETag, servletResponse.getHeader("ETag"));
164165
}
165166

167+
@Test
168+
public void checkNotModifiedTimeStampForHEAD() {
169+
long currentTime = new Date().getTime();
170+
servletRequest.setMethod("HEAD");
171+
servletRequest.addHeader("If-Modified-Since", currentTime);
172+
173+
request.checkNotModified(currentTime);
174+
175+
assertEquals(304, servletResponse.getStatus());
176+
}
177+
178+
@Test
179+
public void checkModifiedTimeStampForHEAD() {
180+
long currentTime = new Date().getTime();
181+
long oneMinuteAgo = currentTime - (1000 * 60);
182+
servletRequest.setMethod("HEAD");
183+
servletRequest.addHeader("If-Modified-Since", oneMinuteAgo);
184+
185+
request.checkNotModified(currentTime);
186+
187+
assertEquals(200, servletResponse.getStatus());
188+
assertEquals(""+currentTime, servletResponse.getHeader("Last-Modified"));
189+
}
190+
191+
@Test
192+
public void checkNotModifiedETagForHEAD() {
193+
String eTag = "\"Foo\"";
194+
servletRequest.setMethod("HEAD");
195+
servletRequest.addHeader("If-None-Match", eTag );
196+
197+
request.checkNotModified(eTag);
198+
199+
assertEquals(304, servletResponse.getStatus());
200+
}
201+
202+
@Test
203+
public void checkModifiedETagForHEAD() {
204+
String currentETag = "\"Foo\"";
205+
String oldEtag = "Bar";
206+
servletRequest.setMethod("HEAD");
207+
servletRequest.addHeader("If-None-Match", oldEtag);
208+
209+
request.checkNotModified(currentETag);
210+
211+
assertEquals(200, servletResponse.getStatus());
212+
assertEquals(currentETag, servletResponse.getHeader("ETag"));
213+
}
214+
166215
}

0 commit comments

Comments
 (0)