什么是 APNs 中的 HPACK 压缩

What is HPACK Compression in APNs

感谢您阅读我的问题。我阅读了许多关于 HTTP/2 的文档,并试图理解在 Java.

中构建 APNs server-side API 最重要的概念

APNs requires the use of HPACK (header compression for HTTP/2), which prevents repeated header keys and values. APNs maintains a small dynamic table for HPACK. To help avoid filling up the APNs HPACK table and necessitating the discarding of table data, encode headers in the following way—especially when sending a large number of streams:

如果我没理解错的话,header压缩需要使用霍夫曼编码。如果我在这方面有误,请指正。

The :path value should be encoded as a literal header field without indexing

The authorization request header, if present, should be encoded as a literal header field without indexing

我为此阅读了 RFC 7541,但不知道他们在说什么。请注意,我试图通过此 post 了解系统和要求以获取知识,而不仅仅是回答具体问题。

The appropriate encoding to employ for the apns-id, apns-expiration, and apns-collapse-id request headers differs depending on whether it is part of the initial or a subsequent POST operation, as follows:

The first time you send these headers, encode them with incremental indexing to allow the header names to be added to the dynamic table

Subsequent times you send these headers, encode them as literal header fields without indexing

当他们说“使用增量索引对它们进行编码以允许将 header 名称添加到动态 table”和“随后发送这些 headers,之后将它们编码为文字 header 字段而不进行索引”。我想理解两个文字 header 字段之一 with/out 索引将帮助我更好地理解这一点。

再次感谢您阅读问题,请帮助我!!

我认为你最好使用一个 Java 库来为你 HTTP/2。

Jetty HttpClient(免责声明,我是维护者)正是这样做的,请参阅 here

如果你想实现 HPACK 因为你想玩得开心,那么你必须花时间仔细阅读 RFC 7541。 作为实现它的起点,您可以阅读许多 Java HPACK 实现,从 Jetty's 到 Netty、Undertow 等

您提出的所有问题(例如,“什么是 'literal header field without indexing'”)在 RFC 各自的部分中有详细说明。

非常粗略地,HPACK 定义了一个将数字映射到字符串的映射table。 两个对等点保持此 table 同步,因此两个 table 始终包含相同的数据(静态)。

当一个节点发送 HPACK 块时,它会发送数字,以便接收节点可以使用数字访问 HPACK table 以获取字符串。

对于 new/custom headers(想想 cookie),发送方发送数字和字符串,以便接收方可以更新其 HPACK table。第一次没有压缩,但第二次发送相同的 header 时,发送方只发送数字,因为它知道另一个对等方已经映射了字符串,这很好HTTP 压缩 headers.