HTTP 标头 If-None-Match: * 是什么意思?

发布于 2024-08-18 21:27:08 字数 219 浏览 5 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-08-25 21:27:08

答案是:视情况而定。

假设我们已经收到

If-None-Match: *
If-Modified-Since: <yesterday date>

今天页面已被更改。

首先,我们看一下 *,它告诉我们:“如果资源存在并且满足条件 (2),则返回 304”。很好,资源存在,但是条件 (2) 指出:“如果日期晚于当前日期,则仅返回 304”。所以这个条件不满足,页面会完整交付。

如果我们没有收到 If-Modified-Since,则响应将为 304。

如果请求时该资源不存在,我们将返回相应的代码(就好像不存在该资源一样) If-None-Match)。

304 应该只在 GET 和 HEAD 请求的响应中返回,并且所有与缓存相关的响应标头都必须存在。对于所有其他类型的请求,您的服务器需要回答 412(前提条件失败)。

我希望它有帮助;)

The answer is: it depends.

Suppose we have received

If-None-Match: *
If-Modified-Since: <yesterday date>

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 ;)

暮年慕年 2024-08-25 21:27:08

引用自 RFC 2616 (HTTP 1.1)

...如果给出“*”并且该资源存在任何当前实体,则服务器不得执行请求的方法,除非需要这样做,因为资源的修改日期与 If- 中提供的修改日期不匹配请求中的 Modified-Since 标头字段。

RFC 继续指出,对于 GET 和 HEAD 请求,服务器应该响应 304(未修改),而不是执行请求,并且对于所有其他请求类型,它们应该响应 412(前提条件失败)。但这仅适用于服务器实际上具有所请求资源的某个版本的情况。如果您没有任何实体,那么您应该处理该请求(可能使用 404,因为您没有任何实体)。

要处理请求,首先要弄清楚如果该标头不存在,服务器会做什么。如果该请求的结果不是为 2xx 或 304,则正常处理该请求。但如果请求的结果是 2xx 或 304,则处理 If-None-Modified 情况。当它是一颗星时,则立即返回 304(除非被 If-Modified-Since 击败)。如果它是一个或多个实体标签,请检查这些标签中的任何一个是否与您计划用作响应的事物的标签相匹配。如果有匹配则返回304;如果没有匹配,则像平常一样提供服务。

稍后在 RFC 中,还有更多内容:

“If-None-Match: *”的含义是,如果源服务器(或缓存,可能使用 Vary 机制)选择了表示,则不得执行该方法,请参阅 14.44) 存在,如果表示不存在,则应该执行。此功能旨在有助于防止 PUT 操作之间的竞争。

也就是说,星号允许客户端说:“如果该文件已经存在任何版本,则不要放置该文件。”

Quoting from RFC 2616 (HTTP 1.1):

... if "*" is given and any current entity exists for that resource, then the server MUST NOT perform the requested method, unless required to do so because the resource's modification date fails to match that supplied in an If-Modified-Since header field in the request.

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:

The meaning of "If-None-Match: *" is that the method MUST NOT be performed if the representation selected by the origin server (or by a cache, possibly using the Vary mechanism, see section 14.44) exists, and SHOULD be performed if the representation does not exist. This feature is intended to be useful in preventing races between PUT operations.

That is, the star allows the client to say, "Don't PUT this file if there's any version of this file already there."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文