nghttp2_submit.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012, 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_submit.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "nghttp2_session.h"
  29. #include "nghttp2_frame.h"
  30. #include "nghttp2_helper.h"
  31. #include "nghttp2_priority_spec.h"
  32. /*
  33. * Detects the dependency error, that is stream attempted to depend on
  34. * itself. If |stream_id| is -1, we use session->next_stream_id as
  35. * stream ID.
  36. *
  37. * This function returns 0 if it succeeds, or one of the following
  38. * error codes:
  39. *
  40. * NGHTTP2_ERR_INVALID_ARGUMENT
  41. * Stream attempted to depend on itself.
  42. */
  43. static int detect_self_dependency(nghttp2_session *session, int32_t stream_id,
  44. const nghttp2_priority_spec *pri_spec) {
  45. assert(pri_spec);
  46. if (stream_id == -1) {
  47. if ((int32_t)session->next_stream_id == pri_spec->stream_id) {
  48. return NGHTTP2_ERR_INVALID_ARGUMENT;
  49. }
  50. return 0;
  51. }
  52. if (stream_id == pri_spec->stream_id) {
  53. return NGHTTP2_ERR_INVALID_ARGUMENT;
  54. }
  55. return 0;
  56. }
  57. /* This function takes ownership of |nva_copy|. Regardless of the
  58. return value, the caller must not free |nva_copy| after this
  59. function returns. */
  60. static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
  61. int32_t stream_id,
  62. const nghttp2_priority_spec *pri_spec,
  63. nghttp2_nv *nva_copy, size_t nvlen,
  64. const nghttp2_data_provider_wrap *dpw,
  65. void *stream_user_data) {
  66. int rv;
  67. uint8_t flags_copy;
  68. nghttp2_outbound_item *item = NULL;
  69. nghttp2_frame *frame = NULL;
  70. nghttp2_headers_category hcat;
  71. nghttp2_mem *mem;
  72. mem = &session->mem;
  73. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  74. if (item == NULL) {
  75. rv = NGHTTP2_ERR_NOMEM;
  76. goto fail;
  77. }
  78. nghttp2_outbound_item_init(item);
  79. if (dpw != NULL && dpw->data_prd.read_callback != NULL) {
  80. item->aux_data.headers.dpw = *dpw;
  81. }
  82. item->aux_data.headers.stream_user_data = stream_user_data;
  83. flags_copy =
  84. (uint8_t)((flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
  85. NGHTTP2_FLAG_END_HEADERS);
  86. if (stream_id == -1) {
  87. if (session->next_stream_id > INT32_MAX) {
  88. rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
  89. goto fail;
  90. }
  91. stream_id = (int32_t)session->next_stream_id;
  92. session->next_stream_id += 2;
  93. hcat = NGHTTP2_HCAT_REQUEST;
  94. } else {
  95. /* More specific categorization will be done later. */
  96. hcat = NGHTTP2_HCAT_HEADERS;
  97. }
  98. frame = &item->frame;
  99. nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
  100. pri_spec, nva_copy, nvlen);
  101. rv = nghttp2_session_add_item(session, item);
  102. if (rv != 0) {
  103. nghttp2_frame_headers_free(&frame->headers, mem);
  104. goto fail2;
  105. }
  106. if (hcat == NGHTTP2_HCAT_REQUEST) {
  107. return stream_id;
  108. }
  109. return 0;
  110. fail:
  111. /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
  112. nghttp2_nv_array_del(nva_copy, mem);
  113. fail2:
  114. nghttp2_mem_free(mem, item);
  115. return rv;
  116. }
  117. static int32_t submit_headers_shared_nva(nghttp2_session *session,
  118. uint8_t flags, int32_t stream_id,
  119. const nghttp2_priority_spec *pri_spec,
  120. const nghttp2_nv *nva, size_t nvlen,
  121. const nghttp2_data_provider_wrap *dpw,
  122. void *stream_user_data) {
  123. int rv;
  124. nghttp2_nv *nva_copy;
  125. nghttp2_priority_spec copy_pri_spec;
  126. nghttp2_mem *mem;
  127. mem = &session->mem;
  128. if (pri_spec) {
  129. copy_pri_spec = *pri_spec;
  130. nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
  131. } else {
  132. nghttp2_priority_spec_default_init(&copy_pri_spec);
  133. }
  134. rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
  135. if (rv < 0) {
  136. return rv;
  137. }
  138. return submit_headers_shared(session, flags, stream_id, &copy_pri_spec,
  139. nva_copy, nvlen, dpw, stream_user_data);
  140. }
  141. int nghttp2_submit_trailer(nghttp2_session *session, int32_t stream_id,
  142. const nghttp2_nv *nva, size_t nvlen) {
  143. if (stream_id <= 0) {
  144. return NGHTTP2_ERR_INVALID_ARGUMENT;
  145. }
  146. return (int)submit_headers_shared_nva(session, NGHTTP2_FLAG_END_STREAM,
  147. stream_id, NULL, nva, nvlen, NULL,
  148. NULL);
  149. }
  150. int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
  151. int32_t stream_id,
  152. const nghttp2_priority_spec *pri_spec,
  153. const nghttp2_nv *nva, size_t nvlen,
  154. void *stream_user_data) {
  155. int rv;
  156. if (stream_id == -1) {
  157. if (session->server) {
  158. return NGHTTP2_ERR_PROTO;
  159. }
  160. } else if (stream_id <= 0) {
  161. return NGHTTP2_ERR_INVALID_ARGUMENT;
  162. }
  163. flags &= NGHTTP2_FLAG_END_STREAM;
  164. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
  165. session->remote_settings.no_rfc7540_priorities != 1) {
  166. rv = detect_self_dependency(session, stream_id, pri_spec);
  167. if (rv != 0) {
  168. return rv;
  169. }
  170. flags |= NGHTTP2_FLAG_PRIORITY;
  171. } else {
  172. pri_spec = NULL;
  173. }
  174. return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
  175. nvlen, NULL, stream_user_data);
  176. }
  177. int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
  178. const uint8_t *opaque_data) {
  179. flags &= NGHTTP2_FLAG_ACK;
  180. return nghttp2_session_add_ping(session, flags, opaque_data);
  181. }
  182. int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
  183. int32_t stream_id,
  184. const nghttp2_priority_spec *pri_spec) {
  185. int rv;
  186. nghttp2_outbound_item *item;
  187. nghttp2_frame *frame;
  188. nghttp2_priority_spec copy_pri_spec;
  189. nghttp2_mem *mem;
  190. (void)flags;
  191. mem = &session->mem;
  192. if (session->remote_settings.no_rfc7540_priorities == 1) {
  193. return 0;
  194. }
  195. if (stream_id == 0 || pri_spec == NULL) {
  196. return NGHTTP2_ERR_INVALID_ARGUMENT;
  197. }
  198. if (stream_id == pri_spec->stream_id) {
  199. return NGHTTP2_ERR_INVALID_ARGUMENT;
  200. }
  201. copy_pri_spec = *pri_spec;
  202. nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
  203. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  204. if (item == NULL) {
  205. return NGHTTP2_ERR_NOMEM;
  206. }
  207. nghttp2_outbound_item_init(item);
  208. frame = &item->frame;
  209. nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
  210. rv = nghttp2_session_add_item(session, item);
  211. if (rv != 0) {
  212. nghttp2_frame_priority_free(&frame->priority);
  213. nghttp2_mem_free(mem, item);
  214. return rv;
  215. }
  216. return 0;
  217. }
  218. int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
  219. int32_t stream_id, uint32_t error_code) {
  220. (void)flags;
  221. if (stream_id == 0) {
  222. return NGHTTP2_ERR_INVALID_ARGUMENT;
  223. }
  224. return nghttp2_session_add_rst_stream(session, stream_id, error_code);
  225. }
  226. int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
  227. int32_t last_stream_id, uint32_t error_code,
  228. const uint8_t *opaque_data, size_t opaque_data_len) {
  229. (void)flags;
  230. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
  231. return 0;
  232. }
  233. return nghttp2_session_add_goaway(session, last_stream_id, error_code,
  234. opaque_data, opaque_data_len,
  235. NGHTTP2_GOAWAY_AUX_NONE);
  236. }
  237. int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
  238. if (!session->server) {
  239. return NGHTTP2_ERR_INVALID_STATE;
  240. }
  241. if (session->goaway_flags) {
  242. return 0;
  243. }
  244. return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
  245. NULL, 0,
  246. NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
  247. }
  248. int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
  249. const nghttp2_settings_entry *iv, size_t niv) {
  250. (void)flags;
  251. return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
  252. }
  253. int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
  254. int32_t stream_id, const nghttp2_nv *nva,
  255. size_t nvlen,
  256. void *promised_stream_user_data) {
  257. nghttp2_outbound_item *item;
  258. nghttp2_frame *frame;
  259. nghttp2_nv *nva_copy;
  260. uint8_t flags_copy;
  261. int32_t promised_stream_id;
  262. int rv;
  263. nghttp2_mem *mem;
  264. (void)flags;
  265. mem = &session->mem;
  266. if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
  267. return NGHTTP2_ERR_INVALID_ARGUMENT;
  268. }
  269. if (!session->server) {
  270. return NGHTTP2_ERR_PROTO;
  271. }
  272. /* All 32bit signed stream IDs are spent. */
  273. if (session->next_stream_id > INT32_MAX) {
  274. return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
  275. }
  276. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  277. if (item == NULL) {
  278. return NGHTTP2_ERR_NOMEM;
  279. }
  280. nghttp2_outbound_item_init(item);
  281. item->aux_data.headers.stream_user_data = promised_stream_user_data;
  282. frame = &item->frame;
  283. rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
  284. if (rv < 0) {
  285. nghttp2_mem_free(mem, item);
  286. return rv;
  287. }
  288. flags_copy = NGHTTP2_FLAG_END_HEADERS;
  289. promised_stream_id = (int32_t)session->next_stream_id;
  290. session->next_stream_id += 2;
  291. nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
  292. promised_stream_id, nva_copy, nvlen);
  293. rv = nghttp2_session_add_item(session, item);
  294. if (rv != 0) {
  295. nghttp2_frame_push_promise_free(&frame->push_promise, mem);
  296. nghttp2_mem_free(mem, item);
  297. return rv;
  298. }
  299. return promised_stream_id;
  300. }
  301. int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
  302. int32_t stream_id,
  303. int32_t window_size_increment) {
  304. int rv;
  305. nghttp2_stream *stream = 0;
  306. (void)flags;
  307. if (window_size_increment == 0) {
  308. return 0;
  309. }
  310. if (stream_id == 0) {
  311. rv = nghttp2_adjust_local_window_size(
  312. &session->local_window_size, &session->recv_window_size,
  313. &session->recv_reduction, &window_size_increment);
  314. if (rv != 0) {
  315. return rv;
  316. }
  317. } else {
  318. stream = nghttp2_session_get_stream(session, stream_id);
  319. if (!stream) {
  320. return 0;
  321. }
  322. rv = nghttp2_adjust_local_window_size(
  323. &stream->local_window_size, &stream->recv_window_size,
  324. &stream->recv_reduction, &window_size_increment);
  325. if (rv != 0) {
  326. return rv;
  327. }
  328. }
  329. if (window_size_increment > 0) {
  330. if (stream_id == 0) {
  331. session->consumed_size =
  332. nghttp2_max_int32(0, session->consumed_size - window_size_increment);
  333. } else {
  334. stream->consumed_size =
  335. nghttp2_max_int32(0, stream->consumed_size - window_size_increment);
  336. }
  337. return nghttp2_session_add_window_update(session, 0, stream_id,
  338. window_size_increment);
  339. }
  340. return 0;
  341. }
  342. int nghttp2_session_set_local_window_size(nghttp2_session *session,
  343. uint8_t flags, int32_t stream_id,
  344. int32_t window_size) {
  345. int32_t window_size_increment;
  346. nghttp2_stream *stream;
  347. int rv;
  348. (void)flags;
  349. if (window_size < 0) {
  350. return NGHTTP2_ERR_INVALID_ARGUMENT;
  351. }
  352. if (stream_id == 0) {
  353. window_size_increment = window_size - session->local_window_size;
  354. if (window_size_increment == 0) {
  355. return 0;
  356. }
  357. if (window_size_increment < 0) {
  358. return nghttp2_adjust_local_window_size(
  359. &session->local_window_size, &session->recv_window_size,
  360. &session->recv_reduction, &window_size_increment);
  361. }
  362. rv = nghttp2_increase_local_window_size(
  363. &session->local_window_size, &session->recv_window_size,
  364. &session->recv_reduction, &window_size_increment);
  365. if (rv != 0) {
  366. return rv;
  367. }
  368. if (window_size_increment > 0) {
  369. return nghttp2_session_add_window_update(session, 0, stream_id,
  370. window_size_increment);
  371. }
  372. return nghttp2_session_update_recv_connection_window_size(session, 0);
  373. } else {
  374. stream = nghttp2_session_get_stream(session, stream_id);
  375. if (stream == NULL) {
  376. return 0;
  377. }
  378. window_size_increment = window_size - stream->local_window_size;
  379. if (window_size_increment == 0) {
  380. return 0;
  381. }
  382. if (window_size_increment < 0) {
  383. return nghttp2_adjust_local_window_size(
  384. &stream->local_window_size, &stream->recv_window_size,
  385. &stream->recv_reduction, &window_size_increment);
  386. }
  387. rv = nghttp2_increase_local_window_size(
  388. &stream->local_window_size, &stream->recv_window_size,
  389. &stream->recv_reduction, &window_size_increment);
  390. if (rv != 0) {
  391. return rv;
  392. }
  393. if (window_size_increment > 0) {
  394. return nghttp2_session_add_window_update(session, 0, stream_id,
  395. window_size_increment);
  396. }
  397. return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
  398. 1);
  399. }
  400. }
  401. int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
  402. int32_t stream_id, const uint8_t *origin,
  403. size_t origin_len, const uint8_t *field_value,
  404. size_t field_value_len) {
  405. nghttp2_mem *mem;
  406. uint8_t *buf, *p;
  407. uint8_t *origin_copy;
  408. uint8_t *field_value_copy;
  409. nghttp2_outbound_item *item;
  410. nghttp2_frame *frame;
  411. nghttp2_ext_altsvc *altsvc;
  412. int rv;
  413. (void)flags;
  414. mem = &session->mem;
  415. if (!session->server) {
  416. return NGHTTP2_ERR_INVALID_STATE;
  417. }
  418. if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  419. return NGHTTP2_ERR_INVALID_ARGUMENT;
  420. }
  421. if (stream_id == 0) {
  422. if (origin_len == 0) {
  423. return NGHTTP2_ERR_INVALID_ARGUMENT;
  424. }
  425. } else if (origin_len != 0) {
  426. return NGHTTP2_ERR_INVALID_ARGUMENT;
  427. }
  428. buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
  429. if (buf == NULL) {
  430. return NGHTTP2_ERR_NOMEM;
  431. }
  432. p = buf;
  433. origin_copy = p;
  434. if (origin_len) {
  435. p = nghttp2_cpymem(p, origin, origin_len);
  436. }
  437. *p++ = '\0';
  438. field_value_copy = p;
  439. if (field_value_len) {
  440. p = nghttp2_cpymem(p, field_value, field_value_len);
  441. }
  442. *p++ = '\0';
  443. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  444. if (item == NULL) {
  445. rv = NGHTTP2_ERR_NOMEM;
  446. goto fail_item_malloc;
  447. }
  448. nghttp2_outbound_item_init(item);
  449. item->aux_data.ext.builtin = 1;
  450. altsvc = &item->ext_frame_payload.altsvc;
  451. frame = &item->frame;
  452. frame->ext.payload = altsvc;
  453. nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
  454. field_value_copy, field_value_len);
  455. rv = nghttp2_session_add_item(session, item);
  456. if (rv != 0) {
  457. nghttp2_frame_altsvc_free(&frame->ext, mem);
  458. nghttp2_mem_free(mem, item);
  459. return rv;
  460. }
  461. return 0;
  462. fail_item_malloc:
  463. free(buf);
  464. return rv;
  465. }
  466. int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
  467. const nghttp2_origin_entry *ov, size_t nov) {
  468. nghttp2_mem *mem;
  469. uint8_t *p;
  470. nghttp2_outbound_item *item;
  471. nghttp2_frame *frame;
  472. nghttp2_ext_origin *origin;
  473. nghttp2_origin_entry *ov_copy;
  474. size_t len = 0;
  475. size_t i;
  476. int rv;
  477. (void)flags;
  478. mem = &session->mem;
  479. if (!session->server) {
  480. return NGHTTP2_ERR_INVALID_STATE;
  481. }
  482. if (nov) {
  483. for (i = 0; i < nov; ++i) {
  484. len += ov[i].origin_len;
  485. }
  486. if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
  487. return NGHTTP2_ERR_INVALID_ARGUMENT;
  488. }
  489. /* The last nov is added for terminal NULL character. */
  490. ov_copy =
  491. nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
  492. if (ov_copy == NULL) {
  493. return NGHTTP2_ERR_NOMEM;
  494. }
  495. p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
  496. for (i = 0; i < nov; ++i) {
  497. ov_copy[i].origin = p;
  498. ov_copy[i].origin_len = ov[i].origin_len;
  499. p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
  500. *p++ = '\0';
  501. }
  502. assert((size_t)(p - (uint8_t *)ov_copy) ==
  503. nov * sizeof(nghttp2_origin_entry) + len + nov);
  504. } else {
  505. ov_copy = NULL;
  506. }
  507. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  508. if (item == NULL) {
  509. rv = NGHTTP2_ERR_NOMEM;
  510. goto fail_item_malloc;
  511. }
  512. nghttp2_outbound_item_init(item);
  513. item->aux_data.ext.builtin = 1;
  514. origin = &item->ext_frame_payload.origin;
  515. frame = &item->frame;
  516. frame->ext.payload = origin;
  517. nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
  518. rv = nghttp2_session_add_item(session, item);
  519. if (rv != 0) {
  520. nghttp2_frame_origin_free(&frame->ext, mem);
  521. nghttp2_mem_free(mem, item);
  522. return rv;
  523. }
  524. return 0;
  525. fail_item_malloc:
  526. free(ov_copy);
  527. return rv;
  528. }
  529. int nghttp2_submit_priority_update(nghttp2_session *session, uint8_t flags,
  530. int32_t stream_id,
  531. const uint8_t *field_value,
  532. size_t field_value_len) {
  533. nghttp2_mem *mem;
  534. uint8_t *buf, *p;
  535. nghttp2_outbound_item *item;
  536. nghttp2_frame *frame;
  537. nghttp2_ext_priority_update *priority_update;
  538. int rv;
  539. (void)flags;
  540. mem = &session->mem;
  541. if (session->server) {
  542. return NGHTTP2_ERR_INVALID_STATE;
  543. }
  544. if (session->remote_settings.no_rfc7540_priorities == 0) {
  545. return 0;
  546. }
  547. if (stream_id == 0 || 4 + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  548. return NGHTTP2_ERR_INVALID_ARGUMENT;
  549. }
  550. if (field_value_len) {
  551. buf = nghttp2_mem_malloc(mem, field_value_len + 1);
  552. if (buf == NULL) {
  553. return NGHTTP2_ERR_NOMEM;
  554. }
  555. p = nghttp2_cpymem(buf, field_value, field_value_len);
  556. *p = '\0';
  557. } else {
  558. buf = NULL;
  559. }
  560. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  561. if (item == NULL) {
  562. rv = NGHTTP2_ERR_NOMEM;
  563. goto fail_item_malloc;
  564. }
  565. nghttp2_outbound_item_init(item);
  566. item->aux_data.ext.builtin = 1;
  567. priority_update = &item->ext_frame_payload.priority_update;
  568. frame = &item->frame;
  569. frame->ext.payload = priority_update;
  570. nghttp2_frame_priority_update_init(&frame->ext, stream_id, buf,
  571. field_value_len);
  572. rv = nghttp2_session_add_item(session, item);
  573. if (rv != 0) {
  574. nghttp2_frame_priority_update_free(&frame->ext, mem);
  575. nghttp2_mem_free(mem, item);
  576. return rv;
  577. }
  578. return 0;
  579. fail_item_malloc:
  580. free(buf);
  581. return rv;
  582. }
  583. static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
  584. const nghttp2_data_provider_wrap *dpw) {
  585. uint8_t flags = NGHTTP2_FLAG_NONE;
  586. if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
  587. flags |= NGHTTP2_FLAG_END_STREAM;
  588. }
  589. if (pri_spec) {
  590. flags |= NGHTTP2_FLAG_PRIORITY;
  591. }
  592. return flags;
  593. }
  594. static int32_t submit_request_shared(nghttp2_session *session,
  595. const nghttp2_priority_spec *pri_spec,
  596. const nghttp2_nv *nva, size_t nvlen,
  597. const nghttp2_data_provider_wrap *dpw,
  598. void *stream_user_data) {
  599. uint8_t flags;
  600. int rv;
  601. if (session->server) {
  602. return NGHTTP2_ERR_PROTO;
  603. }
  604. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
  605. session->remote_settings.no_rfc7540_priorities != 1) {
  606. rv = detect_self_dependency(session, -1, pri_spec);
  607. if (rv != 0) {
  608. return rv;
  609. }
  610. } else {
  611. pri_spec = NULL;
  612. }
  613. flags = set_request_flags(pri_spec, dpw);
  614. return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
  615. dpw, stream_user_data);
  616. }
  617. int32_t nghttp2_submit_request(nghttp2_session *session,
  618. const nghttp2_priority_spec *pri_spec,
  619. const nghttp2_nv *nva, size_t nvlen,
  620. const nghttp2_data_provider *data_prd,
  621. void *stream_user_data) {
  622. nghttp2_data_provider_wrap dpw;
  623. return submit_request_shared(session, pri_spec, nva, nvlen,
  624. nghttp2_data_provider_wrap_v1(&dpw, data_prd),
  625. stream_user_data);
  626. }
  627. int32_t nghttp2_submit_request2(nghttp2_session *session,
  628. const nghttp2_priority_spec *pri_spec,
  629. const nghttp2_nv *nva, size_t nvlen,
  630. const nghttp2_data_provider2 *data_prd,
  631. void *stream_user_data) {
  632. nghttp2_data_provider_wrap dpw;
  633. return submit_request_shared(session, pri_spec, nva, nvlen,
  634. nghttp2_data_provider_wrap_v2(&dpw, data_prd),
  635. stream_user_data);
  636. }
  637. static uint8_t set_response_flags(const nghttp2_data_provider_wrap *dpw) {
  638. uint8_t flags = NGHTTP2_FLAG_NONE;
  639. if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
  640. flags |= NGHTTP2_FLAG_END_STREAM;
  641. }
  642. return flags;
  643. }
  644. static int submit_response_shared(nghttp2_session *session, int32_t stream_id,
  645. const nghttp2_nv *nva, size_t nvlen,
  646. const nghttp2_data_provider_wrap *dpw) {
  647. uint8_t flags;
  648. if (stream_id <= 0) {
  649. return NGHTTP2_ERR_INVALID_ARGUMENT;
  650. }
  651. if (!session->server) {
  652. return NGHTTP2_ERR_PROTO;
  653. }
  654. flags = set_response_flags(dpw);
  655. return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
  656. dpw, NULL);
  657. }
  658. int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
  659. const nghttp2_nv *nva, size_t nvlen,
  660. const nghttp2_data_provider *data_prd) {
  661. nghttp2_data_provider_wrap dpw;
  662. return submit_response_shared(session, stream_id, nva, nvlen,
  663. nghttp2_data_provider_wrap_v1(&dpw, data_prd));
  664. }
  665. int nghttp2_submit_response2(nghttp2_session *session, int32_t stream_id,
  666. const nghttp2_nv *nva, size_t nvlen,
  667. const nghttp2_data_provider2 *data_prd) {
  668. nghttp2_data_provider_wrap dpw;
  669. return submit_response_shared(session, stream_id, nva, nvlen,
  670. nghttp2_data_provider_wrap_v2(&dpw, data_prd));
  671. }
  672. int nghttp2_submit_data_shared(nghttp2_session *session, uint8_t flags,
  673. int32_t stream_id,
  674. const nghttp2_data_provider_wrap *dpw) {
  675. int rv;
  676. nghttp2_outbound_item *item;
  677. nghttp2_frame *frame;
  678. nghttp2_data_aux_data *aux_data;
  679. uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
  680. nghttp2_mem *mem;
  681. mem = &session->mem;
  682. if (stream_id == 0) {
  683. return NGHTTP2_ERR_INVALID_ARGUMENT;
  684. }
  685. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  686. if (item == NULL) {
  687. return NGHTTP2_ERR_NOMEM;
  688. }
  689. nghttp2_outbound_item_init(item);
  690. frame = &item->frame;
  691. aux_data = &item->aux_data.data;
  692. aux_data->dpw = *dpw;
  693. aux_data->eof = 0;
  694. aux_data->flags = nflags;
  695. /* flags are sent on transmission */
  696. nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
  697. rv = nghttp2_session_add_item(session, item);
  698. if (rv != 0) {
  699. nghttp2_frame_data_free(&frame->data);
  700. nghttp2_mem_free(mem, item);
  701. return rv;
  702. }
  703. return 0;
  704. }
  705. int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
  706. int32_t stream_id,
  707. const nghttp2_data_provider *data_prd) {
  708. nghttp2_data_provider_wrap dpw;
  709. assert(data_prd);
  710. return nghttp2_submit_data_shared(
  711. session, flags, stream_id, nghttp2_data_provider_wrap_v1(&dpw, data_prd));
  712. }
  713. int nghttp2_submit_data2(nghttp2_session *session, uint8_t flags,
  714. int32_t stream_id,
  715. const nghttp2_data_provider2 *data_prd) {
  716. nghttp2_data_provider_wrap dpw;
  717. assert(data_prd);
  718. return nghttp2_submit_data_shared(
  719. session, flags, stream_id, nghttp2_data_provider_wrap_v2(&dpw, data_prd));
  720. }
  721. ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
  722. const nghttp2_settings_entry *iv,
  723. size_t niv) {
  724. return (ssize_t)nghttp2_pack_settings_payload2(buf, buflen, iv, niv);
  725. }
  726. nghttp2_ssize nghttp2_pack_settings_payload2(uint8_t *buf, size_t buflen,
  727. const nghttp2_settings_entry *iv,
  728. size_t niv) {
  729. if (!nghttp2_iv_check(iv, niv)) {
  730. return NGHTTP2_ERR_INVALID_ARGUMENT;
  731. }
  732. if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
  733. return NGHTTP2_ERR_INSUFF_BUFSIZE;
  734. }
  735. return (nghttp2_ssize)nghttp2_frame_pack_settings_payload(buf, iv, niv);
  736. }
  737. int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
  738. uint8_t flags, int32_t stream_id, void *payload) {
  739. int rv;
  740. nghttp2_outbound_item *item;
  741. nghttp2_frame *frame;
  742. nghttp2_mem *mem;
  743. mem = &session->mem;
  744. if (type <= NGHTTP2_CONTINUATION) {
  745. return NGHTTP2_ERR_INVALID_ARGUMENT;
  746. }
  747. if (!session->callbacks.pack_extension_callback2 &&
  748. !session->callbacks.pack_extension_callback) {
  749. return NGHTTP2_ERR_INVALID_STATE;
  750. }
  751. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  752. if (item == NULL) {
  753. return NGHTTP2_ERR_NOMEM;
  754. }
  755. nghttp2_outbound_item_init(item);
  756. frame = &item->frame;
  757. nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
  758. rv = nghttp2_session_add_item(session, item);
  759. if (rv != 0) {
  760. nghttp2_frame_extension_free(&frame->ext);
  761. nghttp2_mem_free(mem, item);
  762. return rv;
  763. }
  764. return 0;
  765. }