nghttp2_frame.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. #include "nghttp2_frame.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include "nghttp2_helper.h"
  31. #include "nghttp2_net.h"
  32. #include "nghttp2_priority_spec.h"
  33. #include "nghttp2_debug.h"
  34. void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
  35. nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
  36. buf[3] = hd->type;
  37. buf[4] = hd->flags;
  38. nghttp2_put_uint32be(&buf[5], (uint32_t)hd->stream_id);
  39. /* ignore hd->reserved for now */
  40. }
  41. void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
  42. hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
  43. hd->type = buf[3];
  44. hd->flags = buf[4];
  45. hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
  46. hd->reserved = 0;
  47. }
  48. void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
  49. uint8_t flags, int32_t stream_id) {
  50. hd->length = length;
  51. hd->type = type;
  52. hd->flags = flags;
  53. hd->stream_id = stream_id;
  54. hd->reserved = 0;
  55. }
  56. void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
  57. int32_t stream_id, nghttp2_headers_category cat,
  58. const nghttp2_priority_spec *pri_spec,
  59. nghttp2_nv *nva, size_t nvlen) {
  60. nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
  61. frame->padlen = 0;
  62. frame->nva = nva;
  63. frame->nvlen = nvlen;
  64. frame->cat = cat;
  65. if (pri_spec) {
  66. frame->pri_spec = *pri_spec;
  67. } else {
  68. nghttp2_priority_spec_default_init(&frame->pri_spec);
  69. }
  70. }
  71. void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
  72. nghttp2_nv_array_del(frame->nva, mem);
  73. }
  74. void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
  75. const nghttp2_priority_spec *pri_spec) {
  76. nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
  77. NGHTTP2_FLAG_NONE, stream_id);
  78. frame->pri_spec = *pri_spec;
  79. }
  80. void nghttp2_frame_priority_free(nghttp2_priority *frame) { (void)frame; }
  81. void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
  82. uint32_t error_code) {
  83. nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
  84. stream_id);
  85. frame->error_code = error_code;
  86. }
  87. void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame) { (void)frame; }
  88. void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
  89. nghttp2_settings_entry *iv, size_t niv) {
  90. nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
  91. NGHTTP2_SETTINGS, flags, 0);
  92. frame->niv = niv;
  93. frame->iv = iv;
  94. }
  95. void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
  96. nghttp2_mem_free(mem, frame->iv);
  97. }
  98. void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
  99. int32_t stream_id,
  100. int32_t promised_stream_id,
  101. nghttp2_nv *nva, size_t nvlen) {
  102. nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
  103. frame->padlen = 0;
  104. frame->nva = nva;
  105. frame->nvlen = nvlen;
  106. frame->promised_stream_id = promised_stream_id;
  107. frame->reserved = 0;
  108. }
  109. void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
  110. nghttp2_mem *mem) {
  111. nghttp2_nv_array_del(frame->nva, mem);
  112. }
  113. void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
  114. const uint8_t *opaque_data) {
  115. nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
  116. if (opaque_data) {
  117. memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
  118. } else {
  119. memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
  120. }
  121. }
  122. void nghttp2_frame_ping_free(nghttp2_ping *frame) { (void)frame; }
  123. void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
  124. uint32_t error_code, uint8_t *opaque_data,
  125. size_t opaque_data_len) {
  126. nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
  127. NGHTTP2_FLAG_NONE, 0);
  128. frame->last_stream_id = last_stream_id;
  129. frame->error_code = error_code;
  130. frame->opaque_data = opaque_data;
  131. frame->opaque_data_len = opaque_data_len;
  132. frame->reserved = 0;
  133. }
  134. void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
  135. nghttp2_mem_free(mem, frame->opaque_data);
  136. }
  137. void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
  138. uint8_t flags, int32_t stream_id,
  139. int32_t window_size_increment) {
  140. nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
  141. frame->window_size_increment = window_size_increment;
  142. frame->reserved = 0;
  143. }
  144. void nghttp2_frame_window_update_free(nghttp2_window_update *frame) {
  145. (void)frame;
  146. }
  147. size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
  148. /* We have iframe->padlen == 0, but iframe->frame.hd.flags may have
  149. NGHTTP2_FLAG_PADDED set. This happens when receiving
  150. CONTINUATION frame, since we don't reset flags after HEADERS was
  151. received. */
  152. if (padlen == 0) {
  153. return 0;
  154. }
  155. return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
  156. }
  157. void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
  158. int32_t stream_id) {
  159. /* At this moment, the length of DATA frame is unknown */
  160. nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
  161. frame->padlen = 0;
  162. }
  163. void nghttp2_frame_data_free(nghttp2_data *frame) { (void)frame; }
  164. void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
  165. uint8_t flags, int32_t stream_id,
  166. void *payload) {
  167. nghttp2_frame_hd_init(&frame->hd, 0, type, flags, stream_id);
  168. frame->payload = payload;
  169. }
  170. void nghttp2_frame_extension_free(nghttp2_extension *frame) { (void)frame; }
  171. void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
  172. uint8_t *origin, size_t origin_len,
  173. uint8_t *field_value, size_t field_value_len) {
  174. nghttp2_ext_altsvc *altsvc;
  175. nghttp2_frame_hd_init(&frame->hd, 2 + origin_len + field_value_len,
  176. NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, stream_id);
  177. altsvc = frame->payload;
  178. altsvc->origin = origin;
  179. altsvc->origin_len = origin_len;
  180. altsvc->field_value = field_value;
  181. altsvc->field_value_len = field_value_len;
  182. }
  183. void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem) {
  184. nghttp2_ext_altsvc *altsvc;
  185. altsvc = frame->payload;
  186. if (altsvc == NULL) {
  187. return;
  188. }
  189. /* We use the same buffer for altsvc->origin and
  190. altsvc->field_value. */
  191. nghttp2_mem_free(mem, altsvc->origin);
  192. }
  193. void nghttp2_frame_origin_init(nghttp2_extension *frame,
  194. nghttp2_origin_entry *ov, size_t nov) {
  195. nghttp2_ext_origin *origin;
  196. size_t payloadlen = 0;
  197. size_t i;
  198. for (i = 0; i < nov; ++i) {
  199. payloadlen += 2 + ov[i].origin_len;
  200. }
  201. nghttp2_frame_hd_init(&frame->hd, payloadlen, NGHTTP2_ORIGIN,
  202. NGHTTP2_FLAG_NONE, 0);
  203. origin = frame->payload;
  204. origin->ov = ov;
  205. origin->nov = nov;
  206. }
  207. void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem) {
  208. nghttp2_ext_origin *origin;
  209. origin = frame->payload;
  210. if (origin == NULL) {
  211. return;
  212. }
  213. /* We use the same buffer for all resources pointed by the field of
  214. origin directly or indirectly. */
  215. nghttp2_mem_free(mem, origin->ov);
  216. }
  217. size_t nghttp2_frame_priority_len(uint8_t flags) {
  218. if (flags & NGHTTP2_FLAG_PRIORITY) {
  219. return NGHTTP2_PRIORITY_SPECLEN;
  220. }
  221. return 0;
  222. }
  223. size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
  224. return nghttp2_frame_priority_len(frame->hd.flags);
  225. }
  226. /*
  227. * Call this function after payload was serialized, but not before
  228. * changing buf->pos and serializing frame header.
  229. *
  230. * This function assumes bufs->cur points to the last buf chain of the
  231. * frame(s).
  232. *
  233. * This function serializes frame header for HEADERS/PUSH_PROMISE and
  234. * handles their successive CONTINUATION frames.
  235. *
  236. * We don't process any padding here.
  237. */
  238. static int frame_pack_headers_shared(nghttp2_bufs *bufs,
  239. nghttp2_frame_hd *frame_hd) {
  240. nghttp2_buf *buf;
  241. nghttp2_buf_chain *ci, *ce;
  242. nghttp2_frame_hd hd;
  243. buf = &bufs->head->buf;
  244. hd = *frame_hd;
  245. hd.length = nghttp2_buf_len(buf);
  246. DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
  247. /* We have multiple frame buffers, which means one or more
  248. CONTINUATION frame is involved. Remove END_HEADERS flag from the
  249. first frame. */
  250. if (bufs->head != bufs->cur) {
  251. hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
  252. }
  253. buf->pos -= NGHTTP2_FRAME_HDLEN;
  254. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  255. if (bufs->head != bufs->cur) {
  256. /* 2nd and later frames are CONTINUATION frames. */
  257. hd.type = NGHTTP2_CONTINUATION;
  258. /* We don't have no flags except for last CONTINUATION */
  259. hd.flags = NGHTTP2_FLAG_NONE;
  260. ce = bufs->cur;
  261. for (ci = bufs->head->next; ci != ce; ci = ci->next) {
  262. buf = &ci->buf;
  263. hd.length = nghttp2_buf_len(buf);
  264. DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
  265. buf->pos -= NGHTTP2_FRAME_HDLEN;
  266. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  267. }
  268. buf = &ci->buf;
  269. hd.length = nghttp2_buf_len(buf);
  270. /* Set END_HEADERS flag for last CONTINUATION */
  271. hd.flags = NGHTTP2_FLAG_END_HEADERS;
  272. DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
  273. buf->pos -= NGHTTP2_FRAME_HDLEN;
  274. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  275. }
  276. return 0;
  277. }
  278. int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
  279. nghttp2_hd_deflater *deflater) {
  280. size_t nv_offset;
  281. int rv;
  282. nghttp2_buf *buf;
  283. assert(bufs->head == bufs->cur);
  284. nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
  285. buf = &bufs->cur->buf;
  286. buf->pos += nv_offset;
  287. buf->last = buf->pos;
  288. /* This call will adjust buf->last to the correct position */
  289. rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
  290. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  291. rv = NGHTTP2_ERR_HEADER_COMP;
  292. }
  293. buf->pos -= nv_offset;
  294. if (rv != 0) {
  295. return rv;
  296. }
  297. if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
  298. nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
  299. }
  300. frame->padlen = 0;
  301. frame->hd.length = nghttp2_bufs_len(bufs);
  302. return frame_pack_headers_shared(bufs, &frame->hd);
  303. }
  304. void nghttp2_frame_pack_priority_spec(uint8_t *buf,
  305. const nghttp2_priority_spec *pri_spec) {
  306. nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
  307. if (pri_spec->exclusive) {
  308. buf[0] |= 0x80;
  309. }
  310. buf[4] = (uint8_t)(pri_spec->weight - 1);
  311. }
  312. void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
  313. const uint8_t *payload) {
  314. int32_t dep_stream_id;
  315. uint8_t exclusive;
  316. int32_t weight;
  317. dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  318. exclusive = (payload[0] & 0x80) > 0;
  319. weight = payload[4] + 1;
  320. nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
  321. }
  322. int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
  323. const uint8_t *payload) {
  324. if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
  325. nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
  326. } else {
  327. nghttp2_priority_spec_default_init(&frame->pri_spec);
  328. }
  329. frame->nva = NULL;
  330. frame->nvlen = 0;
  331. return 0;
  332. }
  333. int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
  334. nghttp2_buf *buf;
  335. assert(bufs->head == bufs->cur);
  336. buf = &bufs->head->buf;
  337. assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
  338. buf->pos -= NGHTTP2_FRAME_HDLEN;
  339. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  340. nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
  341. buf->last += NGHTTP2_PRIORITY_SPECLEN;
  342. return 0;
  343. }
  344. void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
  345. const uint8_t *payload) {
  346. nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
  347. }
  348. int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
  349. nghttp2_rst_stream *frame) {
  350. nghttp2_buf *buf;
  351. assert(bufs->head == bufs->cur);
  352. buf = &bufs->head->buf;
  353. assert(nghttp2_buf_avail(buf) >= 4);
  354. buf->pos -= NGHTTP2_FRAME_HDLEN;
  355. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  356. nghttp2_put_uint32be(buf->last, frame->error_code);
  357. buf->last += 4;
  358. return 0;
  359. }
  360. void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
  361. const uint8_t *payload) {
  362. frame->error_code = nghttp2_get_uint32(payload);
  363. }
  364. int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
  365. nghttp2_buf *buf;
  366. assert(bufs->head == bufs->cur);
  367. buf = &bufs->head->buf;
  368. if (nghttp2_buf_avail(buf) < frame->hd.length) {
  369. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  370. }
  371. buf->pos -= NGHTTP2_FRAME_HDLEN;
  372. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  373. buf->last +=
  374. nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
  375. return 0;
  376. }
  377. size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
  378. const nghttp2_settings_entry *iv,
  379. size_t niv) {
  380. size_t i;
  381. for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
  382. nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
  383. nghttp2_put_uint32be(buf + 2, iv[i].value);
  384. }
  385. return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
  386. }
  387. void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
  388. nghttp2_settings_entry *iv,
  389. size_t niv) {
  390. frame->iv = iv;
  391. frame->niv = niv;
  392. }
  393. void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
  394. const uint8_t *payload) {
  395. iv->settings_id = nghttp2_get_uint16(&payload[0]);
  396. iv->value = nghttp2_get_uint32(&payload[2]);
  397. }
  398. int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
  399. size_t *niv_ptr,
  400. const uint8_t *payload,
  401. size_t payloadlen,
  402. nghttp2_mem *mem) {
  403. size_t i;
  404. *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
  405. if (*niv_ptr == 0) {
  406. *iv_ptr = NULL;
  407. return 0;
  408. }
  409. *iv_ptr =
  410. nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
  411. if (*iv_ptr == NULL) {
  412. return NGHTTP2_ERR_NOMEM;
  413. }
  414. for (i = 0; i < *niv_ptr; ++i) {
  415. size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
  416. nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
  417. }
  418. return 0;
  419. }
  420. int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
  421. nghttp2_push_promise *frame,
  422. nghttp2_hd_deflater *deflater) {
  423. size_t nv_offset = 4;
  424. int rv;
  425. nghttp2_buf *buf;
  426. assert(bufs->head == bufs->cur);
  427. buf = &bufs->cur->buf;
  428. buf->pos += nv_offset;
  429. buf->last = buf->pos;
  430. /* This call will adjust buf->last to the correct position */
  431. rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
  432. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  433. rv = NGHTTP2_ERR_HEADER_COMP;
  434. }
  435. buf->pos -= nv_offset;
  436. if (rv != 0) {
  437. return rv;
  438. }
  439. nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
  440. frame->padlen = 0;
  441. frame->hd.length = nghttp2_bufs_len(bufs);
  442. return frame_pack_headers_shared(bufs, &frame->hd);
  443. }
  444. int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
  445. const uint8_t *payload) {
  446. frame->promised_stream_id =
  447. nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  448. frame->nva = NULL;
  449. frame->nvlen = 0;
  450. return 0;
  451. }
  452. int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
  453. nghttp2_buf *buf;
  454. assert(bufs->head == bufs->cur);
  455. buf = &bufs->head->buf;
  456. assert(nghttp2_buf_avail(buf) >= 8);
  457. buf->pos -= NGHTTP2_FRAME_HDLEN;
  458. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  459. buf->last =
  460. nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
  461. return 0;
  462. }
  463. void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
  464. const uint8_t *payload) {
  465. memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
  466. }
  467. int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
  468. int rv;
  469. nghttp2_buf *buf;
  470. assert(bufs->head == bufs->cur);
  471. buf = &bufs->head->buf;
  472. buf->pos -= NGHTTP2_FRAME_HDLEN;
  473. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  474. nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
  475. buf->last += 4;
  476. nghttp2_put_uint32be(buf->last, frame->error_code);
  477. buf->last += 4;
  478. rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
  479. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  480. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  481. }
  482. if (rv != 0) {
  483. return rv;
  484. }
  485. return 0;
  486. }
  487. void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
  488. const uint8_t *payload,
  489. uint8_t *var_gift_payload,
  490. size_t var_gift_payloadlen) {
  491. frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  492. frame->error_code = nghttp2_get_uint32(payload + 4);
  493. frame->opaque_data = var_gift_payload;
  494. frame->opaque_data_len = var_gift_payloadlen;
  495. }
  496. int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
  497. const uint8_t *payload,
  498. size_t payloadlen, nghttp2_mem *mem) {
  499. uint8_t *var_gift_payload;
  500. size_t var_gift_payloadlen;
  501. if (payloadlen > 8) {
  502. var_gift_payloadlen = payloadlen - 8;
  503. } else {
  504. var_gift_payloadlen = 0;
  505. }
  506. payloadlen -= var_gift_payloadlen;
  507. if (!var_gift_payloadlen) {
  508. var_gift_payload = NULL;
  509. } else {
  510. var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
  511. if (var_gift_payload == NULL) {
  512. return NGHTTP2_ERR_NOMEM;
  513. }
  514. memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
  515. }
  516. nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
  517. var_gift_payloadlen);
  518. return 0;
  519. }
  520. int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
  521. nghttp2_window_update *frame) {
  522. nghttp2_buf *buf;
  523. assert(bufs->head == bufs->cur);
  524. buf = &bufs->head->buf;
  525. assert(nghttp2_buf_avail(buf) >= 4);
  526. buf->pos -= NGHTTP2_FRAME_HDLEN;
  527. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  528. nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
  529. buf->last += 4;
  530. return 0;
  531. }
  532. void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
  533. const uint8_t *payload) {
  534. frame->window_size_increment =
  535. nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
  536. }
  537. int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  538. int rv;
  539. nghttp2_buf *buf;
  540. nghttp2_ext_altsvc *altsvc;
  541. /* This is required with --disable-assert. */
  542. (void)rv;
  543. altsvc = frame->payload;
  544. buf = &bufs->head->buf;
  545. assert(nghttp2_buf_avail(buf) >=
  546. 2 + altsvc->origin_len + altsvc->field_value_len);
  547. buf->pos -= NGHTTP2_FRAME_HDLEN;
  548. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  549. nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
  550. buf->last += 2;
  551. rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
  552. assert(rv == 0);
  553. rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
  554. assert(rv == 0);
  555. return 0;
  556. }
  557. void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
  558. size_t origin_len, uint8_t *payload,
  559. size_t payloadlen) {
  560. nghttp2_ext_altsvc *altsvc;
  561. uint8_t *p;
  562. altsvc = frame->payload;
  563. p = payload;
  564. altsvc->origin = p;
  565. p += origin_len;
  566. altsvc->origin_len = origin_len;
  567. altsvc->field_value = p;
  568. altsvc->field_value_len = (size_t)(payload + payloadlen - p);
  569. }
  570. int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
  571. const uint8_t *payload,
  572. size_t payloadlen, nghttp2_mem *mem) {
  573. uint8_t *buf;
  574. size_t origin_len;
  575. if (payloadlen < 2) {
  576. return NGHTTP2_FRAME_SIZE_ERROR;
  577. }
  578. origin_len = nghttp2_get_uint16(payload);
  579. buf = nghttp2_mem_malloc(mem, payloadlen - 2);
  580. if (!buf) {
  581. return NGHTTP2_ERR_NOMEM;
  582. }
  583. nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
  584. nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
  585. return 0;
  586. }
  587. int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  588. nghttp2_buf *buf;
  589. nghttp2_ext_origin *origin;
  590. nghttp2_origin_entry *orig;
  591. size_t i;
  592. origin = frame->payload;
  593. buf = &bufs->head->buf;
  594. if (nghttp2_buf_avail(buf) < frame->hd.length) {
  595. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  596. }
  597. buf->pos -= NGHTTP2_FRAME_HDLEN;
  598. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  599. for (i = 0; i < origin->nov; ++i) {
  600. orig = &origin->ov[i];
  601. nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
  602. buf->last += 2;
  603. buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
  604. }
  605. assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
  606. return 0;
  607. }
  608. int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
  609. const uint8_t *payload,
  610. size_t payloadlen, nghttp2_mem *mem) {
  611. nghttp2_ext_origin *origin;
  612. const uint8_t *p, *end;
  613. uint8_t *dst;
  614. size_t originlen;
  615. nghttp2_origin_entry *ov;
  616. size_t nov = 0;
  617. size_t len = 0;
  618. origin = frame->payload;
  619. p = end = payload;
  620. if (payloadlen) {
  621. end += payloadlen;
  622. }
  623. for (; p != end;) {
  624. if (end - p < 2) {
  625. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  626. }
  627. originlen = nghttp2_get_uint16(p);
  628. p += 2;
  629. if (originlen == 0) {
  630. continue;
  631. }
  632. if (originlen > (size_t)(end - p)) {
  633. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  634. }
  635. p += originlen;
  636. /* 1 for terminal NULL */
  637. len += originlen + 1;
  638. ++nov;
  639. }
  640. if (nov == 0) {
  641. origin->ov = NULL;
  642. origin->nov = 0;
  643. return 0;
  644. }
  645. len += nov * sizeof(nghttp2_origin_entry);
  646. ov = nghttp2_mem_malloc(mem, len);
  647. if (ov == NULL) {
  648. return NGHTTP2_ERR_NOMEM;
  649. }
  650. origin->ov = ov;
  651. origin->nov = nov;
  652. dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
  653. p = payload;
  654. for (; p != end;) {
  655. originlen = nghttp2_get_uint16(p);
  656. p += 2;
  657. if (originlen == 0) {
  658. continue;
  659. }
  660. ov->origin = dst;
  661. ov->origin_len = originlen;
  662. dst = nghttp2_cpymem(dst, p, originlen);
  663. *dst++ = '\0';
  664. p += originlen;
  665. ++ov;
  666. }
  667. return 0;
  668. }
  669. nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
  670. size_t niv, nghttp2_mem *mem) {
  671. nghttp2_settings_entry *iv_copy;
  672. size_t len = niv * sizeof(nghttp2_settings_entry);
  673. if (len == 0) {
  674. return NULL;
  675. }
  676. iv_copy = nghttp2_mem_malloc(mem, len);
  677. if (iv_copy == NULL) {
  678. return NULL;
  679. }
  680. memcpy(iv_copy, iv, len);
  681. return iv_copy;
  682. }
  683. int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
  684. if (a->namelen != b->namelen || a->valuelen != b->valuelen) {
  685. return 0;
  686. }
  687. if (a->name == NULL || b->name == NULL) {
  688. assert(a->namelen == 0);
  689. assert(b->namelen == 0);
  690. } else if (memcmp(a->name, b->name, a->namelen) != 0) {
  691. return 0;
  692. }
  693. if (a->value == NULL || b->value == NULL) {
  694. assert(a->valuelen == 0);
  695. assert(b->valuelen == 0);
  696. } else if (memcmp(a->value, b->value, a->valuelen) != 0) {
  697. return 0;
  698. }
  699. return 1;
  700. }
  701. void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
  702. nghttp2_mem_free(mem, nva);
  703. }
  704. static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
  705. size_t blen) {
  706. int rv;
  707. if (alen == blen) {
  708. return memcmp(a, b, alen);
  709. }
  710. if (alen < blen) {
  711. rv = memcmp(a, b, alen);
  712. if (rv == 0) {
  713. return -1;
  714. }
  715. return rv;
  716. }
  717. rv = memcmp(a, b, blen);
  718. if (rv == 0) {
  719. return 1;
  720. }
  721. return rv;
  722. }
  723. int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
  724. return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
  725. }
  726. static int nv_compar(const void *lhs, const void *rhs) {
  727. const nghttp2_nv *a = (const nghttp2_nv *)lhs;
  728. const nghttp2_nv *b = (const nghttp2_nv *)rhs;
  729. int rv;
  730. rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
  731. if (rv == 0) {
  732. return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
  733. }
  734. return rv;
  735. }
  736. void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
  737. qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
  738. }
  739. int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
  740. size_t nvlen, nghttp2_mem *mem) {
  741. size_t i;
  742. uint8_t *data = NULL;
  743. size_t buflen = 0;
  744. nghttp2_nv *p;
  745. if (nvlen == 0) {
  746. *nva_ptr = NULL;
  747. return 0;
  748. }
  749. for (i = 0; i < nvlen; ++i) {
  750. /* + 1 for null-termination */
  751. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
  752. buflen += nva[i].namelen + 1;
  753. }
  754. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
  755. buflen += nva[i].valuelen + 1;
  756. }
  757. }
  758. buflen += sizeof(nghttp2_nv) * nvlen;
  759. *nva_ptr = nghttp2_mem_malloc(mem, buflen);
  760. if (*nva_ptr == NULL) {
  761. return NGHTTP2_ERR_NOMEM;
  762. }
  763. p = *nva_ptr;
  764. data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
  765. for (i = 0; i < nvlen; ++i) {
  766. p->flags = nva[i].flags;
  767. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
  768. p->name = nva[i].name;
  769. p->namelen = nva[i].namelen;
  770. } else {
  771. if (nva[i].namelen) {
  772. memcpy(data, nva[i].name, nva[i].namelen);
  773. }
  774. p->name = data;
  775. p->namelen = nva[i].namelen;
  776. data[p->namelen] = '\0';
  777. nghttp2_downcase(p->name, p->namelen);
  778. data += nva[i].namelen + 1;
  779. }
  780. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
  781. p->value = nva[i].value;
  782. p->valuelen = nva[i].valuelen;
  783. } else {
  784. if (nva[i].valuelen) {
  785. memcpy(data, nva[i].value, nva[i].valuelen);
  786. }
  787. p->value = data;
  788. p->valuelen = nva[i].valuelen;
  789. data[p->valuelen] = '\0';
  790. data += nva[i].valuelen + 1;
  791. }
  792. ++p;
  793. }
  794. return 0;
  795. }
  796. int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
  797. size_t i;
  798. for (i = 0; i < niv; ++i) {
  799. switch (iv[i].settings_id) {
  800. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  801. break;
  802. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  803. break;
  804. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  805. if (iv[i].value != 0 && iv[i].value != 1) {
  806. return 0;
  807. }
  808. break;
  809. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  810. if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
  811. return 0;
  812. }
  813. break;
  814. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  815. if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
  816. iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
  817. return 0;
  818. }
  819. break;
  820. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  821. break;
  822. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  823. if (iv[i].value != 0 && iv[i].value != 1) {
  824. return 0;
  825. }
  826. break;
  827. }
  828. }
  829. return 1;
  830. }
  831. static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
  832. size_t trail_padlen;
  833. size_t newlen;
  834. DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
  835. memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
  836. --buf->pos;
  837. buf->pos[4] |= NGHTTP2_FLAG_PADDED;
  838. newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
  839. nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
  840. if (framehd_only) {
  841. return;
  842. }
  843. trail_padlen = padlen - 1;
  844. buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
  845. /* zero out padding */
  846. memset(buf->last, 0, trail_padlen);
  847. /* extend buffers trail_padlen bytes, since we ate previous padlen -
  848. trail_padlen byte(s) */
  849. buf->last += trail_padlen;
  850. }
  851. int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
  852. size_t padlen, int framehd_only) {
  853. nghttp2_buf *buf;
  854. if (padlen == 0) {
  855. DEBUGF("send: padlen = 0, nothing to do\n");
  856. return 0;
  857. }
  858. /*
  859. * We have arranged bufs like this:
  860. *
  861. * 0 1 2 3
  862. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  863. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  864. * | |Frame header | Frame payload... :
  865. * +-+-----------------+-------------------------------------------+
  866. * | |Frame header | Frame payload... :
  867. * +-+-----------------+-------------------------------------------+
  868. * | |Frame header | Frame payload... :
  869. * +-+-----------------+-------------------------------------------+
  870. *
  871. * We arranged padding so that it is included in the first frame
  872. * completely. For padded frame, we are going to adjust buf->pos of
  873. * frame which includes padding and serialize (memmove) frame header
  874. * in the correct position. Also extends buf->last to include
  875. * padding.
  876. */
  877. buf = &bufs->head->buf;
  878. assert(nghttp2_buf_avail(buf) >= padlen - 1);
  879. frame_set_pad(buf, padlen, framehd_only);
  880. hd->length += padlen;
  881. hd->flags |= NGHTTP2_FLAG_PADDED;
  882. DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
  883. return 0;
  884. }