nghttp3_qpack.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2013 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef NGHTTP3_QPACK_H
  27. #define NGHTTP3_QPACK_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <nghttp3/nghttp3.h>
  32. #include "nghttp3_rcbuf.h"
  33. #include "nghttp3_map.h"
  34. #include "nghttp3_pq.h"
  35. #include "nghttp3_ringbuf.h"
  36. #include "nghttp3_buf.h"
  37. #include "nghttp3_ksl.h"
  38. #include "nghttp3_qpack_huffman.h"
  39. #define NGHTTP3_QPACK_INT_MAX ((1ull << 62) - 1)
  40. /* NGHTTP3_QPACK_MAX_NAMELEN is the maximum (compressed) length of
  41. header name this library can decode. */
  42. #define NGHTTP3_QPACK_MAX_NAMELEN 256
  43. /* NGHTTP3_QPACK_MAX_VALUELEN is the maximum (compressed) length of
  44. header value this library can decode. */
  45. #define NGHTTP3_QPACK_MAX_VALUELEN 65536
  46. /* NGHTTP3_QPACK_MAX_ENCODERLEN is the maximum encoder stream length
  47. that a decoder accepts without completely processing a single field
  48. section. */
  49. #define NGHTTP3_QPACK_MAX_ENCODERLEN (128 * 1024)
  50. /* NGHTTP3_QPACK_MAX_DECODERLEN is the maximum decoder stream length
  51. that an encoder accepts without completely encoding a single field
  52. section. */
  53. #define NGHTTP3_QPACK_MAX_DECODERLEN (4 * 1024)
  54. /* nghttp3_qpack_indexing_mode is a indexing strategy. */
  55. typedef enum nghttp3_qpack_indexing_mode {
  56. /* NGHTTP3_QPACK_INDEXING_MODE_LITERAL means that header field
  57. should not be inserted into dynamic table. */
  58. NGHTTP3_QPACK_INDEXING_MODE_LITERAL,
  59. /* NGHTTP3_QPACK_INDEXING_MODE_STORE means that header field can be
  60. inserted into dynamic table. */
  61. NGHTTP3_QPACK_INDEXING_MODE_STORE,
  62. /* NGHTTP3_QPACK_INDEXING_MODE_NEVER means that header field should
  63. not be inserted into dynamic table and this must be true for all
  64. forwarding paths. */
  65. NGHTTP3_QPACK_INDEXING_MODE_NEVER,
  66. } nghttp3_qpack_indexing_mode;
  67. typedef struct nghttp3_qpack_entry nghttp3_qpack_entry;
  68. struct nghttp3_qpack_entry {
  69. /* The header field name/value pair */
  70. nghttp3_qpack_nv nv;
  71. /* map_next points to the entry which shares same bucket in hash
  72. table. */
  73. nghttp3_qpack_entry *map_next;
  74. /* sum is the sum of all entries inserted up to this entry. This
  75. value does not contain the space required for this entry. */
  76. size_t sum;
  77. /* absidx is the absolute index of this entry. */
  78. uint64_t absidx;
  79. /* The hash value for header name (nv.name). */
  80. uint32_t hash;
  81. };
  82. /* The entry used for static table. */
  83. typedef struct nghttp3_qpack_static_entry {
  84. uint64_t absidx;
  85. int32_t token;
  86. uint32_t hash;
  87. } nghttp3_qpack_static_entry;
  88. typedef struct nghttp3_qpack_static_header {
  89. nghttp3_rcbuf name;
  90. nghttp3_rcbuf value;
  91. int32_t token;
  92. } nghttp3_qpack_static_header;
  93. /*
  94. * nghttp3_qpack_header_block_ref is created per encoded header block
  95. * and includes the required insert count and the minimum insert count
  96. * of dynamic table entry it refers to.
  97. */
  98. typedef struct nghttp3_qpack_header_block_ref {
  99. nghttp3_pq_entry max_cnts_pe;
  100. nghttp3_pq_entry min_cnts_pe;
  101. /* max_cnt is the required insert count. */
  102. uint64_t max_cnt;
  103. /* min_cnt is the minimum insert count of dynamic table entry it
  104. refers to. In other words, this is the minimum absolute index of
  105. dynamic header table entry this encoded block refers to plus
  106. 1. */
  107. uint64_t min_cnt;
  108. } nghttp3_qpack_header_block_ref;
  109. int nghttp3_qpack_header_block_ref_new(nghttp3_qpack_header_block_ref **pref,
  110. uint64_t max_cnt, uint64_t min_cnt,
  111. const nghttp3_mem *mem);
  112. void nghttp3_qpack_header_block_ref_del(nghttp3_qpack_header_block_ref *ref,
  113. const nghttp3_mem *mem);
  114. typedef struct nghttp3_qpack_stream {
  115. int64_t stream_id;
  116. /* refs is an array of pointer to nghttp3_qpack_header_block_ref in
  117. the order of the time they are encoded. HTTP/3 allows multiple
  118. header blocks (e.g., non-final response headers, final response
  119. headers, trailers, and push promises) per stream. */
  120. nghttp3_ringbuf refs;
  121. /* max_cnts is a priority queue sorted by descending order of
  122. max_cnt of nghttp3_qpack_header_block_ref. */
  123. nghttp3_pq max_cnts;
  124. } nghttp3_qpack_stream;
  125. int nghttp3_qpack_stream_new(nghttp3_qpack_stream **pstream, int64_t stream_id,
  126. const nghttp3_mem *mem);
  127. void nghttp3_qpack_stream_del(nghttp3_qpack_stream *stream,
  128. const nghttp3_mem *mem);
  129. uint64_t nghttp3_qpack_stream_get_max_cnt(const nghttp3_qpack_stream *stream);
  130. int nghttp3_qpack_stream_add_ref(nghttp3_qpack_stream *stream,
  131. nghttp3_qpack_header_block_ref *ref);
  132. void nghttp3_qpack_stream_pop_ref(nghttp3_qpack_stream *stream);
  133. #define NGHTTP3_QPACK_ENTRY_OVERHEAD 32
  134. typedef struct nghttp3_qpack_context {
  135. /* dtable is a dynamic table */
  136. nghttp3_ringbuf dtable;
  137. /* mem is memory allocator */
  138. const nghttp3_mem *mem;
  139. /* dtable_size is abstracted buffer size of dtable as described in
  140. the spec. This is the sum of length of name/value in dtable +
  141. NGHTTP3_QPACK_ENTRY_OVERHEAD bytes overhead per each entry. */
  142. size_t dtable_size;
  143. size_t dtable_sum;
  144. /* hard_max_dtable_capacity is the upper bound of
  145. max_dtable_capacity. */
  146. size_t hard_max_dtable_capacity;
  147. /* max_dtable_capacity is the maximum capacity of the dynamic
  148. table. */
  149. size_t max_dtable_capacity;
  150. /* max_blocked_streams is the maximum number of stream which can be
  151. blocked. */
  152. size_t max_blocked_streams;
  153. /* next_absidx is the next absolute index for nghttp3_qpack_entry.
  154. It is equivalent to insert count. */
  155. uint64_t next_absidx;
  156. /* If inflate/deflate error occurred, this value is set to 1 and
  157. further invocation of inflate/deflate will fail with
  158. NGHTTP3_ERR_QPACK_FATAL. */
  159. uint8_t bad;
  160. } nghttp3_qpack_context;
  161. typedef struct nghttp3_qpack_read_state {
  162. nghttp3_qpack_huffman_decode_context huffman_ctx;
  163. nghttp3_buf namebuf;
  164. nghttp3_buf valuebuf;
  165. nghttp3_rcbuf *name;
  166. nghttp3_rcbuf *value;
  167. uint64_t left;
  168. size_t prefix;
  169. size_t shift;
  170. uint64_t absidx;
  171. int never;
  172. int dynamic;
  173. int huffman_encoded;
  174. } nghttp3_qpack_read_state;
  175. void nghttp3_qpack_read_state_free(nghttp3_qpack_read_state *rstate);
  176. void nghttp3_qpack_read_state_reset(nghttp3_qpack_read_state *rstate);
  177. #define NGHTTP3_QPACK_MAP_SIZE 64
  178. typedef struct nghttp3_qpack_map {
  179. nghttp3_qpack_entry *table[NGHTTP3_QPACK_MAP_SIZE];
  180. } nghttp3_qpack_map;
  181. /* nghttp3_qpack_decoder_stream_state is a set of states when decoding
  182. decoder stream. */
  183. typedef enum nghttp3_qpack_decoder_stream_state {
  184. NGHTTP3_QPACK_DS_STATE_OPCODE,
  185. NGHTTP3_QPACK_DS_STATE_READ_NUMBER,
  186. } nghttp3_qpack_decoder_stream_state;
  187. /* nghttp3_qpack_decoder_stream_opcode is opcode used in decoder
  188. stream. */
  189. typedef enum nghttp3_qpack_decoder_stream_opcode {
  190. NGHTTP3_QPACK_DS_OPCODE_ICNT_INCREMENT,
  191. NGHTTP3_QPACK_DS_OPCODE_SECTION_ACK,
  192. NGHTTP3_QPACK_DS_OPCODE_STREAM_CANCEL,
  193. } nghttp3_qpack_decoder_stream_opcode;
  194. /* QPACK encoder flags */
  195. /* NGHTTP3_QPACK_ENCODER_FLAG_NONE indicates that no flag is set. */
  196. #define NGHTTP3_QPACK_ENCODER_FLAG_NONE 0x00u
  197. /* NGHTTP3_QPACK_ENCODER_FLAG_PENDING_SET_DTABLE_CAP indicates that
  198. Set Dynamic Table Capacity is required. */
  199. #define NGHTTP3_QPACK_ENCODER_FLAG_PENDING_SET_DTABLE_CAP 0x01u
  200. struct nghttp3_qpack_encoder {
  201. nghttp3_qpack_context ctx;
  202. /* dtable_map is a map of hash to nghttp3_qpack_entry to provide
  203. fast access to an entry in dynamic table. */
  204. nghttp3_qpack_map dtable_map;
  205. /* streams is a map of stream ID to nghttp3_qpack_stream to keep
  206. track of unacknowledged streams. */
  207. nghttp3_map streams;
  208. /* blocked_streams is an ordered list of nghttp3_qpack_stream, in
  209. descending order of max_cnt, to search the unblocked streams by
  210. received known count. */
  211. nghttp3_ksl blocked_streams;
  212. /* min_cnts is a priority queue of nghttp3_qpack_header_block_ref
  213. sorted by ascending order of min_cnt to know that an entry can be
  214. evicted from dynamic table. */
  215. nghttp3_pq min_cnts;
  216. /* krcnt is Known Received Count. */
  217. uint64_t krcnt;
  218. /* state is a current state of reading decoder stream. */
  219. nghttp3_qpack_decoder_stream_state state;
  220. /* opcode is a decoder stream opcode being processed. */
  221. nghttp3_qpack_decoder_stream_opcode opcode;
  222. /* rstate is a set of intermediate state which are used to process
  223. decoder stream. */
  224. nghttp3_qpack_read_state rstate;
  225. /* min_dtable_update is the minimum dynamic table size required. */
  226. size_t min_dtable_update;
  227. /* last_max_dtable_update is the dynamic table size last
  228. requested. */
  229. size_t last_max_dtable_update;
  230. /* uninterrupted_decoderlen is the number of bytes read from decoder
  231. stream without encoding a single field section. */
  232. size_t uninterrupted_decoderlen;
  233. /* flags is bitwise OR of zero or more of
  234. NGHTTP3_QPACK_ENCODER_FLAG_*. */
  235. uint8_t flags;
  236. };
  237. /*
  238. * nghttp3_qpack_encoder_init initializes |encoder|.
  239. * |hard_max_dtable_capacity| is the upper bound of the dynamic table
  240. * capacity. |mem| is a memory allocator.
  241. *
  242. * This function returns 0 if it succeeds, or one of the following
  243. * negative error codes:
  244. *
  245. * NGHTTP3_ERR_NOMEM
  246. * Out of memory.
  247. */
  248. int nghttp3_qpack_encoder_init(nghttp3_qpack_encoder *encoder,
  249. size_t hard_max_dtable_capacity,
  250. const nghttp3_mem *mem);
  251. /*
  252. * nghttp3_qpack_encoder_free frees memory allocated for |encoder|.
  253. * This function does not free memory pointed by |encoder|.
  254. */
  255. void nghttp3_qpack_encoder_free(nghttp3_qpack_encoder *encoder);
  256. /*
  257. * nghttp3_qpack_encoder_encode_nv encodes |nv|. It writes request
  258. * stream into |rbuf| and writes encoder stream into |ebuf|. |nv| is
  259. * a header field to encode. |base| is base. |allow_blocking| is
  260. * nonzero if this stream can be blocked (or it has been blocked
  261. * already).
  262. *
  263. * This function returns 0 if it succeeds, or one of the following
  264. * negative error codes:
  265. *
  266. * NGHTTP3_ERR_NOMEM
  267. * Out of memory.
  268. */
  269. int nghttp3_qpack_encoder_encode_nv(nghttp3_qpack_encoder *encoder,
  270. uint64_t *pmax_cnt, uint64_t *pmin_cnt,
  271. nghttp3_buf *rbuf, nghttp3_buf *ebuf,
  272. const nghttp3_nv *nv, uint64_t base,
  273. int allow_blocking);
  274. /* nghttp3_qpack_lookup_result stores a result of table lookup. */
  275. typedef struct nghttp3_qpack_lookup_result {
  276. /* index is an index of matched entry. -1 if no match is made. */
  277. nghttp3_ssize index;
  278. /* name_value_match is nonzero if both name and value are
  279. matched. */
  280. int name_value_match;
  281. /* pb_index is the absolute index of matched post-based dynamic
  282. table entry. -1 if no such entry exists. */
  283. nghttp3_ssize pb_index;
  284. } nghttp3_qpack_lookup_result;
  285. /*
  286. * nghttp3_qpack_lookup_stable searches |nv| in static table. |token|
  287. * is a token of nv->name and it is -1 if there is no corresponding
  288. * token defined. |indexing_mode| provides indexing strategy.
  289. */
  290. nghttp3_qpack_lookup_result
  291. nghttp3_qpack_lookup_stable(const nghttp3_nv *nv, int32_t token,
  292. nghttp3_qpack_indexing_mode indexing_mode);
  293. /*
  294. * nghttp3_qpack_encoder_lookup_dtable searches |nv| in dynamic table.
  295. * |token| is a token of nv->name and it is -1 if there is no
  296. * corresponding token defined. |hash| is a hash of nv->name.
  297. * |indexing_mode| provides indexing strategy. |krcnt| is Known
  298. * Received Count. |allow_blocking| is nonzero if this stream can be
  299. * blocked (or it has been blocked already).
  300. */
  301. nghttp3_qpack_lookup_result nghttp3_qpack_encoder_lookup_dtable(
  302. nghttp3_qpack_encoder *encoder, const nghttp3_nv *nv, int32_t token,
  303. uint32_t hash, nghttp3_qpack_indexing_mode indexing_mode, uint64_t krcnt,
  304. int allow_blocking);
  305. /*
  306. * nghttp3_qpack_encoder_write_field_section_prefix writes Encoded
  307. * Field Section Prefix into |pbuf|. |ricnt| is Required Insert
  308. * Count. |base| is Base.
  309. *
  310. * This function returns 0 if it succeeds, or one of the following
  311. * negative error codes:
  312. *
  313. * NGHTTP3_ERR_NOMEM
  314. * Out of memory.
  315. */
  316. int nghttp3_qpack_encoder_write_field_section_prefix(
  317. nghttp3_qpack_encoder *encoder, nghttp3_buf *pbuf, uint64_t ricnt,
  318. uint64_t base);
  319. /*
  320. * nghttp3_qpack_encoder_write_static_indexed writes Indexed Header
  321. * Field to |rbuf|. |absidx| is an absolute index into static table.
  322. *
  323. * This function returns 0 if it succeeds, or one of the following
  324. * negative error codes:
  325. *
  326. * NGHTTP3_ERR_NOMEM
  327. * Out of memory.
  328. */
  329. int nghttp3_qpack_encoder_write_static_indexed(nghttp3_qpack_encoder *encoder,
  330. nghttp3_buf *rbuf,
  331. uint64_t absidx);
  332. /*
  333. * nghttp3_qpack_encoder_write_dynamic_indexed writes Indexed Header
  334. * Field to |rbuf|. |absidx| is an absolute index into dynamic table.
  335. * |base| is base.
  336. *
  337. * This function returns 0 if it succeeds, or one of the following
  338. * negative error codes:
  339. *
  340. * NGHTTP3_ERR_NOMEM
  341. * Out of memory.
  342. */
  343. int nghttp3_qpack_encoder_write_dynamic_indexed(nghttp3_qpack_encoder *encoder,
  344. nghttp3_buf *rbuf,
  345. uint64_t absidx, uint64_t base);
  346. /*
  347. * nghttp3_qpack_encoder_write_static_indexed writes Literal Header
  348. * Field With Name Reference to |rbuf|. |absidx| is an absolute index
  349. * into static table to reference a name. |nv| is a header field to
  350. * encode.
  351. *
  352. * This function returns 0 if it succeeds, or one of the following
  353. * negative error codes:
  354. *
  355. * NGHTTP3_ERR_NOMEM
  356. * Out of memory.
  357. */
  358. int nghttp3_qpack_encoder_write_static_indexed_name(
  359. nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx,
  360. const nghttp3_nv *nv);
  361. /*
  362. * nghttp3_qpack_encoder_write_dynamic_indexed writes Literal Header
  363. * Field With Name Reference to |rbuf|. |absidx| is an absolute index
  364. * into dynamic table to reference a name. |base| is a base. |nv| is
  365. * a header field to encode.
  366. *
  367. * This function returns 0 if it succeeds, or one of the following
  368. * negative error codes:
  369. *
  370. * NGHTTP3_ERR_NOMEM
  371. * Out of memory.
  372. */
  373. int nghttp3_qpack_encoder_write_dynamic_indexed_name(
  374. nghttp3_qpack_encoder *encoder, nghttp3_buf *rbuf, uint64_t absidx,
  375. uint64_t base, const nghttp3_nv *nv);
  376. /*
  377. * nghttp3_qpack_encoder_write_literal writes Literal Header Field
  378. * With Literal Name to |rbuf|. |nv| is a header field to encode.
  379. *
  380. * This function returns 0 if it succeeds, or one of the following
  381. * negative error codes:
  382. *
  383. * NGHTTP3_ERR_NOMEM
  384. * Out of memory.
  385. */
  386. int nghttp3_qpack_encoder_write_literal(nghttp3_qpack_encoder *encoder,
  387. nghttp3_buf *rbuf,
  388. const nghttp3_nv *nv);
  389. /*
  390. * nghttp3_qpack_encoder_write_static_insert writes Insert With Name
  391. * Reference to |ebuf|. |absidx| is an absolute index into static
  392. * table to reference a name. |nv| is a header field to insert.
  393. *
  394. * This function returns 0 if it succeeds, or one of the following
  395. * negative error codes:
  396. *
  397. * NGHTTP3_ERR_NOMEM
  398. * Out of memory.
  399. */
  400. int nghttp3_qpack_encoder_write_static_insert(nghttp3_qpack_encoder *encoder,
  401. nghttp3_buf *ebuf,
  402. uint64_t absidx,
  403. const nghttp3_nv *nv);
  404. /*
  405. * nghttp3_qpack_encoder_write_dynamic_insert writes Insert With Name
  406. * Reference to |ebuf|. |absidx| is an absolute index into dynamic
  407. * table to reference a name. |nv| is a header field to insert.
  408. *
  409. * This function returns 0 if it succeeds, or one of the following
  410. * negative error codes:
  411. *
  412. * NGHTTP3_ERR_NOMEM
  413. * Out of memory.
  414. */
  415. int nghttp3_qpack_encoder_write_dynamic_insert(nghttp3_qpack_encoder *encoder,
  416. nghttp3_buf *ebuf,
  417. uint64_t absidx,
  418. const nghttp3_nv *nv);
  419. /*
  420. * nghttp3_qpack_encoder_write_duplicate_insert writes Duplicate to
  421. * |ebuf|. |absidx| is an absolute index into dynamic table to
  422. * reference an entry.
  423. *
  424. * This function returns 0 if it succeeds, or one of the following
  425. * negative error codes:
  426. *
  427. * NGHTTP3_ERR_NOMEM
  428. * Out of memory.
  429. */
  430. int nghttp3_qpack_encoder_write_duplicate_insert(nghttp3_qpack_encoder *encoder,
  431. nghttp3_buf *ebuf,
  432. uint64_t absidx);
  433. /*
  434. * nghttp3_qpack_encoder_write_literal_insert writes Insert With
  435. * Literal Name to |ebuf|. |nv| is a header field to insert.
  436. *
  437. * This function returns 0 if it succeeds, or one of the following
  438. * negative error codes:
  439. *
  440. * NGHTTP3_ERR_NOMEM
  441. * Out of memory.
  442. */
  443. int nghttp3_qpack_encoder_write_literal_insert(nghttp3_qpack_encoder *encoder,
  444. nghttp3_buf *ebuf,
  445. const nghttp3_nv *nv);
  446. int nghttp3_qpack_encoder_stream_is_blocked(nghttp3_qpack_encoder *encoder,
  447. nghttp3_qpack_stream *stream);
  448. /*
  449. * nghttp3_qpack_encoder_block_stream blocks |stream|.
  450. *
  451. * This function returns 0 if it succeeds, or one of the following
  452. * negative error codes:
  453. *
  454. * NGHTTP3_ERR_NOMEM
  455. * Out of memory.
  456. */
  457. int nghttp3_qpack_encoder_block_stream(nghttp3_qpack_encoder *encoder,
  458. nghttp3_qpack_stream *stream);
  459. /*
  460. * nghttp3_qpack_encoder_unblock_stream unblocks |stream|.
  461. */
  462. void nghttp3_qpack_encoder_unblock_stream(nghttp3_qpack_encoder *encoder,
  463. nghttp3_qpack_stream *stream);
  464. /*
  465. * nghttp3_qpack_encoder_unblock unblocks stream whose max_cnt is less
  466. * than or equal to |max_cnt|.
  467. */
  468. void nghttp3_qpack_encoder_unblock(nghttp3_qpack_encoder *encoder,
  469. uint64_t max_cnt);
  470. /*
  471. * nghttp3_qpack_encoder_find_stream returns stream whose stream ID is
  472. * |stream_id|. This function returns NULL if there is no such
  473. * stream.
  474. */
  475. nghttp3_qpack_stream *
  476. nghttp3_qpack_encoder_find_stream(nghttp3_qpack_encoder *encoder,
  477. int64_t stream_id);
  478. uint64_t nghttp3_qpack_encoder_get_min_cnt(nghttp3_qpack_encoder *encoder);
  479. /*
  480. * nghttp3_qpack_encoder_shrink_dtable shrinks dynamic table so that
  481. * the dynamic table size is less than or equal to maximum size.
  482. */
  483. void nghttp3_qpack_encoder_shrink_dtable(nghttp3_qpack_encoder *encoder);
  484. /*
  485. * nghttp3_qpack_encoder_process_dtable_update processes pending
  486. * dynamic table size update. It might write encoder stream into
  487. * |ebuf|.
  488. *
  489. * This function returns 0 if it succeeds, or one of the following
  490. * negative error codes:
  491. *
  492. * NGHTTP3_ERR_NOMEM
  493. * Out of memory.
  494. */
  495. int nghttp3_qpack_encoder_process_dtable_update(nghttp3_qpack_encoder *encoder,
  496. nghttp3_buf *ebuf);
  497. /*
  498. * nghttp3_qpack_encoder_write_set_dtable_cap writes Set Dynamic Table
  499. * Capacity. to |ebuf|. |cap| is the capacity of dynamic table.
  500. *
  501. * This function returns 0 if it succeeds, or one of the following
  502. * negative error codes:
  503. *
  504. * NGHTTP3_ERR_NOMEM
  505. * Out of memory.
  506. */
  507. int nghttp3_qpack_encoder_write_set_dtable_cap(nghttp3_qpack_encoder *encoder,
  508. nghttp3_buf *ebuf, size_t cap);
  509. /*
  510. * nghttp3_qpack_context_dtable_add adds |qnv| to dynamic table. If
  511. * |ctx| is a part of encoder, |dtable_map| is not NULL. |hash| is a
  512. * hash value of name.
  513. *
  514. * This function returns 0 if it succeeds, or one of the following
  515. * negative error codes:
  516. *
  517. * NGHTTP3_ERR_NOMEM
  518. * Out of memory.
  519. */
  520. int nghttp3_qpack_context_dtable_add(nghttp3_qpack_context *ctx,
  521. nghttp3_qpack_nv *qnv,
  522. nghttp3_qpack_map *dtable_map,
  523. uint32_t hash);
  524. /*
  525. * nghttp3_qpack_encoder_dtable_static_add adds |nv| to dynamic table
  526. * by referencing static table entry at an absolute index |absidx|.
  527. * The hash of name is given as |hash|.
  528. *
  529. * This function returns 0 if it succeeds, or one of the following
  530. * negative error codes:
  531. *
  532. * NGHTTP3_ERR_NOMEM
  533. * Out of memory.
  534. */
  535. int nghttp3_qpack_encoder_dtable_static_add(nghttp3_qpack_encoder *encoder,
  536. uint64_t absidx,
  537. const nghttp3_nv *nv,
  538. uint32_t hash);
  539. /*
  540. * nghttp3_qpack_encoder_dtable_dynamic_add adds |nv| to dynamic table
  541. * by referencing dynamic table entry at an absolute index |absidx|.
  542. * The hash of name is given as |hash|.
  543. *
  544. * This function returns 0 if it succeeds, or one of the following
  545. * negative error codes:
  546. *
  547. * NGHTTP3_ERR_NOMEM
  548. * Out of memory.
  549. */
  550. int nghttp3_qpack_encoder_dtable_dynamic_add(nghttp3_qpack_encoder *encoder,
  551. uint64_t absidx,
  552. const nghttp3_nv *nv,
  553. uint32_t hash);
  554. /*
  555. * nghttp3_qpack_encoder_dtable_duplicate_add duplicates dynamic table
  556. * entry at an absolute index |absidx|.
  557. *
  558. * This function returns 0 if it succeeds, or one of the following
  559. * negative error codes:
  560. *
  561. * NGHTTP3_ERR_NOMEM
  562. * Out of memory.
  563. */
  564. int nghttp3_qpack_encoder_dtable_duplicate_add(nghttp3_qpack_encoder *encoder,
  565. uint64_t absidx);
  566. /*
  567. * nghttp3_qpack_encoder_dtable_literal_add adds |nv| to dynamic
  568. * table. |token| is a token of name and is -1 if it has no token
  569. * value defined. |hash| is a hash of name.
  570. *
  571. * NGHTTP3_ERR_NOMEM Out of memory.
  572. */
  573. int nghttp3_qpack_encoder_dtable_literal_add(nghttp3_qpack_encoder *encoder,
  574. const nghttp3_nv *nv,
  575. int32_t token, uint32_t hash);
  576. /*
  577. * `nghttp3_qpack_encoder_ack_header` tells |encoder| that header
  578. * block for a stream denoted by |stream_id| was acknowledged by
  579. * decoder.
  580. *
  581. * This function returns 0 if it succeeds, or one of the following
  582. * negative error codes:
  583. *
  584. * :macro:`NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR`
  585. * Section Acknowledgement for a stream denoted by |stream_id| is
  586. * unexpected.
  587. */
  588. int nghttp3_qpack_encoder_ack_header(nghttp3_qpack_encoder *encoder,
  589. int64_t stream_id);
  590. /*
  591. * `nghttp3_qpack_encoder_add_icnt` increments known received count of
  592. * |encoder| by |n|.
  593. *
  594. * This function returns 0 if it succeeds, or one of the following
  595. * negative error codes:
  596. *
  597. * :macro:`NGHTTP3_ERR_NOMEM`
  598. * Out of memory.
  599. * :macro:`NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR`
  600. * |n| is too large.
  601. */
  602. int nghttp3_qpack_encoder_add_icnt(nghttp3_qpack_encoder *encoder, uint64_t n);
  603. /*
  604. * `nghttp3_qpack_encoder_cancel_stream` tells |encoder| that stream
  605. * denoted by |stream_id| is cancelled. This function is provided for
  606. * debugging purpose only. In HTTP/3, |encoder| knows this by reading
  607. * decoder stream with `nghttp3_qpack_encoder_read_decoder()`.
  608. */
  609. void nghttp3_qpack_encoder_cancel_stream(nghttp3_qpack_encoder *encoder,
  610. int64_t stream_id);
  611. /*
  612. * nghttp3_qpack_context_dtable_get returns dynamic table entry whose
  613. * absolute index is |absidx|. This function assumes that such entry
  614. * exists.
  615. */
  616. nghttp3_qpack_entry *
  617. nghttp3_qpack_context_dtable_get(nghttp3_qpack_context *ctx, uint64_t absidx);
  618. /*
  619. * nghttp3_qpack_context_dtable_top returns latest dynamic table
  620. * entry. This function assumes dynamic table is not empty.
  621. */
  622. nghttp3_qpack_entry *
  623. nghttp3_qpack_context_dtable_top(nghttp3_qpack_context *ctx);
  624. /*
  625. * nghttp3_qpack_entry_init initializes |ent|. |qnv| is a header
  626. * field. |sum| is the sum of table space occupied by all entries
  627. * inserted so far. It does not include this entry. |absidx| is an
  628. * absolute index of this entry. |hash| is a hash of header field
  629. * name. This function increases reference count of qnv->nv.name and
  630. * qnv->nv.value.
  631. */
  632. void nghttp3_qpack_entry_init(nghttp3_qpack_entry *ent, nghttp3_qpack_nv *qnv,
  633. size_t sum, uint64_t absidx, uint32_t hash);
  634. /*
  635. * nghttp3_qpack_entry_free frees memory allocated for |ent|.
  636. */
  637. void nghttp3_qpack_entry_free(nghttp3_qpack_entry *ent);
  638. /*
  639. * nghttp3_qpack_put_varint_len returns the required number of bytes
  640. * to encode |n| with |prefix| bits.
  641. */
  642. size_t nghttp3_qpack_put_varint_len(uint64_t n, size_t prefix);
  643. /*
  644. * nghttp3_qpack_put_varint encodes |n| using variable integer
  645. * encoding with |prefix| bits into |buf|. This function assumes the
  646. * buffer pointed by |buf| has enough space. This function returns
  647. * the one byte beyond the last write (buf +
  648. * nghttp3_qpack_put_varint_len(n, prefix)).
  649. */
  650. uint8_t *nghttp3_qpack_put_varint(uint8_t *buf, uint64_t n, size_t prefix);
  651. /* nghttp3_qpack_encoder_stream_state is a set of states for encoder
  652. stream decoding. */
  653. typedef enum nghttp3_qpack_encoder_stream_state {
  654. NGHTTP3_QPACK_ES_STATE_OPCODE,
  655. NGHTTP3_QPACK_ES_STATE_READ_INDEX,
  656. NGHTTP3_QPACK_ES_STATE_CHECK_NAME_HUFFMAN,
  657. NGHTTP3_QPACK_ES_STATE_READ_NAMELEN,
  658. NGHTTP3_QPACK_ES_STATE_READ_NAME_HUFFMAN,
  659. NGHTTP3_QPACK_ES_STATE_READ_NAME,
  660. NGHTTP3_QPACK_ES_STATE_CHECK_VALUE_HUFFMAN,
  661. NGHTTP3_QPACK_ES_STATE_READ_VALUELEN,
  662. NGHTTP3_QPACK_ES_STATE_READ_VALUE_HUFFMAN,
  663. NGHTTP3_QPACK_ES_STATE_READ_VALUE,
  664. } nghttp3_qpack_encoder_stream_state;
  665. /* nghttp3_qpack_encoder_stream_opcode is a set of opcodes used in
  666. encoder stream. */
  667. typedef enum nghttp3_qpack_encoder_stream_opcode {
  668. NGHTTP3_QPACK_ES_OPCODE_INSERT_INDEXED,
  669. NGHTTP3_QPACK_ES_OPCODE_INSERT,
  670. NGHTTP3_QPACK_ES_OPCODE_DUPLICATE,
  671. NGHTTP3_QPACK_ES_OPCODE_SET_DTABLE_CAP,
  672. } nghttp3_qpack_encoder_stream_opcode;
  673. /* nghttp3_qpack_request_stream_state is a set of states for request
  674. stream decoding. */
  675. typedef enum nghttp3_qpack_request_stream_state {
  676. NGHTTP3_QPACK_RS_STATE_RICNT,
  677. NGHTTP3_QPACK_RS_STATE_DBASE_SIGN,
  678. NGHTTP3_QPACK_RS_STATE_DBASE,
  679. NGHTTP3_QPACK_RS_STATE_OPCODE,
  680. NGHTTP3_QPACK_RS_STATE_READ_INDEX,
  681. NGHTTP3_QPACK_RS_STATE_CHECK_NAME_HUFFMAN,
  682. NGHTTP3_QPACK_RS_STATE_READ_NAMELEN,
  683. NGHTTP3_QPACK_RS_STATE_READ_NAME_HUFFMAN,
  684. NGHTTP3_QPACK_RS_STATE_READ_NAME,
  685. NGHTTP3_QPACK_RS_STATE_CHECK_VALUE_HUFFMAN,
  686. NGHTTP3_QPACK_RS_STATE_READ_VALUELEN,
  687. NGHTTP3_QPACK_RS_STATE_READ_VALUE_HUFFMAN,
  688. NGHTTP3_QPACK_RS_STATE_READ_VALUE,
  689. NGHTTP3_QPACK_RS_STATE_BLOCKED,
  690. } nghttp3_qpack_request_stream_state;
  691. /* nghttp3_qpack_request_stream_opcode is a set of opcodes used in
  692. request stream. */
  693. typedef enum nghttp3_qpack_request_stream_opcode {
  694. NGHTTP3_QPACK_RS_OPCODE_INDEXED,
  695. NGHTTP3_QPACK_RS_OPCODE_INDEXED_PB,
  696. NGHTTP3_QPACK_RS_OPCODE_INDEXED_NAME,
  697. NGHTTP3_QPACK_RS_OPCODE_INDEXED_NAME_PB,
  698. NGHTTP3_QPACK_RS_OPCODE_LITERAL,
  699. } nghttp3_qpack_request_stream_opcode;
  700. struct nghttp3_qpack_decoder {
  701. nghttp3_qpack_context ctx;
  702. /* state is a current state of reading encoder stream. */
  703. nghttp3_qpack_encoder_stream_state state;
  704. /* opcode is an encoder stream opcode being processed. */
  705. nghttp3_qpack_encoder_stream_opcode opcode;
  706. /* rstate is a set of intermediate state which are used to process
  707. encoder stream. */
  708. nghttp3_qpack_read_state rstate;
  709. /* dbuf is decoder stream. */
  710. nghttp3_buf dbuf;
  711. /* written_icnt is Insert Count written to decoder stream so far. */
  712. uint64_t written_icnt;
  713. /* max_concurrent_streams is the number of concurrent streams that a
  714. remote endpoint can open, including both bidirectional and
  715. unidirectional streams which potentially receives QPACK encoded
  716. HEADER frame. */
  717. size_t max_concurrent_streams;
  718. /* uninterrupted_encoderlen is the number of bytes read from encoder
  719. stream without completing a single field section. */
  720. size_t uninterrupted_encoderlen;
  721. };
  722. /*
  723. * nghttp3_qpack_decoder_init initializes |decoder|.
  724. * |hard_max_dtable_capacity| is the upper bound of the dynamic table
  725. * capacity. |max_blocked_streams| is the maximum number of stream
  726. * which can be blocked. |mem| is a memory allocator.
  727. *
  728. * This function returns 0 if it succeeds, or one of the following
  729. * negative error codes:
  730. *
  731. * NGHTTP3_ERR_NOMEM
  732. * Out of memory.
  733. */
  734. int nghttp3_qpack_decoder_init(nghttp3_qpack_decoder *decoder,
  735. size_t hard_max_dtable_capacity,
  736. size_t max_blocked_streams,
  737. const nghttp3_mem *mem);
  738. /*
  739. * nghttp3_qpack_decoder_free frees memory allocated for |decoder|.
  740. * This function does not free memory pointed by |decoder|.
  741. */
  742. void nghttp3_qpack_decoder_free(nghttp3_qpack_decoder *decoder);
  743. /*
  744. * nghttp3_qpack_decoder_dtable_indexed_add adds entry received in
  745. * Insert With Name Reference to dynamic table.
  746. *
  747. * This function returns 0 if it succeeds, or one of the following
  748. * negative error codes:
  749. *
  750. * NGHTTP3_ERR_NOMEM
  751. * Out of memory.
  752. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  753. * Space required for a decoded entry exceeds max dynamic table
  754. * size.
  755. */
  756. int nghttp3_qpack_decoder_dtable_indexed_add(nghttp3_qpack_decoder *decoder);
  757. /*
  758. * nghttp3_qpack_decoder_dtable_static_add adds entry received in
  759. * Insert With Name Reference (static) to dynamic table.
  760. *
  761. * This function returns 0 if it succeeds, or one of the following
  762. * negative error codes:
  763. *
  764. * NGHTTP3_ERR_NOMEM
  765. * Out of memory.
  766. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  767. * Space required for a decoded entry exceeds max dynamic table
  768. * size.
  769. */
  770. int nghttp3_qpack_decoder_dtable_static_add(nghttp3_qpack_decoder *decoder);
  771. /*
  772. * nghttp3_qpack_decoder_dtable_dynamic_add adds entry received in
  773. * Insert With Name Reference (dynamic) to dynamic table.
  774. *
  775. * This function returns 0 if it succeeds, or one of the following
  776. * negative error codes:
  777. *
  778. * NGHTTP3_ERR_NOMEM
  779. * Out of memory.
  780. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  781. * Space required for a decoded entry exceeds max dynamic table
  782. * size.
  783. */
  784. int nghttp3_qpack_decoder_dtable_dynamic_add(nghttp3_qpack_decoder *decoder);
  785. /*
  786. * nghttp3_qpack_decoder_dtable_duplicate_add adds entry received in
  787. * Duplicate to dynamic table.
  788. *
  789. * This function returns 0 if it succeeds, or one of the following
  790. * negative error codes:
  791. *
  792. * NGHTTP3_ERR_NOMEM
  793. * Out of memory.
  794. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  795. * Space required for a decoded entry exceeds max dynamic table
  796. * size.
  797. */
  798. int nghttp3_qpack_decoder_dtable_duplicate_add(nghttp3_qpack_decoder *decoder);
  799. /*
  800. * nghttp3_qpack_decoder_dtable_literal_add adds entry received in
  801. * Insert With Literal Name to dynamic table.
  802. *
  803. * This function returns 0 if it succeeds, or one of the following
  804. * negative error codes:
  805. *
  806. * NGHTTP3_ERR_NOMEM
  807. * Out of memory.
  808. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  809. * Space required for a decoded entry exceeds max dynamic table
  810. * size.
  811. */
  812. int nghttp3_qpack_decoder_dtable_literal_add(nghttp3_qpack_decoder *decoder);
  813. struct nghttp3_qpack_stream_context {
  814. /* rstate is a set of intermediate state which are used to process
  815. request stream. */
  816. nghttp3_qpack_read_state rstate;
  817. const nghttp3_mem *mem;
  818. int64_t stream_id;
  819. /* ricnt is Required Insert Count to decode this header block. */
  820. uint64_t ricnt;
  821. /* base is Base in Header Block Prefix. */
  822. uint64_t base;
  823. /* state is a current state of reading request stream. */
  824. nghttp3_qpack_request_stream_state state;
  825. /* opcode is a request stream opcode being processed. */
  826. nghttp3_qpack_request_stream_opcode opcode;
  827. /* dbase_sign is the delta base sign in Header Block Prefix. */
  828. int dbase_sign;
  829. };
  830. /*
  831. * nghttp3_qpack_stream_context_init initializes |sctx|.
  832. */
  833. void nghttp3_qpack_stream_context_init(nghttp3_qpack_stream_context *sctx,
  834. int64_t stream_id,
  835. const nghttp3_mem *mem);
  836. /*
  837. * nghttp3_qpack_stream_context_free frees memory allocated for
  838. * |sctx|. This function does not free memory pointed by |sctx|.
  839. */
  840. void nghttp3_qpack_stream_context_free(nghttp3_qpack_stream_context *sctx);
  841. /*
  842. * nghttp3_qpack_decoder_reconstruct_ricnt reconstructs Required
  843. * Insert Count from the encoded form |encricnt| and stores Required
  844. * Insert Count in |*dest|.
  845. *
  846. * This function returns 0 if it succeeds, or one of the following
  847. * negative error codes:
  848. *
  849. * NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED
  850. * Unable to reconstruct Required Insert Count.
  851. */
  852. int nghttp3_qpack_decoder_reconstruct_ricnt(nghttp3_qpack_decoder *decoder,
  853. uint64_t *dest, uint64_t encricnt);
  854. /*
  855. * nghttp3_qpack_decoder_rel2abs converts relative index rstate->left
  856. * received in encoder stream to absolute index and stores it in
  857. * rstate->absidx.
  858. *
  859. * This function returns 0 if it succeeds, or one of the following
  860. * negative error codes:
  861. *
  862. * NGHTTP3_ERR_QPACK_ENCODER_STREAM
  863. * Relative index is invalid.
  864. */
  865. int nghttp3_qpack_decoder_rel2abs(nghttp3_qpack_decoder *decoder,
  866. nghttp3_qpack_read_state *rstate);
  867. /*
  868. * nghttp3_qpack_decoder_brel2abs converts Base relative index
  869. * rstate->left received in request stream to absolute index and
  870. * stores it in rstate->absidx.
  871. *
  872. * This function returns 0 if it succeeds, or one of the following
  873. * negative error codes:
  874. *
  875. * NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED
  876. * Base relative index is invalid.
  877. */
  878. int nghttp3_qpack_decoder_brel2abs(nghttp3_qpack_decoder *decoder,
  879. nghttp3_qpack_stream_context *sctx);
  880. /*
  881. * nghttp3_qpack_decoder_pbrel2abs converts Post-Base relative index
  882. * rstate->left received in request stream to absolute index and
  883. * stores it in rstate->absidx.
  884. *
  885. * This function returns 0 if it succeeds, or one of the following
  886. * negative error codes:
  887. *
  888. * NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED
  889. * Post-Base relative index is invalid.
  890. */
  891. int nghttp3_qpack_decoder_pbrel2abs(nghttp3_qpack_decoder *decoder,
  892. nghttp3_qpack_stream_context *sctx);
  893. void nghttp3_qpack_decoder_emit_indexed(nghttp3_qpack_decoder *decoder,
  894. nghttp3_qpack_stream_context *sctx,
  895. nghttp3_qpack_nv *nv);
  896. int nghttp3_qpack_decoder_emit_indexed_name(nghttp3_qpack_decoder *decoder,
  897. nghttp3_qpack_stream_context *sctx,
  898. nghttp3_qpack_nv *nv);
  899. void nghttp3_qpack_decoder_emit_literal(nghttp3_qpack_decoder *decoder,
  900. nghttp3_qpack_stream_context *sctx,
  901. nghttp3_qpack_nv *nv);
  902. /*
  903. * nghttp3_qpack_decoder_write_section_ack writes Section
  904. * Acknowledgement to decoder stream.
  905. *
  906. * This function returns 0 if it succeeds, or one of the following
  907. * negative error codes:
  908. *
  909. * NGHTTP3_ERR_NOMEM
  910. * Out of memory.
  911. * NGHTTP3_ERR_QPACK_FATAL
  912. * Decoder stream overflow.
  913. */
  914. int nghttp3_qpack_decoder_write_section_ack(
  915. nghttp3_qpack_decoder *decoder, const nghttp3_qpack_stream_context *sctx);
  916. #endif /* !defined(NGHTTP3_QPACK_H) */