rest - 我对 Roy Fielding 的 HTTP cookie 的 REST 替代方案的解释是否正确?

标签 rest http cookies uri application-state

HTTP cookies 违反了 REST 架构风格,因为它们独立于应用程序状态并且没有语义,根据 Roy Fielding 的博士论文基于网络的软件架构的架构风格和设计§ 6.3.4.2 ‘Cookies’:

An example of where an inappropriate extension has been made to the protocol to support features that contradict the desired properties of the generic interface is the introduction of site-wide state information in the form of HTTP cookies. Cookie interaction fails to match REST's model of application state, often resulting in confusion for the typical browser application.

Cookies also violate REST because they allow data to be passed without sufficiently identifying its semantics, thus becoming a concern for both security and privacy. The combination of cookies with the Referer [sic] header field makes it possible to track a user as they browse between sites.

因此他建议采用以下替代方案:

As a result, cookie-based applications on the Web will never be reliable. The same functionality should have been accomplished via anonymous authentication and true client-side state. A state mechanism that involves preferences can be more efficiently implemented using judicious use of context-setting URI rather than cookies, where judicious means one URI per state rather than an unbounded number of URI due to the embedding of a user-id. Likewise, the use of cookies to identify a user-specific "shopping basket" within a server-side database could be more efficiently implemented by defining the semantics of shopping items within the hypermedia data formats, allowing the user agent to select and store those items within their own client-side shopping basket, complete with a URI to be used for check-out when the client is ready to purchase.

我对他的用户偏好示例的理解如下。假设某个网站允许用户在 URI /preferences 的首选项页面中选择浅色主题(默认)和深色主题(例如 Stack Overflow)。当用户选择深色主题时,他应该被重定向到 URI /preferences?theme=dark,其 HTML 表示形式将与 URI /preferences 的 HTML 表示形式相同>,只不过现在处于深色模式,并且查询 ?theme=dark 将附加到所有嵌入的超链接中。这样,如果用户选择 URI /home?theme=dark(而不是 /home)处嵌入主页的超链接,则该内容的 HTML 表示形式主页也将处于深色模式,并且查询 ?theme=dark 也将附加到其所有嵌入的超链接中。要恢复到浅色主题,用户可以在 URI /preferences?theme=dark 处选择首选项页面的嵌入式超链接,在首选项页面中选择浅色主题,并且应该重定向到 URI /preferences 其 HTML 表示形式将与 URI /preferences?theme=dark 的 HTML 表示形式相同,只不过它现在处于浅色模式并且查询 ?theme=dark 将从所有嵌入的超链接中删除。这是罗伊·菲尔丁的意思吗?

同样,对于他的购物车示例,当用户将产品 i 添加到购物车时,他应该被重定向到带有查询 ?product-{i}={product- 的 URI i}&quantity-{i}={quantity-i} 其 HTML 表示形式将将该查询附加到其所有嵌入的超链接中。这样,当用户选择结账超链接 /checkout?product-1={product-1}&quantity-1={quantity-1}&...&product-n={product-n}&quantity-n= {quantity-n},购物车的内容发送到网站。这是罗伊·菲尔丁的意思吗?

最佳答案

我相信您在第一种情况下正确地解释了菲尔丁的论文,但在第二种情况下则不然。

您对“首选项”的解释似乎完全正确:拥有表示形式包含相同信息但不同表示形式的多个资源是完全合理的,例如将深色主题和浅色主题作为并行资源结构。

但我认为您误解了菲尔丁提出的“客户端购物篮”。他并不是建议引入可编辑的服务器端资源(毕竟,这种功能已经存在于我们今天的网络中);而是引入一种通用语言,用于在客户端上存储有趣的客户端状态片段。

换句话说,Fielding 正在谈论在 HTML 标准中引入一些控件(类似于 Web 表单的控件),这些控件将允许人们保存一些信息,这些信息稍后会在实际下订单时加载到表单中.

如果您愿意的话,想象一下一种特殊的表单,在提交时,它会编辑网络浏览器本身本地的资源。因此,您可以从目录中挑选商品,这样您的本地购物车资源就会被修改。当您准备结帐时,购物车中的内容将可以发送到服务器。

同样,表单是通用的,可以用于许多不同的领域,所以我们希望这个购物车管道是通用的,这样它就可以用于任何类型的“复制此信息”稍后使用”机制。

诀窍(这并没有发生)是定义一个标准,然后让每个人(浏览器制造商)以足够相似的方式实现这些标准,以便一切正常。

关于rest - 我对 Roy Fielding 的 HTTP cookie 的 REST 替代方案的解释是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67655475/

相关文章:

rest - JPA 2.0 元模型是否可用于确定双向关联的双方

POST 和 GET/PUT 的 Rest API 不同模型

rest - 如何从元数据中查找 OData 版本

ios - Soundcloud 下载超时

ruby-on-rails - 获取大量图像,确定它们是否损坏

linux - 在 lighttpd 中为 cookie 设置 http only 和 secure 标志

REST API 访问控制从访问 token 与路径参数中提取主题

http - swift NSURLConnectction 不工作

ruby-on-rails - Cookie 与 CookieStore 的 session

javascript - 如何使用 CSS、Javascript 或 Cookie 禁用后退按钮上的动画回复?