aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFicus Kirkpatrick <ficus@android.com>2015-02-15 17:35:54 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-02-15 17:35:55 +0000
commit2d134b2a0dc521c05c56930a23df1218dc4976a3 (patch)
treefc17378b2024f91eed2c9f70a77404c8c6528cfe /src
parent0cc8d620fe12412f90ba5121bdffb1a4e2f032c2 (diff)
parent8e33d93dc1aae9bb9dbde3b80af8a76ba28f0e19 (diff)
downloadvolley-2d134b2a0dc521c05c56930a23df1218dc4976a3.tar.gz
Merge "Adds Cache-Control "stale-while-revalidate""
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/android/volley/toolbox/HttpHeaderParser.java11
-rw-r--r--src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java18
2 files changed, 28 insertions, 1 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
index e342c9e..da04490 100644
--- a/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
+++ b/src/main/java/com/android/volley/toolbox/HttpHeaderParser.java
@@ -45,7 +45,9 @@ public class HttpHeaderParser {
long lastModified = 0;
long serverExpires = 0;
long softExpire = 0;
+ long finalExpire = 0;
long maxAge = 0;
+ long staleWhileRevalidate = 0;
boolean hasCacheControl = false;
String serverEtag = null;
@@ -69,6 +71,11 @@ public class HttpHeaderParser {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
+ } else if (token.startsWith("stale-while-revalidate=")) {
+ try {
+ staleWhileRevalidate = Long.parseLong(token.substring(23));
+ } catch (Exception e) {
+ }
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
maxAge = 0;
}
@@ -91,16 +98,18 @@ public class HttpHeaderParser {
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
+ finalExpire = softExpire + staleWhileRevalidate * 1000;
} else if (serverDate > 0 && serverExpires >= serverDate) {
// Default semantic for Expire header in HTTP specification is softExpire.
softExpire = now + (serverExpires - serverDate);
+ finalExpire = softExpire;
}
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
- entry.ttl = entry.softTtl;
+ entry.ttl = finalExpire;
entry.serverDate = serverDate;
entry.lastModified = lastModified;
entry.responseHeaders = headers;
diff --git a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
index 01ff2c2..60c2727 100644
--- a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
+++ b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
@@ -139,6 +139,24 @@ public class HttpHeaderParserTest {
assertEquals(entry.softTtl, entry.ttl);
}
+ @Test public void testParseCacheHeaders_staleWhileRevalidate() {
+ long now = System.currentTimeMillis();
+ headers.put("Date", rfc1123Date(now));
+ headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));
+
+ // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day
+ // - stale-while-revalidate (entry.ttl) indicates that the asset may
+ // continue to be served stale for up to additional 7 days
+ headers.put("Cache-Control", "max-age=86400, stale-while-revalidate=604800");
+
+ Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
+
+ assertNotNull(entry);
+ assertNull(entry.etag);
+ assertEqualsWithin(now + 24 * ONE_HOUR_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS);
+ assertEqualsWithin(now + 8 * 24 * ONE_HOUR_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
+ }
+
@Test public void parseCacheHeaders_cacheControlNoCache() {
long now = System.currentTimeMillis();
headers.put("Date", rfc1123Date(now));