Example #1
0
  public static Cache.Entry parseIgnoreCacheHeaders(
      NetworkResponse response, String url, long softttl, long hardttl) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;
    long serverDate = 0;
    String serverEtag = null;
    String headerValue;
    headerValue = headers.get("Date");
    if (headerValue != null) {
      serverDate = parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    // entry expires
    // completely
    final long softExpire = now + softttl;
    final long ttl = now + hardttl;

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = ttl;
    entry.serverDate = serverDate;
    entry.responseHeaders = headers;

    return entry;
  }
Example #2
0
  /**
   * Extracts a {@link Cache.Entry} from a {@link com.android.volley.NetworkResponse}. Cache-control
   * headers are ignored. SoftTtl == 3 mins, ttl == 24 hours.
   *
   * @param response The network response to parse headers from
   * @return a cache entry for the given response, or null if the response is not cacheable.
   */
  public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;
    long serverDate = 0;
    String serverEtag = null;
    String headerValue;

    headerValue = headers.get("Date");
    if (headerValue != null) {
      serverDate = com.android.volley.toolbox.HttpHeaderParser.parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    final long cacheHitButRefreshed =
        3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
    final long cacheExpired =
        24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
    final long softExpire = now + cacheHitButRefreshed;
    final long ttl = now + cacheExpired;

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = ttl;
    entry.serverDate = serverDate;
    entry.responseHeaders = headers;

    return entry;
  }
Example #3
0
  /**
   * Extracts a {@link Cache.Entry} from a {@link NetworkResponse}.
   *
   * @param response The network response to parse headers from
   * @return a cache entry for the given response, or null if the response is not cacheable.
   */
  public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;

    long serverDate = 0;
    long serverExpires = 0;
    long softExpire = 0;
    long maxAge = 0;
    long lastModify = 0;
    boolean hasCacheControl = false;

    String serverEtag = null;
    String headerValue;

    headerValue = headers.get("Date");
    if (headerValue != null) {
      serverDate = parseDateAsEpoch(headerValue);
    }

    headerValue = headers.get("Cache-Control");
    if (headerValue != null) {
      hasCacheControl = true;
      String[] tokens = headerValue.split(",");
      for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i].trim();
        if (token.equals("no-cache") || token.equals("no-store")) {
          return null;
        } else if (token.startsWith("max-age=")) {
          try {
            maxAge = Long.parseLong(token.substring(8));
          } catch (Exception e) {
          }
        } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
          maxAge = 0;
        }
      }
    }

    headerValue = headers.get("Expires");
    if (headerValue != null) {
      serverExpires = parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    // Cache-Control takes precedence over an Expires header, even if both exist and Expires
    // is more restrictive.
    if (hasCacheControl) {
      softExpire = now + maxAge * 1000;
    } else if (serverDate > 0 && serverExpires >= serverDate) {
      // Default semantic for Expire header in HTTP specification is softExpire.
      softExpire = now + (serverExpires - serverDate);
    }

    headerValue = headers.get("Last-Modified");
    if (headerValue != null) {
      lastModify = parseDateAsEpoch(headerValue);
    }

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = entry.softTtl;
    entry.serverDate = serverDate;
    entry.lastModify = lastModify;
    entry.responseHeaders = headers;

    return entry;
  }