为什么同一资源的不同表示有不同的 Etag?

发布于 2024-11-02 12:44:20 字数 182 浏览 0 评论 0原文

我了解使用 etag 进行乐观并发控制(例如,在 RESTful 风格的架构中),并且我读到 etag 对于同一资源的不同表示应该是不同的。这是为什么?

最终,我们是否不想知道资源是否已更改,以便我们可以处理并发修改?我什至很难想象资源的表示何时会在资源本身不发生变化的情况下发生变化,所以我显然缺少一些基本的理解。

I understand the use of etags for optimistic concurrency control (e.g. in a RESTful style of architecture), and I've read that etags should be different for different representations of the same resource. Why is that?

Ultimately aren't we interested in knowing if the resource has changed so we can handle concurrent modifications? I'm having a hard time even envisioning when a resource's representation would change without the resource itself changing, so I'm obviously missing some basic understanding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

疾风者 2024-11-09 12:44:20

好问题,我认为这是一个值得争论的问题。

我想大多数人会说ETag不仅代表资源版本,还代表内容类型。这对于根据内容类型、语言等缓存响应是有意义的。

请查看以下链接:

Good question, and I think it's a matter of some debate.

I think that most would say that the ETag represents not only the resource version, but also the content type. This would make sense for caching responses based on content type, language, etc.

Check out the following links:

雨轻弹 2024-11-09 12:44:20

当您阐述事实或阅读 HTTP&HTTPbis 规范时,这不是一个争论的问题。

ETag 是一种缓存和并发控制的手段。
弱 ETag 只是穷人缓存的一种手段。

在缓存 (GET) 方面 - uri + content-type + etag 可以帮助您节省带宽,方法是不响应有效负载,而仅响应 304 状态代码。

就并发控制(POST;PUT;PATCH)而言 - 根据 URI + 内容类型 + 位精确的响应负载计算 ETag 是浮躁的。为什么?

  • 如果您基于整个对象(响应负载的超集)计算 ETag(即您的负载给出 a+b,但该对象实际上是 a+b+c),那么例如执行 PATCH 将最终失败,因为ETag 已更改...您刷新...您获得相同的数据,但 ETag 不同...您使用新的 ETag 重试 PATCH,现在它可以工作了。 失败
  • 如果您根据有效负载的子集计算 ETag,您实际上是在完全没有任何透明度的情况下迫使用户无法控制不安全调用的条件。即使与该 ETag 关联的数据已更改,PATCH 也会成功,这显然不是 HTTP 请求的预期方式。 FAIL

条件请求应使用类似于“鉴于我的世界观仍然相同,则执行请求。否则失败”的语义来处理。我的世界观是由过去的响应(URI + 标头 + 有效负载)构成的。

This is not a matter of debate when you lay out facts or when you read the HTTP&HTTPbis spec.

ETag is a means of caching and concurrency control.
Weak ETags is only a means of poor-man's caching.

In terms of caching (GET) - uri + content-type + etag can help you save bandwidth by not responding with the payload as well, but just with 304 status code.

In terms of concurrency control (POST;PUT;PATCH) - it is impetuous to have the ETag calculated based on URI + content-type + bit-exact response payload. Why?

  • If you calculate the ETag based on a whole object, a superset of the response payload (ie. your payload gives a+b, but the object is actually a+b+c), then doing a PATCH for instance will end up failing because the ETag changed... you refresh.. you get the same data but a different ETag... you retry the PATCH with the new ETag, now it works. FAIL
  • If you calculate the ETag based on a subset of the payload you are actually forcing the user out of being in control of the conditions for the unsafe call without any transparency at all. A PATCH will succeed even if the data associated with that ETag has changed, which is obviously not how the HTTP request was intended. FAIL

Conditional requests should be treated with a semantic similar to "Given that my view of the world is still the same, then perform the request. Fail otherwise". My view of the world is made out of a past response (URI + headers + payload).

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