Skip to content

Commit

Permalink
Fix request cache-control to match spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Nov 1, 2014
1 parent a4c0d59 commit ddac008
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
43 changes: 43 additions & 0 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,28 @@ private void testMethodInvalidates(String requestMethod) throws Exception {
assertEquals("A", get(url).body().string());
}

@Test public void clientSideNoStore() throws Exception {
server.enqueue(new MockResponse()
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.enqueue(new MockResponse()
.addHeader("Cache-Control: max-age=60")
.setBody("B"));

Request request1 = new Request.Builder()
.url(server.getUrl("/"))
.cacheControl(new CacheControl.Builder().noStore().build())
.build();
Response response1 = client.newCall(request1).execute();
assertEquals("A", response1.body().string());

Request request2 = new Request.Builder()
.url(server.getUrl("/"))
.build();
Response response2 = client.newCall(request2).execute();
assertEquals("B", response2.body().string());
}

@Test public void nonIdentityEncodingAndConditionalCache() throws Exception {
assertNonIdentityEncodingCached(
new MockResponse().addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS))
Expand Down Expand Up @@ -958,6 +980,27 @@ private void assertNonIdentityEncodingCached(MockResponse response) throws Excep
assertEquals("110 HttpURLConnection \"Response is stale\"", response.header("Warning"));
}

@Test public void requestMaxStaleDirectiveWithNoValue() throws IOException {
// Add a stale response to the cache.
server.enqueue(new MockResponse()
.setBody("A")
.addHeader("Cache-Control: max-age=120")
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
server.enqueue(new MockResponse()
.setBody("B"));

assertEquals("A", get(server.getUrl("/")).body().string());

// With max-stale, we'll return that stale response.
Request request = new Request.Builder()
.url(server.getUrl("/"))
.header("Cache-Control", "max-stale")
.build();
Response response = client.newCall(request).execute();
assertEquals("A", response.body().string());
assertEquals("110 HttpURLConnection \"Response is stale\"", response.header("Warning"));
}

@Test public void requestMaxStaleNotHonoredWithMustRevalidate() throws IOException {
server.enqueue(new MockResponse()
.setBody("A")
Expand Down
8 changes: 4 additions & 4 deletions okhttp/src/main/java/com/squareup/okhttp/CacheControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,17 @@ public static CacheControl parse(Headers headers) {
} else if ("no-store".equalsIgnoreCase(directive)) {
noStore = true;
} else if ("max-age".equalsIgnoreCase(directive)) {
maxAgeSeconds = HeaderParser.parseSeconds(parameter);
maxAgeSeconds = HeaderParser.parseSeconds(parameter, -1);
} else if ("s-maxage".equalsIgnoreCase(directive)) {
sMaxAgeSeconds = HeaderParser.parseSeconds(parameter);
sMaxAgeSeconds = HeaderParser.parseSeconds(parameter, -1);
} else if ("public".equalsIgnoreCase(directive)) {
isPublic = true;
} else if ("must-revalidate".equalsIgnoreCase(directive)) {
mustRevalidate = true;
} else if ("max-stale".equalsIgnoreCase(directive)) {
maxStaleSeconds = HeaderParser.parseSeconds(parameter);
maxStaleSeconds = HeaderParser.parseSeconds(parameter, Integer.MAX_VALUE);
} else if ("min-fresh".equalsIgnoreCase(directive)) {
minFreshSeconds = HeaderParser.parseSeconds(parameter);
minFreshSeconds = HeaderParser.parseSeconds(parameter, -1);
} else if ("only-if-cached".equalsIgnoreCase(directive)) {
onlyIfCached = true;
} else if ("no-transform".equalsIgnoreCase(directive)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public static boolean isCacheable(Response response, Request request) {
return false;
}

if (responseCaching.noStore()) {
// A 'no-store' directive on request or response prevents the response from being cached.
CacheControl requestCaching = request.cacheControl();
if (responseCaching.noStore() || requestCaching.noStore()) {
return false;
}

Expand Down Expand Up @@ -124,7 +126,7 @@ public Factory(long nowMillis, Request request, Response cacheResponse) {
} else if ("ETag".equalsIgnoreCase(fieldName)) {
etag = value;
} else if ("Age".equalsIgnoreCase(fieldName)) {
ageSeconds = HeaderParser.parseSeconds(value);
ageSeconds = HeaderParser.parseSeconds(value, -1);
} else if (OkHeaders.SENT_MILLIS.equalsIgnoreCase(fieldName)) {
sentRequestMillis = Long.parseLong(value);
} else if (OkHeaders.RECEIVED_MILLIS.equalsIgnoreCase(fieldName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public static int skipWhitespace(String input, int pos) {

/**
* Returns {@code value} as a positive integer, or 0 if it is negative, or
* -1 if it cannot be parsed.
* {@code defaultValue} if it cannot be parsed.
*/
public static int parseSeconds(String value) {
public static int parseSeconds(String value, int defaultValue) {
try {
long seconds = Long.parseLong(value);
if (seconds > Integer.MAX_VALUE) {
Expand All @@ -60,7 +60,7 @@ public static int parseSeconds(String value) {
return (int) seconds;
}
} catch (NumberFormatException e) {
return -1;
return defaultValue;
}
}

Expand Down

0 comments on commit ddac008

Please sign in to comment.