Changeset 13778 in josm for trunk/src


Ignore:
Timestamp:
2018-05-16T22:12:13+02:00 (6 years ago)
Author:
wiktorn
Message:

Prefer Cache-Control header over Expires header

According to RFC2616 Cache-Control takes precedence over Expires header.

See: #16249

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r13733 r13778  
    437437        CacheEntryAttributes ret = new CacheEntryAttributes();
    438438
    439         Long lng = urlConn.getExpiration();
    440         if (lng.equals(0L)) {
    441             try {
    442                 String str = urlConn.getHeaderField("Cache-Control");
    443                 if (str != null) {
    444                     for (String token: str.split(",")) {
    445                         if (token.startsWith("max-age=")) {
    446                             lng = TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring(8))) + System.currentTimeMillis();
    447                         }
     439        /*
     440         * according to https://www.ietf.org/rfc/rfc2616.txt Cache-Control takes precedence over max-age
     441         * max-age is for private caches, s-max-age is for shared caches. We take any value that is larger
     442         */
     443        Long expiration = 0L;
     444        String cacheControl = urlConn.getHeaderField("Cache-Control");
     445        if (cacheControl != null) {
     446            for (String token: cacheControl.split(",")) {
     447                try {
     448                    if (token.startsWith("max-age=")) {
     449                        expiration = Math.max(expiration,
     450                                TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring("max-age=".length())))
     451                                + System.currentTimeMillis()
     452                                );
    448453                    }
     454                    if (token.startsWith("s-max-age=")) {
     455                        expiration = Math.max(expiration,
     456                                TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring("s-max-age=".length())))
     457                                + System.currentTimeMillis()
     458                                );
     459                    }
     460                } catch (NumberFormatException e) {
     461                    // ignore malformed Cache-Control headers
     462                    Logging.trace(e);
    449463                }
    450             } catch (NumberFormatException e) {
    451                 // ignore malformed Cache-Control headers
    452                 Logging.trace(e);
    453             }
    454             if (lng.equals(0L)) {
    455                 lng = System.currentTimeMillis() + DEFAULT_EXPIRE_TIME;
    456             }
    457         }
    458 
    459         ret.setExpirationTime(Math.max(minimumExpiryTime + System.currentTimeMillis(), lng));
     464            }
     465        }
     466
     467        if (expiration.equals(0L)) {
     468            expiration = urlConn.getExpiration();
     469        }
     470
     471        // if nothing is found - set default
     472        if (expiration.equals(0L)) {
     473            expiration = System.currentTimeMillis() + DEFAULT_EXPIRE_TIME;
     474        }
     475
     476        ret.setExpirationTime(Math.max(minimumExpiryTime + System.currentTimeMillis(), expiration));
    460477        ret.setLastModification(now);
    461478        ret.setEtag(urlConn.getHeaderField("ETag"));
Note: See TracChangeset for help on using the changeset viewer.