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"
36 void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
37 nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
40 nghttp2_put_uint32be(&buf[5], hd->stream_id);
41 /* ignore hd->reserved for now */
44 void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
45 hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
48 hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
52 void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
53 uint8_t flags, int32_t stream_id) {
57 hd->stream_id = stream_id;
61 void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
62 int32_t stream_id, nghttp2_headers_category cat,
63 const nghttp2_priority_spec *pri_spec,
64 nghttp2_nv *nva, size_t nvlen) {
65 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
72 frame->pri_spec = *pri_spec;
74 nghttp2_priority_spec_default_init(&frame->pri_spec);
78 void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
79 nghttp2_nv_array_del(frame->nva, mem);
82 void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
83 const nghttp2_priority_spec *pri_spec) {
84 nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
85 NGHTTP2_FLAG_NONE, stream_id);
86 frame->pri_spec = *pri_spec;
89 void nghttp2_frame_priority_free(nghttp2_priority *frame _U_) {}
91 void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
92 uint32_t error_code) {
93 nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
95 frame->error_code = error_code;
98 void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame _U_) {}
100 void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
101 nghttp2_settings_entry *iv, size_t niv) {
102 nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
103 NGHTTP2_SETTINGS, flags, 0);
108 void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
109 nghttp2_mem_free(mem, frame->iv);
112 void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
114 int32_t promised_stream_id,
115 nghttp2_nv *nva, size_t nvlen) {
116 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
119 frame->nvlen = nvlen;
120 frame->promised_stream_id = promised_stream_id;
124 void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
126 nghttp2_nv_array_del(frame->nva, mem);
129 void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
130 const uint8_t *opaque_data) {
131 nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
133 memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
135 memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
139 void nghttp2_frame_ping_free(nghttp2_ping *frame _U_) {}
141 void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
142 uint32_t error_code, uint8_t *opaque_data,
143 size_t opaque_data_len) {
144 nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
145 NGHTTP2_FLAG_NONE, 0);
146 frame->last_stream_id = last_stream_id;
147 frame->error_code = error_code;
148 frame->opaque_data = opaque_data;
149 frame->opaque_data_len = opaque_data_len;
153 void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
154 nghttp2_mem_free(mem, frame->opaque_data);
157 void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
158 uint8_t flags, int32_t stream_id,
159 int32_t window_size_increment) {
160 nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
161 frame->window_size_increment = window_size_increment;
165 void nghttp2_frame_window_update_free(nghttp2_window_update *frame _U_) {}
167 size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
168 return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
171 void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
173 /* At this moment, the length of DATA frame is unknown */
174 nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
178 void nghttp2_frame_data_free(nghttp2_data *frame _U_) {}
180 size_t nghttp2_frame_priority_len(uint8_t flags) {
181 if (flags & NGHTTP2_FLAG_PRIORITY) {
182 return NGHTTP2_PRIORITY_SPECLEN;
188 size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
189 return nghttp2_frame_priority_len(frame->hd.flags);
193 * Call this function after payload was serialized, but not before
194 * changing buf->pos and serializing frame header.
196 * This function assumes bufs->cur points to the last buf chain of the
199 * This function serializes frame header for HEADERS/PUSH_PROMISE and
200 * handles their successive CONTINUATION frames.
202 * We don't process any padding here.
204 static int frame_pack_headers_shared(nghttp2_bufs *bufs,
205 nghttp2_frame_hd *frame_hd) {
207 nghttp2_buf_chain *ci, *ce;
210 buf = &bufs->head->buf;
213 hd.length = nghttp2_buf_len(buf);
215 DEBUGF(fprintf(stderr, "send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n",
218 /* We have multiple frame buffers, which means one or more
219 CONTINUATION frame is involved. Remove END_HEADERS flag from the
221 if (bufs->head != bufs->cur) {
222 hd.flags &= ~NGHTTP2_FLAG_END_HEADERS;
225 buf->pos -= NGHTTP2_FRAME_HDLEN;
226 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
228 if (bufs->head != bufs->cur) {
229 /* 2nd and later frames are CONTINUATION frames. */
230 hd.type = NGHTTP2_CONTINUATION;
231 /* We don't have no flags except for last CONTINUATION */
232 hd.flags = NGHTTP2_FLAG_NONE;
236 for (ci = bufs->head->next; ci != ce; ci = ci->next) {
239 hd.length = nghttp2_buf_len(buf);
241 DEBUGF(fprintf(stderr, "send: int CONTINUATION, payloadlen=%zu\n",
244 buf->pos -= NGHTTP2_FRAME_HDLEN;
245 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
249 hd.length = nghttp2_buf_len(buf);
250 /* Set END_HEADERS flag for last CONTINUATION */
251 hd.flags = NGHTTP2_FLAG_END_HEADERS;
253 DEBUGF(fprintf(stderr, "send: last CONTINUATION, payloadlen=%zu\n",
256 buf->pos -= NGHTTP2_FRAME_HDLEN;
257 nghttp2_frame_pack_frame_hd(buf->pos, &hd);
263 int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
264 nghttp2_hd_deflater *deflater) {
269 assert(bufs->head == bufs->cur);
271 nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
273 buf = &bufs->cur->buf;
275 buf->pos += nv_offset;
276 buf->last = buf->pos;
278 /* This call will adjust buf->last to the correct position */
279 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
281 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
282 rv = NGHTTP2_ERR_HEADER_COMP;
285 buf->pos -= nv_offset;
291 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
292 nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
296 frame->hd.length = nghttp2_bufs_len(bufs);
298 return frame_pack_headers_shared(bufs, &frame->hd);
301 void nghttp2_frame_pack_priority_spec(uint8_t *buf,
302 const nghttp2_priority_spec *pri_spec) {
303 nghttp2_put_uint32be(buf, pri_spec->stream_id);
304 if (pri_spec->exclusive) {
307 buf[4] = pri_spec->weight - 1;
310 void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
312 const uint8_t *payload,
313 size_t payloadlen _U_) {
314 int32_t dep_stream_id;
318 dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
319 exclusive = (payload[0] & 0x80) > 0;
320 weight = payload[4] + 1;
322 nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
325 int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
326 const uint8_t *payload,
328 if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
329 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags,
330 payload, payloadlen);
332 nghttp2_priority_spec_default_init(&frame->pri_spec);
341 int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
344 assert(bufs->head == bufs->cur);
346 buf = &bufs->head->buf;
348 assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
350 buf->pos -= NGHTTP2_FRAME_HDLEN;
352 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
354 nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
356 buf->last += NGHTTP2_PRIORITY_SPECLEN;
361 void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
362 const uint8_t *payload,
364 nghttp2_frame_unpack_priority_spec(&frame->pri_spec, frame->hd.flags, payload,
368 int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
369 nghttp2_rst_stream *frame) {
372 assert(bufs->head == bufs->cur);
374 buf = &bufs->head->buf;
376 assert(nghttp2_buf_avail(buf) >= 4);
378 buf->pos -= NGHTTP2_FRAME_HDLEN;
380 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
382 nghttp2_put_uint32be(buf->last, frame->error_code);
388 void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
389 const uint8_t *payload,
390 size_t payloadlen _U_) {
391 frame->error_code = nghttp2_get_uint32(payload);
394 int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
397 assert(bufs->head == bufs->cur);
399 buf = &bufs->head->buf;
401 if (nghttp2_buf_avail(buf) < (ssize_t)frame->hd.length) {
402 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
405 buf->pos -= NGHTTP2_FRAME_HDLEN;
407 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
410 nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
415 size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
416 const nghttp2_settings_entry *iv,
419 for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
420 nghttp2_put_uint16be(buf, iv[i].settings_id);
421 nghttp2_put_uint32be(buf + 2, iv[i].value);
423 return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
426 int nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
427 nghttp2_settings_entry *iv,
428 size_t niv, nghttp2_mem *mem) {
429 size_t payloadlen = niv * sizeof(nghttp2_settings_entry);
434 frame->iv = nghttp2_mem_malloc(mem, payloadlen);
436 if (frame->iv == NULL) {
437 return NGHTTP2_ERR_NOMEM;
440 memcpy(frame->iv, iv, payloadlen);
447 void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
448 const uint8_t *payload) {
449 iv->settings_id = nghttp2_get_uint16(&payload[0]);
450 iv->value = nghttp2_get_uint32(&payload[2]);
453 int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
455 const uint8_t *payload,
460 *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
469 nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
471 if (*iv_ptr == NULL) {
472 return NGHTTP2_ERR_NOMEM;
475 for (i = 0; i < *niv_ptr; ++i) {
476 size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
477 nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
483 int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
484 nghttp2_push_promise *frame,
485 nghttp2_hd_deflater *deflater) {
486 size_t nv_offset = 4;
490 assert(bufs->head == bufs->cur);
492 buf = &bufs->cur->buf;
494 buf->pos += nv_offset;
495 buf->last = buf->pos;
497 /* This call will adjust buf->last to the correct position */
498 rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
500 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
501 rv = NGHTTP2_ERR_HEADER_COMP;
504 buf->pos -= nv_offset;
510 nghttp2_put_uint32be(buf->pos, frame->promised_stream_id);
513 frame->hd.length = nghttp2_bufs_len(bufs);
515 return frame_pack_headers_shared(bufs, &frame->hd);
518 int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
519 const uint8_t *payload,
520 size_t payloadlen _U_) {
521 frame->promised_stream_id =
522 nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
528 int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
531 assert(bufs->head == bufs->cur);
533 buf = &bufs->head->buf;
535 assert(nghttp2_buf_avail(buf) >= 8);
537 buf->pos -= NGHTTP2_FRAME_HDLEN;
539 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
542 nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
547 void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
548 const uint8_t *payload,
549 size_t payloadlen _U_) {
550 memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
553 int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
557 assert(bufs->head == bufs->cur);
559 buf = &bufs->head->buf;
561 buf->pos -= NGHTTP2_FRAME_HDLEN;
563 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
565 nghttp2_put_uint32be(buf->last, frame->last_stream_id);
568 nghttp2_put_uint32be(buf->last, frame->error_code);
571 rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
573 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
574 return NGHTTP2_ERR_FRAME_SIZE_ERROR;
584 void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
585 const uint8_t *payload,
586 size_t payloadlen _U_,
587 uint8_t *var_gift_payload,
588 size_t var_gift_payloadlen) {
589 frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
590 frame->error_code = nghttp2_get_uint32(payload + 4);
592 frame->opaque_data = var_gift_payload;
593 frame->opaque_data_len = var_gift_payloadlen;
596 int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
597 const uint8_t *payload,
598 size_t payloadlen, nghttp2_mem *mem) {
599 uint8_t *var_gift_payload;
600 size_t var_gift_payloadlen;
602 if (payloadlen > 8) {
603 var_gift_payloadlen = payloadlen - 8;
605 var_gift_payloadlen = 0;
608 payloadlen -= var_gift_payloadlen;
610 if (!var_gift_payloadlen) {
611 var_gift_payload = NULL;
613 var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
615 if (var_gift_payload == NULL) {
616 return NGHTTP2_ERR_NOMEM;
619 memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
622 nghttp2_frame_unpack_goaway_payload(frame, payload, payloadlen,
623 var_gift_payload, var_gift_payloadlen);
628 int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
629 nghttp2_window_update *frame) {
632 assert(bufs->head == bufs->cur);
634 buf = &bufs->head->buf;
636 assert(nghttp2_buf_avail(buf) >= 4);
638 buf->pos -= NGHTTP2_FRAME_HDLEN;
640 nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
642 nghttp2_put_uint32be(buf->last, frame->window_size_increment);
648 void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
649 const uint8_t *payload,
650 size_t payloadlen _U_) {
651 frame->window_size_increment =
652 nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
655 nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
656 size_t niv, nghttp2_mem *mem) {
657 nghttp2_settings_entry *iv_copy;
658 size_t len = niv * sizeof(nghttp2_settings_entry);
664 iv_copy = nghttp2_mem_malloc(mem, len);
666 if (iv_copy == NULL) {
670 memcpy(iv_copy, iv, len);
675 int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
676 return a->namelen == b->namelen && a->valuelen == b->valuelen &&
677 memcmp(a->name, b->name, a->namelen) == 0 &&
678 memcmp(a->value, b->value, a->valuelen) == 0;
681 void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
682 nghttp2_mem_free(mem, nva);
685 static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
690 return memcmp(a, b, alen);
694 rv = memcmp(a, b, alen);
703 rv = memcmp(a, b, blen);
712 int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
713 return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
716 static int nv_compar(const void *lhs, const void *rhs) {
717 const nghttp2_nv *a = (const nghttp2_nv *)lhs;
718 const nghttp2_nv *b = (const nghttp2_nv *)rhs;
721 rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
724 return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
730 void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
731 qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
734 int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
735 size_t nvlen, nghttp2_mem *mem) {
741 for (i = 0; i < nvlen; ++i) {
742 /* + 2 for null-termination */
743 buflen += nva[i].namelen + nva[i].valuelen + 2;
752 buflen += sizeof(nghttp2_nv) * nvlen;
754 *nva_ptr = nghttp2_mem_malloc(mem, buflen);
756 if (*nva_ptr == NULL) {
757 return NGHTTP2_ERR_NOMEM;
761 data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
763 for (i = 0; i < nvlen; ++i) {
764 p->flags = nva[i].flags;
766 memcpy(data, nva[i].name, nva[i].namelen);
768 p->namelen = nva[i].namelen;
769 data[p->namelen] = '\0';
770 nghttp2_downcase(p->name, p->namelen);
771 data += nva[i].namelen + 1;
772 memcpy(data, nva[i].value, nva[i].valuelen);
774 p->valuelen = nva[i].valuelen;
775 data[p->valuelen] = '\0';
776 data += nva[i].valuelen + 1;
782 int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
784 for (i = 0; i < niv; ++i) {
785 switch (iv[i].settings_id) {
786 case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
787 if (iv[i].value > NGHTTP2_MAX_HEADER_TABLE_SIZE) {
791 case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
793 case NGHTTP2_SETTINGS_ENABLE_PUSH:
794 if (iv[i].value != 0 && iv[i].value != 1) {
798 case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
799 if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
803 case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
804 if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
805 iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
809 case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
816 static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
820 DEBUGF(fprintf(stderr, "send: padlen=%zu, shift left 1 bytes\n", padlen));
822 memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
826 buf->pos[4] |= NGHTTP2_FLAG_PADDED;
828 newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
829 nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
835 trail_padlen = padlen - 1;
836 buf->pos[NGHTTP2_FRAME_HDLEN] = trail_padlen;
838 /* zero out padding */
839 memset(buf->last, 0, trail_padlen);
840 /* extend buffers trail_padlen bytes, since we ate previous padlen -
841 trail_padlen byte(s) */
842 buf->last += trail_padlen;
845 int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
846 size_t padlen, int framehd_only) {
850 DEBUGF(fprintf(stderr, "send: padlen = 0, nothing to do\n"));
856 * We have arranged bufs like this:
859 * 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
860 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861 * | |Frame header | Frame payload... :
862 * +-+-----------------+-------------------------------------------+
863 * | |Frame header | Frame payload... :
864 * +-+-----------------+-------------------------------------------+
865 * | |Frame header | Frame payload... :
866 * +-+-----------------+-------------------------------------------+
868 * We arranged padding so that it is included in the first frame
869 * completely. For padded frame, we are going to adjust buf->pos of
870 * frame which includes padding and serialize (memmove) frame header
871 * in the correct position. Also extends buf->last to include
875 buf = &bufs->head->buf;
877 assert(nghttp2_buf_avail(buf) >= (ssize_t)padlen - 1);
879 frame_set_pad(buf, padlen, framehd_only);
881 hd->length += padlen;
882 hd->flags |= NGHTTP2_FLAG_PADDED;
884 DEBUGF(fprintf(stderr, "send: final payloadlen=%zu, padlen=%zu\n", hd->length,