HTTP 标头 If-None-Match: * 是什么意思?
以下 HTTP 1.1 标头是什么意思?
如果无匹配:*
我理解它在使用强或弱 ETag 甚至 ETag 列表时的情况,但我不明白它在使用星号 (*) 时的用途。
编辑: 如果有一些伪代码(PHP 代码也很好)可以显示如何/什么回答“If-None-Match: *”,那就太好了。
What does the following HTTP 1.1 header mean?
If-None-Match: *
I understand it when using a strong or weak ETag or even a list of ETags, but I don't understant it's use when using star (*).
Edit: Would be nice to have some pseudocode (PHP code good also) that would show how/what to answer to "If-None-Match: *".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是:视情况而定。
假设我们已经收到
今天页面已被更改。
首先,我们看一下
*
,它告诉我们:“如果资源存在并且满足条件 (2),则返回 304”。很好,资源存在,但是条件 (2) 指出:“如果日期晚于当前日期,则仅返回 304”。所以这个条件不满足,页面会完整交付。如果我们没有收到
If-Modified-Since
,则响应将为 304。如果请求时该资源不存在,我们将返回相应的代码(就好像不存在该资源一样)
If-None-Match
)。304 应该只在 GET 和 HEAD 请求的响应中返回,并且所有与缓存相关的响应标头都必须存在。对于所有其他类型的请求,您的服务器需要回答 412(前提条件失败)。
我希望它有帮助;)
The answer is: it depends.
Suppose we have received
And the page has been altered today.
First, we take a look at the
*
which tells us: "Return 304 if the resource is there and condition (2) is met". Fine, the resource exists, BUT condition (2) states: "Only return 304, if the date is later than current". So this condition is not met, and the page will be delivered completely.If we hadn't received
If-Modified-Since
, the response would have been 304.If the resource hadn't existed upon request, we'd have returned the appropriate code (as if there was no
If-None-Match
).304 should only be returned in response for GET and HEAD requests, and all cache-related response headers have to be there. For all other types of request your server needs to be answering 412 (Precondition failed).
I hope it helps ;)
引用自 RFC 2616 (HTTP 1.1):
RFC 继续指出,对于 GET 和 HEAD 请求,服务器应该响应 304(未修改),而不是执行请求,并且对于所有其他请求类型,它们应该响应 412(前提条件失败)。但这仅适用于服务器实际上具有所请求资源的某个版本的情况。如果您没有任何实体,那么您应该处理该请求(可能使用 404,因为您没有任何实体)。
要处理请求,首先要弄清楚如果该标头不存在,服务器会做什么。如果该请求的结果不是为 2xx 或 304,则正常处理该请求。但如果请求的结果是 2xx 或 304,则处理 If-None-Modified 情况。当它是一颗星时,则立即返回 304(除非被 If-Modified-Since 击败)。如果它是一个或多个实体标签,请检查这些标签中的任何一个是否与您计划用作响应的事物的标签相匹配。如果有匹配则返回304;如果没有匹配,则像平常一样提供服务。
稍后在 RFC 中,还有更多内容:
也就是说,星号允许客户端说:“如果该文件已经存在任何版本,则不要放置该文件。”
Quoting from RFC 2616 (HTTP 1.1):
The RFC goes on to say that instead of performing the request, servers should respond with 304 (not modified) for GET and HEAD requests, and that they should respond with 412 (precondition failed) for all other request types. But that's only if the server actually has some version of the requested resource. If you don't have any entities, then you should handle the request (probably with a 404 since you don't have anything).
To handle a request, first figure out what the server would do if that header weren't present. If the result of that request would not be 2xx or 304, then serve the request as normal. But if the result of the request would be 2xx or 304, then handle the If-None-Modified case. When it's a star, then just return 304 immediately (unless trumped by If-Modified-Since). If it's one or more entity tags, then check whether any of those tags matches the tag of the thing you were planning on serving as a response. If there's any match, return 304; if there's no match, then serve it as you normally would.
Later in the RFC, there's more:
That is, the star allows the client to say, "Don't PUT this file if there's any version of this file already there."