nghttp2_submit.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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(
  147. session, NGHTTP2_FLAG_END_STREAM, stream_id, NULL, nva, nvlen, NULL, NULL);
  148. }
  149. int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
  150. int32_t stream_id,
  151. const nghttp2_priority_spec *pri_spec,
  152. const nghttp2_nv *nva, size_t nvlen,
  153. void *stream_user_data) {
  154. int rv;
  155. if (stream_id == -1) {
  156. if (session->server) {
  157. return NGHTTP2_ERR_PROTO;
  158. }
  159. } else if (stream_id <= 0) {
  160. return NGHTTP2_ERR_INVALID_ARGUMENT;
  161. }
  162. flags &= NGHTTP2_FLAG_END_STREAM;
  163. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
  164. session->remote_settings.no_rfc7540_priorities != 1) {
  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 (session->remote_settings.no_rfc7540_priorities == 1) {
  192. return 0;
  193. }
  194. if (stream_id == 0 || pri_spec == NULL) {
  195. return NGHTTP2_ERR_INVALID_ARGUMENT;
  196. }
  197. if (stream_id == pri_spec->stream_id) {
  198. return NGHTTP2_ERR_INVALID_ARGUMENT;
  199. }
  200. copy_pri_spec = *pri_spec;
  201. nghttp2_priority_spec_normalize_weight(&copy_pri_spec);
  202. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  203. if (item == NULL) {
  204. return NGHTTP2_ERR_NOMEM;
  205. }
  206. nghttp2_outbound_item_init(item);
  207. frame = &item->frame;
  208. nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
  209. rv = nghttp2_session_add_item(session, item);
  210. if (rv != 0) {
  211. nghttp2_frame_priority_free(&frame->priority);
  212. nghttp2_mem_free(mem, item);
  213. return rv;
  214. }
  215. return 0;
  216. }
  217. int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
  218. int32_t stream_id, uint32_t error_code) {
  219. (void)flags;
  220. if (stream_id == 0) {
  221. return NGHTTP2_ERR_INVALID_ARGUMENT;
  222. }
  223. return nghttp2_session_add_rst_stream(session, stream_id, error_code);
  224. }
  225. int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
  226. int32_t last_stream_id, uint32_t error_code,
  227. const uint8_t *opaque_data, size_t opaque_data_len) {
  228. (void)flags;
  229. if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
  230. return 0;
  231. }
  232. return nghttp2_session_add_goaway(session, last_stream_id, error_code,
  233. opaque_data, opaque_data_len,
  234. NGHTTP2_GOAWAY_AUX_NONE);
  235. }
  236. int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
  237. if (!session->server) {
  238. return NGHTTP2_ERR_INVALID_STATE;
  239. }
  240. if (session->goaway_flags) {
  241. return 0;
  242. }
  243. return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
  244. NULL, 0,
  245. NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
  246. }
  247. int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
  248. const nghttp2_settings_entry *iv, size_t niv) {
  249. (void)flags;
  250. return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
  251. }
  252. int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
  253. int32_t stream_id, const nghttp2_nv *nva,
  254. size_t nvlen,
  255. void *promised_stream_user_data) {
  256. nghttp2_outbound_item *item;
  257. nghttp2_frame *frame;
  258. nghttp2_nv *nva_copy;
  259. uint8_t flags_copy;
  260. int32_t promised_stream_id;
  261. int rv;
  262. nghttp2_mem *mem;
  263. (void)flags;
  264. mem = &session->mem;
  265. if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
  266. return NGHTTP2_ERR_INVALID_ARGUMENT;
  267. }
  268. if (!session->server) {
  269. return NGHTTP2_ERR_PROTO;
  270. }
  271. /* All 32bit signed stream IDs are spent. */
  272. if (session->next_stream_id > INT32_MAX) {
  273. return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
  274. }
  275. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  276. if (item == NULL) {
  277. return NGHTTP2_ERR_NOMEM;
  278. }
  279. nghttp2_outbound_item_init(item);
  280. item->aux_data.headers.stream_user_data = promised_stream_user_data;
  281. frame = &item->frame;
  282. rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
  283. if (rv < 0) {
  284. nghttp2_mem_free(mem, item);
  285. return rv;
  286. }
  287. flags_copy = NGHTTP2_FLAG_END_HEADERS;
  288. promised_stream_id = (int32_t)session->next_stream_id;
  289. session->next_stream_id += 2;
  290. nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
  291. promised_stream_id, nva_copy, nvlen);
  292. rv = nghttp2_session_add_item(session, item);
  293. if (rv != 0) {
  294. nghttp2_frame_push_promise_free(&frame->push_promise, mem);
  295. nghttp2_mem_free(mem, item);
  296. return rv;
  297. }
  298. return promised_stream_id;
  299. }
  300. int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
  301. int32_t stream_id,
  302. int32_t window_size_increment) {
  303. int rv;
  304. nghttp2_stream *stream = 0;
  305. (void)flags;
  306. if (window_size_increment == 0) {
  307. return 0;
  308. }
  309. if (stream_id == 0) {
  310. rv = nghttp2_adjust_local_window_size(
  311. &session->local_window_size, &session->recv_window_size,
  312. &session->recv_reduction, &window_size_increment);
  313. if (rv != 0) {
  314. return rv;
  315. }
  316. } else {
  317. stream = nghttp2_session_get_stream(session, stream_id);
  318. if (!stream) {
  319. return 0;
  320. }
  321. rv = nghttp2_adjust_local_window_size(
  322. &stream->local_window_size, &stream->recv_window_size,
  323. &stream->recv_reduction, &window_size_increment);
  324. if (rv != 0) {
  325. return rv;
  326. }
  327. }
  328. if (window_size_increment > 0) {
  329. if (stream_id == 0) {
  330. session->consumed_size =
  331. nghttp2_max_int32(0, session->consumed_size - window_size_increment);
  332. } else {
  333. stream->consumed_size =
  334. nghttp2_max_int32(0, stream->consumed_size - window_size_increment);
  335. }
  336. return nghttp2_session_add_window_update(session, 0, stream_id,
  337. window_size_increment);
  338. }
  339. return 0;
  340. }
  341. int nghttp2_session_set_local_window_size(nghttp2_session *session,
  342. uint8_t flags, int32_t stream_id,
  343. int32_t window_size) {
  344. int32_t window_size_increment;
  345. nghttp2_stream *stream;
  346. int rv;
  347. (void)flags;
  348. if (window_size < 0) {
  349. return NGHTTP2_ERR_INVALID_ARGUMENT;
  350. }
  351. if (stream_id == 0) {
  352. window_size_increment = window_size - session->local_window_size;
  353. if (window_size_increment == 0) {
  354. return 0;
  355. }
  356. if (window_size_increment < 0) {
  357. return nghttp2_adjust_local_window_size(
  358. &session->local_window_size, &session->recv_window_size,
  359. &session->recv_reduction, &window_size_increment);
  360. }
  361. rv = nghttp2_increase_local_window_size(
  362. &session->local_window_size, &session->recv_window_size,
  363. &session->recv_reduction, &window_size_increment);
  364. if (rv != 0) {
  365. return rv;
  366. }
  367. if (window_size_increment > 0) {
  368. return nghttp2_session_add_window_update(session, 0, stream_id,
  369. window_size_increment);
  370. }
  371. return nghttp2_session_update_recv_connection_window_size(session, 0);
  372. } else {
  373. stream = nghttp2_session_get_stream(session, stream_id);
  374. if (stream == NULL) {
  375. return 0;
  376. }
  377. window_size_increment = window_size - stream->local_window_size;
  378. if (window_size_increment == 0) {
  379. return 0;
  380. }
  381. if (window_size_increment < 0) {
  382. return nghttp2_adjust_local_window_size(
  383. &stream->local_window_size, &stream->recv_window_size,
  384. &stream->recv_reduction, &window_size_increment);
  385. }
  386. rv = nghttp2_increase_local_window_size(
  387. &stream->local_window_size, &stream->recv_window_size,
  388. &stream->recv_reduction, &window_size_increment);
  389. if (rv != 0) {
  390. return rv;
  391. }
  392. if (window_size_increment > 0) {
  393. return nghttp2_session_add_window_update(session, 0, stream_id,
  394. window_size_increment);
  395. }
  396. return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
  397. 1);
  398. }
  399. }
  400. int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
  401. int32_t stream_id, const uint8_t *origin,
  402. size_t origin_len, const uint8_t *field_value,
  403. size_t field_value_len) {
  404. nghttp2_mem *mem;
  405. uint8_t *buf, *p;
  406. uint8_t *origin_copy;
  407. uint8_t *field_value_copy;
  408. nghttp2_outbound_item *item;
  409. nghttp2_frame *frame;
  410. nghttp2_ext_altsvc *altsvc;
  411. int rv;
  412. (void)flags;
  413. mem = &session->mem;
  414. if (!session->server) {
  415. return NGHTTP2_ERR_INVALID_STATE;
  416. }
  417. if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  418. return NGHTTP2_ERR_INVALID_ARGUMENT;
  419. }
  420. if (stream_id == 0) {
  421. if (origin_len == 0) {
  422. return NGHTTP2_ERR_INVALID_ARGUMENT;
  423. }
  424. } else if (origin_len != 0) {
  425. return NGHTTP2_ERR_INVALID_ARGUMENT;
  426. }
  427. buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
  428. if (buf == NULL) {
  429. return NGHTTP2_ERR_NOMEM;
  430. }
  431. p = buf;
  432. origin_copy = p;
  433. if (origin_len) {
  434. p = nghttp2_cpymem(p, origin, origin_len);
  435. }
  436. *p++ = '\0';
  437. field_value_copy = p;
  438. if (field_value_len) {
  439. p = nghttp2_cpymem(p, field_value, field_value_len);
  440. }
  441. *p++ = '\0';
  442. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  443. if (item == NULL) {
  444. rv = NGHTTP2_ERR_NOMEM;
  445. goto fail_item_malloc;
  446. }
  447. nghttp2_outbound_item_init(item);
  448. item->aux_data.ext.builtin = 1;
  449. altsvc = &item->ext_frame_payload.altsvc;
  450. frame = &item->frame;
  451. frame->ext.payload = altsvc;
  452. nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
  453. field_value_copy, field_value_len);
  454. rv = nghttp2_session_add_item(session, item);
  455. if (rv != 0) {
  456. nghttp2_frame_altsvc_free(&frame->ext, mem);
  457. nghttp2_mem_free(mem, item);
  458. return rv;
  459. }
  460. return 0;
  461. fail_item_malloc:
  462. free(buf);
  463. return rv;
  464. }
  465. int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
  466. const nghttp2_origin_entry *ov, size_t nov) {
  467. nghttp2_mem *mem;
  468. uint8_t *p;
  469. nghttp2_outbound_item *item;
  470. nghttp2_frame *frame;
  471. nghttp2_ext_origin *origin;
  472. nghttp2_origin_entry *ov_copy;
  473. size_t len = 0;
  474. size_t i;
  475. int rv;
  476. (void)flags;
  477. mem = &session->mem;
  478. if (!session->server) {
  479. return NGHTTP2_ERR_INVALID_STATE;
  480. }
  481. if (nov) {
  482. for (i = 0; i < nov; ++i) {
  483. len += ov[i].origin_len;
  484. }
  485. if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
  486. return NGHTTP2_ERR_INVALID_ARGUMENT;
  487. }
  488. /* The last nov is added for terminal NULL character. */
  489. ov_copy =
  490. nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
  491. if (ov_copy == NULL) {
  492. return NGHTTP2_ERR_NOMEM;
  493. }
  494. p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
  495. for (i = 0; i < nov; ++i) {
  496. ov_copy[i].origin = p;
  497. ov_copy[i].origin_len = ov[i].origin_len;
  498. p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
  499. *p++ = '\0';
  500. }
  501. assert((size_t)(p - (uint8_t *)ov_copy) ==
  502. nov * sizeof(nghttp2_origin_entry) + len + nov);
  503. } else {
  504. ov_copy = NULL;
  505. }
  506. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  507. if (item == NULL) {
  508. rv = NGHTTP2_ERR_NOMEM;
  509. goto fail_item_malloc;
  510. }
  511. nghttp2_outbound_item_init(item);
  512. item->aux_data.ext.builtin = 1;
  513. origin = &item->ext_frame_payload.origin;
  514. frame = &item->frame;
  515. frame->ext.payload = origin;
  516. nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
  517. rv = nghttp2_session_add_item(session, item);
  518. if (rv != 0) {
  519. nghttp2_frame_origin_free(&frame->ext, mem);
  520. nghttp2_mem_free(mem, item);
  521. return rv;
  522. }
  523. return 0;
  524. fail_item_malloc:
  525. free(ov_copy);
  526. return rv;
  527. }
  528. int nghttp2_submit_priority_update(nghttp2_session *session, uint8_t flags,
  529. int32_t stream_id,
  530. const uint8_t *field_value,
  531. size_t field_value_len) {
  532. nghttp2_mem *mem;
  533. uint8_t *buf, *p;
  534. nghttp2_outbound_item *item;
  535. nghttp2_frame *frame;
  536. nghttp2_ext_priority_update *priority_update;
  537. int rv;
  538. (void)flags;
  539. mem = &session->mem;
  540. if (session->server) {
  541. return NGHTTP2_ERR_INVALID_STATE;
  542. }
  543. if (session->remote_settings.no_rfc7540_priorities == 0) {
  544. return 0;
  545. }
  546. if (stream_id == 0 || 4 + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
  547. return NGHTTP2_ERR_INVALID_ARGUMENT;
  548. }
  549. if (field_value_len) {
  550. buf = nghttp2_mem_malloc(mem, field_value_len + 1);
  551. if (buf == NULL) {
  552. return NGHTTP2_ERR_NOMEM;
  553. }
  554. p = nghttp2_cpymem(buf, field_value, field_value_len);
  555. *p = '\0';
  556. } else {
  557. buf = NULL;
  558. }
  559. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  560. if (item == NULL) {
  561. rv = NGHTTP2_ERR_NOMEM;
  562. goto fail_item_malloc;
  563. }
  564. nghttp2_outbound_item_init(item);
  565. item->aux_data.ext.builtin = 1;
  566. priority_update = &item->ext_frame_payload.priority_update;
  567. frame = &item->frame;
  568. frame->ext.payload = priority_update;
  569. nghttp2_frame_priority_update_init(&frame->ext, stream_id, buf,
  570. field_value_len);
  571. rv = nghttp2_session_add_item(session, item);
  572. if (rv != 0) {
  573. nghttp2_frame_priority_update_free(&frame->ext, mem);
  574. nghttp2_mem_free(mem, item);
  575. return rv;
  576. }
  577. return 0;
  578. fail_item_malloc:
  579. free(buf);
  580. return rv;
  581. }
  582. static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
  583. const nghttp2_data_provider_wrap *dpw) {
  584. uint8_t flags = NGHTTP2_FLAG_NONE;
  585. if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
  586. flags |= NGHTTP2_FLAG_END_STREAM;
  587. }
  588. if (pri_spec) {
  589. flags |= NGHTTP2_FLAG_PRIORITY;
  590. }
  591. return flags;
  592. }
  593. static int32_t submit_request_shared(nghttp2_session *session,
  594. const nghttp2_priority_spec *pri_spec,
  595. const nghttp2_nv *nva, size_t nvlen,
  596. const nghttp2_data_provider_wrap *dpw,
  597. void *stream_user_data) {
  598. uint8_t flags;
  599. int rv;
  600. if (session->server) {
  601. return NGHTTP2_ERR_PROTO;
  602. }
  603. if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec) &&
  604. session->remote_settings.no_rfc7540_priorities != 1) {
  605. rv = detect_self_dependency(session, -1, pri_spec);
  606. if (rv != 0) {
  607. return rv;
  608. }
  609. } else {
  610. pri_spec = NULL;
  611. }
  612. flags = set_request_flags(pri_spec, dpw);
  613. return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
  614. dpw, stream_user_data);
  615. }
  616. int32_t nghttp2_submit_request(nghttp2_session *session,
  617. const nghttp2_priority_spec *pri_spec,
  618. const nghttp2_nv *nva, size_t nvlen,
  619. const nghttp2_data_provider *data_prd,
  620. void *stream_user_data) {
  621. nghttp2_data_provider_wrap dpw;
  622. return submit_request_shared(session, pri_spec, nva, nvlen,
  623. nghttp2_data_provider_wrap_v1(&dpw, data_prd),
  624. stream_user_data);
  625. }
  626. int32_t nghttp2_submit_request2(nghttp2_session *session,
  627. const nghttp2_priority_spec *pri_spec,
  628. const nghttp2_nv *nva, size_t nvlen,
  629. const nghttp2_data_provider2 *data_prd,
  630. void *stream_user_data) {
  631. nghttp2_data_provider_wrap dpw;
  632. return submit_request_shared(session, pri_spec, nva, nvlen,
  633. nghttp2_data_provider_wrap_v2(&dpw, data_prd),
  634. stream_user_data);
  635. }
  636. static uint8_t set_response_flags(const nghttp2_data_provider_wrap *dpw) {
  637. uint8_t flags = NGHTTP2_FLAG_NONE;
  638. if (dpw == NULL || dpw->data_prd.read_callback == NULL) {
  639. flags |= NGHTTP2_FLAG_END_STREAM;
  640. }
  641. return flags;
  642. }
  643. static int submit_response_shared(nghttp2_session *session, int32_t stream_id,
  644. const nghttp2_nv *nva, size_t nvlen,
  645. const nghttp2_data_provider_wrap *dpw) {
  646. uint8_t flags;
  647. if (stream_id <= 0) {
  648. return NGHTTP2_ERR_INVALID_ARGUMENT;
  649. }
  650. if (!session->server) {
  651. return NGHTTP2_ERR_PROTO;
  652. }
  653. flags = set_response_flags(dpw);
  654. return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
  655. dpw, NULL);
  656. }
  657. int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
  658. const nghttp2_nv *nva, size_t nvlen,
  659. const nghttp2_data_provider *data_prd) {
  660. nghttp2_data_provider_wrap dpw;
  661. return submit_response_shared(session, stream_id, nva, nvlen,
  662. nghttp2_data_provider_wrap_v1(&dpw, data_prd));
  663. }
  664. int nghttp2_submit_response2(nghttp2_session *session, int32_t stream_id,
  665. const nghttp2_nv *nva, size_t nvlen,
  666. const nghttp2_data_provider2 *data_prd) {
  667. nghttp2_data_provider_wrap dpw;
  668. return submit_response_shared(session, stream_id, nva, nvlen,
  669. nghttp2_data_provider_wrap_v2(&dpw, data_prd));
  670. }
  671. int nghttp2_submit_data_shared(nghttp2_session *session, uint8_t flags,
  672. int32_t stream_id,
  673. const nghttp2_data_provider_wrap *dpw) {
  674. int rv;
  675. nghttp2_outbound_item *item;
  676. nghttp2_frame *frame;
  677. nghttp2_data_aux_data *aux_data;
  678. uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
  679. nghttp2_mem *mem;
  680. mem = &session->mem;
  681. if (stream_id == 0) {
  682. return NGHTTP2_ERR_INVALID_ARGUMENT;
  683. }
  684. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  685. if (item == NULL) {
  686. return NGHTTP2_ERR_NOMEM;
  687. }
  688. nghttp2_outbound_item_init(item);
  689. frame = &item->frame;
  690. aux_data = &item->aux_data.data;
  691. aux_data->dpw = *dpw;
  692. aux_data->eof = 0;
  693. aux_data->flags = nflags;
  694. /* flags are sent on transmission */
  695. nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
  696. rv = nghttp2_session_add_item(session, item);
  697. if (rv != 0) {
  698. nghttp2_frame_data_free(&frame->data);
  699. nghttp2_mem_free(mem, item);
  700. return rv;
  701. }
  702. return 0;
  703. }
  704. int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
  705. int32_t stream_id,
  706. const nghttp2_data_provider *data_prd) {
  707. nghttp2_data_provider_wrap dpw;
  708. assert(data_prd);
  709. return nghttp2_submit_data_shared(
  710. session, flags, stream_id, nghttp2_data_provider_wrap_v1(&dpw, data_prd));
  711. }
  712. int nghttp2_submit_data2(nghttp2_session *session, uint8_t flags,
  713. int32_t stream_id,
  714. const nghttp2_data_provider2 *data_prd) {
  715. nghttp2_data_provider_wrap dpw;
  716. assert(data_prd);
  717. return nghttp2_submit_data_shared(
  718. session, flags, stream_id, nghttp2_data_provider_wrap_v2(&dpw, data_prd));
  719. }
  720. ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
  721. const nghttp2_settings_entry *iv,
  722. size_t niv) {
  723. return (ssize_t)nghttp2_pack_settings_payload2(buf, buflen, iv, niv);
  724. }
  725. nghttp2_ssize nghttp2_pack_settings_payload2(uint8_t *buf, size_t buflen,
  726. const nghttp2_settings_entry *iv,
  727. size_t niv) {
  728. if (!nghttp2_iv_check(iv, niv)) {
  729. return NGHTTP2_ERR_INVALID_ARGUMENT;
  730. }
  731. if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
  732. return NGHTTP2_ERR_INSUFF_BUFSIZE;
  733. }
  734. return (nghttp2_ssize)nghttp2_frame_pack_settings_payload(buf, iv, niv);
  735. }
  736. int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
  737. uint8_t flags, int32_t stream_id, void *payload) {
  738. int rv;
  739. nghttp2_outbound_item *item;
  740. nghttp2_frame *frame;
  741. nghttp2_mem *mem;
  742. mem = &session->mem;
  743. if (type <= NGHTTP2_CONTINUATION) {
  744. return NGHTTP2_ERR_INVALID_ARGUMENT;
  745. }
  746. if (!session->callbacks.pack_extension_callback2 &&
  747. !session->callbacks.pack_extension_callback) {
  748. return NGHTTP2_ERR_INVALID_STATE;
  749. }
  750. item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  751. if (item == NULL) {
  752. return NGHTTP2_ERR_NOMEM;
  753. }
  754. nghttp2_outbound_item_init(item);
  755. frame = &item->frame;
  756. nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
  757. rv = nghttp2_session_add_item(session, item);
  758. if (rv != 0) {
  759. nghttp2_frame_extension_free(&frame->ext);
  760. nghttp2_mem_free(mem, item);
  761. return rv;
  762. }
  763. return 0;
  764. }