http - If-Modified-Since 在浏览器中覆盖 If-None-Match

标签 http http-headers browser-cache http-caching

我有一个网络服务器,它为特定的 url 提供一个 ETag。当浏览器对该 url 执行请求时,它会提供 http header “If-None-Match”,其中包含服务器先前对该 url 的响应中包含的 ETag 值。现在,如果我以编程方式添加请求 header “If-Modified-Since”并将其设置为 future 或过去的日期(无关紧要),浏览器将停止发送“If-None-Match” header 。我在 FireFox 和 Chrome 中都看到过这种情况(未在任何其他浏览器上测试过)。我无法从 HTTP/1.1 规范中得出结论,情况应该如此。为什么会这样?

这是一个简单的代码示例,可以重现该场景。该代码假定服务器使用 Etag header 进行响应。

var request = new XMLHttpRequest();
request.open("GET", someUrl, true);
request.onreadystatechange = function(){};

// This statement makes the browser stop sending the "If-None-Match" header
request.setRequestHeader("If-Modified-Since", "Sat, 29 Oct 1994 19:43:31 GMT"); 

request.send(null);

最佳答案

Now, if I programatically add the request-header "If-Modified-Since" and set it to a either a future or past date (doesn't matter), the browser stops sending the "If-None-Match"-header.

如果服务器为给定资源同时提供 Etag 和 Last-Modified header ,则它们不应该:

HTTP/1.1 clients [...] if both an entity tag and a Last-Modified value have been provided by the origin server, SHOULD use both validators in cache-conditional requests. This allows both HTTP/1.0 and HTTP/1.1 caches to respond appropriately.

编辑:当调用 XmlHttpRequest.open() 时,实现使用 nsIHttpChannel 准备请求,其中使用 If-Modified-SinceIf-None-Match header (如果它可以找到本地缓存项)。

现在,当您使用 If-(Un)Modified-SinceIf-None-Match 调用 SetRequestHeader() 时,它'我将从请求中清除两个 header 并设置您的值。

关于http - If-Modified-Since 在浏览器中覆盖 If-None-Match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834138/

相关文章:

javascript - 如何缓存 javascript 文件

java - 用于 java 的谷歌安全浏览查找 api 代码

javascript - DHTMLX dhtmlxForm.send() 抛出 HTTP 状态 400 - "The request sent by the client was syntactically incorrect."

php - 无法通过 PHP 修改内容安全策略 header

http-headers - 计算 Content-Digest 字段的差异

python - 网比 : how to set http status code to 300

javascript - Mozilla 不更新源代码

python - 使用后退按钮后防止重新填充和/或重新提交 Django 表单

javascript - 向服务器查询文件

python - 我可以使用 tornado 管道化 HTTP 1.1 请求吗?