覆盖“缓存控制” HTTP响应中的值 [英] Override the "cache-control" values in a HTTP response

查看:142
本文介绍了覆盖“缓存控制” HTTP响应中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  HTTP / 1.1 200 OK $ b $我有一个网页,当我访问资料时返回以下标题: b日期:2013年6月29日星期六15:57:25 GMT 
服务器:Apache
内容长度:2247515
Cache-Control:no-cache,no-store,must-revalidate, max-age = -1
Pragma:no-cache,no-store
Expires:-1
Connection:close

使用chrome扩展名,我想修改这个响应头,以便实际缓存材料而不是浪费带宽。



我有以下示例代码:

  chrome.webRequest。 onHeadersReceived.addListener(function(details)
{
//删除所需元素
removeHeader(details.responseHeaders,'pragma');
removeHeader(details.responseHeaders,'expires ');

//修改缓存控制
updateHeader(details.responseHeaders,'cache-control','max-age = 3600; )

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

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

正确地修改头文件为这样(基于console.log()输出):

  HTTP / 1.1 200 OK 
日期:2013年6月29日星期六15:57:25 GMT
Server:Apache
Content-Length:2247515
Cache-Control:max-age = 3600
连接:关闭

但是,基于我试图检查的所有内容,我无法看到实际发生的任何证据:


  1. 缓存不包含此文件的条目

  2. Network 开发者控制台中的code>标签对HTTP响应完全没有任何改变(为了确保它的正确性,我试图将它改为甚至微不足道的修改不是一个错误,但仍然没有改变)。

我能找到的唯一真正的提示是这个问题这表明我的方法仍然有效,并且该段落介绍了 webRequest API文档,它表明这将无法正常工作(但并不能解释为什么我无法进行任何更改):


请注意,Web请求API将网络
堆栈抽象为扩展。在内部,一个URL请求可以分成几个HTTP请求(例如从大文件中获取单个字节范围
),或者可以由网络堆栈处理,而不需要
与网络通信。出于这个原因,API不
提供发送到网络的最终HTTP头。对于
例子,所有与缓存相关的头文件对
扩展名都是不可见的。


无论如何(我无法修改 HTTP响应头),所以我认为这是我的第一个担心。



任何建议,在哪里我可能会出错或如何去找到这里出了什么问题?

如果不可能,是否有其他方法来实现我我试图实现?

解决方案

我最近花了几个小时试图获取文件缓存,并发现 chrome.webRequest chrome.declarativeWebRequest API 不能强制资源被缓存。不能。
$ b

Cache-Control (和其他)响应标头可以更改,但它只会在 getResponseHeader 方法中可见。不在缓存行为中。


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?

解决方案

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.

这篇关于覆盖“缓存控制” HTTP响应中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆