6. 二进制格式 (Binary Format)
本节描述第 2.4 节中所有表示形式的详细二进制格式.
6.1. indexed header field representation
indexed header field representation 标识静态表或动态表中的一个条目 (见第 2.3 节).
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 1 | Index (7+) |
+---+---------------------------+
Figure 5: Indexed Header Field
indexed header field 以 '1' 这个 1 位模式开始, 后面跟随匹配 header field 的索引, 该索引用带有 7 位前缀的整数表示 (见第 5.1 节).
索引值 0 不被使用. 如果在 indexed header field representation 中发现该值, 必须 (MUST) 将其视为解码错误.
6.2. literal header field representation
literal header field representation 包含 literal header field 值. header field 名称可以作为字面量提供, 也可以通过引用静态表或动态表中的现有表条目来提供 (见第 2.3 节).
本规范定义三种 literal header field representation 形式: with incremental indexing, without indexing, 以及 never indexed.
6.2.1. literal header field with incremental indexing
literal header field with incremental indexing representation 会将一个 header field 追加到已解码的 header list, 并将其作为新条目插入 dynamic table.
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 1 | Index (6+) |
+---+---+-----------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 6: Literal Header Field with Incremental Indexing -- Indexed Name
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 1 | 0 |
+---+---+-----------------------+
| H | Name Length (7+) |
+---+---------------------------+
| Name String (Length octets) |
+---+---------------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 7: Literal Header Field with Incremental Indexing -- New Name
literal header field with incremental indexing representation 以 '01' 这个 2 位模式开始.
如果 header field 名称与 static table 或 dynamic table 中存储的某个条目的 header field 名称匹配, 则可以使用该条目的索引表示这个 header field 名称. 在这种情况下, 条目的索引用带有 6 位前缀的整数表示 (见第 5.1 节). 该值始终非零.
否则, header field 名称表示为字符串字面量 (见第 5.2 节). 使用值 0 代替 6 位索引, 后面跟随 header field 名称.
任一形式的 header field 名称表示之后, 都跟随表示为字符串字面量的 header field 值 (见第 5.2 节).
6.2.2. literal header field without indexing
literal header field without indexing representation 会将一个 header field 追加到已解码的 header list, 但不改变 dynamic table.
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | Index (4+) |
+---+---+-----------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 8: Literal Header Field without Indexing -- Indexed Name
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+-----------------------+
| H | Name Length (7+) |
+---+---------------------------+
| Name String (Length octets) |
+---+---------------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 9: Literal Header Field without Indexing -- New Name
literal header field without indexing representation 以 '0000' 这个 4 位模式开始.
如果 header field 名称与 static table 或 dynamic table 中存储的某个条目的 header field 名称匹配, 则可以使用该条目的索引表示这个 header field 名称. 在这种情况下, 条目的索引用带有 4 位前缀的整数表示 (见第 5.1 节). 该值始终非零.
否则, header field 名称表示为字符串字面量 (见第 5.2 节). 使用值 0 代替 4 位索引, 后面跟随 header field 名称.
任一形式的 header field 名称表示之后, 都跟随表示为字符串字面量的 header field 值 (见第 5.2 节).
6.2.3. literal header field never indexed
literal header field never indexed representation 会将一个 header field 追加到已解码的 header list, 但不改变 dynamic table. 中介必须 (MUST) 使用相同的表示形式来编码这个 header field.
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 1 | Index (4+) |
+---+---+-----------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 10: Literal Header Field Never Indexed -- Indexed Name
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 1 | 0 |
+---+---+-----------------------+
| H | Name Length (7+) |
+---+---------------------------+
| Name String (Length octets) |
+---+---------------------------+
| H | Value Length (7+) |
+---+---------------------------+
| Value String (Length octets) |
+-------------------------------+
Figure 11: Literal Header Field Never Indexed -- New Name
literal header field never indexed representation 以 '0001' 这个 4 位模式开始.
当某个 header field 表示为 literal header field never indexed 时, 它必须 (MUST) 始终使用这一特定的 literal representation 编码. 特别是, 当对等方发送一个它收到时已表示为 literal header field never indexed 的 header field 时, 它必须 (MUST) 使用相同的表示形式转发该 header field.
这种表示形式用于保护不应因压缩而承担风险的 header field 值 (更多细节见第 7.1 节).
该表示形式的编码与 literal header field without indexing 相同 (见第 6.2.2 节).
6.3. dynamic table size update
dynamic table size update 表示 dynamic table 大小发生变化.
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| 0 | 0 | 1 | Max size (5+) |
+---+---------------------------+
Figure 12: Maximum Dynamic Table Size Change
dynamic table size update 以 '001' 这个 3 位模式开始, 后面跟随新的最大大小, 该大小用带有 5 位前缀的整数表示 (见第 5.1 节).
新的最大大小必须 (MUST) 小于或等于使用 HPACK 的协议所确定的限制. 超过该限制的值必须 (MUST) 被视为解码错误. 在 HTTP/2 中, 该限制是编码器从解码器接收并确认的 SETTINGS_HEADER_TABLE_SIZE 参数的最后一个值 (见 [HTTP2] 的第 6.5.2 节和第 6.5.3 节).
减小 dynamic table 的最大大小可能导致条目被驱逐 (见第 4.3 节).