coil-kt / coil

Image loading for Android backed by Kotlin Coroutines.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible bug when writing response to the disk cache in case of HTTP_NOT_MODIFIED, when only metadata is updated

casaflowa opened this issue · comments

Describe the bug
This is the code snipped in question (HttpUriFetcher):

// Write the response to the disk cache.
if (response.code == HTTP_NOT_MODIFIED && cacheResponse != null) {
    // Only update the metadata.
    val combinedResponse = response.newBuilder()
        .headers(combineHeaders(CacheResponse(response).responseHeaders, response.headers))
        .build()
    fileSystem.write(editor.metadata) {
        CacheResponse(combinedResponse).writeTo(this)
    }
}

and in particular this line: .headers(combineHeaders(CacheResponse(response).responseHeaders, response.headers))
From what I see are CacheResponse(response).responseHeaders and response.headers referencing the same object.
I assume cacheResponse.responseHeaders and response.headers should be combined instead.

Changing cacheResponse.responseHeaders and response.headers would probably fix my problem too, which depends on loading images from disk, but first checks if the image has been updated by asking the server (must-revalidate / etag).

I found this issue, because in my case cacheResponse holds the info about cache-control = must-revalidate (which is already stored in the metadata), which gets lost when metadata is overwritten, since the response (with code HTTP_NOT_MODIFIED) doesn't contain the cache-control headers anymore.
=> My temporary fix is adding this cache-control headers to the response of the must-revalidate-Request.

Version
io.coil-kt:coil-base:2.0.0-rc02

Great catch! Thanks for the report. Fixed here: #1261

ezoic increase your site revenue