Skip to content

Commit

Permalink
Merge pull request square#1347 from nfuller/PrivateCache
Browse files Browse the repository at this point in the history
Private cache improvements
  • Loading branch information
swankjesse committed Jan 26, 2015
2 parents 621d270 + 4048663 commit 3ee539e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class CacheControlTest {
assertFalse(cacheControl.noStore());
assertEquals(-1, cacheControl.maxAgeSeconds());
assertEquals(-1, cacheControl.sMaxAgeSeconds());
assertFalse(cacheControl.isPrivate());
assertFalse(cacheControl.isPublic());
assertFalse(cacheControl.mustRevalidate());
assertEquals(-1, cacheControl.maxStaleSeconds());
Expand Down Expand Up @@ -62,6 +63,7 @@ public final class CacheControlTest {

// These members are accessible to response headers only.
assertEquals(-1, cacheControl.sMaxAgeSeconds());
assertFalse(cacheControl.isPrivate());
assertFalse(cacheControl.isPublic());
assertFalse(cacheControl.mustRevalidate());
}
Expand All @@ -83,7 +85,7 @@ public final class CacheControlTest {
}

@Test public void parse() throws Exception {
String header = "no-cache, no-store, max-age=1, s-maxage=2, public, must-revalidate, "
String header = "no-cache, no-store, max-age=1, s-maxage=2, private, public, must-revalidate, "
+ "max-stale=3, min-fresh=4, only-if-cached, no-transform";
CacheControl cacheControl = CacheControl.parse(new Headers.Builder()
.set("Cache-Control", header)
Expand All @@ -92,6 +94,7 @@ public final class CacheControlTest {
assertTrue(cacheControl.noStore());
assertEquals(1, cacheControl.maxAgeSeconds());
assertEquals(2, cacheControl.sMaxAgeSeconds());
assertTrue(cacheControl.isPrivate());
assertTrue(cacheControl.isPublic());
assertTrue(cacheControl.mustRevalidate());
assertEquals(3, cacheControl.maxStaleSeconds());
Expand All @@ -101,6 +104,26 @@ public final class CacheControlTest {
assertEquals(header, cacheControl.toString());
}

@Test public void parseIgnoreCacheControlExtensions() throws Exception {
// Example from http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.6
String header = "private, community=\"UCI\"";
CacheControl cacheControl = CacheControl.parse(new Headers.Builder()
.set("Cache-Control", header)
.build());
assertFalse(cacheControl.noCache());
assertFalse(cacheControl.noStore());
assertEquals(-1, cacheControl.maxAgeSeconds());
assertEquals(-1, cacheControl.sMaxAgeSeconds());
assertTrue(cacheControl.isPrivate());
assertFalse(cacheControl.isPublic());
assertFalse(cacheControl.mustRevalidate());
assertEquals(-1, cacheControl.maxStaleSeconds());
assertEquals(-1, cacheControl.minFreshSeconds());
assertFalse(cacheControl.onlyIfCached());
assertFalse(cacheControl.noTransform());
assertEquals(header, cacheControl.toString());
}

@Test public void parseCacheControlAndPragmaAreCombined() {
Headers headers =
Headers.of("Cache-Control", "max-age=12", "Pragma", "must-revalidate", "Pragma", "public");
Expand Down
17 changes: 14 additions & 3 deletions okhttp/src/main/java/com/squareup/okhttp/CacheControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class CacheControl {
private final boolean noStore;
private final int maxAgeSeconds;
private final int sMaxAgeSeconds;
private final boolean isPrivate;
private final boolean isPublic;
private final boolean mustRevalidate;
private final int maxStaleSeconds;
Expand All @@ -44,12 +45,13 @@ public final class CacheControl {
String headerValue; // Lazily computed, if absent.

private CacheControl(boolean noCache, boolean noStore, int maxAgeSeconds, int sMaxAgeSeconds,
boolean isPublic, boolean mustRevalidate, int maxStaleSeconds, int minFreshSeconds,
boolean onlyIfCached, boolean noTransform, String headerValue) {
boolean isPrivate, boolean isPublic, boolean mustRevalidate, int maxStaleSeconds,
int minFreshSeconds, boolean onlyIfCached, boolean noTransform, String headerValue) {
this.noCache = noCache;
this.noStore = noStore;
this.maxAgeSeconds = maxAgeSeconds;
this.sMaxAgeSeconds = sMaxAgeSeconds;
this.isPrivate = isPrivate;
this.isPublic = isPublic;
this.mustRevalidate = mustRevalidate;
this.maxStaleSeconds = maxStaleSeconds;
Expand All @@ -64,6 +66,7 @@ private CacheControl(Builder builder) {
this.noStore = builder.noStore;
this.maxAgeSeconds = builder.maxAgeSeconds;
this.sMaxAgeSeconds = -1;
this.isPrivate = false;
this.isPublic = false;
this.mustRevalidate = false;
this.maxStaleSeconds = builder.maxStaleSeconds;
Expand Down Expand Up @@ -106,6 +109,10 @@ public int sMaxAgeSeconds() {
return sMaxAgeSeconds;
}

public boolean isPrivate() {
return isPrivate;
}

public boolean isPublic() {
return isPublic;
}
Expand Down Expand Up @@ -146,6 +153,7 @@ public static CacheControl parse(Headers headers) {
boolean noStore = false;
int maxAgeSeconds = -1;
int sMaxAgeSeconds = -1;
boolean isPrivate = false;
boolean isPublic = false;
boolean mustRevalidate = false;
int maxStaleSeconds = -1;
Expand Down Expand Up @@ -212,6 +220,8 @@ public static CacheControl parse(Headers headers) {
maxAgeSeconds = HeaderParser.parseSeconds(parameter, -1);
} else if ("s-maxage".equalsIgnoreCase(directive)) {
sMaxAgeSeconds = HeaderParser.parseSeconds(parameter, -1);
} else if ("private".equalsIgnoreCase(directive)) {
isPrivate = true;
} else if ("public".equalsIgnoreCase(directive)) {
isPublic = true;
} else if ("must-revalidate".equalsIgnoreCase(directive)) {
Expand All @@ -231,7 +241,7 @@ public static CacheControl parse(Headers headers) {
if (!canUseHeaderValue) {
headerValue = null;
}
return new CacheControl(noCache, noStore, maxAgeSeconds, sMaxAgeSeconds, isPublic,
return new CacheControl(noCache, noStore, maxAgeSeconds, sMaxAgeSeconds, isPrivate, isPublic,
mustRevalidate, maxStaleSeconds, minFreshSeconds, onlyIfCached, noTransform, headerValue);
}

Expand All @@ -246,6 +256,7 @@ private String headerValue() {
if (noStore) result.append("no-store, ");
if (maxAgeSeconds != -1) result.append("max-age=").append(maxAgeSeconds).append(", ");
if (sMaxAgeSeconds != -1) result.append("s-maxage=").append(sMaxAgeSeconds).append(", ");
if (isPrivate) result.append("private, ");
if (isPublic) result.append("public, ");
if (mustRevalidate) result.append("must-revalidate, ");
if (maxStaleSeconds != -1) result.append("max-stale=").append(maxStaleSeconds).append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ public static boolean isCacheable(Response response, Request request) {
case HTTP_MOVED_TEMP:
case HTTP_TEMP_REDIRECT:
// These codes can only be cached with the right response headers.
// http://tools.ietf.org/html/rfc7234#section-3
// s-maxage is not checked because OkHttp is a private cache that should ignore s-maxage.
if (response.header("Expires") != null
|| response.cacheControl().maxAgeSeconds() != -1
|| response.cacheControl().sMaxAgeSeconds() != -1
|| response.cacheControl().isPublic()) {
|| response.cacheControl().isPublic()
|| response.cacheControl().isPrivate()) {
break;
}
// Fall-through.
Expand Down

0 comments on commit 3ee539e

Please sign in to comment.