2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012, 2013 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "nghttp2_submit.h"
30 #include "nghttp2_session.h"
31 #include "nghttp2_frame.h"
32 #include "nghttp2_helper.h"
33 #include "nghttp2_priority_spec.h"
36 * Detects the dependency error, that is stream attempted to depend on
37 * itself. If |stream_id| is -1, we use session->next_stream_id as
40 * This function returns 0 if it succeeds, or one of the following
43 * NGHTTP2_ERR_INVALID_ARGUMENT
44 * Stream attempted to depend on itself.
46 static int detect_self_dependency(nghttp2_session *session, int32_t stream_id,
47 const nghttp2_priority_spec *pri_spec) {
50 if (stream_id == -1) {
51 if ((int32_t)session->next_stream_id == pri_spec->stream_id) {
52 return NGHTTP2_ERR_INVALID_ARGUMENT;
57 if (stream_id == pri_spec->stream_id) {
58 return NGHTTP2_ERR_INVALID_ARGUMENT;
64 /* This function takes ownership of |nva_copy|. Regardless of the
65 return value, the caller must not free |nva_copy| after this
67 static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
69 const nghttp2_priority_spec *pri_spec,
70 nghttp2_nv *nva_copy, size_t nvlen,
71 const nghttp2_data_provider *data_prd,
72 void *stream_user_data) {
75 nghttp2_outbound_item *item = NULL;
76 nghttp2_frame *frame = NULL;
77 nghttp2_headers_category hcat;
82 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
84 rv = NGHTTP2_ERR_NOMEM;
88 nghttp2_outbound_item_init(item);
90 if (data_prd != NULL && data_prd->read_callback != NULL) {
91 item->aux_data.headers.data_prd = *data_prd;
94 item->aux_data.headers.stream_user_data = stream_user_data;
97 (uint8_t)((flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
98 NGHTTP2_FLAG_END_HEADERS);
100 if (stream_id == -1) {
101 if (session->next_stream_id > INT32_MAX) {
102 rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
106 stream_id = (int32_t)session->next_stream_id;
107 session->next_stream_id += 2;
109 hcat = NGHTTP2_HCAT_REQUEST;
111 /* More specific categorization will be done later. */
112 hcat = NGHTTP2_HCAT_HEADERS;
115 frame = &item->frame;
117 nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
118 pri_spec, nva_copy, nvlen);
120 rv = nghttp2_session_add_item(session, item);
123 nghttp2_frame_headers_free(&frame->headers, mem);
127 if (hcat == NGHTTP2_HCAT_REQUEST) {
134 /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
135 nghttp2_nv_array_del(nva_copy, mem);
137 nghttp2_mem_free(mem, item);
142 static int32_t submit_headers_shared_nva(nghttp2_session *session,
143 uint8_t flags, int32_t stream_id,
144 const nghttp2_priority_spec *pri_spec,
145 const nghttp2_nv *nva, size_t nvlen,
146 const nghttp2_data_provider *data_prd,
147 void *stream_user_data) {
149 nghttp2_nv *nva_copy;
150 nghttp2_priority_spec copy_pri_spec;
156 copy_pri_spec = *pri_spec;
157 nghttp2_priority_spec_normalize_weight(©_pri_spec);
159 nghttp2_priority_spec_default_init(©_pri_spec);
162 rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
167 return submit_headers_shared(session, flags, stream_id, ©_pri_spec,
168 nva_copy, nvlen, data_prd, stream_user_data);
171 int nghttp2_submit_trailer(nghttp2_session *session, int32_t stream_id,
172 const nghttp2_nv *nva, size_t nvlen) {
173 if (stream_id <= 0) {
174 return NGHTTP2_ERR_INVALID_ARGUMENT;
177 return (int)submit_headers_shared_nva(session, NGHTTP2_FLAG_END_STREAM,
178 stream_id, NULL, nva, nvlen, NULL,
182 int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
184 const nghttp2_priority_spec *pri_spec,
185 const nghttp2_nv *nva, size_t nvlen,
186 void *stream_user_data) {
189 if (stream_id == -1) {
190 if (session->server) {
191 return NGHTTP2_ERR_PROTO;
193 } else if (stream_id <= 0) {
194 return NGHTTP2_ERR_INVALID_ARGUMENT;
197 flags &= NGHTTP2_FLAG_END_STREAM;
199 if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
200 rv = detect_self_dependency(session, stream_id, pri_spec);
205 flags |= NGHTTP2_FLAG_PRIORITY;
210 return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
211 nvlen, NULL, stream_user_data);
214 int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
215 const uint8_t *opaque_data) {
216 flags &= NGHTTP2_FLAG_ACK;
217 return nghttp2_session_add_ping(session, flags, opaque_data);
220 int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
222 const nghttp2_priority_spec *pri_spec) {
224 nghttp2_outbound_item *item;
225 nghttp2_frame *frame;
226 nghttp2_priority_spec copy_pri_spec;
232 if (stream_id == 0 || pri_spec == NULL) {
233 return NGHTTP2_ERR_INVALID_ARGUMENT;
236 if (stream_id == pri_spec->stream_id) {
237 return NGHTTP2_ERR_INVALID_ARGUMENT;
240 copy_pri_spec = *pri_spec;
242 nghttp2_priority_spec_normalize_weight(©_pri_spec);
244 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
247 return NGHTTP2_ERR_NOMEM;
250 nghttp2_outbound_item_init(item);
252 frame = &item->frame;
254 nghttp2_frame_priority_init(&frame->priority, stream_id, ©_pri_spec);
256 rv = nghttp2_session_add_item(session, item);
259 nghttp2_frame_priority_free(&frame->priority);
260 nghttp2_mem_free(mem, item);
268 int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags,
269 int32_t stream_id, uint32_t error_code) {
272 if (stream_id == 0) {
273 return NGHTTP2_ERR_INVALID_ARGUMENT;
276 return nghttp2_session_add_rst_stream(session, stream_id, error_code);
279 int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags,
280 int32_t last_stream_id, uint32_t error_code,
281 const uint8_t *opaque_data, size_t opaque_data_len) {
284 if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
287 return nghttp2_session_add_goaway(session, last_stream_id, error_code,
288 opaque_data, opaque_data_len,
289 NGHTTP2_GOAWAY_AUX_NONE);
292 int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
293 if (!session->server) {
294 return NGHTTP2_ERR_INVALID_STATE;
296 if (session->goaway_flags) {
299 return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
301 NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
304 int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags,
305 const nghttp2_settings_entry *iv, size_t niv) {
307 return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
310 int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags,
311 int32_t stream_id, const nghttp2_nv *nva,
313 void *promised_stream_user_data) {
314 nghttp2_outbound_item *item;
315 nghttp2_frame *frame;
316 nghttp2_nv *nva_copy;
318 int32_t promised_stream_id;
325 if (stream_id <= 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
326 return NGHTTP2_ERR_INVALID_ARGUMENT;
329 if (!session->server) {
330 return NGHTTP2_ERR_PROTO;
333 /* All 32bit signed stream IDs are spent. */
334 if (session->next_stream_id > INT32_MAX) {
335 return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
338 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
340 return NGHTTP2_ERR_NOMEM;
343 nghttp2_outbound_item_init(item);
345 item->aux_data.headers.stream_user_data = promised_stream_user_data;
347 frame = &item->frame;
349 rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
351 nghttp2_mem_free(mem, item);
355 flags_copy = NGHTTP2_FLAG_END_HEADERS;
357 promised_stream_id = (int32_t)session->next_stream_id;
358 session->next_stream_id += 2;
360 nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
361 promised_stream_id, nva_copy, nvlen);
363 rv = nghttp2_session_add_item(session, item);
366 nghttp2_frame_push_promise_free(&frame->push_promise, mem);
367 nghttp2_mem_free(mem, item);
372 return promised_stream_id;
375 int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
377 int32_t window_size_increment) {
379 nghttp2_stream *stream = 0;
382 if (window_size_increment == 0) {
385 if (stream_id == 0) {
386 rv = nghttp2_adjust_local_window_size(
387 &session->local_window_size, &session->recv_window_size,
388 &session->recv_reduction, &window_size_increment);
393 stream = nghttp2_session_get_stream(session, stream_id);
398 rv = nghttp2_adjust_local_window_size(
399 &stream->local_window_size, &stream->recv_window_size,
400 &stream->recv_reduction, &window_size_increment);
406 if (window_size_increment > 0) {
407 if (stream_id == 0) {
408 session->consumed_size =
409 nghttp2_max(0, session->consumed_size - window_size_increment);
411 stream->consumed_size =
412 nghttp2_max(0, stream->consumed_size - window_size_increment);
415 return nghttp2_session_add_window_update(session, 0, stream_id,
416 window_size_increment);
421 int nghttp2_session_set_local_window_size(nghttp2_session *session,
422 uint8_t flags, int32_t stream_id,
423 int32_t window_size) {
424 int32_t window_size_increment;
425 nghttp2_stream *stream;
429 if (window_size < 0) {
430 return NGHTTP2_ERR_INVALID_ARGUMENT;
433 if (stream_id == 0) {
434 window_size_increment = window_size - session->local_window_size;
436 if (window_size_increment == 0) {
440 if (window_size_increment < 0) {
441 return nghttp2_adjust_local_window_size(
442 &session->local_window_size, &session->recv_window_size,
443 &session->recv_reduction, &window_size_increment);
446 rv = nghttp2_increase_local_window_size(
447 &session->local_window_size, &session->recv_window_size,
448 &session->recv_reduction, &window_size_increment);
454 if (window_size_increment > 0) {
455 return nghttp2_session_add_window_update(session, 0, stream_id,
456 window_size_increment);
459 return nghttp2_session_update_recv_connection_window_size(session, 0);
461 stream = nghttp2_session_get_stream(session, stream_id);
463 if (stream == NULL) {
467 window_size_increment = window_size - stream->local_window_size;
469 if (window_size_increment == 0) {
473 if (window_size_increment < 0) {
474 return nghttp2_adjust_local_window_size(
475 &stream->local_window_size, &stream->recv_window_size,
476 &stream->recv_reduction, &window_size_increment);
479 rv = nghttp2_increase_local_window_size(
480 &stream->local_window_size, &stream->recv_window_size,
481 &stream->recv_reduction, &window_size_increment);
487 if (window_size_increment > 0) {
488 return nghttp2_session_add_window_update(session, 0, stream_id,
489 window_size_increment);
492 return nghttp2_session_update_recv_stream_window_size(session, stream, 0,
499 int nghttp2_submit_altsvc(nghttp2_session *session, uint8_t flags,
500 int32_t stream_id, const uint8_t *origin,
501 size_t origin_len, const uint8_t *field_value,
502 size_t field_value_len) {
505 uint8_t *origin_copy;
506 uint8_t *field_value_copy;
507 nghttp2_outbound_item *item;
508 nghttp2_frame *frame;
509 nghttp2_ext_altsvc *altsvc;
515 if (!session->server) {
516 return NGHTTP2_ERR_INVALID_STATE;
519 if (2 + origin_len + field_value_len > NGHTTP2_MAX_PAYLOADLEN) {
520 return NGHTTP2_ERR_INVALID_ARGUMENT;
523 if (stream_id == 0) {
524 if (origin_len == 0) {
525 return NGHTTP2_ERR_INVALID_ARGUMENT;
527 } else if (origin_len != 0) {
528 return NGHTTP2_ERR_INVALID_ARGUMENT;
531 buf = nghttp2_mem_malloc(mem, origin_len + field_value_len + 2);
533 return NGHTTP2_ERR_NOMEM;
540 p = nghttp2_cpymem(p, origin, origin_len);
544 field_value_copy = p;
545 if (field_value_len) {
546 p = nghttp2_cpymem(p, field_value, field_value_len);
550 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
552 rv = NGHTTP2_ERR_NOMEM;
553 goto fail_item_malloc;
556 nghttp2_outbound_item_init(item);
558 item->aux_data.ext.builtin = 1;
560 altsvc = &item->ext_frame_payload.altsvc;
562 frame = &item->frame;
563 frame->ext.payload = altsvc;
565 nghttp2_frame_altsvc_init(&frame->ext, stream_id, origin_copy, origin_len,
566 field_value_copy, field_value_len);
568 rv = nghttp2_session_add_item(session, item);
570 nghttp2_frame_altsvc_free(&frame->ext, mem);
571 nghttp2_mem_free(mem, item);
584 int nghttp2_submit_origin(nghttp2_session *session, uint8_t flags,
585 const nghttp2_origin_entry *ov, size_t nov) {
588 nghttp2_outbound_item *item;
589 nghttp2_frame *frame;
590 nghttp2_ext_origin *origin;
591 nghttp2_origin_entry *ov_copy;
599 if (!session->server) {
600 return NGHTTP2_ERR_INVALID_STATE;
604 for (i = 0; i < nov; ++i) {
605 len += ov[i].origin_len;
608 if (2 * nov + len > NGHTTP2_MAX_PAYLOADLEN) {
609 return NGHTTP2_ERR_INVALID_ARGUMENT;
612 /* The last nov is added for terminal NULL character. */
614 nghttp2_mem_malloc(mem, nov * sizeof(nghttp2_origin_entry) + len + nov);
615 if (ov_copy == NULL) {
616 return NGHTTP2_ERR_NOMEM;
619 p = (uint8_t *)ov_copy + nov * sizeof(nghttp2_origin_entry);
621 for (i = 0; i < nov; ++i) {
622 ov_copy[i].origin = p;
623 ov_copy[i].origin_len = ov[i].origin_len;
624 p = nghttp2_cpymem(p, ov[i].origin, ov[i].origin_len);
628 assert((size_t)(p - (uint8_t *)ov_copy) ==
629 nov * sizeof(nghttp2_origin_entry) + len + nov);
634 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
636 rv = NGHTTP2_ERR_NOMEM;
637 goto fail_item_malloc;
640 nghttp2_outbound_item_init(item);
642 item->aux_data.ext.builtin = 1;
644 origin = &item->ext_frame_payload.origin;
646 frame = &item->frame;
647 frame->ext.payload = origin;
649 nghttp2_frame_origin_init(&frame->ext, ov_copy, nov);
651 rv = nghttp2_session_add_item(session, item);
653 nghttp2_frame_origin_free(&frame->ext, mem);
654 nghttp2_mem_free(mem, item);
667 static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
668 const nghttp2_data_provider *data_prd) {
669 uint8_t flags = NGHTTP2_FLAG_NONE;
670 if (data_prd == NULL || data_prd->read_callback == NULL) {
671 flags |= NGHTTP2_FLAG_END_STREAM;
675 flags |= NGHTTP2_FLAG_PRIORITY;
681 int32_t nghttp2_submit_request(nghttp2_session *session,
682 const nghttp2_priority_spec *pri_spec,
683 const nghttp2_nv *nva, size_t nvlen,
684 const nghttp2_data_provider *data_prd,
685 void *stream_user_data) {
689 if (session->server) {
690 return NGHTTP2_ERR_PROTO;
693 if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
694 rv = detect_self_dependency(session, -1, pri_spec);
702 flags = set_request_flags(pri_spec, data_prd);
704 return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
705 data_prd, stream_user_data);
708 static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
709 uint8_t flags = NGHTTP2_FLAG_NONE;
710 if (data_prd == NULL || data_prd->read_callback == NULL) {
711 flags |= NGHTTP2_FLAG_END_STREAM;
716 int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
717 const nghttp2_nv *nva, size_t nvlen,
718 const nghttp2_data_provider *data_prd) {
721 if (stream_id <= 0) {
722 return NGHTTP2_ERR_INVALID_ARGUMENT;
725 if (!session->server) {
726 return NGHTTP2_ERR_PROTO;
729 flags = set_response_flags(data_prd);
730 return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
734 int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
736 const nghttp2_data_provider *data_prd) {
738 nghttp2_outbound_item *item;
739 nghttp2_frame *frame;
740 nghttp2_data_aux_data *aux_data;
741 uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
746 if (stream_id == 0) {
747 return NGHTTP2_ERR_INVALID_ARGUMENT;
750 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
752 return NGHTTP2_ERR_NOMEM;
755 nghttp2_outbound_item_init(item);
757 frame = &item->frame;
758 aux_data = &item->aux_data.data;
759 aux_data->data_prd = *data_prd;
761 aux_data->flags = nflags;
763 /* flags are sent on transmission */
764 nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
766 rv = nghttp2_session_add_item(session, item);
768 nghttp2_frame_data_free(&frame->data);
769 nghttp2_mem_free(mem, item);
775 ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
776 const nghttp2_settings_entry *iv,
778 if (!nghttp2_iv_check(iv, niv)) {
779 return NGHTTP2_ERR_INVALID_ARGUMENT;
782 if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
783 return NGHTTP2_ERR_INSUFF_BUFSIZE;
786 return (ssize_t)nghttp2_frame_pack_settings_payload(buf, iv, niv);
789 int nghttp2_submit_extension(nghttp2_session *session, uint8_t type,
790 uint8_t flags, int32_t stream_id, void *payload) {
792 nghttp2_outbound_item *item;
793 nghttp2_frame *frame;
798 if (type <= NGHTTP2_CONTINUATION) {
799 return NGHTTP2_ERR_INVALID_ARGUMENT;
802 if (!session->callbacks.pack_extension_callback) {
803 return NGHTTP2_ERR_INVALID_STATE;
806 item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
808 return NGHTTP2_ERR_NOMEM;
811 nghttp2_outbound_item_init(item);
813 frame = &item->frame;
814 nghttp2_frame_extension_init(&frame->ext, type, flags, stream_id, payload);
816 rv = nghttp2_session_add_item(session, item);
818 nghttp2_frame_extension_free(&frame->ext);
819 nghttp2_mem_free(mem, item);