Skip to content

Commit

Permalink
dropwizard-bundles#37 Updating isCachedClientSide method to ignore If…
Browse files Browse the repository at this point in the history
…-Modified-Since when If-None-Match is also specified.
  • Loading branch information
mattmetlis committed Sep 4, 2019
1 parent e07f06b commit e4ec79b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/main/java/io/dropwizard/bundles/assets/AssetServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,26 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
}

private boolean isCachedClientSide(HttpServletRequest req, Asset cachedAsset) {
return cachedAsset.getETag().equals(req.getHeader(HttpHeaders.IF_NONE_MATCH))
|| (req.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE)
>= cachedAsset.getLastModifiedTime());
String ifNoneMatchHeader = req.getHeader(HttpHeaders.IF_NONE_MATCH);
long ifModifiedSinceHeader = req.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);

boolean ifNoneMatchHeaderExists = ifNoneMatchHeader != null && !ifNoneMatchHeader.isEmpty();
boolean ifModifiedSinceHeaderExists = ifModifiedSinceHeader >= 0;

// If there is an If-None-Match header, determine caching based on eTag.
// Per RFC 7232, if If-None-Match is present, ignore If-Modified-Since.
if (ifNoneMatchHeaderExists) {
return cachedAsset.getETag().equals(ifNoneMatchHeader);
} else if (ifModifiedSinceHeaderExists) {
// If there is no If-None-Match header, but there is an If-Modified-Since header,
// determine caching based on last modified time.
return ifModifiedSinceHeader >= cachedAsset.getLastModifiedTime();
} else {
// If there is neither an If-None-Match header nor am If-Modified-Since header,
// we cannot determine that the entity is cached.
return false;
}

}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/io/dropwizard/bundles/assets/AssetServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,58 @@ public void supportsIfModifiedSinceRequests() throws Exception {
.isEqualTo(304);
}

@Test
// Verify conformance with the HTTP/1.1 spec which requires ignoring the If-Modified-Since
// header when the If-None-Match header is present (RFC 7232).
public void supportsIfNoneMatchAndIfModifiedSinceRequests() throws Exception {
response = makeRequest();
final long lastModifiedTime = response.getDateField(HttpHeaders.LAST_MODIFIED);
final String correctEtag = response.get(HttpHeaders.ETAG);

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag);
response = makeRequest();
final int statusWithMatchingLastModifiedTimeAndMatchingEtag = response.getStatus();

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime - 100);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag);
response = makeRequest();
final int statusWithStaleLastModifiedTimeAndMatchingEtag = response.getStatus();

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime + 100);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag);
response = makeRequest();
final int statusWithRecentLastModifiedTimeAndMatchingEtag = response.getStatus();

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag + "FOO");
response = makeRequest();
final int statusWithMatchingLastModifiedTimeAndNonMatchingEtag = response.getStatus();

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime - 100);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag + "FOO");
response = makeRequest();
final int statusWithStaleLastModifiedTimeAndNonMatchingEtag = response.getStatus();

request.putDateField(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedTime + 100);
request.setHeader(HttpHeaders.IF_NONE_MATCH, correctEtag + "FOO");
response = makeRequest();
final int statusWithRecentLastModifiedTimeAndNonMatchingEtag = response.getStatus();

assertThat(statusWithMatchingLastModifiedTimeAndMatchingEtag)
.isEqualTo(304);
assertThat(statusWithStaleLastModifiedTimeAndMatchingEtag)
.isEqualTo(304);
assertThat(statusWithRecentLastModifiedTimeAndMatchingEtag)
.isEqualTo(304);
assertThat(statusWithMatchingLastModifiedTimeAndNonMatchingEtag)
.isEqualTo(200);
assertThat(statusWithStaleLastModifiedTimeAndNonMatchingEtag)
.isEqualTo(200);
assertThat(statusWithRecentLastModifiedTimeAndNonMatchingEtag)
.isEqualTo(200);
}

@Test
public void guessesMimeTypes() throws Exception {
response = makeRequest();
Expand Down

0 comments on commit e4ec79b

Please sign in to comment.