Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating isCachedClientSide method to ignore If-Modified-Since when If-None-Match is also specified #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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