12c593315Sopenharmony_ciTutorial: HPACK API 22c593315Sopenharmony_ci=================== 32c593315Sopenharmony_ci 42c593315Sopenharmony_ciIn this tutorial, we describe basic use of nghttp2's HPACK API. We 52c593315Sopenharmony_cibriefly describe the APIs for deflating and inflating header fields. 62c593315Sopenharmony_ciThe full example of using these APIs, `deflate.c`_, is attached at the 72c593315Sopenharmony_ciend of this page. It also resides in the examples directory in the 82c593315Sopenharmony_ciarchive or repository. 92c593315Sopenharmony_ci 102c593315Sopenharmony_ciDeflating (encoding) headers 112c593315Sopenharmony_ci---------------------------- 122c593315Sopenharmony_ci 132c593315Sopenharmony_ciFirst we need to initialize a :type:`nghttp2_hd_deflater` object using 142c593315Sopenharmony_cithe `nghttp2_hd_deflate_new()` function:: 152c593315Sopenharmony_ci 162c593315Sopenharmony_ci int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr, 172c593315Sopenharmony_ci size_t deflate_hd_table_bufsize_max); 182c593315Sopenharmony_ci 192c593315Sopenharmony_ciThis function allocates a :type:`nghttp2_hd_deflater` object, 202c593315Sopenharmony_ciinitializes it, and assigns its pointer to ``*deflater_ptr``. The 212c593315Sopenharmony_ci*deflate_hd_table_bufsize_max* is the upper bound of header table size 222c593315Sopenharmony_cithe deflater will use. This will limit the memory usage by the 232c593315Sopenharmony_cideflater object for the dynamic header table. If in doubt, just 242c593315Sopenharmony_cispecify 4096 here, which is the default upper bound of dynamic header 252c593315Sopenharmony_citable buffer size. 262c593315Sopenharmony_ci 272c593315Sopenharmony_ciTo encode header fields, use the `nghttp2_hd_deflate_hd()` function:: 282c593315Sopenharmony_ci 292c593315Sopenharmony_ci ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, 302c593315Sopenharmony_ci uint8_t *buf, size_t buflen, 312c593315Sopenharmony_ci const nghttp2_nv *nva, size_t nvlen); 322c593315Sopenharmony_ci 332c593315Sopenharmony_ciThe *deflater* is the deflater object initialized by 342c593315Sopenharmony_ci`nghttp2_hd_deflate_new()` described above. The encoded byte string is 352c593315Sopenharmony_ciwritten to the buffer *buf*, which has length *buflen*. The *nva* is 362c593315Sopenharmony_cia pointer to an array of headers fields, each of type 372c593315Sopenharmony_ci:type:`nghttp2_nv`. *nvlen* is the number of header fields which 382c593315Sopenharmony_ci*nva* contains. 392c593315Sopenharmony_ci 402c593315Sopenharmony_ciIt is important to initialize and assign all members of 412c593315Sopenharmony_ci:type:`nghttp2_nv`. For security sensitive header fields (such as 422c593315Sopenharmony_cicookies), set the :macro:`NGHTTP2_NV_FLAG_NO_INDEX` flag in 432c593315Sopenharmony_ci:member:`nghttp2_nv.flags`. Setting this flag prevents recovery of 442c593315Sopenharmony_cisensitive header fields by compression based attacks: This is achieved 452c593315Sopenharmony_ciby not inserting the header field into the dynamic header table. 462c593315Sopenharmony_ci 472c593315Sopenharmony_ci`nghttp2_hd_deflate_hd()` processes all headers given in *nva*. The 482c593315Sopenharmony_ci*nva* must include all request or response header fields to be sent in 492c593315Sopenharmony_cione HEADERS (or optionally following (multiple) CONTINUATION 502c593315Sopenharmony_ciframe(s)). The *buf* must have enough space to store the encoded 512c593315Sopenharmony_ciresult, otherwise the function will fail. To estimate the upper bound 522c593315Sopenharmony_ciof the encoded result length, use `nghttp2_hd_deflate_bound()`:: 532c593315Sopenharmony_ci 542c593315Sopenharmony_ci size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater, 552c593315Sopenharmony_ci const nghttp2_nv *nva, size_t nvlen); 562c593315Sopenharmony_ci 572c593315Sopenharmony_ciPass this function the same parameters (*deflater*, *nva*, and 582c593315Sopenharmony_ci*nvlen*) which will be passed to `nghttp2_hd_deflate_hd()`. 592c593315Sopenharmony_ci 602c593315Sopenharmony_ciSubsequent calls to `nghttp2_hd_deflate_hd()` will use the current 612c593315Sopenharmony_ciencoder state and perform differential encoding, which yields HPAC's 622c593315Sopenharmony_cifundamental compression gain. 632c593315Sopenharmony_ci 642c593315Sopenharmony_ciIf `nghttp2_hd_deflate_hd()` fails, the failure is fatal and any 652c593315Sopenharmony_cifurther calls with the same deflater object will fail. Thus it's very 662c593315Sopenharmony_ciimportant to use `nghttp2_hd_deflate_bound()` to determine the 672c593315Sopenharmony_cirequired size of the output buffer. 682c593315Sopenharmony_ci 692c593315Sopenharmony_ciTo delete a :type:`nghttp2_hd_deflater` object, use the 702c593315Sopenharmony_ci`nghttp2_hd_deflate_del()` function. 712c593315Sopenharmony_ci 722c593315Sopenharmony_ciInflating (decoding) headers 732c593315Sopenharmony_ci---------------------------- 742c593315Sopenharmony_ci 752c593315Sopenharmony_ciA :type:`nghttp2_hd_inflater` object is used to inflate compressed 762c593315Sopenharmony_ciheader data. To initialize the object, use 772c593315Sopenharmony_ci`nghttp2_hd_inflate_new()`:: 782c593315Sopenharmony_ci 792c593315Sopenharmony_ci int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr); 802c593315Sopenharmony_ci 812c593315Sopenharmony_ciTo inflate header data, use `nghttp2_hd_inflate_hd2()`:: 822c593315Sopenharmony_ci 832c593315Sopenharmony_ci ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater, 842c593315Sopenharmony_ci nghttp2_nv *nv_out, int *inflate_flags, 852c593315Sopenharmony_ci const uint8_t *in, size_t inlen, 862c593315Sopenharmony_ci int in_final); 872c593315Sopenharmony_ci 882c593315Sopenharmony_ci`nghttp2_hd_inflate_hd2()` reads a stream of bytes and outputs a 892c593315Sopenharmony_cisingle header field at a time. Multiple calls are normally required to 902c593315Sopenharmony_ciread a full stream of bytes and output all of the header fields. 912c593315Sopenharmony_ci 922c593315Sopenharmony_ciThe *inflater* is the inflater object initialized above. The *nv_out* 932c593315Sopenharmony_ciis a pointer to a :type:`nghttp2_nv` into which one header field may 942c593315Sopenharmony_cibe stored. The *in* is a pointer to input data, and *inlen* is its 952c593315Sopenharmony_cilength. The caller is not required to specify the whole deflated 962c593315Sopenharmony_ciheader data via *in* at once: Instead it can call this function 972c593315Sopenharmony_cimultiple times as additional data bytes become available. If 982c593315Sopenharmony_ci*in_final* is nonzero, it tells the function that the passed data is 992c593315Sopenharmony_cithe final sequence of deflated header data. 1002c593315Sopenharmony_ci 1012c593315Sopenharmony_ciThe *inflate_flags* is an output parameter; on success the function 1022c593315Sopenharmony_cisets it to a bitset of flags. It will be described later. 1032c593315Sopenharmony_ci 1042c593315Sopenharmony_ciThis function returns when each header field is inflated. When this 1052c593315Sopenharmony_cihappens, the function sets the :macro:`NGHTTP2_HD_INFLATE_EMIT` flag 1062c593315Sopenharmony_ciin *inflate_flags*, and a header field is stored in *nv_out*. The 1072c593315Sopenharmony_cireturn value indicates the number of bytes read from *in* processed so 1082c593315Sopenharmony_cifar, which may be less than *inlen*. The caller should call the 1092c593315Sopenharmony_cifunction repeatedly until all bytes are processed. Processed bytes 1102c593315Sopenharmony_cishould be removed from *in*, and *inlen* should be adjusted 1112c593315Sopenharmony_ciappropriately. 1122c593315Sopenharmony_ci 1132c593315Sopenharmony_ciIf *in_final* is nonzero and all given data was processed, the 1142c593315Sopenharmony_cifunction sets the :macro:`NGHTTP2_HD_INFLATE_FINAL` flag in 1152c593315Sopenharmony_ci*inflate_flags*. When you see this flag set, call the 1162c593315Sopenharmony_ci`nghttp2_hd_inflate_end_headers()` function. 1172c593315Sopenharmony_ci 1182c593315Sopenharmony_ciIf *in_final* is zero and the :macro:`NGHTTP2_HD_INFLATE_EMIT` flag is 1192c593315Sopenharmony_cinot set, it indicates that all given data was processed. The caller 1202c593315Sopenharmony_ciis required to pass additional data. 1212c593315Sopenharmony_ci 1222c593315Sopenharmony_ciExample usage of `nghttp2_hd_inflate_hd2()` is shown in the 1232c593315Sopenharmony_ci`inflate_header_block()` function in `deflate.c`_. 1242c593315Sopenharmony_ci 1252c593315Sopenharmony_ciFinally, to delete a :type:`nghttp2_hd_inflater` object, use 1262c593315Sopenharmony_ci`nghttp2_hd_inflate_del()`. 127