互联网工程任务组 (IETF) M. Nottingham
请求评论:8288 2017年9月
废止:5988
类别:标准轨道
ISSN: 2070-1721

Web 链接


摘要

本规范定义了 Web 上资源之间关系(“链接”)的模型,以及这些关系的类型(“链接关系类型”)。

它还定义了在 HTTP 首部中使用 Link 首部字段对这些链接进行序列化的方式。

本备忘录状态

这是一个互联网标准轨道文档。

本文件是互联网工程任务组(IETF)的成果,代表 IETF 社区的共识。它已接受公开审查并由互联网工程指导小组(IESG)批准发布。有关互联网标准的更多信息,请参阅 RFC 7841 第2节

有关本文档当前状态、任何勘误以及如何提供反馈的信息,可在 https://www.rfc-editor.org/info/rfc8288 获取。

Copyright Notice

Copyright (c) 2017 IETF Trust and the persons identified as the document authors. All rights reserved.

This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.

This document may contain material from IETF Documents or IETF Contributions published or made publicly available before November 10, 2008. The person(s) controlling the copyright in some of this material may not have granted the IETF Trust the right to allow modifications of such material outside the IETF Standards Process. Without obtaining an adequate license from the person(s) controlling the copyright in such materials, this document may not be modified outside the IETF Standards Process, and derivative works of it may not be created outside the IETF Standards Process, except to format it for publication as an RFC or to translate it into languages other than English.

1. 引言

本规范定义了 Web 上资源之间关系(“链接”)的模型以及这些关系的类型(“链接关系类型”)。

HTML [W3C.REC-html5-20141028] 与 Atom [RFC4287] 都有定义良好的链接概念;第 2 节 将其泛化为一个框架,涵盖这些格式中的链接以及(可能的)其它场景。

此外,第 3 节 定义了一个用于传递此类链接的 HTTP 首部字段。

1.1. 符号约定

文中关键词 “MUST”、“MUST NOT”、“REQUIRED”、“SHALL”、“SHALL NOT”、“SHOULD”、“SHOULD NOT”、“RECOMMENDED”、“NOT RECOMMENDED”、“MAY” 和 “OPTIONAL” 在仅当它们以全部大写出现时,应按 BCP 14 中的定义进行解释,参见 [RFC2119][RFC8174]

本文件使用增强巴科斯-诺尔范式(ABNF)表示法(参见 [RFC5234])以及 [RFC7230] 中的 #rule,并显式包含其定义的以下规则:quoted-string、token、SP(空格)、BWS(bad whitespace)、OWS(optional whitespace)、RWS(required whitespace)、LOALPHA、DIGIT。

此外,还包含以下规则:

1.2. 合规性与错误处理

与合规性与错误处理相关的要求参见 [RFC7230] 第 2.5 节,这些要求也适用于本文档。

4. IANA 注意事项

5. 安全性注意事项

Link 首部字段的内容不提供机密性、隐私或完整性保证。使用带有 HTTP 的传输层安全(TLS)(参见 [RFC2818])是目前提供这些端到端属性的唯一方式。

链接应用应当考虑自动跟随、信任或以其它方式使用从 HTTP 首部字段收集到的链接所打开的攻击向量。

例如,使用 “anchor” 参数将链接的上下文与另一个资源关联的 Link 首部字段不能被信任,因为它们实际上是第三方的断言,可能是错误的或恶意的。应用可以通过指定仅在资源之间建立某种关系(例如共享相同的 authority)时才保留此类链接来缓解该风险,否则应丢弃这些链接。

解析链接存在多种风险,取决于所使用的应用。例如,Referer 首部(参见 [RFC7231])可能在其值中暴露有关应用状态(包括私人信息)。同样,cookie(参见 [RFC6265])也是一种机制,若被使用,可能成为攻击向量。应用可以通过谨慎地指定这些机制的工作方式来缓解这些风险。

Link 首部字段广泛使用 IRI 与 URI。有关 IRI 的安全注意事项,请参见 [RFC3987] 第 8 节。有关 URI 的安全注意事项,请参见 [RFC3986] 第 7 节。有关 HTTP 首部字段的安全注意事项,请参见 [RFC7230] 第 9 节。

6. 国际化注意事项

为了在不支持 IRI 的序列化中表达链接目标,链接目标可能需要被转换为 URI。这包括 Link HTTP 首部字段。

类似地,Link 首部字段的 anchor 参数不支持 IRI;因此在包含之前必须将 IRI 转换为 URI。

为了便于比较,关系类型被定义为 URI 而非 IRI。预计它们不会被直接展示给终端用户。

注意已注册的关系名称要求为小写 ASCII 字母。

7. 参考文献

7.1. 规范性参考文献

[RFC2119]
Bradner, S., “Key words for use in RFCs to Indicate Requirement Levels”, BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997, <https://www.rfc-editor.org/info/rfc2119>.
[RFC3864]
Klyne, G., Nottingham, M., and J. Mogul, “Registration Procedures for Message Header Fields”, BCP 90, RFC 3864, DOI 10.17487/RFC3864, September 2004, <https://www.rfc-editor.org/info/rfc3864>.
[RFC3986]
Berners-Lee, T., Fielding, R., and L. Masinter, “Uniform Resource Identifier (URI): Generic Syntax”, STD 66, RFC 3986, DOI 10.17487/RFC3986, January 2005, <https://www.rfc-editor.org/info/rfc3986>.
[RFC3987]
Duerst, M. and M. Suignard, “Internationalized Resource Identifiers (IRIs)”, RFC 3987, DOI 10.17487/RFC3987, January 2005, <https://www.rfc-editor.org/info/rfc3987>.
[RFC5234]
Crocker, D., Ed. and P. Overell, “Augmented BNF for Syntax Specifications: ABNF”, STD 68, RFC 5234, DOI 10.17487/RFC5234, January 2008, <https://www.rfc-editor.org/info/rfc5234>.
[RFC5646]
Phillips, A., Ed. and M. Davis, Ed., “Tags for Identifying Languages”, BCP 47, RFC 5646, DOI 10.17487/RFC5646, September 2009, <https://www.rfc-editor.org/info/rfc5646>.
[RFC6838]
Freed, N., Klensin, J., and T. Hansen, “Media Type Specifications and Registration Procedures”, BCP 13, RFC 6838, DOI 10.17487/RFC6838, January 2013, <https://www.rfc-editor.org/info/rfc6838>.
[RFC7230]
Fielding, R., Ed. and J. Reschke, Ed., “Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing”, RFC 7230, DOI 10.17487/RFC7230, June 2014, <https://www.rfc-editor.org/info/rfc7230>.
[RFC7231]
Fielding, R., Ed. and J. Reschke, Ed., “Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content”, RFC 7231, DOI 10.17487/RFC7231, June 2014, <https://www.rfc-editor.org/info/rfc7231>.
[RFC8126]
Cotton, M., Leiba, B., and T. Narten, “Guidelines for Writing an IANA Considerations Section in RFCs”, BCP 26, RFC 8126, DOI 10.17487/RFC8126, June 2017, <https://www.rfc-editor.org/info/rfc8126>.
[RFC8174]
Leiba, B., “Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words”, BCP 14, RFC 8174, DOI 10.17487/RFC8174, May 2017, <https://www.rfc-editor.org/info/rfc8174>.
[RFC8187]
Reschke, J., “Indicating Character Encoding and Language for HTTP Header Field Parameters”, RFC 8187, DOI 10.17487/RFC8187, September 2017, <https://www.rfc-editor.org/info/rfc8187>.
[W3C.REC-css3-mediaqueries-20120619]
Rivoal, F., “Media Queries”, World Wide Web Consortium Recommendation REC-css3-mediaqueries-20120619, June 2012, <http://www.w3.org/TR/2012/REC-css3-mediaqueries-20120619>.

7.2. 补充性参考文献

[RFC2046]
Freed, N. and N. Borenstein, “Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types”, RFC 2046, DOI 10.17487/RFC2046, November 1996, <https://www.rfc-editor.org/info/rfc2046>.
[RFC2818]
Rescorla, E., “HTTP Over TLS”, RFC 2818, DOI 10.17487/RFC2818, May 2000, <https://www.rfc-editor.org/info/rfc2818>.
[RFC4287]
Nottingham, M., Ed. and R. Sayre, Ed., “The Atom Syndication Format”, RFC 4287, DOI 10.17487/RFC4287, December 2005, <https://www.rfc-editor.org/info/rfc4287>.
[RFC6265]
Barth, A., “HTTP State Management Mechanism”, RFC 6265, DOI 10.17487/RFC6265, April 2011, <https://www.rfc-editor.org/info/rfc6265>.
[W3C.REC-html5-20141028]
Hickson, I., Berjon, R., Faulkner, S., Leithead, T., Navara, E., O&#039;Connor, T., and S. Pfeiffer, “HTML5”, World Wide Web Consortium Recommendation REC-html5-20141028, October 2014, <http://www.w3.org/TR/2014/REC-html5-20141028>.

Appendix B. 解析 Link 首部字段的算法

本附录概述了一组非规范性的算法:用于从首部集合中解析 Link 首部、解析 Link 首部字段值,以及解析字段值通用部分的算法。

这些算法比 ABNF 所定义的语法更为宽松;其中体现的错误处理是一种合理的方法,但并非强制性要求。因此这些算法仅供建议,在出现分歧的情况下,正确行为由本规范主体定义。

B.1. 解析包含链接的首部集合

该算法可用于解析 HTTP 首部集合中包含的 Link 首部字段。给定一个由(字符串 field_name, 字符串 field_value)对组成的 header_set,假定为 ASCII 编码,它返回一个链接对象列表。

  1. 令 field_values 为 header_set 中其 field_name 与 “link” 不区分大小写匹配的成员列表。
  2. 令 links 为一个空列表。
  3. 对于 field_values 中的每个 field_value:
    1. 令 value_links 为从 field_value 中执行 “解析 Link 字段值”(见附录 B.2) 的结果。
    2. 将 value_links 的每个成员附加到 links。
  4. 返回 links。

B.2. 解析 Link 字段值

该算法从 Link 首部字段解析零个或多个以逗号分隔的 link-values。给定一个字符串 field_value,假定为 ASCII 编码,它返回一个链接对象列表。

  1. 令 links 为空列表。
  2. 当 field_value 有内容时:
    1. 消费任何前导 OWS。
    2. 如果第一个字符不是 “<”,则返回 links。
    3. 丢弃第一个字符(“<”)。
    4. 消费直到但不包括第一个 “>” 字符或 field_value 结束,并令结果为 target_string。
    5. 如果下一个字符不是 “>”,则返回 links。
    6. 丢弃前导的 “>” 字符。
    7. 令 link_parameters 为从 field_value 中解析参数(附录 B.3) 的结果(消费其中零个或多个字符)。
    8. 令 target_uri 为对 target_string 进行相对解析(按 [RFC3986] 第 5.2 节)的结果。注意正文中携带的任何 base URI 不会被使用。
    9. 令 relations_string 为 link_parameters 中第一个元组的第二项,该元组的第一项匹配字符串 “rel”;如果不存在则 relations_string 为空字符串("")。
    10. 按 RWS 拆分 relations_string(并在过程中移除),得到 relation_types 字符串列表。
    11. 令 context_string 为 link_parameters 中第一个元组的第二项,该元组的第一项匹配字符串 “anchor”。若不存在,则 context_string 为承载 Link 首部的表示的 URL(按 [RFC7231] 第 3.1.4.1 节),以 URI 形式序列化;若该 URL 为匿名,则 context_string 为 null。
    12. 令 context_uri 为对 context_string 进行相对解析(按 [RFC3986] 第 5.2 节)的结果,除非 context_string 为 null,在此情况下 context 为 null。注意正文中携带的任何 base URI 不会被使用。
    13. 令 target_attributes 为空列表。
    14. 对于 link_parameters 的每个元组 (param_name, param_value):
      1. 如果 param_name 匹配 “rel” 或 “anchor”,则跳过该元组。
      2. 如果 param_name 匹配 “media”、“title”、“title*” 或 “type” 且 target_attributes 中已包含第一个元素匹配 param_name 的元组,则跳过该元组。
      3. 将 (param_name, param_value) 附加到 target_attributes。
    15. 令 star_param_names 为 link_parameters 中那些 param_name 最后字符为星号(“*”)的参数名集合。
    16. 对于 star_param_names 中的每个 star_param_name:
      1. 令 base_param_name 为去掉最后一个字符后的 star_param_name。
      2. 如果实现选择不支持名为 base_param_name 的参数的国际化形式(包括但不限于其被该参数规范禁止),则从 link_parameters 中移除所有第一成员为 star_param_name 的元组,然后继续下一个 star_param_name。
      3. 从 link_parameters 中移除所有第一成员为 base_param_name 的元组。
      4. 将 link_parameters 中所有第一成员为 star_param_name 的元组的第一成员改为 base_param_name。
    17. 对于 relation_types 中的每个 relation_type:
      1. 将 relation_type 规范化为小写。
      2. 将一个链接对象附加到 links,包含目标 target_uri、关系类型 relation_type、上下文 context_uri 以及目标属性 target_attributes。
  3. 返回 links。

B.3. 解析参数

该算法从首部字段值解析参数。给定输入 input(ASCII 字符串),它返回其中包含的一组 (字符串 parameter_name, 字符串 parameter_value) 元组列表。input 会被修改以移除已解析的参数。

  1. 令 parameters 为空列表。
  2. 当 input 有内容时:
    1. 消费任何前导 OWS。
    2. 如果第一个字符不是 “;”,则返回 parameters。
    3. 丢弃前导的 “;” 字符。
    4. 消费任何前导 OWS。
    5. 消费直到但不包括第一个 BWS、“=”、“;”、“,” 字符或输入结束,并令结果为 parameter_name。
    6. 消费任何前导 BWS。
    7. 如果下一个字符是 “=”:
      1. 丢弃前导的 “=” 字符。
      2. 消费任何前导 BWS。
      3. 如果下一个字符是 DQUOTE,则令 parameter_value 为从 input 中解析引用字符串(附录 B.4)的结果(消费其中零个或多个字符)。
      4. 否则,消费直到但不包括第一个 “;” 或 “,” 字符,或直到输入结束,并令结果为 parameter_value。
      5. 如果 parameter_name 的最后字符为星号(“*”),则按 [RFC8187] 对 parameter_value 解码。在遇到不可恢复错误时继续处理输入。
    8. 否则:
      1. 令 parameter_value 为空字符串。
    9. 将 parameter_name 规范化为小写。
    10. 将 (parameter_name, parameter_value) 附加到 parameters。
    11. 消费任何前导 OWS。
    12. 如果下一个字符是 “,” 或输入结束,停止处理输入并返回 parameters。

B.4. 解析引用字符串

该算法解析引用字符串(按 [RFC7230] 第 3.2.6 节)。给定输入 input(ASCII 字符串),它返回一个去引号后的字符串。input 会被修改以移除已解析的字符串。

  1. 令 output 为空字符串。
  2. 如果 input 的第一个字符不是 DQUOTE,则返回 output。
  3. 丢弃第一个字符。
  4. 当 input 有内容时:
    1. 如果第一个字符是反斜杠(“\”):
      1. 丢弃第一个字符。
      2. 如果没有更多输入,返回 output。
      3. 否则,消费第一个字符并将其附加到 output。
    2. 否则,如果第一个字符是 DQUOTE,则丢弃它并返回 output。
    3. 否则,消费第一个字符并将其附加到 output。
  5. 返回 output。

Appendix C. 相对于 RFC 5988 的更改

本规范与其前身 RFC 5988 存在以下差异:

  • 初始的关系类型注册已被移除,因为它们已由 RFC 5988 注册。
  • 引言已被简化。
  • “Link Relation Application Data” 注册表已被删除。
  • 并入了勘误(errata)。
  • 更新了参考文献。
  • 澄清了链接的基数。
  • 术语从 “target IRI” 与 “context IRI” 更改为分别为 “link target” 与 “link context”。
  • 使为已注册关系类型分配 URI 成为序列化特定的操作。
  • 移除了此前误导性的声明,即 Link 首部字段在语义上等同于 HTML 与 Atom 链接。
  • 更明确地定义并使用了“链接序列化(link serialisations)”与“链接应用(link applications)”。
  • 澄清了目标属性的基数(通用层面及对 “type” 的规定)。
  • 纠正了 Link 首部字段默认链接上下文的定义,使其依赖于表示的标识(依据 RFC 7231)。
  • 定义了建议的 Link 首部解析算法。
  • 规定了目标属性值空间及其定义。
  • ABNF 已更新以兼容 [RFC7230],特别是显式化了空白符。
  • HTTP 首部字段上的某些参数现在可以以 token 形式出现。
  • HTTP 首部字段上的参数现在可以无值。
  • 引用字符串的处理现在由 [RFC7230] 定义。
  • type 首部字段参数现在需要被引用(因为 token 不允许 “/”)。

作者地址

Mark Nottingham
Email: mnot@mnot.net
URI: https://www.mnot.net/