nghttp2_submit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 *data_prd,
  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 (data_prd != NULL && data_prd->read_callback != NULL) {
  80. item->aux_data.headers.data_prd = *data_prd;
  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 *data_prd,
  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, data_prd, 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. rv = detect_self_dependency(session, stream_id, pri_spec);
  166. if (rv != 0) {
  167. return rv;
  168. }
  169. flags |= NGHTTP2_FLAG_PRIORITY;
  170. } else {
  171. pri_spec = NULL;
  172. }
  173. return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
  174. nvlen, NULL, stream_user_data);
  175. }
  176. int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
  177. const uint8_t *opaque_data) {
  178. flags &= NGHTTP2_FLAG_ACK;
  179. return nghttp2_session_add_ping(session, flags, opaque_data);
  180. }
  181. int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
  182. int32_t stream_id,
  183. const nghttp2_priority_spec *pri_spec) {
  184. int rv;
  185. nghttp2_outbound_item *item;
  186. nghttp2_frame *frame;
  187. nghttp2_priority_spec copy_pri_spec;
  188. nghttp2_mem *mem;
  189. (void)flags;
  190. mem = &session->mem;
  191. if (stream_id == 0 || pri_spec == NULL) {
  192. return NGHTTP2_ERR_INVALID_ARGUMENT;
  193. }
  194. if (stream_id == pri_spec->stream_id) {
  195. return NGHTTP2_ERR_INVALID_ARGUMENT;
  196. }
  197. copy_pri_spec = *pri_spec;
  198. nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
  199. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  200. if (item == NULL) {
  201. return NGHTTP2_ERR_NOMEM;
  202. }
  203. nghttp2_outbound_item_init(item);
  204. frame = &item->frame;
  205. nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
  206. rv = nghttp2_session_add_item(session, item);
  207. if (rv != 0) {
  208. nghttp2_frame_priority_free(&frame->priority);
  209. nghttp2_mem_free(mem, item);
  210. return rv;
  211. }
  212. return 0;
  213. }
  214. int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
  215. int32_t stream_id, uint32_t error_code) {
  216. (void)flags;
  217. if (stream_id == 0) {
  218. return NGHTTP2_ERR_INVALID_ARGUMENT;
  219. }
  220. return nghttp2_session_add_rst_stream(session, stream_id, error_code);
  221. }
  222. int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
  223. int32_t last_stream_id, uint32_t error_code,
  224. const uint8_t *opaque_data, size_t opaque_data_len) {
  225. (void)flags;
  226. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
  227. return 0;
  228. }
  229. return nghttp2_session_add_goaway(session, last_stream_id, error_code,
  230. opaque_data, opaque_data_len,
  231. NGHTTP2_GOAWAY_AUX_NONE);
  232. }
  233. int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
  234. if (!session->server) {
  235. return NGHTTP2_ERR_INVALID_STATE;
  236. }
  237. if (session->goaway_flags) {
  238. return 0;
  239. }
  240. return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
  241. NULL, 0,
  242. NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
  243. }
  244. int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
  245. const nghttp2_settings_entry *iv, size_t niv) {
  246. (void)flags;
  247. return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
  248. }
  249. int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
  250. int32_t stream_id, const nghttp2_nv *nva,
  251. size_t nvlen,
  252. void *promised_stream_user_data) {
  253. nghttp2_outbound_item *item;
  254. nghttp2_frame *frame;
  255. nghttp2_nv *nva_copy;
  256. uint8_t flags_copy;
  257. int32_t promised_stream_id;
  258. int rv;
  259. nghttp2_mem *mem;
  260. (void)flags;
  261. mem = &session->mem;
  262. if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
  263. return NGHTTP2_ERR_INVALID_ARGUMENT;
  264. }
  265. if (!session->server) {
  266. return NGHTTP2_ERR_PROTO;
  267. }
  268. /* All 32bit signed stream IDs are spent. */
  269. if (session->next_stream_id > INT32_MAX) {
  270. return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
  271. }
  272. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  273. if (item == NULL) {
  274. return NGHTTP2_ERR_NOMEM;
  275. }
  276. nghttp2_outbound_item_init(item);
  277. item->aux_data.headers.stream_user_data = promised_stream_user_data;
  278. frame = &item->frame;
  279. rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
  280. if (rv < 0) {
  281. nghttp2_mem_free(mem, item);
  282. return rv;
  283. }
  284. flags_copy = NGHTTP2_FLAG_END_HEADERS;
  285. promised_stream_id = (int32_t)session->next_stream_id;
  286. session->next_stream_id += 2;
  287. nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
  288. promised_stream_id, nva_copy, nvlen);
  289. rv = nghttp2_session_add_item(session, item);
  290. if (rv != 0) {
  291. nghttp2_frame_push_promise_free(&frame->push_promise, mem);
  292. nghttp2_mem_free(mem, item);
  293. return rv;
  294. }
  295. return promised_stream_id;
  296. }
  297. int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
  298. int32_t stream_id,
  299. int32_t window_size_increment) {
  300. int rv;
  301. nghttp2_stream *stream = 0;
  302. (void)flags;
  303. if (window_size_increment == 0) {
  304. return 0;
  305. }
  306. if (stream_id == 0) {
  307. rv = nghttp2_adjust_local_window_size(
  308. &session->local_window_size, &session->recv_window_size,
  309. &session->recv_reduction, &window_size_increment);
  310. if (rv != 0) {
  311. return rv;
  312. }
  313. } else {
  314. stream = nghttp2_session_get_stream(session, stream_id);
  315. if (!stream) {
  316. return 0;
  317. }
  318. rv = nghttp2_adjust_local_window_size(
  319. &stream->local_window_size, &stream->recv_window_size,
  320. &stream->recv_reduction, &window_size_increment);
  321. if (rv != 0) {
  322. return rv;
  323. }
  324. }
  325. if (window_size_increment > 0) {
  326. if (stream_id == 0) {
  327. session->consumed_size =
  328. nghttp2_max(0, session->consumed_size - window_size_increment);
  329. } else {
  330. stream->consumed_size =
  331. nghttp2_max(0, stream->consumed_size - window_size_increment);
  332. }
  333. return nghttp2_session_add_window_update(session, 0, stream_id,
  334. window_size_increment);
  335. }
  336. return 0;
  337. }
  338. int nghttp2_session_set_local_window_size(nghttp2_session *session,
  339. uint8_t flags, int32_t stream_id,
  340. int32_t window_size) {
  341. int32_t window_size_increment;
  342. nghttp2_stream *stream;
  343. int rv;
  344. (void)flags;
  345. if (window_size < 0) {
  346. return NGHTTP2_ERR_INVALID_ARGUMENT;
  347. }
  348. if (stream_id == 0) {
  349. window_size_increment = window_size - session->local_window_size;
  350. if (window_size_increment == 0) {
  351. return 0;
  352. }
  353. if (window_size_increment < 0) {
  354. return nghttp2_adjust_local_window_size(
  355. &session->local_window_size, &session->recv_window_size,
  356. &session->recv_reduction, &window_size_increment);
  357. }
  358. rv = nghttp2_increase_local_window_size(
  359. &session->local_window_size, &session->recv_window_size,
  360. &session->recv_reduction, &window_size_increment);
  361. if (rv != 0) {
  362. return rv;
  363. }
  364. if (window_size_increment > 0) {
  365. return nghttp2_session_add_window_update(session, 0, stream_id,
  366. window_size_increment);
  367. }
  368. return nghttp2_session_update_recv_connection_window_size(session, 0);
  369. } else {
  370. stream = nghttp2_session_get_stream(session, stream_id);
  371. if (stream == NULL) {
  372. return 0;
  373. }
  374. window_size_increment = window_size - stream->local_window_size;
  375. if (window_size_increment == 0) {
  376. return 0;
  377. }
  378. if (window_size_increment < 0) {
  379. return nghttp2_adjust_local_window_size(
  380. &stream->local_window_size, &stream->recv_window_size,
  381. &stream->recv_reduction, &window_size_increment);
  382. }
  383. rv = nghttp2_increase_local_window_size(
  384. &stream->local_window_size, &stream->recv_window_size,
  385. &stream->recv_reduction, &window_size_increment);
  386. if (rv != 0) {
  387. return rv;
  388. }
  389. if (window_size_increment > 0) {
  390. return nghttp2_session_add_window_update(session, 0, stream_id,
  391. window_size_increment);
  392. }
  393. return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
  394. 1);
  395. }
  396. }
  397. int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
  398. int32_t stream_id, const uint8_t *origin,
  399. size_t origin_len, const uint8_t *field_value,
  400. size_t field_value_len) {
  401. nghttp2_mem *mem;
  402. uint8_t *buf, *p;
  403. uint8_t *origin_copy;
  404. uint8_t *field_value_copy;
  405. nghttp2_outbound_item *item;
  406. nghttp2_frame *frame;
  407. nghttp2_ext_altsvc *altsvc;
  408. int rv;
  409. (void)flags;
  410. mem = &session->mem;
  411. if (!session->server) {
  412. return NGHTTP2_ERR_INVALID_STATE;
  413. }
  414. if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  415. return NGHTTP2_ERR_INVALID_ARGUMENT;
  416. }
  417. if (stream_id == 0) {
  418. if (origin_len == 0) {
  419. return NGHTTP2_ERR_INVALID_ARGUMENT;
  420. }
  421. } else if (origin_len != 0) {
  422. return NGHTTP2_ERR_INVALID_ARGUMENT;
  423. }
  424. buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
  425. if (buf == NULL) {
  426. return NGHTTP2_ERR_NOMEM;
  427. }
  428. p = buf;
  429. origin_copy = p;
  430. if (origin_len) {
  431. p = nghttp2_cpymem(p, origin, origin_len);
  432. }
  433. *p++ = '\0';
  434. field_value_copy = p;
  435. if (field_value_len) {
  436. p = nghttp2_cpymem(p, field_value, field_value_len);
  437. }
  438. *p++ = '\0';
  439. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  440. if (item == NULL) {
  441. rv = NGHTTP2_ERR_NOMEM;
  442. goto fail_item_malloc;
  443. }
  444. nghttp2_outbound_item_init(item);
  445. item->aux_data.ext.builtin = 1;
  446. altsvc = &item->ext_frame_payload.altsvc;
  447. frame = &item->frame;
  448. frame->ext.payload = altsvc;
  449. nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
  450. field_value_copy, field_value_len);
  451. rv = nghttp2_session_add_item(session, item);
  452. if (rv != 0) {
  453. nghttp2_frame_altsvc_free(&frame->ext, mem);
  454. nghttp2_mem_free(mem, item);
  455. return rv;
  456. }
  457. return 0;
  458. fail_item_malloc:
  459. free(buf);
  460. return rv;
  461. }
  462. int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
  463. const nghttp2_origin_entry *ov, size_t nov) {
  464. nghttp2_mem *mem;
  465. uint8_t *p;
  466. nghttp2_outbound_item *item;
  467. nghttp2_frame *frame;
  468. nghttp2_ext_origin *origin;
  469. nghttp2_origin_entry *ov_copy;
  470. size_t len = 0;
  471. size_t i;
  472. int rv;
  473. (void)flags;
  474. mem = &session->mem;
  475. if (!session->server) {
  476. return NGHTTP2_ERR_INVALID_STATE;
  477. }
  478. if (nov) {
  479. for (i = 0; i < nov; ++i) {
  480. len += ov[i].origin_len;
  481. }
  482. if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
  483. return NGHTTP2_ERR_INVALID_ARGUMENT;
  484. }
  485. /* The last nov is added for terminal NULL character. */
  486. ov_copy =
  487. nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
  488. if (ov_copy == NULL) {
  489. return NGHTTP2_ERR_NOMEM;
  490. }
  491. p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
  492. for (i = 0; i < nov; ++i) {
  493. ov_copy[i].origin = p;
  494. ov_copy[i].origin_len = ov[i].origin_len;
  495. p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
  496. *p++ = '\0';
  497. }
  498. assert((size_t)(p - (uint8_t *)ov_copy) ==
  499. nov * sizeof(nghttp2_origin_entry) + len + nov);
  500. } else {
  501. ov_copy = NULL;
  502. }
  503. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  504. if (item == NULL) {
  505. rv = NGHTTP2_ERR_NOMEM;
  506. goto fail_item_malloc;
  507. }
  508. nghttp2_outbound_item_init(item);
  509. item->aux_data.ext.builtin = 1;
  510. origin = &item->ext_frame_payload.origin;
  511. frame = &item->frame;
  512. frame->ext.payload = origin;
  513. nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
  514. rv = nghttp2_session_add_item(session, item);
  515. if (rv != 0) {
  516. nghttp2_frame_origin_free(&frame->ext, mem);
  517. nghttp2_mem_free(mem, item);
  518. return rv;
  519. }
  520. return 0;
  521. fail_item_malloc:
  522. free(ov_copy);
  523. return rv;
  524. }
  525. static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
  526. const nghttp2_data_provider *data_prd) {
  527. uint8_t flags = NGHTTP2_FLAG_NONE;
  528. if (data_prd == NULL || data_prd->read_callback == NULL) {
  529. flags |= NGHTTP2_FLAG_END_STREAM;
  530. }
  531. if (pri_spec) {
  532. flags |= NGHTTP2_FLAG_PRIORITY;
  533. }
  534. return flags;
  535. }
  536. int32_t nghttp2_submit_request(nghttp2_session *session,
  537. const nghttp2_priority_spec *pri_spec,
  538. const nghttp2_nv *nva, size_t nvlen,
  539. const nghttp2_data_provider *data_prd,
  540. void *stream_user_data) {
  541. uint8_t flags;
  542. int rv;
  543. if (session->server) {
  544. return NGHTTP2_ERR_PROTO;
  545. }
  546. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
  547. rv = detect_self_dependency(session, -1, pri_spec);
  548. if (rv != 0) {
  549. return rv;
  550. }
  551. } else {
  552. pri_spec = NULL;
  553. }
  554. flags = set_request_flags(pri_spec, data_prd);
  555. return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
  556. data_prd, stream_user_data);
  557. }
  558. static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
  559. uint8_t flags = NGHTTP2_FLAG_NONE;
  560. if (data_prd == NULL || data_prd->read_callback == NULL) {
  561. flags |= NGHTTP2_FLAG_END_STREAM;
  562. }
  563. return flags;
  564. }
  565. int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
  566. const nghttp2_nv *nva, size_t nvlen,
  567. const nghttp2_data_provider *data_prd) {
  568. uint8_t flags;
  569. if (stream_id <= 0) {
  570. return NGHTTP2_ERR_INVALID_ARGUMENT;
  571. }
  572. if (!session->server) {
  573. return NGHTTP2_ERR_PROTO;
  574. }
  575. flags = set_response_flags(data_prd);
  576. return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
  577. data_prd, NULL);
  578. }
  579. int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
  580. int32_t stream_id,
  581. const nghttp2_data_provider *data_prd) {
  582. int rv;
  583. nghttp2_outbound_item *item;
  584. nghttp2_frame *frame;
  585. nghttp2_data_aux_data *aux_data;
  586. uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
  587. nghttp2_mem *mem;
  588. mem = &session->mem;
  589. if (stream_id == 0) {
  590. return NGHTTP2_ERR_INVALID_ARGUMENT;
  591. }
  592. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  593. if (item == NULL) {
  594. return NGHTTP2_ERR_NOMEM;
  595. }
  596. nghttp2_outbound_item_init(item);
  597. frame = &item->frame;
  598. aux_data = &item->aux_data.data;
  599. aux_data->data_prd = *data_prd;
  600. aux_data->eof = 0;
  601. aux_data->flags = nflags;
  602. /* flags are sent on transmission */
  603. nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
  604. rv = nghttp2_session_add_item(session, item);
  605. if (rv != 0) {
  606. nghttp2_frame_data_free(&frame->data);
  607. nghttp2_mem_free(mem, item);
  608. return rv;
  609. }
  610. return 0;
  611. }
  612. ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
  613. const nghttp2_settings_entry *iv,
  614. size_t niv) {
  615. if (!nghttp2_iv_check(iv, niv)) {
  616. return NGHTTP2_ERR_INVALID_ARGUMENT;
  617. }
  618. if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
  619. return NGHTTP2_ERR_INSUFF_BUFSIZE;
  620. }
  621. return (ssize_t)nghttp2_frame_pack_settings_payload(buf, iv, niv);
  622. }
  623. int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
  624. uint8_t flags, int32_t stream_id, void *payload) {
  625. int rv;
  626. nghttp2_outbound_item *item;
  627. nghttp2_frame *frame;
  628. nghttp2_mem *mem;
  629. mem = &session->mem;
  630. if (type <= NGHTTP2_CONTINUATION) {
  631. return NGHTTP2_ERR_INVALID_ARGUMENT;
  632. }
  633. if (!session->callbacks.pack_extension_callback) {
  634. return NGHTTP2_ERR_INVALID_STATE;
  635. }
  636. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  637. if (item == NULL) {
  638. return NGHTTP2_ERR_NOMEM;
  639. }
  640. nghttp2_outbound_item_init(item);
  641. frame = &item->frame;
  642. nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
  643. rv = nghttp2_session_add_item(session, item);
  644. if (rv != 0) {
  645. nghttp2_frame_extension_free(&frame->ext);
  646. nghttp2_mem_free(mem, item);
  647. return rv;
  648. }
  649. return 0;
  650. }