nghttp2_hd.h 14 KB

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