Skip to content

Instantly share code, notes, and snippets.

@jmfield2
Created December 23, 2016 04:39
Show Gist options
  • Save jmfield2/a51b4970b1a8e97db4427533019c117a to your computer and use it in GitHub Desktop.
Save jmfield2/a51b4970b1a8e97db4427533019c117a to your computer and use it in GitHub Desktop.
Grizzly HTTP IfModifiedSince
diff --git a/modules/http-server/src/main/java/org/glassfish/grizzly/http/server/filecache/FileCache.java b/modules/http-server/src/main/java/org/glassfish/grizzly/http/server/filecache/FileCache.java
index 3acd028..9f71b16 100644
--- a/modules/http-server/src/main/java/org/glassfish/grizzly/http/server/filecache/FileCache.java
+++ b/modules/http-server/src/main/java/org/glassfish/grizzly/http/server/filecache/FileCache.java
@@ -819,7 +819,10 @@ public class FileCache implements MonitoringAware<FileCacheProbe> {
// If an If-None-Match header has been specified,
// If-Modified-Since is ignored.
if ((request.getHeader(Header.IfNoneMatch) == null)
- && (headerValue - lastModified <= 1000)) {
+ && (headerValue - lastModified >= 1000)) {
+ /* If the date from client is < date of file, it has been modified
+ If the date from client is > date of file, it hasn't been modified
+ */
// The entity has not been modified since the date
// specified by the client. This is not an error case.
return HttpStatus.NOT_MODIFIED_304;
diff --git a/modules/http-server/src/test/java/org/glassfish/grizzly/http/server/FileCacheTest.java b/modules/http-server/src/test/java/org/glassfish/grizzly/http/server/FileCacheTest.java
index 41e7e06..5cbc0b9 100644
--- a/modules/http-server/src/test/java/org/glassfish/grizzly/http/server/FileCacheTest.java
+++ b/modules/http-server/src/test/java/org/glassfish/grizzly/http/server/FileCacheTest.java
@@ -773,6 +773,8 @@ public class FileCacheTest {
final HttpContent response1 = responseFuture.get(10, TimeUnit.SECONDS);
assertEquals("Cached data mismatch. Response=" + response1.getHttpHeader(),
pattern, response1.getContent().toStringContent());
+
+ /* IfModifiedSince > File Modified == 304 */
final Date date = new Date(file.lastModified());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
cal.setTime(date);
@@ -794,9 +796,10 @@ public class FileCacheTest {
assertTrue("content-length is set", response2.getHttpHeader().getContentLength() == -1);
assertFalse("transfer-encoding is set", response2.getHttpHeader().isChunked());
+ /* IfModifiedSince < File Modified == 200 */
cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
cal.setTime(date);
- cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 2);
+ cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 2);
ifModifiedSinceValue = convertToDate(cal.getTime().getTime());
final HttpRequestPacket request3 = HttpRequestPacket.builder()
.method("GET")
@@ -811,6 +814,25 @@ public class FileCacheTest {
assertEquals("200 is expected", 200, ((HttpResponsePacket) response3.getHttpHeader()).getStatus());
assertTrue("non-empty body is expected", response3.getContent().hasRemaining());
+
+ /* IfModifiedSince > File Modified == 304 */
+ cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
+ cal.setTime(date);
+ cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 2);
+ ifModifiedSinceValue = convertToDate(cal.getTime().getTime());
+ final HttpRequestPacket request4 = HttpRequestPacket.builder()
+ .method("GET")
+ .uri(requestPath)
+ .protocol("HTTP/1.1")
+ .header("Host", "localhost")
+ .header("If-Modified-Since", ifModifiedSinceValue)
+ .build();
+ responseFuture.reset();
+ c.write(request4);
+ final HttpContent response4 = responseFuture.get(10, TimeUnit.SECONDS);
+
+ assertEquals("304 is expected", 304, ((HttpResponsePacket) response4.getHttpHeader()).getStatus());
+ assertTrue("empty body is expected", !response4.getContent().hasRemaining());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment