nghttp2_frame.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 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_FRAME_H
  26. #define NGHTTP2_FRAME_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <nghttp2/nghttp2.h>
  31. #include "nghttp2_hd.h"
  32. #include "nghttp2_buf.h"
  33. #define NGHTTP2_STREAM_ID_MASK ((1u << 31) - 1)
  34. #define NGHTTP2_PRI_GROUP_ID_MASK ((1u << 31) - 1)
  35. #define NGHTTP2_PRIORITY_MASK ((1u << 31) - 1)
  36. #define NGHTTP2_WINDOW_SIZE_INCREMENT_MASK ((1u << 31) - 1)
  37. #define NGHTTP2_SETTINGS_ID_MASK ((1 << 24) - 1)
  38. /* The number of bytes of frame header. */
  39. #define NGHTTP2_FRAME_HDLEN 9
  40. #define NGHTTP2_MAX_FRAME_SIZE_MAX ((1 << 24) - 1)
  41. #define NGHTTP2_MAX_FRAME_SIZE_MIN (1 << 14)
  42. #define NGHTTP2_MAX_PAYLOADLEN 16384
  43. /* The one frame buffer length for transmission. We may use several of
  44. them to support CONTINUATION. To account for Pad Length field, we
  45. allocate extra 1 byte, which saves extra large memcopying. */
  46. #define NGHTTP2_FRAMEBUF_CHUNKLEN \
  47. (NGHTTP2_FRAME_HDLEN + 1 + NGHTTP2_MAX_PAYLOADLEN)
  48. /* The default length of DATA frame payload. */
  49. #define NGHTTP2_DATA_PAYLOADLEN NGHTTP2_MAX_FRAME_SIZE_MIN
  50. /* Maximum headers block size to send, calculated using
  51. nghttp2_hd_deflate_bound(). This is the default value, and can be
  52. overridden by nghttp2_option_set_max_send_header_block_length(). */
  53. #define NGHTTP2_MAX_HEADERSLEN 65536
  54. /* The number of bytes for each SETTINGS entry */
  55. #define NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH 6
  56. /* Length of priority related fields in HEADERS/PRIORITY frames */
  57. #define NGHTTP2_PRIORITY_SPECLEN 5
  58. /* Maximum length of padding in bytes. */
  59. #define NGHTTP2_MAX_PADLEN 256
  60. /* Union of extension frame payload */
  61. typedef union {
  62. nghttp2_ext_altsvc altsvc;
  63. nghttp2_ext_origin origin;
  64. nghttp2_ext_priority_update priority_update;
  65. } nghttp2_ext_frame_payload;
  66. void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd);
  67. void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf);
  68. /**
  69. * Initializes frame header |hd| with given parameters. Reserved bit
  70. * is set to 0.
  71. */
  72. void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
  73. uint8_t flags, int32_t stream_id);
  74. /**
  75. * Returns the number of priority field depending on the |flags|. If
  76. * |flags| has neither NGHTTP2_FLAG_PRIORITY_GROUP nor
  77. * NGHTTP2_FLAG_PRIORITY_DEPENDENCY set, return 0.
  78. */
  79. size_t nghttp2_frame_priority_len(uint8_t flags);
  80. /**
  81. * Packs the |pri_spec| in |buf|. This function assumes |buf| has
  82. * enough space for serialization.
  83. */
  84. void nghttp2_frame_pack_priority_spec(uint8_t *buf,
  85. const nghttp2_priority_spec *pri_spec);
  86. /**
  87. * Unpacks the priority specification from payload |payload| of length
  88. * |payloadlen| to |pri_spec|. The |flags| is used to determine what
  89. * kind of priority specification is in |payload|. This function
  90. * assumes the |payload| contains whole priority specification.
  91. */
  92. void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
  93. const uint8_t *payload);
  94. /*
  95. * Returns the offset from the HEADERS frame payload where the
  96. * compressed header block starts. The frame payload does not include
  97. * frame header.
  98. */
  99. size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame);
  100. /*
  101. * Packs HEADERS frame |frame| in wire format and store it in |bufs|.
  102. * This function expands |bufs| as necessary to store frame.
  103. *
  104. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  105. * before calling this function.
  106. *
  107. * frame->hd.length is assigned after length is determined during
  108. * packing process. CONTINUATION frames are also serialized in this
  109. * function. This function does not handle padding.
  110. *
  111. * This function returns 0 if it succeeds, or returns one of the
  112. * following negative error codes:
  113. *
  114. * NGHTTP2_ERR_HEADER_COMP
  115. * The deflate operation failed.
  116. * NGHTTP2_ERR_NOMEM
  117. * Out of memory.
  118. */
  119. int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
  120. nghttp2_hd_deflater *deflater);
  121. /*
  122. * Unpacks HEADERS frame byte sequence into |frame|. This function
  123. * only unapcks bytes that come before name/value header block and
  124. * after possible Pad Length field.
  125. *
  126. * This function always succeeds and returns 0.
  127. */
  128. int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
  129. const uint8_t *payload);
  130. /*
  131. * Packs PRIORITY frame |frame| in wire format and store it in
  132. * |bufs|.
  133. *
  134. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  135. * before calling this function.
  136. *
  137. * This function always succeeds and returns 0.
  138. */
  139. int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame);
  140. /*
  141. * Unpacks PRIORITY wire format into |frame|.
  142. */
  143. void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
  144. const uint8_t *payload);
  145. /*
  146. * Packs RST_STREAM frame |frame| in wire frame format and store it in
  147. * |bufs|.
  148. *
  149. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  150. * before calling this function.
  151. *
  152. * This function always succeeds and returns 0.
  153. */
  154. int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
  155. nghttp2_rst_stream *frame);
  156. /*
  157. * Unpacks RST_STREAM frame byte sequence into |frame|.
  158. */
  159. void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
  160. const uint8_t *payload);
  161. /*
  162. * Packs SETTINGS frame |frame| in wire format and store it in
  163. * |bufs|.
  164. *
  165. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  166. * before calling this function.
  167. *
  168. * This function returns 0 if it succeeds, or returns one of the
  169. * following negative error codes:
  170. *
  171. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  172. * The length of the frame is too large.
  173. */
  174. int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame);
  175. /*
  176. * Packs the |iv|, which includes |niv| entries, in the |buf|,
  177. * assuming the |buf| has at least 8 * |niv| bytes.
  178. *
  179. * Returns the number of bytes written into the |buf|.
  180. */
  181. size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
  182. const nghttp2_settings_entry *iv,
  183. size_t niv);
  184. void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
  185. const uint8_t *payload);
  186. /*
  187. * Initializes payload of frame->settings. The |frame| takes
  188. * ownership of |iv|.
  189. */
  190. void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
  191. nghttp2_settings_entry *iv,
  192. size_t niv);
  193. /*
  194. * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are
  195. * assigned to the |*niv_ptr|. This function allocates enough memory
  196. * to store the result in |*iv_ptr|. The caller is responsible to free
  197. * |*iv_ptr| after its use.
  198. *
  199. * This function returns 0 if it succeeds or one of the following
  200. * negative error codes:
  201. *
  202. * NGHTTP2_ERR_NOMEM
  203. * Out of memory.
  204. */
  205. int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
  206. size_t *niv_ptr,
  207. const uint8_t *payload,
  208. size_t payloadlen, nghttp2_mem *mem);
  209. /*
  210. * Packs PUSH_PROMISE frame |frame| in wire format and store it in
  211. * |bufs|. This function expands |bufs| as necessary to store
  212. * frame.
  213. *
  214. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  215. * before calling this function.
  216. *
  217. * frame->hd.length is assigned after length is determined during
  218. * packing process. CONTINUATION frames are also serialized in this
  219. * function. This function does not handle padding.
  220. *
  221. * This function returns 0 if it succeeds, or returns one of the
  222. * following negative error codes:
  223. *
  224. * NGHTTP2_ERR_HEADER_COMP
  225. * The deflate operation failed.
  226. * NGHTTP2_ERR_NOMEM
  227. * Out of memory.
  228. */
  229. int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
  230. nghttp2_push_promise *frame,
  231. nghttp2_hd_deflater *deflater);
  232. /*
  233. * Unpacks PUSH_PROMISE frame byte sequence into |frame|. This
  234. * function only unapcks bytes that come before name/value header
  235. * block and after possible Pad Length field.
  236. *
  237. * This function returns 0 if it succeeds or one of the following
  238. * negative error codes:
  239. *
  240. * NGHTTP2_ERR_PROTO
  241. * TODO END_HEADERS flag is not set
  242. */
  243. int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
  244. const uint8_t *payload);
  245. /*
  246. * Packs PING frame |frame| in wire format and store it in
  247. * |bufs|.
  248. *
  249. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  250. * before calling this function.
  251. *
  252. * This function always succeeds and returns 0.
  253. */
  254. int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame);
  255. /*
  256. * Unpacks PING wire format into |frame|.
  257. */
  258. void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
  259. const uint8_t *payload);
  260. /*
  261. * Packs GOAWAY frame |frame| in wire format and store it in |bufs|.
  262. * This function expands |bufs| as necessary to store frame.
  263. *
  264. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  265. * before calling this function.
  266. *
  267. * This function returns 0 if it succeeds or one of the following
  268. * negative error codes:
  269. *
  270. * NGHTTP2_ERR_NOMEM
  271. * Out of memory.
  272. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  273. * The length of the frame is too large.
  274. */
  275. int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame);
  276. /*
  277. * Unpacks GOAWAY wire format into |frame|. The |payload| of length
  278. * |payloadlen| contains first 8 bytes of payload. The
  279. * |var_gift_payload| of length |var_gift_payloadlen| contains
  280. * remaining payload and its buffer is gifted to the function and then
  281. * |frame|. The |var_gift_payloadlen| must be freed by
  282. * nghttp2_frame_goaway_free().
  283. */
  284. void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
  285. const uint8_t *payload,
  286. uint8_t *var_gift_payload,
  287. size_t var_gift_payloadlen);
  288. /*
  289. * Unpacks GOAWAY wire format into |frame|. This function only exists
  290. * for unit test. After allocating buffer for debug data, this
  291. * function internally calls nghttp2_frame_unpack_goaway_payload().
  292. *
  293. * This function returns 0 if it succeeds, or one of the following
  294. * negative error codes:
  295. *
  296. * NGHTTP2_ERR_NOMEM
  297. * Out of memory.
  298. */
  299. int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
  300. const uint8_t *payload,
  301. size_t payloadlen, nghttp2_mem *mem);
  302. /*
  303. * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it
  304. * in |bufs|.
  305. *
  306. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  307. * before calling this function.
  308. *
  309. * This function always succeeds and returns 0.
  310. */
  311. int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
  312. nghttp2_window_update *frame);
  313. /*
  314. * Unpacks WINDOW_UPDATE frame byte sequence into |frame|.
  315. */
  316. void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
  317. const uint8_t *payload);
  318. /*
  319. * Packs ALTSVC frame |frame| in wire frame format and store it in
  320. * |bufs|.
  321. *
  322. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  323. * before calling this function.
  324. *
  325. * This function always succeeds and returns 0.
  326. */
  327. int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *ext);
  328. /*
  329. * Unpacks ALTSVC wire format into |frame|. The |payload| of
  330. * |payloadlen| bytes contains frame payload. This function assumes
  331. * that frame->payload points to the nghttp2_ext_altsvc object.
  332. *
  333. * This function always succeeds and returns 0.
  334. */
  335. void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
  336. size_t origin_len, uint8_t *payload,
  337. size_t payloadlen);
  338. /*
  339. * Unpacks ALTSVC wire format into |frame|. This function only exists
  340. * for unit test. After allocating buffer for fields, this function
  341. * internally calls nghttp2_frame_unpack_altsvc_payload().
  342. *
  343. * This function returns 0 if it succeeds, or one of the following
  344. * negative error codes:
  345. *
  346. * NGHTTP2_ERR_NOMEM
  347. * Out of memory.
  348. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  349. * The payload is too small.
  350. */
  351. int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
  352. const uint8_t *payload,
  353. size_t payloadlen, nghttp2_mem *mem);
  354. /*
  355. * Packs ORIGIN frame |frame| in wire frame format and store it in
  356. * |bufs|.
  357. *
  358. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  359. * before calling this function.
  360. *
  361. * This function returns 0 if it succeeds, or one of the following
  362. * negative error codes:
  363. *
  364. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  365. * The length of the frame is too large.
  366. */
  367. int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *ext);
  368. /*
  369. * Unpacks ORIGIN wire format into |frame|. The |payload| of length
  370. * |payloadlen| contains the frame payload.
  371. *
  372. * This function returns 0 if it succeeds, or one of the following
  373. * negative error codes:
  374. *
  375. * NGHTTP2_ERR_NOMEM
  376. * Out of memory.
  377. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  378. * The payload is too small.
  379. */
  380. int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
  381. const uint8_t *payload,
  382. size_t payloadlen, nghttp2_mem *mem);
  383. /*
  384. * Packs PRIORITY_UPDATE frame |frame| in wire frame format and store
  385. * it in |bufs|.
  386. *
  387. * The caller must make sure that nghttp2_bufs_reset(bufs) is called
  388. * before calling this function.
  389. *
  390. * This function always succeeds and returns 0.
  391. */
  392. int nghttp2_frame_pack_priority_update(nghttp2_bufs *bufs,
  393. nghttp2_extension *ext);
  394. /*
  395. * Unpacks PRIORITY_UPDATE wire format into |frame|. The |payload| of
  396. * |payloadlen| bytes contains frame payload. This function assumes
  397. * that frame->payload points to the nghttp2_ext_priority_update
  398. * object.
  399. *
  400. * This function always succeeds and returns 0.
  401. */
  402. void nghttp2_frame_unpack_priority_update_payload(nghttp2_extension *frame,
  403. uint8_t *payload,
  404. size_t payloadlen);
  405. /*
  406. * Initializes HEADERS frame |frame| with given values. |frame| takes
  407. * ownership of |nva|, so caller must not free it. If |stream_id| is
  408. * not assigned yet, it must be -1.
  409. */
  410. void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
  411. int32_t stream_id, nghttp2_headers_category cat,
  412. const nghttp2_priority_spec *pri_spec,
  413. nghttp2_nv *nva, size_t nvlen);
  414. void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem);
  415. void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
  416. const nghttp2_priority_spec *pri_spec);
  417. void nghttp2_frame_priority_free(nghttp2_priority *frame);
  418. void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
  419. uint32_t error_code);
  420. void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame);
  421. /*
  422. * Initializes PUSH_PROMISE frame |frame| with given values. |frame|
  423. * takes ownership of |nva|, so caller must not free it.
  424. */
  425. void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
  426. int32_t stream_id,
  427. int32_t promised_stream_id,
  428. nghttp2_nv *nva, size_t nvlen);
  429. void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
  430. nghttp2_mem *mem);
  431. /*
  432. * Initializes SETTINGS frame |frame| with given values. |frame| takes
  433. * ownership of |iv|, so caller must not free it. The |flags| are
  434. * bitwise-OR of one or more of nghttp2_settings_flag.
  435. */
  436. void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
  437. nghttp2_settings_entry *iv, size_t niv);
  438. void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem);
  439. /*
  440. * Initializes PING frame |frame| with given values. If the
  441. * |opqeue_data| is not NULL, it must point to 8 bytes memory region
  442. * of data. The data pointed by |opaque_data| is copied. It can be
  443. * NULL. In this case, 8 bytes NULL is used.
  444. */
  445. void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
  446. const uint8_t *opque_data);
  447. void nghttp2_frame_ping_free(nghttp2_ping *frame);
  448. /*
  449. * Initializes GOAWAY frame |frame| with given values. On success,
  450. * this function takes ownership of |opaque_data|, so caller must not
  451. * free it. If the |opaque_data_len| is 0, opaque_data could be NULL.
  452. */
  453. void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
  454. uint32_t error_code, uint8_t *opaque_data,
  455. size_t opaque_data_len);
  456. void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem);
  457. void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
  458. uint8_t flags, int32_t stream_id,
  459. int32_t window_size_increment);
  460. void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
  461. void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
  462. uint8_t flags, int32_t stream_id,
  463. void *payload);
  464. void nghttp2_frame_extension_free(nghttp2_extension *frame);
  465. /*
  466. * Initializes ALTSVC frame |frame| with given values. This function
  467. * assumes that frame->payload points to nghttp2_ext_altsvc object.
  468. * Also |origin| and |field_value| are allocated in single buffer,
  469. * starting |origin|. On success, this function takes ownership of
  470. * |origin|, so caller must not free it.
  471. */
  472. void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
  473. uint8_t *origin, size_t origin_len,
  474. uint8_t *field_value, size_t field_value_len);
  475. /*
  476. * Frees up resources under |frame|. This function does not free
  477. * nghttp2_ext_altsvc object pointed by frame->payload. This function
  478. * only frees origin pointed by nghttp2_ext_altsvc.origin. Therefore,
  479. * other fields must be allocated in the same buffer with origin.
  480. */
  481. void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem);
  482. /*
  483. * Initializes ORIGIN frame |frame| with given values. This function
  484. * assumes that frame->payload points to nghttp2_ext_origin object.
  485. * Also |ov| and the memory pointed by the field of its elements are
  486. * allocated in single buffer, starting with |ov|. On success, this
  487. * function takes ownership of |ov|, so caller must not free it.
  488. */
  489. void nghttp2_frame_origin_init(nghttp2_extension *frame,
  490. nghttp2_origin_entry *ov, size_t nov);
  491. /*
  492. * Frees up resources under |frame|. This function does not free
  493. * nghttp2_ext_origin object pointed by frame->payload. This function
  494. * only frees nghttp2_ext_origin.ov. Therefore, other fields must be
  495. * allocated in the same buffer with ov.
  496. */
  497. void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem);
  498. /*
  499. * Initializes PRIORITY_UPDATE frame |frame| with given values. This
  500. * function assumes that frame->payload points to
  501. * nghttp2_ext_priority_update object. On success, this function
  502. * takes ownership of |field_value|, so caller must not free it.
  503. */
  504. void nghttp2_frame_priority_update_init(nghttp2_extension *frame,
  505. int32_t stream_id, uint8_t *field_value,
  506. size_t field_value_len);
  507. /*
  508. * Frees up resources under |frame|. This function does not free
  509. * nghttp2_ext_priority_update object pointed by frame->payload. This
  510. * function only frees field_value pointed by
  511. * nghttp2_ext_priority_update.field_value.
  512. */
  513. void nghttp2_frame_priority_update_free(nghttp2_extension *frame,
  514. nghttp2_mem *mem);
  515. /*
  516. * Returns the number of padding bytes after payload. The total
  517. * padding length is given in the |padlen|. The returned value does
  518. * not include the Pad Length field. If |padlen| is 0, this function
  519. * returns 0, regardless of frame->hd.flags.
  520. */
  521. size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen);
  522. void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
  523. int32_t stream_id);
  524. void nghttp2_frame_data_free(nghttp2_data *frame);
  525. /*
  526. * Makes copy of |iv| and return the copy. The |niv| is the number of
  527. * entries in |iv|. This function returns the pointer to the copy if
  528. * it succeeds, or NULL.
  529. */
  530. nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
  531. size_t niv, nghttp2_mem *mem);
  532. /*
  533. * Sorts the |nva| in ascending order of name and value. If names are
  534. * equivalent, sort them by value.
  535. */
  536. void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
  537. /*
  538. * Copies name/value pairs from |nva|, which contains |nvlen| pairs,
  539. * to |*nva_ptr|, which is dynamically allocated so that all items can
  540. * be stored. The resultant name and value in nghttp2_nv are
  541. * guaranteed to be NULL-terminated even if the input is not
  542. * null-terminated.
  543. *
  544. * The |*nva_ptr| must be freed using nghttp2_nv_array_del().
  545. *
  546. * This function returns 0 if it succeeds or one of the following
  547. * negative error codes:
  548. *
  549. * NGHTTP2_ERR_NOMEM
  550. * Out of memory.
  551. */
  552. int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
  553. size_t nvlen, nghttp2_mem *mem);
  554. /*
  555. * Returns nonzero if the name/value pair |a| equals to |b|. The name
  556. * is compared in case-sensitive, because we ensure that this function
  557. * is called after the name is lower-cased.
  558. */
  559. int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
  560. /*
  561. * Frees |nva|.
  562. */
  563. void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem);
  564. /*
  565. * Checks that the |iv|, which includes |niv| entries, does not have
  566. * invalid values.
  567. *
  568. * This function returns nonzero if it succeeds, or 0.
  569. */
  570. int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv);
  571. /*
  572. * Sets Pad Length field and flags and adjusts frame header position
  573. * of each buffers in |bufs|. The number of padding is given in the
  574. * |padlen| including Pad Length field. The |hd| is the frame header
  575. * for the serialized data. This function fills zeros padding region
  576. * unless framehd_only is nonzero.
  577. *
  578. * This function returns 0 if it succeeds, or one of the following
  579. * negative error codes:
  580. *
  581. * NGHTTP2_ERR_NOMEM
  582. * Out of memory.
  583. * NGHTTP2_ERR_FRAME_SIZE_ERROR
  584. * The length of the resulting frame is too large.
  585. */
  586. int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
  587. size_t padlen, int framehd_only);
  588. #endif /* NGHTTP2_FRAME_H */