Ticket #11193: expires-version1.patch

File expires-version1.patch, 3.1 KB (added by wiktorn, 10 years ago)

patch version 1

  • src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

     
    6666        } catch (SecurityException e) {
    6767            log.log(Level.WARNING,
    6868                    "Failed to access system property ''java.io.tmpdir'' for security reasons. Exception was: "
    69                     + e.toString());
     69                            + e.toString());
    7070            throw e; // rethrow
    7171        }
    7272        try {
     
    293293                loadTagsFromFile();
    294294
    295295                fileMtime = tileFile.lastModified();
    296                 if (now - fileMtime > maxAge)
    297                     return false;
    298 
     296                if (tile.getValue("expires") != null) {
     297                    try {
     298                        // if there was Expires/Cache-Control header provided, check against it
     299                        if (now > Long.parseLong(tile.getValue("expires")))
     300                            return false;
     301                    } catch (NumberFormatException e) {}
     302                } else {
     303                    // otherwise check against static cache policy
     304                    if (now - fileMtime > maxAge)
     305                        return false;
     306                }
    299307                if ("no-tile".equals(tile.getValue("tile-info"))) {
    300308                    tile.setError("No tile at this zoom level");
    301309                    if (tileFile.exists()) {
     
    405413            File file = getTileFile();
    406414            file.getParentFile().mkdirs();
    407415            try (
    408                 FileOutputStream f = new FileOutputStream(file)
    409             ) {
     416                    FileOutputStream f = new FileOutputStream(file)
     417                    ) {
    410418                f.write(rawData);
    411419            } catch (Exception e) {
    412420                log.log(Level.SEVERE, "Failed to save tile content: {0}", e.getLocalizedMessage());
  • src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

     
    108108        if (str != null) {
    109109            tile.putValue("tile-info", str);
    110110        }
     111
     112        // get Expiration or Cache-Control: max-age= value
     113        Long lng = urlConn.getExpiration();
     114        if (lng == null) {
     115            str = urlConn.getHeaderField("Cache-Control");
     116            if (str != null) {
     117                for (String token: str.split(",")) {
     118                    if (token.startsWith("max-age=")) {
     119                        lng = Long.parseLong(token.substring(8))+System.currentTimeMillis();
     120                    }
     121                }
     122            }
     123        }
     124        if (lng != null) {
     125            tile.putValue("expires", lng.toString());
     126        }
    111127    }
    112128
    113129    protected void prepareHttpUrlConnection(HttpURLConnection urlConn) {