nghttp2_submit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. } else {
  365. stream = nghttp2_session_get_stream(session, stream_id);
  366. if (stream == NULL) {
  367. return 0;
  368. }
  369. window_size_increment = window_size - stream->local_window_size;
  370. if (window_size_increment == 0) {
  371. return 0;
  372. }
  373. if (window_size_increment < 0) {
  374. return nghttp2_adjust_local_window_size(
  375. &stream->local_window_size, &stream->recv_window_size,
  376. &stream->recv_reduction, &window_size_increment);
  377. }
  378. rv = nghttp2_increase_local_window_size(
  379. &stream->local_window_size, &stream->recv_window_size,
  380. &stream->recv_reduction, &window_size_increment);
  381. if (rv != 0) {
  382. return rv;
  383. }
  384. }
  385. if (window_size_increment > 0) {
  386. return nghttp2_session_add_window_update(session, 0, stream_id,
  387. window_size_increment);
  388. }
  389. return 0;
  390. }
  391. int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
  392. int32_t stream_id, const uint8_t *origin,
  393. size_t origin_len, const uint8_t *field_value,
  394. size_t field_value_len) {
  395. nghttp2_mem *mem;
  396. uint8_t *buf, *p;
  397. uint8_t *origin_copy;
  398. uint8_t *field_value_copy;
  399. nghttp2_outbound_item *item;
  400. nghttp2_frame *frame;
  401. nghttp2_ext_altsvc *altsvc;
  402. int rv;
  403. (void)flags;
  404. mem = &session->mem;
  405. if (!session->server) {
  406. return NGHTTP2_ERR_INVALID_STATE;
  407. }
  408. if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  409. return NGHTTP2_ERR_INVALID_ARGUMENT;
  410. }
  411. if (stream_id == 0) {
  412. if (origin_len == 0) {
  413. return NGHTTP2_ERR_INVALID_ARGUMENT;
  414. }
  415. } else if (origin_len != 0) {
  416. return NGHTTP2_ERR_INVALID_ARGUMENT;
  417. }
  418. buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
  419. if (buf == NULL) {
  420. return NGHTTP2_ERR_NOMEM;
  421. }
  422. p = buf;
  423. origin_copy = p;
  424. if (origin_len) {
  425. p = nghttp2_cpymem(p, origin, origin_len);
  426. }
  427. *p++ = '\0';
  428. field_value_copy = p;
  429. if (field_value_len) {
  430. p = nghttp2_cpymem(p, field_value, field_value_len);
  431. }
  432. *p++ = '\0';
  433. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  434. if (item == NULL) {
  435. rv = NGHTTP2_ERR_NOMEM;
  436. goto fail_item_malloc;
  437. }
  438. nghttp2_outbound_item_init(item);
  439. item->aux_data.ext.builtin = 1;
  440. altsvc = &item->ext_frame_payload.altsvc;
  441. frame = &item->frame;
  442. frame->ext.payload = altsvc;
  443. nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
  444. field_value_copy, field_value_len);
  445. rv = nghttp2_session_add_item(session, item);
  446. if (rv != 0) {
  447. nghttp2_frame_altsvc_free(&frame->ext, mem);
  448. nghttp2_mem_free(mem, item);
  449. return rv;
  450. }
  451. return 0;
  452. fail_item_malloc:
  453. free(buf);
  454. return rv;
  455. }
  456. int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
  457. const nghttp2_origin_entry *ov, size_t nov) {
  458. nghttp2_mem *mem;
  459. uint8_t *p;
  460. nghttp2_outbound_item *item;
  461. nghttp2_frame *frame;
  462. nghttp2_ext_origin *origin;
  463. nghttp2_origin_entry *ov_copy;
  464. size_t len = 0;
  465. size_t i;
  466. int rv;
  467. (void)flags;
  468. mem = &session->mem;
  469. if (!session->server) {
  470. return NGHTTP2_ERR_INVALID_STATE;
  471. }
  472. if (nov) {
  473. for (i = 0; i < nov; ++i) {
  474. len += ov[i].origin_len;
  475. }
  476. if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
  477. return NGHTTP2_ERR_INVALID_ARGUMENT;
  478. }
  479. /* The last nov is added for terminal NULL character. */
  480. ov_copy =
  481. nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
  482. if (ov_copy == NULL) {
  483. return NGHTTP2_ERR_NOMEM;
  484. }
  485. p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
  486. for (i = 0; i < nov; ++i) {
  487. ov_copy[i].origin = p;
  488. ov_copy[i].origin_len = ov[i].origin_len;
  489. p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
  490. *p++ = '\0';
  491. }
  492. assert((size_t)(p - (uint8_t *)ov_copy) ==
  493. nov * sizeof(nghttp2_origin_entry) + len + nov);
  494. } else {
  495. ov_copy = NULL;
  496. }
  497. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  498. if (item == NULL) {
  499. rv = NGHTTP2_ERR_NOMEM;
  500. goto fail_item_malloc;
  501. }
  502. nghttp2_outbound_item_init(item);
  503. item->aux_data.ext.builtin = 1;
  504. origin = &item->ext_frame_payload.origin;
  505. frame = &item->frame;
  506. frame->ext.payload = origin;
  507. nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
  508. rv = nghttp2_session_add_item(session, item);
  509. if (rv != 0) {
  510. nghttp2_frame_origin_free(&frame->ext, mem);
  511. nghttp2_mem_free(mem, item);
  512. return rv;
  513. }
  514. return 0;
  515. fail_item_malloc:
  516. free(ov_copy);
  517. return rv;
  518. }
  519. static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
  520. const nghttp2_data_provider *data_prd) {
  521. uint8_t flags = NGHTTP2_FLAG_NONE;
  522. if (data_prd == NULL || data_prd->read_callback == NULL) {
  523. flags |= NGHTTP2_FLAG_END_STREAM;
  524. }
  525. if (pri_spec) {
  526. flags |= NGHTTP2_FLAG_PRIORITY;
  527. }
  528. return flags;
  529. }
  530. int32_t nghttp2_submit_request(nghttp2_session *session,
  531. const nghttp2_priority_spec *pri_spec,
  532. const nghttp2_nv *nva, size_t nvlen,
  533. const nghttp2_data_provider *data_prd,
  534. void *stream_user_data) {
  535. uint8_t flags;
  536. int rv;
  537. if (session->server) {
  538. return NGHTTP2_ERR_PROTO;
  539. }
  540. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
  541. rv = detect_self_dependency(session, -1, pri_spec);
  542. if (rv != 0) {
  543. return rv;
  544. }
  545. } else {
  546. pri_spec = NULL;
  547. }
  548. flags = set_request_flags(pri_spec, data_prd);
  549. return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
  550. data_prd, stream_user_data);
  551. }
  552. static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
  553. uint8_t flags = NGHTTP2_FLAG_NONE;
  554. if (data_prd == NULL || data_prd->read_callback == NULL) {
  555. flags |= NGHTTP2_FLAG_END_STREAM;
  556. }
  557. return flags;
  558. }
  559. int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
  560. const nghttp2_nv *nva, size_t nvlen,
  561. const nghttp2_data_provider *data_prd) {
  562. uint8_t flags;
  563. if (stream_id <= 0) {
  564. return NGHTTP2_ERR_INVALID_ARGUMENT;
  565. }
  566. if (!session->server) {
  567. return NGHTTP2_ERR_PROTO;
  568. }
  569. flags = set_response_flags(data_prd);
  570. return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
  571. data_prd, NULL);
  572. }
  573. int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
  574. int32_t stream_id,
  575. const nghttp2_data_provider *data_prd) {
  576. int rv;
  577. nghttp2_outbound_item *item;
  578. nghttp2_frame *frame;
  579. nghttp2_data_aux_data *aux_data;
  580. uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
  581. nghttp2_mem *mem;
  582. mem = &session->mem;
  583. if (stream_id == 0) {
  584. return NGHTTP2_ERR_INVALID_ARGUMENT;
  585. }
  586. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  587. if (item == NULL) {
  588. return NGHTTP2_ERR_NOMEM;
  589. }
  590. nghttp2_outbound_item_init(item);
  591. frame = &item->frame;
  592. aux_data = &item->aux_data.data;
  593. aux_data->data_prd = *data_prd;
  594. aux_data->eof = 0;
  595. aux_data->flags = nflags;
  596. /* flags are sent on transmission */
  597. nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
  598. rv = nghttp2_session_add_item(session, item);
  599. if (rv != 0) {
  600. nghttp2_frame_data_free(&frame->data);
  601. nghttp2_mem_free(mem, item);
  602. return rv;
  603. }
  604. return 0;
  605. }
  606. ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
  607. const nghttp2_settings_entry *iv,
  608. size_t niv) {
  609. if (!nghttp2_iv_check(iv, niv)) {
  610. return NGHTTP2_ERR_INVALID_ARGUMENT;
  611. }
  612. if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
  613. return NGHTTP2_ERR_INSUFF_BUFSIZE;
  614. }
  615. return (ssize_t)nghttp2_frame_pack_settings_payload(buf, iv, niv);
  616. }
  617. int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
  618. uint8_t flags, int32_t stream_id, void *payload) {
  619. int rv;
  620. nghttp2_outbound_item *item;
  621. nghttp2_frame *frame;
  622. nghttp2_mem *mem;
  623. mem = &session->mem;
  624. if (type <= NGHTTP2_CONTINUATION) {
  625. return NGHTTP2_ERR_INVALID_ARGUMENT;
  626. }
  627. if (!session->callbacks.pack_extension_callback) {
  628. return NGHTTP2_ERR_INVALID_STATE;
  629. }
  630. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  631. if (item == NULL) {
  632. return NGHTTP2_ERR_NOMEM;
  633. }
  634. nghttp2_outbound_item_init(item);
  635. frame = &item->frame;
  636. nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
  637. rv = nghttp2_session_add_item(session, item);
  638. if (rv != 0) {
  639. nghttp2_frame_extension_free(&frame->ext);
  640. nghttp2_mem_free(mem, item);
  641. return rv;
  642. }
  643. return 0;
  644. }