Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override the "cache-control" values in a HTTP response

I have a web page that returns the following header when I access material:

HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: no-cache, no-store, must-revalidate, max-age=-1
Pragma: no-cache, no-store
Expires: -1
Connection: close

Using a chrome extension, I want to modify this response header so that the material is actually cached instead of wasting bandwidth.

I have the following sample code:

chrome.webRequest.onHeadersReceived.addListener(function(details) 
    {
        // Delete the required elements
        removeHeader(details.responseHeaders, 'pragma');
        removeHeader(details.responseHeaders, 'expires');

        // Modify cache-control
        updateHeader(details.responseHeaders, 'cache-control', 'max-age=3600;')

        console.log(details.url);
        console.log(details.responseHeaders);

        return{responseHeaders: details.responseHeaders};
    },
    {urls: ["<all_urls>"]}, ['blocking', 'responseHeaders']
);

Which correctly modifies the header to something like this (based on the console.log() output):

HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: max-age=3600
Connection: close

But based on everything I have tried to check this, I cannot see any evidence whatsoever that this has actually happened:

  1. The cache does not contain an entry for this file
  2. The Network tab in the Developer Console shows no change at all to the HTTP response (I have tried changing it to even trivial modifications just for the sake of ensuring that its not a error, but still no change).

The only real hints I can find are this question which suggests that my approach still works and this paragraph on the webRequest API documentation which suggests that this won't work (but doesn't explain why I can't get any changes whatsoever):

Note that the web request API presents an abstraction of the network stack to the extension. Internally, one URL request can be split into several HTTP requests (for example to fetch individual byte ranges from a large file) or can be handled by the network stack without communicating with the network. For this reason, the API does not provide the final HTTP headers that are sent to the network. For example, all headers that are related to caching are invisible to the extension.

Nothing is working whatsoever (I can't modify the HTTP response header at all) so I think that's my first concern.

Any suggestions at where I could be going wrong or how to go about finding what is going wrong here?

If its not possible, are there any other ways to achieve what I am trying to achieve?

like image 202
btalb Avatar asked Jun 29 '13 16:06

btalb


People also ask

What is Cache-Control on HTTP response?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).

How do I disable HTTP caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Which of the option provides control over caching in header field of HTTP response?

The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).

What is the default value of Cache-Control?

If present and greater than 0, a Cache-control: no-store header from the remote server is ignored and the response is cached. The default value is 0. This behavior violates the HTTP standard. If present and greater than 0, a Cache-control: private header from the remote server is ignored and the response is cached.


1 Answers

I have recently spent some hours on trying to get a file cached, and discovered that the chrome.webRequest and chrome.declarativeWebRequest APIs cannot force resources to be cached. In no way.

The Cache-Control (and other) response headers can be changed, but it will only be visible in the getResponseHeader method. Not in the caching behaviour.

like image 163
Rob W Avatar answered Oct 16 '22 04:10

Rob W