2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 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_frame.h"
32 #include "nghttp2_helper.h"
33 #include "nghttp2_net.h"
34 #include "nghttp2_priority_spec.h"
35 #include "nghttp2_debug.h"
37 void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
38 nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
41 nghttp2_put_uint32be(&buf[5], (uint32_t)hd->stream_id);
42 /* ignore hd->reserved for now */
45 void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
46 hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
49 hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
53 void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
54 uint8_t flags, int32_t stream_id) {
58 hd->stream_id = stream_id;
62 void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
63 int32_t stream_id, nghttp2_headers_category cat,
64 const nghttp2_priority_spec *pri_spec,
65 nghttp2_nv *nva, size_t nvlen) {
66 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
73 frame->pri_spec = *pri_spec;
75 nghttp2_priority_spec_default_init(&frame->pri_spec);
79 void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
80 nghttp2_nv_array_del(frame->nva, mem);
83 void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
84 const nghttp2_priority_spec *pri_spec) {
85 nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
86 NGHTTP2_FLAG_NONE, stream_id);
87 frame->pri_spec = *pri_spec;
90 void nghttp2_frame_priority_free(nghttp2_priority *frame) { (void)frame; }
92 void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
93 uint32_t error_code) {
94 nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
96 frame->error_code = error_code;
99 void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame) { (void)frame; }
101 void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
102 nghttp2_settings_entry *iv, size_t niv) {
103 nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
104 NGHTTP2_SETTINGS, flags, 0);
109 void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
110 nghttp2_mem_free(mem, frame->iv);
113 void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
115 int32_t promised_stream_id,
116 nghttp2_nv *nva, size_t nvlen) {
117 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
120 frame->nvlen = nvlen;
121 frame->promised_stream_id = promised_stream_id;
125 void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
127 nghttp2_nv_array_del(frame->nva, mem);
130 void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
131 const uint8_t *opaque_data) {
132 nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
134 memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
136 memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
140 void nghttp2_frame_ping_free(nghttp2_ping *frame) { (void)frame; }
142 void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
143 uint32_t error_code, uint8_t *opaque_data,
144 size_t opaque_data_len) {
145 nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
146 NGHTTP2_FLAG_NONE, 0);
147 frame->last_stream_id = last_stream_id;
148 frame->error_code = error_code;
149 frame->opaque_data = opaque_data;
150 frame->opaque_data_len = opaque_data_len;
154 void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
155 nghttp2_mem_free(mem, frame->opaque_data);
158 void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
159 uint8_t flags, int32_t stream_id,
160 int32_t window_size_increment) {
161 nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
162 frame->window_size_increment = window_size_increment;
166 void nghttp2_frame_window_update_free(nghttp2_window_update *frame) {
170 size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
171 /* We have iframe->padlen == 0, but iframe->frame.hd.flags may have
172 NGHTTP2_FLAG_PADDED set. This happens when receiving
173 CONTINUATION frame, since we don't reset flags after HEADERS was
178 return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
181 void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
183 /* At this moment, the length of DATA frame is unknown */
184 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
188 void nghttp2_frame_data_free(nghttp2_data *frame) { (void)frame; }
190 void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
191 uint8_t flags, int32_t stream_id,
193 nghttp2_frame_hd_init(&frame->hd, 0, type, flags, stream_id);
194 frame->payload = payload;
197 void nghttp2_frame_extension_free(nghttp2_extension *frame) { (void)frame; }
199 void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
200 uint8_t *origin, size_t origin_len,
201 uint8_t *field_value, size_t field_value_len) {
202 nghttp2_ext_altsvc *altsvc;
204 nghttp2_frame_hd_init(&frame->hd, 2 + origin_len + field_value_len,
205 NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, stream_id);
207 altsvc = frame->payload;
208 altsvc->origin = origin;
209 altsvc->origin_len = origin_len;
210 altsvc->field_value = field_value;
211 altsvc->field_value_len = field_value_len;
214 void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem) {
215 nghttp2_ext_altsvc *altsvc;
217 altsvc = frame->payload;
218 if (altsvc == NULL) {
221 /* We use the same buffer for altsvc->origin and
222 altsvc->field_value. */
223 nghttp2_mem_free(mem, altsvc->origin);
226 void nghttp2_frame_origin_init(nghttp2_extension *frame,
227 nghttp2_origin_entry *ov, size_t nov) {
228 nghttp2_ext_origin *origin;
229 size_t payloadlen = 0;
232 for (i = 0; i < nov; ++i) {
233 payloadlen += 2 + ov[i].origin_len;
236 nghttp2_frame_hd_init(&frame->hd, payloadlen, NGHTTP2_ORIGIN,
237 NGHTTP2_FLAG_NONE, 0);
239 origin = frame->payload;
244 void nghttp2_frame_origin_free(nghttp2_extension *frame, nghttp2_mem *mem) {
245 nghttp2_ext_origin *origin;
247 origin = frame->payload;
248 if (origin == NULL) {
251 /* We use the same buffer for all resources pointed by the field of
252 origin directly or indirectly. */
253 nghttp2_mem_free(mem, origin->ov);
256 size_t nghttp2_frame_priority_len(uint8_t flags) {
257 if (flags & NGHTTP2_FLAG_PRIORITY) {
258 return NGHTTP2_PRIORITY_SPECLEN;
264 size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
265 return nghttp2_frame_priority_len(frame->hd.flags);
269 * Call this function after payload was serialized, but not before
270 * changing buf->pos and serializing frame header.
272 * This function assumes bufs->cur points to the last buf chain of the
275 * This function serializes frame header for HEADERS/PUSH_PROMISE and
276 * handles their successive CONTINUATION frames.
278 * We don't process any padding here.
280 static int frame_pack_headers_shared(nghttp2_bufs *bufs,
281 nghttp2_frame_hd *frame_hd) {
283 nghttp2_buf_chain *ci, *ce;
286 buf = &bufs->head->buf;
289 hd.length = nghttp2_buf_len(buf);
291 DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
293 /* We have multiple frame buffers, which means one or more
294 CONTINUATION frame is involved. Remove END_HEADERS flag from the
296 if (bufs->head != bufs->cur) {
297 hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
300 buf->pos -= NGHTTP2_FRAME_HDLEN;
301 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
303 if (bufs->head != bufs->cur) {
304 /* 2nd and later frames are CONTINUATION frames. */
305 hd.type = NGHTTP2_CONTINUATION;
306 /* We don't have no flags except for last CONTINUATION */
307 hd.flags = NGHTTP2_FLAG_NONE;
311 for (ci = bufs->head->next; ci != ce; ci = ci->next) {
314 hd.length = nghttp2_buf_len(buf);
316 DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
318 buf->pos -= NGHTTP2_FRAME_HDLEN;
319 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
323 hd.length = nghttp2_buf_len(buf);
324 /* Set END_HEADERS flag for last CONTINUATION */
325 hd.flags = NGHTTP2_FLAG_END_HEADERS;
327 DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
329 buf->pos -= NGHTTP2_FRAME_HDLEN;
330 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
336 int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
337 nghttp2_hd_deflater *deflater) {
342 assert(bufs->head == bufs->cur);
344 nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
346 buf = &bufs->cur->buf;
348 buf->pos += nv_offset;
349 buf->last = buf->pos;
351 /* This call will adjust buf->last to the correct position */
352 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
354 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
355 rv = NGHTTP2_ERR_HEADER_COMP;
358 buf->pos -= nv_offset;
364 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
365 nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
369 frame->hd.length = nghttp2_bufs_len(bufs);
371 return frame_pack_headers_shared(bufs, &frame->hd);
374 void nghttp2_frame_pack_priority_spec(uint8_t *buf,
375 const nghttp2_priority_spec *pri_spec) {
376 nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
377 if (pri_spec->exclusive) {
380 buf[4] = (uint8_t)(pri_spec->weight - 1);
383 void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
384 const uint8_t *payload) {
385 int32_t dep_stream_id;
389 dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
390 exclusive = (payload[0] & 0x80) > 0;
391 weight = payload[4] + 1;
393 nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
396 int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
397 const uint8_t *payload) {
398 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
399 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
401 nghttp2_priority_spec_default_init(&frame->pri_spec);
410 int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
413 assert(bufs->head == bufs->cur);
415 buf = &bufs->head->buf;
417 assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
419 buf->pos -= NGHTTP2_FRAME_HDLEN;
421 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
423 nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
425 buf->last += NGHTTP2_PRIORITY_SPECLEN;
430 void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
431 const uint8_t *payload) {
432 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
435 int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
436 nghttp2_rst_stream *frame) {
439 assert(bufs->head == bufs->cur);
441 buf = &bufs->head->buf;
443 assert(nghttp2_buf_avail(buf) >= 4);
445 buf->pos -= NGHTTP2_FRAME_HDLEN;
447 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
449 nghttp2_put_uint32be(buf->last, frame->error_code);
455 void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
456 const uint8_t *payload) {
457 frame->error_code = nghttp2_get_uint32(payload);
460 int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
463 assert(bufs->head == bufs->cur);
465 buf = &bufs->head->buf;
467 if (nghttp2_buf_avail(buf) < frame->hd.length) {
468 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
471 buf->pos -= NGHTTP2_FRAME_HDLEN;
473 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
476 nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
481 size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
482 const nghttp2_settings_entry *iv,
485 for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
486 nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
487 nghttp2_put_uint32be(buf + 2, iv[i].value);
489 return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
492 void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
493 nghttp2_settings_entry *iv,
499 void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
500 const uint8_t *payload) {
501 iv->settings_id = nghttp2_get_uint16(&payload[0]);
502 iv->value = nghttp2_get_uint32(&payload[2]);
505 int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
507 const uint8_t *payload,
512 *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
521 nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
523 if (*iv_ptr == NULL) {
524 return NGHTTP2_ERR_NOMEM;
527 for (i = 0; i < *niv_ptr; ++i) {
528 size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
529 nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
535 int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
536 nghttp2_push_promise *frame,
537 nghttp2_hd_deflater *deflater) {
538 size_t nv_offset = 4;
542 assert(bufs->head == bufs->cur);
544 buf = &bufs->cur->buf;
546 buf->pos += nv_offset;
547 buf->last = buf->pos;
549 /* This call will adjust buf->last to the correct position */
550 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
552 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
553 rv = NGHTTP2_ERR_HEADER_COMP;
556 buf->pos -= nv_offset;
562 nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
565 frame->hd.length = nghttp2_bufs_len(bufs);
567 return frame_pack_headers_shared(bufs, &frame->hd);
570 int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
571 const uint8_t *payload) {
572 frame->promised_stream_id =
573 nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
579 int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
582 assert(bufs->head == bufs->cur);
584 buf = &bufs->head->buf;
586 assert(nghttp2_buf_avail(buf) >= 8);
588 buf->pos -= NGHTTP2_FRAME_HDLEN;
590 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
593 nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
598 void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
599 const uint8_t *payload) {
600 memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
603 int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
607 assert(bufs->head == bufs->cur);
609 buf = &bufs->head->buf;
611 buf->pos -= NGHTTP2_FRAME_HDLEN;
613 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
615 nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
618 nghttp2_put_uint32be(buf->last, frame->error_code);
621 rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
623 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
624 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
634 void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
635 const uint8_t *payload,
636 uint8_t *var_gift_payload,
637 size_t var_gift_payloadlen) {
638 frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
639 frame->error_code = nghttp2_get_uint32(payload + 4);
641 frame->opaque_data = var_gift_payload;
642 frame->opaque_data_len = var_gift_payloadlen;
645 int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
646 const uint8_t *payload,
647 size_t payloadlen, nghttp2_mem *mem) {
648 uint8_t *var_gift_payload;
649 size_t var_gift_payloadlen;
651 if (payloadlen > 8) {
652 var_gift_payloadlen = payloadlen - 8;
654 var_gift_payloadlen = 0;
657 payloadlen -= var_gift_payloadlen;
659 if (!var_gift_payloadlen) {
660 var_gift_payload = NULL;
662 var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
664 if (var_gift_payload == NULL) {
665 return NGHTTP2_ERR_NOMEM;
668 memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
671 nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
672 var_gift_payloadlen);
677 int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
678 nghttp2_window_update *frame) {
681 assert(bufs->head == bufs->cur);
683 buf = &bufs->head->buf;
685 assert(nghttp2_buf_avail(buf) >= 4);
687 buf->pos -= NGHTTP2_FRAME_HDLEN;
689 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
691 nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
697 void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
698 const uint8_t *payload) {
699 frame->window_size_increment =
700 nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
703 int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
706 nghttp2_ext_altsvc *altsvc;
708 /* This is required with --disable-assert. */
711 altsvc = frame->payload;
713 buf = &bufs->head->buf;
715 assert(nghttp2_buf_avail(buf) >=
716 2 + altsvc->origin_len + altsvc->field_value_len);
718 buf->pos -= NGHTTP2_FRAME_HDLEN;
720 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
722 nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
725 rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
729 rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
736 void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
737 size_t origin_len, uint8_t *payload,
739 nghttp2_ext_altsvc *altsvc;
742 altsvc = frame->payload;
749 altsvc->origin_len = origin_len;
751 altsvc->field_value = p;
752 altsvc->field_value_len = (size_t)(payload + payloadlen - p);
755 int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
756 const uint8_t *payload,
757 size_t payloadlen, nghttp2_mem *mem) {
761 if (payloadlen < 2) {
762 return NGHTTP2_FRAME_SIZE_ERROR;
765 origin_len = nghttp2_get_uint16(payload);
767 buf = nghttp2_mem_malloc(mem, payloadlen - 2);
769 return NGHTTP2_ERR_NOMEM;
772 nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
774 nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
779 int nghttp2_frame_pack_origin(nghttp2_bufs *bufs, nghttp2_extension *frame) {
781 nghttp2_ext_origin *origin;
782 nghttp2_origin_entry *orig;
785 origin = frame->payload;
787 buf = &bufs->head->buf;
789 if (nghttp2_buf_avail(buf) < frame->hd.length) {
790 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
793 buf->pos -= NGHTTP2_FRAME_HDLEN;
795 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
797 for (i = 0; i < origin->nov; ++i) {
798 orig = &origin->ov[i];
799 nghttp2_put_uint16be(buf->last, (uint16_t)orig->origin_len);
801 buf->last = nghttp2_cpymem(buf->last, orig->origin, orig->origin_len);
804 assert(nghttp2_buf_len(buf) == NGHTTP2_FRAME_HDLEN + frame->hd.length);
809 int nghttp2_frame_unpack_origin_payload(nghttp2_extension *frame,
810 const uint8_t *payload,
811 size_t payloadlen, nghttp2_mem *mem) {
812 nghttp2_ext_origin *origin;
813 const uint8_t *p, *end;
816 nghttp2_origin_entry *ov;
820 origin = frame->payload;
822 end = p + payloadlen;
826 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
828 originlen = nghttp2_get_uint16(p);
830 if (originlen == 0) {
833 if (originlen > (size_t)(end - p)) {
834 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
837 /* 1 for terminal NULL */
838 len += originlen + 1;
849 len += nov * sizeof(nghttp2_origin_entry);
851 ov = nghttp2_mem_malloc(mem, len);
853 return NGHTTP2_ERR_NOMEM;
859 dst = (uint8_t *)ov + nov * sizeof(nghttp2_origin_entry);
863 originlen = nghttp2_get_uint16(p);
865 if (originlen == 0) {
869 ov->origin_len = originlen;
870 dst = nghttp2_cpymem(dst, p, originlen);
879 nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
880 size_t niv, nghttp2_mem *mem) {
881 nghttp2_settings_entry *iv_copy;
882 size_t len = niv * sizeof(nghttp2_settings_entry);
888 iv_copy = nghttp2_mem_malloc(mem, len);
890 if (iv_copy == NULL) {
894 memcpy(iv_copy, iv, len);
899 int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
900 return a->namelen == b->namelen && a->valuelen == b->valuelen &&
901 memcmp(a->name, b->name, a->namelen) == 0 &&
902 memcmp(a->value, b->value, a->valuelen) == 0;
905 void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
906 nghttp2_mem_free(mem, nva);
909 static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
914 return memcmp(a, b, alen);
918 rv = memcmp(a, b, alen);
927 rv = memcmp(a, b, blen);
936 int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
937 return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
940 static int nv_compar(const void *lhs, const void *rhs) {
941 const nghttp2_nv *a = (const nghttp2_nv *)lhs;
942 const nghttp2_nv *b = (const nghttp2_nv *)rhs;
945 rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
948 return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
954 void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
955 qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
958 int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
959 size_t nvlen, nghttp2_mem *mem) {
961 uint8_t *data = NULL;
971 for (i = 0; i < nvlen; ++i) {
972 /* + 1 for null-termination */
973 if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
974 buflen += nva[i].namelen + 1;
976 if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
977 buflen += nva[i].valuelen + 1;
981 buflen += sizeof(nghttp2_nv) * nvlen;
983 *nva_ptr = nghttp2_mem_malloc(mem, buflen);
985 if (*nva_ptr == NULL) {
986 return NGHTTP2_ERR_NOMEM;
990 data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
992 for (i = 0; i < nvlen; ++i) {
993 p->flags = nva[i].flags;
995 if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
996 p->name = nva[i].name;
997 p->namelen = nva[i].namelen;
999 if (nva[i].namelen) {
1000 memcpy(data, nva[i].name, nva[i].namelen);
1003 p->namelen = nva[i].namelen;
1004 data[p->namelen] = '\0';
1005 nghttp2_downcase(p->name, p->namelen);
1006 data += nva[i].namelen + 1;
1009 if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
1010 p->value = nva[i].value;
1011 p->valuelen = nva[i].valuelen;
1013 if (nva[i].valuelen) {
1014 memcpy(data, nva[i].value, nva[i].valuelen);
1017 p->valuelen = nva[i].valuelen;
1018 data[p->valuelen] = '\0';
1019 data += nva[i].valuelen + 1;
1027 int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
1029 for (i = 0; i < niv; ++i) {
1030 switch (iv[i].settings_id) {
1031 case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
1033 case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
1035 case NGHTTP2_SETTINGS_ENABLE_PUSH:
1036 if (iv[i].value != 0 && iv[i].value != 1) {
1040 case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
1041 if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
1045 case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
1046 if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
1047 iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
1051 case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
1053 case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
1054 if (iv[i].value != 0 && iv[i].value != 1) {
1063 static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
1064 size_t trail_padlen;
1067 DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
1069 memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
1073 buf->pos[4] |= NGHTTP2_FLAG_PADDED;
1075 newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
1076 nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
1082 trail_padlen = padlen - 1;
1083 buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
1085 /* zero out padding */
1086 memset(buf->last, 0, trail_padlen);
1087 /* extend buffers trail_padlen bytes, since we ate previous padlen -
1088 trail_padlen byte(s) */
1089 buf->last += trail_padlen;
1092 int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
1093 size_t padlen, int framehd_only) {
1097 DEBUGF("send: padlen = 0, nothing to do\n");
1103 * We have arranged bufs like this:
1106 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1108 * | |Frame header | Frame payload... :
1109 * +-+-----------------+-------------------------------------------+
1110 * | |Frame header | Frame payload... :
1111 * +-+-----------------+-------------------------------------------+
1112 * | |Frame header | Frame payload... :
1113 * +-+-----------------+-------------------------------------------+
1115 * We arranged padding so that it is included in the first frame
1116 * completely. For padded frame, we are going to adjust buf->pos of
1117 * frame which includes padding and serialize (memmove) frame header
1118 * in the correct position. Also extends buf->last to include
1122 buf = &bufs->head->buf;
1124 assert(nghttp2_buf_avail(buf) >= padlen - 1);
1126 frame_set_pad(buf, padlen, framehd_only);
1128 hd->length += padlen;
1129 hd->flags |= NGHTTP2_FLAG_PADDED;
1131 DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);