Changeset 19693 in josm


Ignore:
Timestamp:
2026-11-20T11:19:57+01:00 (less than one hour ago)
Author:
stoecker
Message:

improve tile caching, patch by wangi, see #25044

Location:
trunk
Files:
2 edited

Legend:

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

    r19533 r19693  
    352352            final HttpClient request = getRequest("GET");
    353353
    354             if (isObjectLoadable() &&
     354            // If-Modified-Since only without an ETag: servers which evaluate both (e.g. Apache serving tiles without a
     355            // Last-Modified) answer 200 to our download time even when the ETag matches, see RFC 9110 section 13.1.3
     356            if (isObjectLoadable() && attributes.getEtag() != null) {
     357                request.setHeader("If-None-Match", attributes.getEtag());
     358            } else if (isObjectLoadable() &&
    355359                    (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
    356360                request.setIfModifiedSince(attributes.getLastModification());
    357             }
    358             if (isObjectLoadable() && attributes.getEtag() != null) {
    359                 request.setHeader("If-None-Match", attributes.getEtag());
    360361            }
    361362
  • trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java

    r19515 r19693  
    44import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
    55import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
     6import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
    67import static com.github.tomakehurst.wiremock.client.WireMock.get;
    78import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
    89import static com.github.tomakehurst.wiremock.client.WireMock.head;
    910import static com.github.tomakehurst.wiremock.client.WireMock.headRequestedFor;
     11import static com.github.tomakehurst.wiremock.client.WireMock.matching;
    1012import static com.github.tomakehurst.wiremock.client.WireMock.status;
    1113import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
    … …  
    541543    }
    542544
     545    /**
     546     * A cache entry with an ETag is checked with If-None-Match only.
     547     * <p>
     548     * Non-regression test: If-Modified-Since was sent too, with the time of the download. Apache serving tiles without
     549     * a Last-Modified (mod_tile) takes the file to be modified now, so it answered 200 although the ETag matched, and
     550     * the host was then wrongly switched to HEAD requests.
     551     * @throws IOException exception
     552     */
     553    @Test
     554    void testCheckUsingETagOnly() throws IOException {
     555        final String eTag = "\"tile-etag\"";
     556        ICacheAccess<String, CacheEntry> cache = getCache();
     557        long expires = TimeUnit.DAYS.toMillis(1);
     558        long testStart = System.currentTimeMillis();
     559        cache.put("etag-only",
     560                new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)),
     561                createEntryAttributes(-1 * expires, 200, testStart, eTag)
     562                );
     563        // like Apache with mod_tile: any If-Modified-Since wins over a matching If-None-Match
     564        tileServer.getWireMock().register(get(urlEqualTo("/etag-only"))
     565                .atPriority(1)
     566                .withHeader("If-Modified-Since", matching(".*"))
     567                .willReturn(aResponse().withHeader("ETag", eTag).withBody("mock entry")));
     568        tileServer.getWireMock().register(get(urlEqualTo("/etag-only"))
     569                .atPriority(2)
     570                .withHeader("If-None-Match", equalTo(eTag))
     571                .willReturn(status(304).withHeader("ETag", eTag)
     572                        .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires))));
     573
     574        // the host of the other tests may already be switched to HEAD requests (the switch is static), so use another
     575        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(
     576                "http://127.0.0.1:" + tileServer.getHttpPort() + "/etag-only", "etag-only");
     577        Listener listener = submitJob(job, false);
     578        tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/etag-only"))
     579                .withHeader("If-None-Match", equalTo(eTag))
     580                .withoutHeader("If-Modified-Since"));
     581        assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data);
     582    }
     583
     584    /**
     585     * A cache entry without an ETag is still checked with If-Modified-Since.
     586     * @throws IOException exception
     587     */
     588    @Test
     589    void testCheckUsingIfModifiedSinceWithoutETag() throws IOException {
     590        ICacheAccess<String, CacheEntry> cache = getCache();
     591        long expires = TimeUnit.DAYS.toMillis(1);
     592        long testStart = System.currentTimeMillis();
     593        cache.put("no-etag",
     594                new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)),
     595                createEntryAttributes(-1 * expires, 200, testStart, null)
     596                );
     597        tileServer.getWireMock().register(get(urlEqualTo("/no-etag"))
     598                .withHeader("If-Modified-Since", matching(".*"))
     599                .willReturn(status(304).withHeader("Expires", TestUtils.getHTTPDate(testStart + expires))));
     600
     601        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(
     602                "http://127.0.0.1:" + tileServer.getHttpPort() + "/no-etag", "no-etag");
     603        Listener listener = submitJob(job, false);
     604        tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/no-etag"))
     605                .withHeader("If-Modified-Since", matching(".*"))
     606                .withoutHeader("If-None-Match"));
     607        assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data);
     608    }
     609
    543610    private void createHeadGetStub(UrlPattern url, long expires, long lastModified, String eTag, String body) {
    544611        tileServer.getWireMock().register(get(url)
Note: See TracChangeset for help on using the changeset viewer.