okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Should okhttp be caching HTTP 307 (or 302) responses according to the w3 document?

Open simtse opened this issue 12 years ago • 3 comments

I am using retrofit and okhttp in my Android app to download documents, images and music files. The files are hosted on Amazon through a CDN so the URLs change often. My backend server will try to use redirects to decrease the need to have to constantly update my content on my mobile app every time the CDN url changes. The devices should also cache responses in the case that the device is offline. For this reason, I am using 301 redirects, which I don't know is the best idea.

I was reading the description for 307 redirect at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html


10.3.8 307 Temporary Redirect

The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.


At http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html,


13.4 - Response Cacheability

A response received with a status code of 200, 203, 206, 300, 301 or 410 MAY be stored by a cache and used in reply to a subsequent request, subject to the expiration mechanism, unless a cache-control directive prohibits caching. However, a cache that does not support the Range and Content-Range headers MUST NOT cache 206 (Partial Content) responses.

A response received with any other status code (e.g. status codes 302 and 307) MUST NOT be returned in a reply to a subsequent request unless there are cache-control directives or another header(s) that explicitly allow it. For example, these include the following: an Expires header (section 14.21); a "max-age", "s-maxage", "must- revalidate", "proxy-revalidate", "public" or "private" cache-control directive (section 14.9).


It seems that the description states that caching is possible. I haven't tried this yet, but I did notice that in okhttp's CacheStrategy.java class, it has the following code:


public static boolean isCacheable(Response response, Request request) { // Always go to network for uncacheable response codes (RFC 2616, 13.4), // This implementation doesn't support caching partial content. int responseCode = response.code(); if (responseCode != HttpURLConnection.HTTP_OK // 200 && responseCode != HttpURLConnection.HTTP_NOT_AUTHORITATIVE // 203 && responseCode != HttpURLConnection.HTTP_MULT_CHOICE // 300 && responseCode != HttpURLConnection.HTTP_MOVED_PERM / 301 && responseCode != HttpURLConnection.HTTP_GONE) { // 410 return false; }


And to verify that caching isn't enabled for from 302-308, the unit tests from CacheTest.java states the following


for (int i = 302; i <= 308; ++i) { assertCached(false, i); }


So the okhttp code explicitly ignores caching for 307, and also 302. I just wanted clarification for this. Is the spec for 307 wrong? Or is something wrong with caching 307 that it was deliberately excluded? If I were to set my own cache headers for 307, it would seem to still be skipped.

Just another background information. I know Amazon will support caching with proper cache headers. But since I have to redirect through my own backend, if I don't have caching ability, my offline access to my redirect URL will not respond that I have a cached version from CDN locally. Of course, if I didn't redirect from my backend and go straight to Amazon, then caching wouldn't be a problem through Amazon. Could you provide some advice in this scenario?

simtse avatar Nov 18 '14 18:11 simtse

It's possible we have this wrong. I'd like to study the specs further before making changes.

swankjesse avatar Nov 28 '14 03:11 swankjesse

And also, study what Chrome & Firefox do!

swankjesse avatar Nov 28 '14 03:11 swankjesse

Yeah, I think we're doing this wrong. The response codes should be used to inform default caching behavior, but the Cache-Control headers should take precedence if they're present.

swankjesse avatar Dec 26 '14 04:12 swankjesse