nghttp2_frame.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. void nghttp2_frame_priority_update_init(nghttp2_extension *frame,
  218. int32_t stream_id, uint8_t *field_value,
  219. size_t field_value_len) {
  220. nghttp2_ext_priority_update *priority_update;
  221. nghttp2_frame_hd_init(&frame->hd, 4 + field_value_len,
  222. NGHTTP2_PRIORITY_UPDATE, NGHTTP2_FLAG_NONE, 0);
  223. priority_update = frame->payload;
  224. priority_update->stream_id = stream_id;
  225. priority_update->field_value = field_value;
  226. priority_update->field_value_len = field_value_len;
  227. }
  228. void nghttp2_frame_priority_update_free(nghttp2_extension *frame,
  229. nghttp2_mem *mem) {
  230. nghttp2_ext_priority_update *priority_update;
  231. priority_update = frame->payload;
  232. if (priority_update == NULL) {
  233. return;
  234. }
  235. nghttp2_mem_free(mem, priority_update->field_value);
  236. }
  237. size_t nghttp2_frame_priority_len(uint8_t flags) {
  238. if (flags & NGHTTP2_FLAG_PRIORITY) {
  239. return NGHTTP2_PRIORITY_SPECLEN;
  240. }
  241. return 0;
  242. }
  243. size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
  244. return nghttp2_frame_priority_len(frame->hd.flags);
  245. }
  246. /*
  247. * Call this function after payload was serialized, but not before
  248. * changing buf->pos and serializing frame header.
  249. *
  250. * This function assumes bufs->cur points to the last buf chain of the
  251. * frame(s).
  252. *
  253. * This function serializes frame header for HEADERS/PUSH_PROMISE and
  254. * handles their successive CONTINUATION frames.
  255. *
  256. * We don't process any padding here.
  257. */
  258. static int frame_pack_headers_shared(nghttp2_bufs *bufs,
  259. nghttp2_frame_hd *frame_hd) {
  260. nghttp2_buf *buf;
  261. nghttp2_buf_chain *ci, *ce;
  262. nghttp2_frame_hd hd;
  263. buf = &bufs->head->buf;
  264. hd = *frame_hd;
  265. hd.length = nghttp2_buf_len(buf);
  266. DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
  267. /* We have multiple frame buffers, which means one or more
  268. CONTINUATION frame is involved. Remove END_HEADERS flag from the
  269. first frame. */
  270. if (bufs->head != bufs->cur) {
  271. hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
  272. }
  273. buf->pos -= NGHTTP2_FRAME_HDLEN;
  274. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  275. if (bufs->head != bufs->cur) {
  276. /* 2nd and later frames are CONTINUATION frames. */
  277. hd.type = NGHTTP2_CONTINUATION;
  278. /* We don't have no flags except for last CONTINUATION */
  279. hd.flags = NGHTTP2_FLAG_NONE;
  280. ce = bufs->cur;
  281. for (ci = bufs->head->next; ci != ce; ci = ci->next) {
  282. buf = &ci->buf;
  283. hd.length = nghttp2_buf_len(buf);
  284. DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
  285. buf->pos -= NGHTTP2_FRAME_HDLEN;
  286. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  287. }
  288. buf = &ci->buf;
  289. hd.length = nghttp2_buf_len(buf);
  290. /* Set END_HEADERS flag for last CONTINUATION */
  291. hd.flags = NGHTTP2_FLAG_END_HEADERS;
  292. DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
  293. buf->pos -= NGHTTP2_FRAME_HDLEN;
  294. nghttp2_frame_pack_frame_hd(buf->pos, &hd);
  295. }
  296. return 0;
  297. }
  298. int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
  299. nghttp2_hd_deflater *deflater) {
  300. size_t nv_offset;
  301. int rv;
  302. nghttp2_buf *buf;
  303. assert(bufs->head == bufs->cur);
  304. nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
  305. buf = &bufs->cur->buf;
  306. buf->pos += nv_offset;
  307. buf->last = buf->pos;
  308. /* This call will adjust buf->last to the correct position */
  309. rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
  310. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  311. rv = NGHTTP2_ERR_HEADER_COMP;
  312. }
  313. buf->pos -= nv_offset;
  314. if (rv != 0) {
  315. return rv;
  316. }
  317. if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
  318. nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
  319. }
  320. frame->padlen = 0;
  321. frame->hd.length = nghttp2_bufs_len(bufs);
  322. return frame_pack_headers_shared(bufs, &frame->hd);
  323. }
  324. void nghttp2_frame_pack_priority_spec(uint8_t *buf,
  325. const nghttp2_priority_spec *pri_spec) {
  326. nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
  327. if (pri_spec->exclusive) {
  328. buf[0] |= 0x80;
  329. }
  330. buf[4] = (uint8_t)(pri_spec->weight - 1);
  331. }
  332. void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
  333. const uint8_t *payload) {
  334. int32_t dep_stream_id;
  335. uint8_t exclusive;
  336. int32_t weight;
  337. dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  338. exclusive = (payload[0] & 0x80) > 0;
  339. weight = payload[4] + 1;
  340. nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
  341. }
  342. void nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
  343. const uint8_t *payload) {
  344. if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
  345. nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
  346. } else {
  347. nghttp2_priority_spec_default_init(&frame->pri_spec);
  348. }
  349. frame->nva = NULL;
  350. frame->nvlen = 0;
  351. }
  352. void nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
  353. nghttp2_buf *buf;
  354. assert(bufs->head == bufs->cur);
  355. buf = &bufs->head->buf;
  356. assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
  357. buf->pos -= NGHTTP2_FRAME_HDLEN;
  358. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  359. nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
  360. buf->last += NGHTTP2_PRIORITY_SPECLEN;
  361. }
  362. void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
  363. const uint8_t *payload) {
  364. nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
  365. }
  366. void nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
  367. nghttp2_rst_stream *frame) {
  368. nghttp2_buf *buf;
  369. assert(bufs->head == bufs->cur);
  370. buf = &bufs->head->buf;
  371. assert(nghttp2_buf_avail(buf) >= 4);
  372. buf->pos -= NGHTTP2_FRAME_HDLEN;
  373. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  374. nghttp2_put_uint32be(buf->last, frame->error_code);
  375. buf->last += 4;
  376. }
  377. void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
  378. const uint8_t *payload) {
  379. frame->error_code = nghttp2_get_uint32(payload);
  380. }
  381. int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
  382. nghttp2_buf *buf;
  383. assert(bufs->head == bufs->cur);
  384. buf = &bufs->head->buf;
  385. if (nghttp2_buf_avail(buf) < frame->hd.length) {
  386. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  387. }
  388. buf->pos -= NGHTTP2_FRAME_HDLEN;
  389. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  390. buf->last +=
  391. nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
  392. return 0;
  393. }
  394. size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
  395. const nghttp2_settings_entry *iv,
  396. size_t niv) {
  397. size_t i;
  398. for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
  399. nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
  400. nghttp2_put_uint32be(buf + 2, iv[i].value);
  401. }
  402. return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
  403. }
  404. void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
  405. nghttp2_settings_entry *iv,
  406. size_t niv) {
  407. frame->iv = iv;
  408. frame->niv = niv;
  409. }
  410. void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
  411. const uint8_t *payload) {
  412. iv->settings_id = nghttp2_get_uint16(&payload[0]);
  413. iv->value = nghttp2_get_uint32(&payload[2]);
  414. }
  415. int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
  416. size_t *niv_ptr,
  417. const uint8_t *payload,
  418. size_t payloadlen,
  419. nghttp2_mem *mem) {
  420. size_t i;
  421. *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
  422. if (*niv_ptr == 0) {
  423. *iv_ptr = NULL;
  424. return 0;
  425. }
  426. *iv_ptr =
  427. nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
  428. if (*iv_ptr == NULL) {
  429. return NGHTTP2_ERR_NOMEM;
  430. }
  431. for (i = 0; i < *niv_ptr; ++i) {
  432. size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
  433. nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
  434. }
  435. return 0;
  436. }
  437. int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
  438. nghttp2_push_promise *frame,
  439. nghttp2_hd_deflater *deflater) {
  440. size_t nv_offset = 4;
  441. int rv;
  442. nghttp2_buf *buf;
  443. assert(bufs->head == bufs->cur);
  444. buf = &bufs->cur->buf;
  445. buf->pos += nv_offset;
  446. buf->last = buf->pos;
  447. /* This call will adjust buf->last to the correct position */
  448. rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
  449. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  450. rv = NGHTTP2_ERR_HEADER_COMP;
  451. }
  452. buf->pos -= nv_offset;
  453. if (rv != 0) {
  454. return rv;
  455. }
  456. nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
  457. frame->padlen = 0;
  458. frame->hd.length = nghttp2_bufs_len(bufs);
  459. return frame_pack_headers_shared(bufs, &frame->hd);
  460. }
  461. void nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
  462. const uint8_t *payload) {
  463. frame->promised_stream_id =
  464. nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  465. frame->nva = NULL;
  466. frame->nvlen = 0;
  467. }
  468. void nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
  469. nghttp2_buf *buf;
  470. assert(bufs->head == bufs->cur);
  471. buf = &bufs->head->buf;
  472. assert(nghttp2_buf_avail(buf) >= 8);
  473. buf->pos -= NGHTTP2_FRAME_HDLEN;
  474. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  475. buf->last =
  476. nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
  477. }
  478. void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
  479. const uint8_t *payload) {
  480. memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
  481. }
  482. int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
  483. int rv;
  484. nghttp2_buf *buf;
  485. assert(bufs->head == bufs->cur);
  486. buf = &bufs->head->buf;
  487. buf->pos -= NGHTTP2_FRAME_HDLEN;
  488. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  489. nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
  490. buf->last += 4;
  491. nghttp2_put_uint32be(buf->last, frame->error_code);
  492. buf->last += 4;
  493. rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
  494. if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
  495. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  496. }
  497. if (rv != 0) {
  498. return rv;
  499. }
  500. return 0;
  501. }
  502. void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
  503. const uint8_t *payload,
  504. uint8_t *var_gift_payload,
  505. size_t var_gift_payloadlen) {
  506. frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  507. frame->error_code = nghttp2_get_uint32(payload + 4);
  508. frame->opaque_data = var_gift_payload;
  509. frame->opaque_data_len = var_gift_payloadlen;
  510. }
  511. int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
  512. const uint8_t *payload,
  513. size_t payloadlen, nghttp2_mem *mem) {
  514. uint8_t *var_gift_payload;
  515. size_t var_gift_payloadlen;
  516. if (payloadlen > 8) {
  517. var_gift_payloadlen = payloadlen - 8;
  518. } else {
  519. var_gift_payloadlen = 0;
  520. }
  521. if (!var_gift_payloadlen) {
  522. var_gift_payload = NULL;
  523. } else {
  524. var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
  525. if (var_gift_payload == NULL) {
  526. return NGHTTP2_ERR_NOMEM;
  527. }
  528. memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
  529. }
  530. nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
  531. var_gift_payloadlen);
  532. return 0;
  533. }
  534. void nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
  535. nghttp2_window_update *frame) {
  536. nghttp2_buf *buf;
  537. assert(bufs->head == bufs->cur);
  538. buf = &bufs->head->buf;
  539. assert(nghttp2_buf_avail(buf) >= 4);
  540. buf->pos -= NGHTTP2_FRAME_HDLEN;
  541. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  542. nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
  543. buf->last += 4;
  544. }
  545. void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
  546. const uint8_t *payload) {
  547. frame->window_size_increment =
  548. nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
  549. }
  550. void nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  551. int rv;
  552. nghttp2_buf *buf;
  553. nghttp2_ext_altsvc *altsvc;
  554. /* This is required with --disable-assert. */
  555. (void)rv;
  556. altsvc = frame->payload;
  557. buf = &bufs->head->buf;
  558. assert(nghttp2_buf_avail(buf) >=
  559. 2 + altsvc->origin_len + altsvc->field_value_len);
  560. buf->pos -= NGHTTP2_FRAME_HDLEN;
  561. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  562. nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
  563. buf->last += 2;
  564. rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
  565. assert(rv == 0);
  566. rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
  567. assert(rv == 0);
  568. }
  569. void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
  570. size_t origin_len, uint8_t *payload,
  571. size_t payloadlen) {
  572. nghttp2_ext_altsvc *altsvc;
  573. uint8_t *p;
  574. altsvc = frame->payload;
  575. p = payload;
  576. altsvc->origin = p;
  577. p += origin_len;
  578. altsvc->origin_len = origin_len;
  579. altsvc->field_value = p;
  580. altsvc->field_value_len = (size_t)(payload + payloadlen - p);
  581. }
  582. int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
  583. const uint8_t *payload,
  584. size_t payloadlen, nghttp2_mem *mem) {
  585. uint8_t *buf;
  586. size_t origin_len;
  587. if (payloadlen < 2) {
  588. return NGHTTP2_FRAME_SIZE_ERROR;
  589. }
  590. origin_len = nghttp2_get_uint16(payload);
  591. buf = nghttp2_mem_malloc(mem, payloadlen - 2);
  592. if (!buf) {
  593. return NGHTTP2_ERR_NOMEM;
  594. }
  595. nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
  596. nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
  597. return 0;
  598. }
  599. int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
  600. nghttp2_buf *buf;
  601. nghttp2_ext_origin *origin;
  602. nghttp2_origin_entry *orig;
  603. size_t i;
  604. origin = frame->payload;
  605. buf = &bufs->head->buf;
  606. if (nghttp2_buf_avail(buf) < frame->hd.length) {
  607. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  608. }
  609. buf->pos -= NGHTTP2_FRAME_HDLEN;
  610. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  611. for (i = 0; i < origin->nov; ++i) {
  612. orig = &origin->ov[i];
  613. nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
  614. buf->last += 2;
  615. buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
  616. }
  617. assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
  618. return 0;
  619. }
  620. int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
  621. const uint8_t *payload,
  622. size_t payloadlen, nghttp2_mem *mem) {
  623. nghttp2_ext_origin *origin;
  624. const uint8_t *p, *end;
  625. uint8_t *dst;
  626. size_t originlen;
  627. nghttp2_origin_entry *ov;
  628. size_t nov = 0;
  629. size_t len = 0;
  630. origin = frame->payload;
  631. p = end = payload;
  632. if (payloadlen) {
  633. end += payloadlen;
  634. }
  635. for (; p != end;) {
  636. if (end - p < 2) {
  637. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  638. }
  639. originlen = nghttp2_get_uint16(p);
  640. p += 2;
  641. if (originlen == 0) {
  642. continue;
  643. }
  644. if (originlen > (size_t)(end - p)) {
  645. return NGHTTP2_ERR_FRAME_SIZE_ERROR;
  646. }
  647. p += originlen;
  648. /* 1 for terminal NULL */
  649. len += originlen + 1;
  650. ++nov;
  651. }
  652. if (nov == 0) {
  653. origin->ov = NULL;
  654. origin->nov = 0;
  655. return 0;
  656. }
  657. len += nov * sizeof(nghttp2_origin_entry);
  658. ov = nghttp2_mem_malloc(mem, len);
  659. if (ov == NULL) {
  660. return NGHTTP2_ERR_NOMEM;
  661. }
  662. origin->ov = ov;
  663. origin->nov = nov;
  664. dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
  665. p = payload;
  666. for (; p != end;) {
  667. originlen = nghttp2_get_uint16(p);
  668. p += 2;
  669. if (originlen == 0) {
  670. continue;
  671. }
  672. ov->origin = dst;
  673. ov->origin_len = originlen;
  674. dst = nghttp2_cpymem(dst, p, originlen);
  675. *dst++ = '\0';
  676. p += originlen;
  677. ++ov;
  678. }
  679. return 0;
  680. }
  681. void nghttp2_frame_pack_priority_update(nghttp2_bufs *bufs,
  682. nghttp2_extension *frame) {
  683. int rv;
  684. nghttp2_buf *buf;
  685. nghttp2_ext_priority_update *priority_update;
  686. /* This is required with --disable-assert. */
  687. (void)rv;
  688. priority_update = frame->payload;
  689. buf = &bufs->head->buf;
  690. assert(nghttp2_buf_avail(buf) >= 4 + priority_update->field_value_len);
  691. buf->pos -= NGHTTP2_FRAME_HDLEN;
  692. nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
  693. nghttp2_put_uint32be(buf->last, (uint32_t)priority_update->stream_id);
  694. buf->last += 4;
  695. rv = nghttp2_bufs_add(bufs, priority_update->field_value,
  696. priority_update->field_value_len);
  697. assert(rv == 0);
  698. }
  699. void nghttp2_frame_unpack_priority_update_payload(nghttp2_extension *frame,
  700. uint8_t *payload,
  701. size_t payloadlen) {
  702. nghttp2_ext_priority_update *priority_update;
  703. assert(payloadlen >= 4);
  704. priority_update = frame->payload;
  705. priority_update->stream_id =
  706. nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
  707. if (payloadlen > 4) {
  708. priority_update->field_value = payload + 4;
  709. priority_update->field_value_len = payloadlen - 4;
  710. } else {
  711. priority_update->field_value = NULL;
  712. priority_update->field_value_len = 0;
  713. }
  714. }
  715. nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
  716. size_t niv, nghttp2_mem *mem) {
  717. nghttp2_settings_entry *iv_copy;
  718. size_t len = niv * sizeof(nghttp2_settings_entry);
  719. if (len == 0) {
  720. return NULL;
  721. }
  722. iv_copy = nghttp2_mem_malloc(mem, len);
  723. if (iv_copy == NULL) {
  724. return NULL;
  725. }
  726. memcpy(iv_copy, iv, len);
  727. return iv_copy;
  728. }
  729. int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
  730. if (a->namelen != b->namelen || a->valuelen != b->valuelen) {
  731. return 0;
  732. }
  733. if (a->name == NULL || b->name == NULL) {
  734. assert(a->namelen == 0);
  735. assert(b->namelen == 0);
  736. } else if (memcmp(a->name, b->name, a->namelen) != 0) {
  737. return 0;
  738. }
  739. if (a->value == NULL || b->value == NULL) {
  740. assert(a->valuelen == 0);
  741. assert(b->valuelen == 0);
  742. } else if (memcmp(a->value, b->value, a->valuelen) != 0) {
  743. return 0;
  744. }
  745. return 1;
  746. }
  747. void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
  748. nghttp2_mem_free(mem, nva);
  749. }
  750. static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
  751. size_t blen) {
  752. int rv;
  753. if (alen == blen) {
  754. return memcmp(a, b, alen);
  755. }
  756. if (alen < blen) {
  757. rv = memcmp(a, b, alen);
  758. if (rv == 0) {
  759. return -1;
  760. }
  761. return rv;
  762. }
  763. rv = memcmp(a, b, blen);
  764. if (rv == 0) {
  765. return 1;
  766. }
  767. return rv;
  768. }
  769. int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
  770. return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
  771. }
  772. static int nv_compar(const void *lhs, const void *rhs) {
  773. const nghttp2_nv *a = (const nghttp2_nv *)lhs;
  774. const nghttp2_nv *b = (const nghttp2_nv *)rhs;
  775. int rv;
  776. rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
  777. if (rv == 0) {
  778. return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
  779. }
  780. return rv;
  781. }
  782. void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
  783. qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
  784. }
  785. int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
  786. size_t nvlen, nghttp2_mem *mem) {
  787. size_t i;
  788. uint8_t *data = NULL;
  789. size_t buflen = 0;
  790. nghttp2_nv *p;
  791. if (nvlen == 0) {
  792. *nva_ptr = NULL;
  793. return 0;
  794. }
  795. for (i = 0; i < nvlen; ++i) {
  796. /* + 1 for null-termination */
  797. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
  798. buflen += nva[i].namelen + 1;
  799. }
  800. if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
  801. buflen += nva[i].valuelen + 1;
  802. }
  803. }
  804. buflen += sizeof(nghttp2_nv) * nvlen;
  805. *nva_ptr = nghttp2_mem_malloc(mem, buflen);
  806. if (*nva_ptr == NULL) {
  807. return NGHTTP2_ERR_NOMEM;
  808. }
  809. p = *nva_ptr;
  810. data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
  811. for (i = 0; i < nvlen; ++i) {
  812. p->flags = nva[i].flags;
  813. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
  814. p->name = nva[i].name;
  815. p->namelen = nva[i].namelen;
  816. } else {
  817. if (nva[i].namelen) {
  818. memcpy(data, nva[i].name, nva[i].namelen);
  819. }
  820. p->name = data;
  821. p->namelen = nva[i].namelen;
  822. data[p->namelen] = '\0';
  823. nghttp2_downcase(p->name, p->namelen);
  824. data += nva[i].namelen + 1;
  825. }
  826. if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
  827. p->value = nva[i].value;
  828. p->valuelen = nva[i].valuelen;
  829. } else {
  830. if (nva[i].valuelen) {
  831. memcpy(data, nva[i].value, nva[i].valuelen);
  832. }
  833. p->value = data;
  834. p->valuelen = nva[i].valuelen;
  835. data[p->valuelen] = '\0';
  836. data += nva[i].valuelen + 1;
  837. }
  838. ++p;
  839. }
  840. return 0;
  841. }
  842. int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
  843. size_t i;
  844. for (i = 0; i < niv; ++i) {
  845. switch (iv[i].settings_id) {
  846. case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
  847. break;
  848. case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
  849. break;
  850. case NGHTTP2_SETTINGS_ENABLE_PUSH:
  851. if (iv[i].value != 0 && iv[i].value != 1) {
  852. return 0;
  853. }
  854. break;
  855. case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
  856. if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
  857. return 0;
  858. }
  859. break;
  860. case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
  861. if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
  862. iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
  863. return 0;
  864. }
  865. break;
  866. case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
  867. break;
  868. case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
  869. if (iv[i].value != 0 && iv[i].value != 1) {
  870. return 0;
  871. }
  872. break;
  873. case NGHTTP2_SETTINGS_NO_RFC7540_PRIORITIES:
  874. if (iv[i].value != 0 && iv[i].value != 1) {
  875. return 0;
  876. }
  877. break;
  878. }
  879. }
  880. return 1;
  881. }
  882. static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
  883. size_t trail_padlen;
  884. size_t newlen;
  885. DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
  886. memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
  887. --buf->pos;
  888. buf->pos[4] |= NGHTTP2_FLAG_PADDED;
  889. newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
  890. nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
  891. if (framehd_only) {
  892. return;
  893. }
  894. trail_padlen = padlen - 1;
  895. buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
  896. /* zero out padding */
  897. memset(buf->last, 0, trail_padlen);
  898. /* extend buffers trail_padlen bytes, since we ate previous padlen -
  899. trail_padlen byte(s) */
  900. buf->last += trail_padlen;
  901. }
  902. void nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
  903. size_t padlen, int framehd_only) {
  904. nghttp2_buf *buf;
  905. if (padlen == 0) {
  906. DEBUGF("send: padlen = 0, nothing to do\n");
  907. return;
  908. }
  909. /*
  910. * We have arranged bufs like this:
  911. *
  912. * 0 1 2 3
  913. * 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
  914. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  915. * | |Frame header | Frame payload... :
  916. * +-+-----------------+-------------------------------------------+
  917. * | |Frame header | Frame payload... :
  918. * +-+-----------------+-------------------------------------------+
  919. * | |Frame header | Frame payload... :
  920. * +-+-----------------+-------------------------------------------+
  921. *
  922. * We arranged padding so that it is included in the first frame
  923. * completely. For padded frame, we are going to adjust buf->pos of
  924. * frame which includes padding and serialize (memmove) frame header
  925. * in the correct position. Also extends buf->last to include
  926. * padding.
  927. */
  928. buf = &bufs->head->buf;
  929. assert(nghttp2_buf_avail(buf) >= padlen - 1);
  930. frame_set_pad(buf, padlen, framehd_only);
  931. hd->length += padlen;
  932. hd->flags |= NGHTTP2_FLAG_PADDED;
  933. DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
  934. }