Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ

private static final String METHOD_GET = "GET";

private static final String METHOD_HEAD = "HEAD";

private HttpServletResponse response;

Expand Down Expand Up @@ -165,6 +166,10 @@ public boolean isUserInRole(String role) {
public boolean isSecure() {
return getRequest().isSecure();
}

private boolean isSafeHttpMethod(String method) {
return METHOD_GET.equals(method) || METHOD_HEAD.equals(method);
}

@Override
public boolean checkNotModified(long lastModifiedTimestamp) {
Expand All @@ -173,7 +178,7 @@ public boolean checkNotModified(long lastModifiedTimestamp) {
long ifModifiedSince = getRequest().getDateHeader(HEADER_IF_MODIFIED_SINCE);
this.notModified = (ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000));
if (this.response != null) {
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) {
if (this.notModified && isSafeHttpMethod(getRequest().getMethod())) {
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
else {
Expand All @@ -191,7 +196,7 @@ public boolean checkNotModified(String eTag) {
String ifNoneMatch = getRequest().getHeader(HEADER_IF_NONE_MATCH);
this.notModified = eTag.equals(ifNoneMatch);
if (this.response != null) {
if (this.notModified && METHOD_GET.equals(getRequest().getMethod())) {
if (this.notModified && isSafeHttpMethod(getRequest().getMethod())) {
this.response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void decoratedNativeRequest() {
}

@Test
public void checkNotModifiedTimeStamp() {
public void checkNotModifiedTimeStampForGET() {
long currentTime = new Date().getTime();
servletRequest.setMethod("GET");
servletRequest.addHeader("If-Modified-Since", currentTime);
Expand All @@ -127,7 +127,7 @@ public void checkNotModifiedTimeStamp() {
}

@Test
public void checkModifiedTimeStamp() {
public void checkModifiedTimeStampForGET() {
long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60);
servletRequest.setMethod("GET");
Expand All @@ -140,7 +140,7 @@ public void checkModifiedTimeStamp() {
}

@Test
public void checkNotModifiedETag() {
public void checkNotModifiedETagForGET() {
String eTag = "\"Foo\"";
servletRequest.setMethod("GET");
servletRequest.addHeader("If-None-Match", eTag );
Expand All @@ -151,7 +151,7 @@ public void checkNotModifiedETag() {
}

@Test
public void checkModifiedETag() {
public void checkModifiedETagForGET() {
String currentETag = "\"Foo\"";
String oldEtag = "Bar";
servletRequest.setMethod("GET");
Expand All @@ -163,4 +163,52 @@ public void checkModifiedETag() {
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"));
}

}