square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.83k stars 9.16k forks source link

Cache-Control: immutable #2878

Closed swankjesse closed 7 years ago

swankjesse commented 8 years ago

https://bitsup.blogspot.ca/2016/05/cache-control-immutable.html?m=1

swankjesse commented 7 years ago

Done! https://github.com/square/okhttp/pull/3384

lxd1047926543 commented 6 years ago

In the section 2 of the RFC8264, it says that if a response is immutable, a reload should ignore a conditional request, but a force reload should not.

Clients SHOULD NOT issue a conditional request during the response's freshness lifetime (e.g., upon a reload) unless explicitly overridden by the user (e.g., a force reload).

And then I found that in Chrome, a force reload, pressing Ctrl + F5, is a request that contains a Cache-Control: no-cache header while a common reload, simply pressing F5, is a request that contains condition headers. So I guess the code below indicates a reload and a force reload, if it is the same meaning with Chrome. Both of them will perform a request from network no matter the response is immutable or not.

CacheControl requestCaching = request.cacheControl();
if (requestCaching.noCache() || hasConditions(request)) {
    return new CacheStrategy(request, null);
}

 CacheControl responseCaching = cacheResponse.cacheControl();
if (responseCaching.immutable()) {
    return new CacheStrategy(null, cacheResponse);
}

Does that contradicts with the RFC? Plus, OkHttp doesn't compute the freshness lifttime before checking whether the response is immutable, are there other considerations?

swankjesse commented 6 years ago

@ lxd1047926543 is this a bug report? Could you open a new issue with a failing test?

lxd1047926543 commented 6 years ago

@swankjesse I haven't encountered a failure, I'm just confused by the difference among OkHttp implementation and the RFC and the other implementations such as Firefox. Firefox implements the Cache-Control: immutable directive, it doesn't issue an conditional request if the cached response is immutable when reloading (F5), but do issue a Cache-Control: no-cache request if the cached response is immutable when forcefully reloading (Ctrl+F5). Thanks for your time.

swankjesse commented 6 years ago

There's no such thing as forceful reloading in OkHttp. The closest we have is making a request without the cache.

StuStirling commented 6 years ago

@lxd1047926543 @swankjesse I have experienced this in the library recently and what we have seen is that as soon as the immutable extension exists, the cached response is always used regardless of the max-age value. Now I'm not an expert on how these headers should be respected but please see my pull request for how I interpret they should work. https://github.com/square/okhttp/pull/4312

StuStirling commented 6 years ago

I have opened issue #4313 to capture the bug