nghttp2_hd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #ifndef NGHTTP2_HD_H
  5. #define NGHTTP2_HD_H
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif /* HAVE_CONFIG_H */
  9. #include "nghttp2.h"
  10. #include "nghttp2_hd_huffman.h"
  11. #include "nghttp2_buf.h"
  12. #include "nghttp2_mem.h"
  13. #include "nghttp2_rcbuf.h"
  14. #define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE NGHTTP2_DEFAULT_HEADER_TABLE_SIZE
  15. #define NGHTTP2_HD_ENTRY_OVERHEAD 32
  16. /* The maximum length of one name/value pair. This is the sum of the
  17. length of name and value. This is not specified by the spec. We
  18. just chose the arbitrary size */
  19. #define NGHTTP2_HD_MAX_NV 65536
  20. /* Default size of maximum table buffer size for encoder. Even if
  21. remote decoder notifies larger buffer size for its decoding,
  22. encoder only uses the memory up to this value. */
  23. #define NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE (1 << 12)
  24. /* Exported for unit test */
  25. #define NGHTTP2_STATIC_TABLE_LENGTH 61
  26. /* Generated by genlibtokenlookup.py */
  27. typedef enum {
  28. NGHTTP2_TOKEN__AUTHORITY = 0,
  29. NGHTTP2_TOKEN__METHOD = 1,
  30. NGHTTP2_TOKEN__PATH = 3,
  31. NGHTTP2_TOKEN__SCHEME = 5,
  32. NGHTTP2_TOKEN__STATUS = 7,
  33. NGHTTP2_TOKEN_ACCEPT_CHARSET = 14,
  34. NGHTTP2_TOKEN_ACCEPT_ENCODING = 15,
  35. NGHTTP2_TOKEN_ACCEPT_LANGUAGE = 16,
  36. NGHTTP2_TOKEN_ACCEPT_RANGES = 17,
  37. NGHTTP2_TOKEN_ACCEPT = 18,
  38. NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN = 19,
  39. NGHTTP2_TOKEN_AGE = 20,
  40. NGHTTP2_TOKEN_ALLOW = 21,
  41. NGHTTP2_TOKEN_AUTHORIZATION = 22,
  42. NGHTTP2_TOKEN_CACHE_CONTROL = 23,
  43. NGHTTP2_TOKEN_CONTENT_DISPOSITION = 24,
  44. NGHTTP2_TOKEN_CONTENT_ENCODING = 25,
  45. NGHTTP2_TOKEN_CONTENT_LANGUAGE = 26,
  46. NGHTTP2_TOKEN_CONTENT_LENGTH = 27,
  47. NGHTTP2_TOKEN_CONTENT_LOCATION = 28,
  48. NGHTTP2_TOKEN_CONTENT_RANGE = 29,
  49. NGHTTP2_TOKEN_CONTENT_TYPE = 30,
  50. NGHTTP2_TOKEN_COOKIE = 31,
  51. NGHTTP2_TOKEN_DATE = 32,
  52. NGHTTP2_TOKEN_ETAG = 33,
  53. NGHTTP2_TOKEN_EXPECT = 34,
  54. NGHTTP2_TOKEN_EXPIRES = 35,
  55. NGHTTP2_TOKEN_FROM = 36,
  56. NGHTTP2_TOKEN_HOST = 37,
  57. NGHTTP2_TOKEN_IF_MATCH = 38,
  58. NGHTTP2_TOKEN_IF_MODIFIED_SINCE = 39,
  59. NGHTTP2_TOKEN_IF_NONE_MATCH = 40,
  60. NGHTTP2_TOKEN_IF_RANGE = 41,
  61. NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE = 42,
  62. NGHTTP2_TOKEN_LAST_MODIFIED = 43,
  63. NGHTTP2_TOKEN_LINK = 44,
  64. NGHTTP2_TOKEN_LOCATION = 45,
  65. NGHTTP2_TOKEN_MAX_FORWARDS = 46,
  66. NGHTTP2_TOKEN_PROXY_AUTHENTICATE = 47,
  67. NGHTTP2_TOKEN_PROXY_AUTHORIZATION = 48,
  68. NGHTTP2_TOKEN_RANGE = 49,
  69. NGHTTP2_TOKEN_REFERER = 50,
  70. NGHTTP2_TOKEN_REFRESH = 51,
  71. NGHTTP2_TOKEN_RETRY_AFTER = 52,
  72. NGHTTP2_TOKEN_SERVER = 53,
  73. NGHTTP2_TOKEN_SET_COOKIE = 54,
  74. NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY = 55,
  75. NGHTTP2_TOKEN_TRANSFER_ENCODING = 56,
  76. NGHTTP2_TOKEN_USER_AGENT = 57,
  77. NGHTTP2_TOKEN_VARY = 58,
  78. NGHTTP2_TOKEN_VIA = 59,
  79. NGHTTP2_TOKEN_WWW_AUTHENTICATE = 60,
  80. NGHTTP2_TOKEN_TE,
  81. NGHTTP2_TOKEN_CONNECTION,
  82. NGHTTP2_TOKEN_KEEP_ALIVE,
  83. NGHTTP2_TOKEN_PROXY_CONNECTION,
  84. NGHTTP2_TOKEN_UPGRADE,
  85. } nghttp2_token;
  86. struct nghttp2_hd_entry;
  87. typedef struct nghttp2_hd_entry nghttp2_hd_entry;
  88. typedef struct {
  89. /* The buffer containing header field name. NULL-termination is
  90. guaranteed. */
  91. nghttp2_rcbuf *name;
  92. /* The buffer containing header field value. NULL-termination is
  93. guaranteed. */
  94. nghttp2_rcbuf *value;
  95. /* nghttp2_token value for name. It could be -1 if we have no token
  96. for that header field name. */
  97. int32_t token;
  98. /* Bitwise OR of one or more of nghttp2_nv_flag. */
  99. uint8_t flags;
  100. } nghttp2_hd_nv;
  101. struct nghttp2_hd_entry {
  102. /* The header field name/value pair */
  103. nghttp2_hd_nv nv;
  104. /* This is solely for nghttp2_hd_{deflate,inflate}_get_table_entry
  105. APIs to keep backward compatibility. */
  106. nghttp2_nv cnv;
  107. /* The next entry which shares same bucket in hash table. */
  108. nghttp2_hd_entry *next;
  109. /* The sequence number. We will increment it by one whenever we
  110. store nghttp2_hd_entry to dynamic header table. */
  111. uint32_t seq;
  112. /* The hash value for header name (nv.name). */
  113. uint32_t hash;
  114. };
  115. /* The entry used for static header table. */
  116. typedef struct {
  117. nghttp2_rcbuf name;
  118. nghttp2_rcbuf value;
  119. nghttp2_nv cnv;
  120. int32_t token;
  121. uint32_t hash;
  122. } nghttp2_hd_static_entry;
  123. typedef struct {
  124. nghttp2_hd_entry **buffer;
  125. size_t mask;
  126. size_t first;
  127. size_t len;
  128. } nghttp2_hd_ringbuf;
  129. typedef enum {
  130. NGHTTP2_HD_OPCODE_NONE,
  131. NGHTTP2_HD_OPCODE_INDEXED,
  132. NGHTTP2_HD_OPCODE_NEWNAME,
  133. NGHTTP2_HD_OPCODE_INDNAME
  134. } nghttp2_hd_opcode;
  135. typedef enum {
  136. NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE,
  137. NGHTTP2_HD_STATE_INFLATE_START,
  138. NGHTTP2_HD_STATE_OPCODE,
  139. NGHTTP2_HD_STATE_READ_TABLE_SIZE,
  140. NGHTTP2_HD_STATE_READ_INDEX,
  141. NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN,
  142. NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN,
  143. NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF,
  144. NGHTTP2_HD_STATE_NEWNAME_READ_NAME,
  145. NGHTTP2_HD_STATE_CHECK_VALUELEN,
  146. NGHTTP2_HD_STATE_READ_VALUELEN,
  147. NGHTTP2_HD_STATE_READ_VALUEHUFF,
  148. NGHTTP2_HD_STATE_READ_VALUE
  149. } nghttp2_hd_inflate_state;
  150. typedef enum {
  151. NGHTTP2_HD_WITH_INDEXING,
  152. NGHTTP2_HD_WITHOUT_INDEXING,
  153. NGHTTP2_HD_NEVER_INDEXING
  154. } nghttp2_hd_indexing_mode;
  155. typedef struct {
  156. /* dynamic header table */
  157. nghttp2_hd_ringbuf hd_table;
  158. /* Memory allocator */
  159. nghttp2_mem *mem;
  160. /* Abstract buffer size of hd_table as described in the spec. This
  161. is the sum of length of name/value in hd_table +
  162. NGHTTP2_HD_ENTRY_OVERHEAD bytes overhead per each entry. */
  163. size_t hd_table_bufsize;
  164. /* The effective header table size. */
  165. size_t hd_table_bufsize_max;
  166. /* Next sequence number for nghttp2_hd_entry */
  167. uint32_t next_seq;
  168. /* If inflate/deflate error occurred, this value is set to 1 and
  169. further invocation of inflate/deflate will fail with
  170. NGHTTP2_ERR_HEADER_COMP. */
  171. uint8_t bad;
  172. } nghttp2_hd_context;
  173. #define HD_MAP_SIZE 128
  174. typedef struct {
  175. nghttp2_hd_entry *table[HD_MAP_SIZE];
  176. } nghttp2_hd_map;
  177. struct nghttp2_hd_deflater {
  178. nghttp2_hd_context ctx;
  179. nghttp2_hd_map map;
  180. /* The upper limit of the header table size the deflater accepts. */
  181. size_t deflate_hd_table_bufsize_max;
  182. /* Minimum header table size notified in the next context update */
  183. size_t min_hd_table_bufsize_max;
  184. /* If nonzero, send header table size using encoding context update
  185. in the next deflate process */
  186. uint8_t notify_table_size_change;
  187. };
  188. struct nghttp2_hd_inflater {
  189. nghttp2_hd_context ctx;
  190. /* Stores current state of huffman decoding */
  191. nghttp2_hd_huff_decode_context huff_decode_ctx;
  192. /* header buffer */
  193. nghttp2_buf namebuf, valuebuf;
  194. nghttp2_rcbuf *namercbuf, *valuercbuf;
  195. /* Pointer to the name/value pair which are used in the current
  196. header emission. */
  197. nghttp2_rcbuf *nv_name_keep, *nv_value_keep;
  198. /* The number of bytes to read */
  199. size_t left;
  200. /* The index in indexed repr or indexed name */
  201. size_t index;
  202. /* The maximum header table size the inflater supports. This is the
  203. same value transmitted in SETTINGS_HEADER_TABLE_SIZE */
  204. size_t settings_hd_table_bufsize_max;
  205. /* Minimum header table size set by nghttp2_hd_inflate_change_table_size */
  206. size_t min_hd_table_bufsize_max;
  207. /* The number of next shift to decode integer */
  208. size_t shift;
  209. nghttp2_hd_opcode opcode;
  210. nghttp2_hd_inflate_state state;
  211. /* nonzero if string is huffman encoded */
  212. uint8_t huffman_encoded;
  213. /* nonzero if deflater requires that current entry is indexed */
  214. uint8_t index_required;
  215. /* nonzero if deflater requires that current entry must not be
  216. indexed */
  217. uint8_t no_index;
  218. };
  219. /*
  220. * Initializes the |ent| members. The reference counts of nv->name
  221. * and nv->value are increased by one for each.
  222. */
  223. void nghttp2_hd_entry_init(nghttp2_hd_entry *ent, nghttp2_hd_nv *nv);
  224. /*
  225. * This function decreases the reference counts of nv->name and
  226. * nv->value.
  227. */
  228. void nghttp2_hd_entry_free(nghttp2_hd_entry *ent);
  229. /*
  230. * Initializes |deflater| for deflating name/values pairs.
  231. *
  232. * The encoder only uses up to
  233. * NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE bytes for header table
  234. * even if the larger value is specified later in
  235. * nghttp2_hd_change_table_size().
  236. *
  237. * This function returns 0 if it succeeds, or one of the following
  238. * negative error codes:
  239. *
  240. * NGHTTP2_ERR_NOMEM
  241. * Out of memory.
  242. */
  243. int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem);
  244. /*
  245. * Initializes |deflater| for deflating name/values pairs.
  246. *
  247. * The encoder only uses up to |max_deflate_dynamic_table_size| bytes
  248. * for header table even if the larger value is specified later in
  249. * nghttp2_hd_change_table_size().
  250. *
  251. * This function returns 0 if it succeeds, or one of the following
  252. * negative error codes:
  253. *
  254. * NGHTTP2_ERR_NOMEM
  255. * Out of memory.
  256. */
  257. int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
  258. size_t max_deflate_dynamic_table_size,
  259. nghttp2_mem *mem);
  260. /*
  261. * Deallocates any resources allocated for |deflater|.
  262. */
  263. void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater);
  264. /*
  265. * Deflates the |nva|, which has the |nvlen| name/value pairs, into
  266. * the |bufs|.
  267. *
  268. * This function expands |bufs| as necessary to store the result. If
  269. * buffers is full and the process still requires more space, this
  270. * function fails and returns NGHTTP2_ERR_HEADER_COMP.
  271. *
  272. * After this function returns, it is safe to delete the |nva|.
  273. *
  274. * This function returns 0 if it succeeds, or one of the following
  275. * negative error codes:
  276. *
  277. * NGHTTP2_ERR_NOMEM
  278. * Out of memory.
  279. * NGHTTP2_ERR_HEADER_COMP
  280. * Deflation process has failed.
  281. * NGHTTP2_ERR_BUFFER_ERROR
  282. * Out of buffer space.
  283. */
  284. int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
  285. nghttp2_bufs *bufs, const nghttp2_nv *nva,
  286. size_t nvlen);
  287. /*
  288. * Initializes |inflater| for inflating name/values pairs.
  289. *
  290. * This function returns 0 if it succeeds, or one of the following
  291. * negative error codes:
  292. *
  293. * :enum:`NGHTTP2_ERR_NOMEM`
  294. * Out of memory.
  295. */
  296. int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem);
  297. /*
  298. * Deallocates any resources allocated for |inflater|.
  299. */
  300. void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater);
  301. /*
  302. * Similar to nghttp2_hd_inflate_hd(), but this takes nghttp2_hd_nv
  303. * instead of nghttp2_nv as output parameter |nv_out|. Other than
  304. * that return values and semantics are the same as
  305. * nghttp2_hd_inflate_hd().
  306. */
  307. ssize_t nghttp2_hd_inflate_hd_nv(nghttp2_hd_inflater *inflater,
  308. nghttp2_hd_nv *nv_out, int *inflate_flags,
  309. const uint8_t *in, size_t inlen, int in_final);
  310. /* For unittesting purpose */
  311. int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t index,
  312. nghttp2_nv *nv, int indexing_mode);
  313. /* For unittesting purpose */
  314. int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
  315. int indexing_mode);
  316. /* For unittesting purpose */
  317. int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size);
  318. /* For unittesting purpose */
  319. nghttp2_hd_nv nghttp2_hd_table_get(nghttp2_hd_context *context, size_t index);
  320. /* For unittesting purpose */
  321. ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
  322. uint32_t initial, size_t shift, uint8_t *in,
  323. uint8_t *last, size_t prefix);
  324. /* Huffman encoding/decoding functions */
  325. /*
  326. * Counts the required bytes to encode |src| with length |len|.
  327. *
  328. * This function returns the number of required bytes to encode given
  329. * data, including padding of prefix of terminal symbol code. This
  330. * function always succeeds.
  331. */
  332. size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len);
  333. /*
  334. * Encodes the given data |src| with length |srclen| to the |bufs|.
  335. * This function expands extra buffers in |bufs| if necessary.
  336. *
  337. * This function returns 0 if it succeeds, or one of the following
  338. * negative error codes:
  339. *
  340. * NGHTTP2_ERR_NOMEM
  341. * Out of memory.
  342. * NGHTTP2_ERR_BUFFER_ERROR
  343. * Out of buffer space.
  344. */
  345. int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,
  346. size_t srclen);
  347. void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx);
  348. /*
  349. * Decodes the given data |src| with length |srclen|. The |ctx| must
  350. * be initialized by nghttp2_hd_huff_decode_context_init(). The result
  351. * will be written to |buf|. This function assumes that |buf| has the
  352. * enough room to store the decoded byte string.
  353. *
  354. * The caller must set the |fin| to nonzero if the given input is the
  355. * final block.
  356. *
  357. * This function returns the number of read bytes from the |in|.
  358. *
  359. * If this function fails, it returns one of the following negative
  360. * return codes:
  361. *
  362. * NGHTTP2_ERR_NOMEM
  363. * Out of memory.
  364. * NGHTTP2_ERR_HEADER_COMP
  365. * Decoding process has failed.
  366. */
  367. ssize_t nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,
  368. nghttp2_buf *buf, const uint8_t *src,
  369. size_t srclen, int fin);
  370. #endif /* NGHTTP2_HD_H */