Can I stop HTTPResponseCache behaving like a shared cache in regard to Cache-Control headers?

255 Views Asked by At

I am trying to use the built in HTTPResponseCache in my app (making requests via the HTTPURLConnection API) but am having problems trying to get it to cache any responses that were requested with an Authorization header included.

The only way I can get it to cache the response at all is to explicitly put 'public' in the Cache-Control response header on the server (s-maxage might work too, haven't tried, but explicitly putting private results in no caching); but this will mean that any intermediate proxies will cache the response to serve to other clients, which is not what I want.

My understanding is that a user agent cache would cache responses requested with Authorization headers by default or with a private header. It seems like the HTTPResponseCache is acting like a shared cache in how it is interpreting the headers, rather than a user agent cache. Or is my understanding of the caching standards not correct?

Is there any way I can get the cache to act like a user agent HTTP cache?

This in my install code:

public static void setupCache(Context context, long httpCacheSize){
  File httpCacheDir = new File(context.getCacheDir(),"http");
  HttpResponseCache.install(httpCacheDir, httpCacheSize);
}

Do I need to do something different here? Or perhaps I need to include some user agent information in my requests?

1

There are 1 best solutions below

0
On BEST ANSWER

Whilst I found no solution to this specific issue, I worked around my problem by refactoring my HTTP client code to use Volley (http://developer.android.com/training/volley/index.html) rather than HTTPURLConnection. The caching facilities in Volley are implemented separately to HTTPResponseCache and implement handling of cache control headers as expected for a user agent cache.