跳到主要内容

附录 A. 汇总 ABNF (Collected ABNF)

在下面汇总的 ABNF 中, 列表规则按 [HTTP] 第 5.6.1 节展开.

本附录把 HTTP/1.1 消息语法中分散定义的规则集中列出, 便于实现者核对解析器. ABNF 规则名、字面量、CRLFOCTETDIGITHEXDIGtokenquoted-string 等符号都属于规范语法, 因此在代码块中保持原样. 中文说明仅解释这些规则之间的关系, 不改变语法定义.

从结构上看, HTTP-message 由 start-line、字段行、空行和可选 message-body 组成; start-line 又分为 request-line 和 status-line. 请求目标 request-target 可以采用 origin-form、absolute-form、authority-form 或 asterisk-form. 分块传输由 chunked-bodychunklast-chunktrailer-section 描述. 这些规则共同限定 HTTP/1.1 报文的起始行、头字段、消息体和 Transfer-Encoding 的解析方式.

附录中若某条规则写成 "see [HTTP]" 或 "see [URI]", 表示其完整定义来自 RFC 9110 或 URI 规范, 本文只引用并组合这些基础规则. 实现时不应把占位引用当作可匹配字面量, 而应解析为被引用规范中的对应 ABNF.

BWS = `<BWS, see [HTTP], Section 5.6.3>`

HTTP-message = start-line CRLF *( field-line CRLF ) CRLF [
message-body ]
HTTP-name = %x48.54.54.50 ; HTTP
HTTP-version = HTTP-name "/" DIGIT "." DIGIT

OWS = `<OWS, see [HTTP], Section 5.6.3>`

RWS = `<RWS, see [HTTP], Section 5.6.3>`

Transfer-Encoding = [ transfer-coding *( OWS "," OWS transfer-coding
) ]

absolute-URI = `<absolute-URI, see [URI], Section 4.3>`
absolute-form = absolute-URI
absolute-path = `<absolute-path, see [HTTP], Section 4.1>`
asterisk-form = "*"
authority = `<authority, see [URI], Section 3.2>`
authority-form = uri-host ":" port

chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
chunk-data = 1*OCTET
chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val
] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
chunk-size = 1*HEXDIG
chunked-body = *chunk last-chunk trailer-section CRLF

field-line = field-name ":" OWS field-value OWS
field-name = `<field-name, see [HTTP], Section 5.1>`
field-value = `<field-value, see [HTTP], Section 5.5>`

last-chunk = 1*"0" [ chunk-ext ] CRLF

message-body = *OCTET
method = token

obs-fold = OWS CRLF RWS
obs-text = `<obs-text, see [HTTP], Section 5.6.4>`
origin-form = absolute-path [ "?" query ]

port = `<port, see [URI], Section 3.2.3>`

query = `<query, see [URI], Section 3.4>`
quoted-string = `<quoted-string, see [HTTP], Section 5.6.4>`

reason-phrase = 1*( HTAB / SP / VCHAR / obs-text )
request-line = method SP request-target SP HTTP-version
request-target = origin-form / absolute-form / authority-form /
asterisk-form

start-line = request-line / status-line
status-code = 3DIGIT
status-line = HTTP-version SP status-code SP [ reason-phrase ]

token = `<token, see [HTTP], Section 5.6.2>`
trailer-section = *( field-line CRLF )
transfer-coding = `<transfer-coding, see [HTTP], Section 10.1.4>`

uri-host = `<host, see [URI], Section 3.2.2>`