nghttp2_frame.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. if (!var_gift_payloadlen) {
  507. var_gift_payload = NULL;
  508. } else {
  509. var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
  510. if (var_gift_payload == NULL) {
  511. return NGHTTP2_ERR_NOMEM;
  512. }
  513. memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
  514. }
  515. nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
  516. var_gift_payloadlen);
  517. return 0;
  518. }
  519. int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
  520. nghttp2_window_update *frame) {
  521. nghttp2_buf *buf;
  522. assert(bufs->head == bufs->cur);
  523. buf = &bufs->head->buf;
  524. assert(nghttp2_buf_avail(buf) >= 4);
  525. buf->pos -= NGHTTP2_FRAME_HDLEN;
  526. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  527. nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
  528. buf->last += 4;
  529. return 0;
  530. }
  531. void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
  532. const uint8_t *payload) {
  533. frame->window_size_increment =
  534. nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
  535. }
  536. int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  537. int rv;
  538. nghttp2_buf *buf;
  539. nghttp2_ext_altsvc *altsvc;
  540. /* This is required with --disable-assert. */
  541. (void)rv;
  542. altsvc = frame->payload;
  543. buf = &bufs->head->buf;
  544. assert(nghttp2_buf_avail(buf) >=
  545. 2 + altsvc->origin_len + altsvc->field_value_len);
  546. buf->pos -= NGHTTP2_FRAME_HDLEN;
  547. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  548. nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
  549. buf->last += 2;
  550. rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
  551. assert(rv == 0);
  552. rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
  553. assert(rv == 0);
  554. return 0;
  555. }
  556. void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
  557. size_t origin_len, uint8_t *payload,
  558. size_t payloadlen) {
  559. nghttp2_ext_altsvc *altsvc;
  560. uint8_t *p;
  561. altsvc = frame->payload;
  562. p = payload;
  563. altsvc->origin = p;
  564. p += origin_len;
  565. altsvc->origin_len = origin_len;
  566. altsvc->field_value = p;
  567. altsvc->field_value_len = (size_t)(payload + payloadlen - p);
  568. }
  569. int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
  570. const uint8_t *payload,
  571. size_t payloadlen, nghttp2_mem *mem) {
  572. uint8_t *buf;
  573. size_t origin_len;
  574. if (payloadlen < 2) {
  575. return NGHTTP2_FRAME_SIZE_ERROR;
  576. }
  577. origin_len = nghttp2_get_uint16(payload);
  578. buf = nghttp2_mem_malloc(mem, payloadlen - 2);
  579. if (!buf) {
  580. return NGHTTP2_ERR_NOMEM;
  581. }
  582. nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
  583. nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
  584. return 0;
  585. }
  586. int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  587. nghttp2_buf *buf;
  588. nghttp2_ext_origin *origin;
  589. nghttp2_origin_entry *orig;
  590. size_t i;
  591. origin = frame->payload;
  592. buf = &bufs->head->buf;
  593. if (nghttp2_buf_avail(buf) < frame->hd.length) {
  594. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  595. }
  596. buf->pos -= NGHTTP2_FRAME_HDLEN;
  597. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  598. for (i = 0; i < origin->nov; ++i) {
  599. orig = &origin->ov[i];
  600. nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
  601. buf->last += 2;
  602. buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
  603. }
  604. assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
  605. return 0;
  606. }
  607. int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
  608. const uint8_t *payload,
  609. size_t payloadlen, nghttp2_mem *mem) {
  610. nghttp2_ext_origin *origin;
  611. const uint8_t *p, *end;
  612. uint8_t *dst;
  613. size_t originlen;
  614. nghttp2_origin_entry *ov;
  615. size_t nov = 0;
  616. size_t len = 0;
  617. origin = frame->payload;
  618. p = end = payload;
  619. if (payloadlen) {
  620. end += payloadlen;
  621. }
  622. for (; p != end;) {
  623. if (end - p < 2) {
  624. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  625. }
  626. originlen = nghttp2_get_uint16(p);
  627. p += 2;
  628. if (originlen == 0) {
  629. continue;
  630. }
  631. if (originlen > (size_t)(end - p)) {
  632. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  633. }
  634. p += originlen;
  635. /* 1 for terminal NULL */
  636. len += originlen + 1;
  637. ++nov;
  638. }
  639. if (nov == 0) {
  640. origin->ov = NULL;
  641. origin->nov = 0;
  642. return 0;
  643. }
  644. len += nov * sizeof(nghttp2_origin_entry);
  645. ov = nghttp2_mem_malloc(mem, len);
  646. if (ov == NULL) {
  647. return NGHTTP2_ERR_NOMEM;
  648. }
  649. origin->ov = ov;
  650. origin->nov = nov;
  651. dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
  652. p = payload;
  653. for (; p != end;) {
  654. originlen = nghttp2_get_uint16(p);
  655. p += 2;
  656. if (originlen == 0) {
  657. continue;
  658. }
  659. ov->origin = dst;
  660. ov->origin_len = originlen;
  661. dst = nghttp2_cpymem(dst, p, originlen);
  662. *dst++ = '\0';
  663. p += originlen;
  664. ++ov;
  665. }
  666. return 0;
  667. }
  668. nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
  669. size_t niv, nghttp2_mem *mem) {
  670. nghttp2_settings_entry *iv_copy;
  671. size_t len = niv * sizeof(nghttp2_settings_entry);
  672. if (len == 0) {
  673. return NULL;
  674. }
  675. iv_copy = nghttp2_mem_malloc(mem, len);
  676. if (iv_copy == NULL) {
  677. return NULL;
  678. }
  679. memcpy(iv_copy, iv, len);
  680. return iv_copy;
  681. }
  682. int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
  683. if (a->namelen != b->namelen || a->valuelen != b->valuelen) {
  684. return 0;
  685. }
  686. if (a->name == NULL || b->name == NULL) {
  687. assert(a->namelen == 0);
  688. assert(b->namelen == 0);
  689. } else if (memcmp(a->name, b->name, a->namelen) != 0) {
  690. return 0;
  691. }
  692. if (a->value == NULL || b->value == NULL) {
  693. assert(a->valuelen == 0);
  694. assert(b->valuelen == 0);
  695. } else if (memcmp(a->value, b->value, a->valuelen) != 0) {
  696. return 0;
  697. }
  698. return 1;
  699. }
  700. void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
  701. nghttp2_mem_free(mem, nva);
  702. }
  703. static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
  704. size_t blen) {
  705. int rv;
  706. if (alen == blen) {
  707. return memcmp(a, b, alen);
  708. }
  709. if (alen < blen) {
  710. rv = memcmp(a, b, alen);
  711. if (rv == 0) {
  712. return -1;
  713. }
  714. return rv;
  715. }
  716. rv = memcmp(a, b, blen);
  717. if (rv == 0) {
  718. return 1;
  719. }
  720. return rv;
  721. }
  722. int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
  723. return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
  724. }
  725. static int nv_compar(const void *lhs, const void *rhs) {
  726. const nghttp2_nv *a = (const nghttp2_nv *)lhs;
  727. const nghttp2_nv *b = (const nghttp2_nv *)rhs;
  728. int rv;
  729. rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
  730. if (rv == 0) {
  731. return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
  732. }
  733. return rv;
  734. }
  735. void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
  736. qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
  737. }
  738. int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
  739. size_t nvlen, nghttp2_mem *mem) {
  740. size_t i;
  741. uint8_t *data = NULL;
  742. size_t buflen = 0;
  743. nghttp2_nv *p;
  744. if (nvlen == 0) {
  745. *nva_ptr = NULL;
  746. return 0;
  747. }
  748. for (i = 0; i < nvlen; ++i) {
  749. /* + 1 for null-termination */
  750. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
  751. buflen += nva[i].namelen + 1;
  752. }
  753. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
  754. buflen += nva[i].valuelen + 1;
  755. }
  756. }
  757. buflen += sizeof(nghttp2_nv) * nvlen;
  758. *nva_ptr = nghttp2_mem_malloc(mem, buflen);
  759. if (*nva_ptr == NULL) {
  760. return NGHTTP2_ERR_NOMEM;
  761. }
  762. p = *nva_ptr;
  763. data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
  764. for (i = 0; i < nvlen; ++i) {
  765. p->flags = nva[i].flags;
  766. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
  767. p->name = nva[i].name;
  768. p->namelen = nva[i].namelen;
  769. } else {
  770. if (nva[i].namelen) {
  771. memcpy(data, nva[i].name, nva[i].namelen);
  772. }
  773. p->name = data;
  774. p->namelen = nva[i].namelen;
  775. data[p->namelen] = '\0';
  776. nghttp2_downcase(p->name, p->namelen);
  777. data += nva[i].namelen + 1;
  778. }
  779. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
  780. p->value = nva[i].value;
  781. p->valuelen = nva[i].valuelen;
  782. } else {
  783. if (nva[i].valuelen) {
  784. memcpy(data, nva[i].value, nva[i].valuelen);
  785. }
  786. p->value = data;
  787. p->valuelen = nva[i].valuelen;
  788. data[p->valuelen] = '\0';
  789. data += nva[i].valuelen + 1;
  790. }
  791. ++p;
  792. }
  793. return 0;
  794. }
  795. int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
  796. size_t i;
  797. for (i = 0; i < niv; ++i) {
  798. switch (iv[i].settings_id) {
  799. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  800. break;
  801. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  802. break;
  803. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  804. if (iv[i].value != 0 && iv[i].value != 1) {
  805. return 0;
  806. }
  807. break;
  808. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  809. if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
  810. return 0;
  811. }
  812. break;
  813. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  814. if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
  815. iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
  816. return 0;
  817. }
  818. break;
  819. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  820. break;
  821. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  822. if (iv[i].value != 0 && iv[i].value != 1) {
  823. return 0;
  824. }
  825. break;
  826. }
  827. }
  828. return 1;
  829. }
  830. static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
  831. size_t trail_padlen;
  832. size_t newlen;
  833. DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
  834. memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
  835. --buf->pos;
  836. buf->pos[4] |= NGHTTP2_FLAG_PADDED;
  837. newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
  838. nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
  839. if (framehd_only) {
  840. return;
  841. }
  842. trail_padlen = padlen - 1;
  843. buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
  844. /* zero out padding */
  845. memset(buf->last, 0, trail_padlen);
  846. /* extend buffers trail_padlen bytes, since we ate previous padlen -
  847. trail_padlen byte(s) */
  848. buf->last += trail_padlen;
  849. }
  850. int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
  851. size_t padlen, int framehd_only) {
  852. nghttp2_buf *buf;
  853. if (padlen == 0) {
  854. DEBUGF("send: padlen = 0, nothing to do\n");
  855. return 0;
  856. }
  857. /*
  858. * We have arranged bufs like this:
  859. *
  860. * 0 1 2 3
  861. * 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
  862. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  863. * | |Frame header | Frame payload... :
  864. * +-+-----------------+-------------------------------------------+
  865. * | |Frame header | Frame payload... :
  866. * +-+-----------------+-------------------------------------------+
  867. * | |Frame header | Frame payload... :
  868. * +-+-----------------+-------------------------------------------+
  869. *
  870. * We arranged padding so that it is included in the first frame
  871. * completely. For padded frame, we are going to adjust buf->pos of
  872. * frame which includes padding and serialize (memmove) frame header
  873. * in the correct position. Also extends buf->last to include
  874. * padding.
  875. */
  876. buf = &bufs->head->buf;
  877. assert(nghttp2_buf_avail(buf) >= padlen - 1);
  878. frame_set_pad(buf, padlen, framehd_only);
  879. hd->length += padlen;
  880. hd->flags |= NGHTTP2_FLAG_PADDED;
  881. DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
  882. return 0;
  883. }