跳到主要内容

5. Link Header Field

Link header field 提供了一种在 HTTP headers 中序列化 links 的方式.

概述 (Overview)

语义等价性 (Semantic Equivalence):

  • 等价于 HTML <LINK> 元素
  • 等价于 Atom atom:link feed-level element

ABNF 语法 (ABNF Syntax)

Link           = "Link" ":" #link-value
link-value = "`<" URI-Reference ">`" *( ";" link-param )
link-param = ( ( "rel" "=" relation-types )
| ( "anchor" "=" <"> URI-Reference <"> )
| ( "rev" "=" relation-types )
| ( "hreflang" "=" Language-Tag )
| ( "media" "=" ( MediaDesc | ( <"> MediaDesc <"> ) ) )
| ( "title" "=" quoted-string )
| ( "title*" "=" ext-value )
| ( "type" "=" ( media-type | quoted-mt ) )
| ( link-extension ) )
relation-types = relation-type
| <"> relation-type *( 1*SP relation-type ) <">
relation-type = reg-rel-type | ext-rel-type
reg-rel-type = LOALPHA *( LOALPHA | DIGIT | "." | "-" )
ext-rel-type = URI

组成部分 (Components)

5.1. Target IRI

  • 以尖括号 <> 中的 URI-Reference 传递
  • Relative URIs 按 RFC 3986, Section 5 进行解析
  • 不应用来自消息内容的 Base IRI

5.2. Context IRI

  • 默认值 (Default): 被请求资源的 IRI
  • 覆盖 (Override): 使用 anchor 参数
  • 可以指定 fragment 或第三方资源
  • Relative URIs 按 RFC 3986 进行解析
  • 实现可以忽略带 anchor 的 links

5.3. Relation Type

  • rel 参数中指定
  • 每个 link-value 中不得出现多于一次
  • rev 参数: DEPRECATED (反向方向)
  • 扩展类型: 必须是 absolute URIs, 如果包含 ; 或 , 则需要加引号

5.4. Target Attributes

标准参数 (Standard Parameters):

参数 (Parameter)描述 (Description)
hreflangtarget resource 的语言提示
media面向样式信息的预期 media/medium
title人类可读的标签
title*已编码标签 (RFC 5987)
typeMedia type 提示

使用规则 (Usage Rules):

  • hreflang: 可以出现多次
  • media: 如果包含 ; 或 , 则必须加引号
  • title: 不得出现多于一次
  • title*: 如果与 title 同时存在, 优先使用 title*
  • type: Media type 提示, 不会覆盖 Content-Type

5.5. 示例 (Examples)

简单导航 (Simple Navigation):

Link: `\`http://example.com/TheBook/chapter2\``; rel="previous";
title="previous chapter"

扩展关系 (Extension Relation):

Link: `</>`; rel="http://example.net/foo"

带编码的多个 Links (Multiple Links with Encoding):

Link: `&lt;/TheBook/chapter2>`;
rel="previous"; title*=UTF-8'de'letztes%20Kapitel,
`&lt;/TheBook/chapter4>`;
rel="next"; title*=UTF-8'de'n%c3%a4chstes%20Kapitel

多个关系 (Multiple Relations):

Link: `\`http://example.org/\``;
rel="start http://example.net/relation/other"

另请参阅 (See Also):