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_hd.h"
31 #include "nghttp2_helper.h"
32 #include "nghttp2_int.h"
34 /* Make scalar initialization form of nghttp2_hd_entry */
35 #define MAKE_STATIC_ENT(N, V, T) \
37 { (uint8_t *)(N), (uint8_t *)(V), sizeof((N)) - 1, sizeof((V)) - 1, 0 } \
38 , (T), 1, NGHTTP2_HD_FLAG_NONE \
41 /* Generated by mkstatictbl.py */
42 /* 3rd parameter is nghttp2_token value for header field name. We use
43 first enum value if same header names are repeated (e.g.,
45 static nghttp2_hd_entry static_table[] = {
46 MAKE_STATIC_ENT(":authority", "", 0),
47 MAKE_STATIC_ENT(":method", "GET", 1),
48 MAKE_STATIC_ENT(":method", "POST", 1),
49 MAKE_STATIC_ENT(":path", "/", 3),
50 MAKE_STATIC_ENT(":path", "/index.html", 3),
51 MAKE_STATIC_ENT(":scheme", "http", 5),
52 MAKE_STATIC_ENT(":scheme", "https", 5),
53 MAKE_STATIC_ENT(":status", "200", 7),
54 MAKE_STATIC_ENT(":status", "204", 7),
55 MAKE_STATIC_ENT(":status", "206", 7),
56 MAKE_STATIC_ENT(":status", "304", 7),
57 MAKE_STATIC_ENT(":status", "400", 7),
58 MAKE_STATIC_ENT(":status", "404", 7),
59 MAKE_STATIC_ENT(":status", "500", 7),
60 MAKE_STATIC_ENT("accept-charset", "", 14),
61 MAKE_STATIC_ENT("accept-encoding", "gzip, deflate", 15),
62 MAKE_STATIC_ENT("accept-language", "", 16),
63 MAKE_STATIC_ENT("accept-ranges", "", 17),
64 MAKE_STATIC_ENT("accept", "", 18),
65 MAKE_STATIC_ENT("access-control-allow-origin", "", 19),
66 MAKE_STATIC_ENT("age", "", 20),
67 MAKE_STATIC_ENT("allow", "", 21),
68 MAKE_STATIC_ENT("authorization", "", 22),
69 MAKE_STATIC_ENT("cache-control", "", 23),
70 MAKE_STATIC_ENT("content-disposition", "", 24),
71 MAKE_STATIC_ENT("content-encoding", "", 25),
72 MAKE_STATIC_ENT("content-language", "", 26),
73 MAKE_STATIC_ENT("content-length", "", 27),
74 MAKE_STATIC_ENT("content-location", "", 28),
75 MAKE_STATIC_ENT("content-range", "", 29),
76 MAKE_STATIC_ENT("content-type", "", 30),
77 MAKE_STATIC_ENT("cookie", "", 31),
78 MAKE_STATIC_ENT("date", "", 32),
79 MAKE_STATIC_ENT("etag", "", 33),
80 MAKE_STATIC_ENT("expect", "", 34),
81 MAKE_STATIC_ENT("expires", "", 35),
82 MAKE_STATIC_ENT("from", "", 36),
83 MAKE_STATIC_ENT("host", "", 37),
84 MAKE_STATIC_ENT("if-match", "", 38),
85 MAKE_STATIC_ENT("if-modified-since", "", 39),
86 MAKE_STATIC_ENT("if-none-match", "", 40),
87 MAKE_STATIC_ENT("if-range", "", 41),
88 MAKE_STATIC_ENT("if-unmodified-since", "", 42),
89 MAKE_STATIC_ENT("last-modified", "", 43),
90 MAKE_STATIC_ENT("link", "", 44),
91 MAKE_STATIC_ENT("location", "", 45),
92 MAKE_STATIC_ENT("max-forwards", "", 46),
93 MAKE_STATIC_ENT("proxy-authenticate", "", 47),
94 MAKE_STATIC_ENT("proxy-authorization", "", 48),
95 MAKE_STATIC_ENT("range", "", 49),
96 MAKE_STATIC_ENT("referer", "", 50),
97 MAKE_STATIC_ENT("refresh", "", 51),
98 MAKE_STATIC_ENT("retry-after", "", 52),
99 MAKE_STATIC_ENT("server", "", 53),
100 MAKE_STATIC_ENT("set-cookie", "", 54),
101 MAKE_STATIC_ENT("strict-transport-security", "", 55),
102 MAKE_STATIC_ENT("transfer-encoding", "", 56),
103 MAKE_STATIC_ENT("user-agent", "", 57),
104 MAKE_STATIC_ENT("vary", "", 58),
105 MAKE_STATIC_ENT("via", "", 59),
106 MAKE_STATIC_ENT("www-authenticate", "", 60),
109 static int memeq(const void *s1, const void *s2, size_t n) {
110 return memcmp(s1, s2, n) == 0;
114 * This function was generated by genlibtokenlookup.py. Inspired by
115 * h2o header lookup. https://github.com/h2o/h2o
117 static int lookup_token(const uint8_t *name, size_t namelen) {
122 if (lstreq("t", name, 1)) {
123 return NGHTTP2_TOKEN_TE;
131 if (lstreq("vi", name, 2)) {
132 return NGHTTP2_TOKEN_VIA;
136 if (lstreq("ag", name, 2)) {
137 return NGHTTP2_TOKEN_AGE;
145 if (lstreq("dat", name, 3)) {
146 return NGHTTP2_TOKEN_DATE;
150 if (lstreq("eta", name, 3)) {
151 return NGHTTP2_TOKEN_ETAG;
155 if (lstreq("lin", name, 3)) {
156 return NGHTTP2_TOKEN_LINK;
160 if (lstreq("fro", name, 3)) {
161 return NGHTTP2_TOKEN_FROM;
165 if (lstreq("hos", name, 3)) {
166 return NGHTTP2_TOKEN_HOST;
170 if (lstreq("var", name, 3)) {
171 return NGHTTP2_TOKEN_VARY;
179 if (lstreq("rang", name, 4)) {
180 return NGHTTP2_TOKEN_RANGE;
184 if (lstreq(":pat", name, 4)) {
185 return NGHTTP2_TOKEN__PATH;
187 if (lstreq(":pat", name, 4)) {
188 return NGHTTP2_TOKEN__PATH;
192 if (lstreq("allo", name, 4)) {
193 return NGHTTP2_TOKEN_ALLOW;
201 if (lstreq("cooki", name, 5)) {
202 return NGHTTP2_TOKEN_COOKIE;
206 if (lstreq("serve", name, 5)) {
207 return NGHTTP2_TOKEN_SERVER;
211 if (lstreq("accep", name, 5)) {
212 return NGHTTP2_TOKEN_ACCEPT;
214 if (lstreq("expec", name, 5)) {
215 return NGHTTP2_TOKEN_EXPECT;
223 if (lstreq(":metho", name, 6)) {
224 return NGHTTP2_TOKEN__METHOD;
226 if (lstreq(":metho", name, 6)) {
227 return NGHTTP2_TOKEN__METHOD;
231 if (lstreq(":schem", name, 6)) {
232 return NGHTTP2_TOKEN__SCHEME;
234 if (lstreq(":schem", name, 6)) {
235 return NGHTTP2_TOKEN__SCHEME;
237 if (lstreq("upgrad", name, 6)) {
238 return NGHTTP2_TOKEN_UPGRADE;
242 if (lstreq("refres", name, 6)) {
243 return NGHTTP2_TOKEN_REFRESH;
247 if (lstreq("refere", name, 6)) {
248 return NGHTTP2_TOKEN_REFERER;
252 if (lstreq(":statu", name, 6)) {
253 return NGHTTP2_TOKEN__STATUS;
255 if (lstreq(":statu", name, 6)) {
256 return NGHTTP2_TOKEN__STATUS;
258 if (lstreq(":statu", name, 6)) {
259 return NGHTTP2_TOKEN__STATUS;
261 if (lstreq(":statu", name, 6)) {
262 return NGHTTP2_TOKEN__STATUS;
264 if (lstreq(":statu", name, 6)) {
265 return NGHTTP2_TOKEN__STATUS;
267 if (lstreq(":statu", name, 6)) {
268 return NGHTTP2_TOKEN__STATUS;
270 if (lstreq(":statu", name, 6)) {
271 return NGHTTP2_TOKEN__STATUS;
273 if (lstreq("expire", name, 6)) {
274 return NGHTTP2_TOKEN_EXPIRES;
282 if (lstreq("if-rang", name, 7)) {
283 return NGHTTP2_TOKEN_IF_RANGE;
287 if (lstreq("if-matc", name, 7)) {
288 return NGHTTP2_TOKEN_IF_MATCH;
292 if (lstreq("locatio", name, 7)) {
293 return NGHTTP2_TOKEN_LOCATION;
301 if (lstreq("keep-aliv", name, 9)) {
302 return NGHTTP2_TOKEN_KEEP_ALIVE;
304 if (lstreq("set-cooki", name, 9)) {
305 return NGHTTP2_TOKEN_SET_COOKIE;
309 if (lstreq("connectio", name, 9)) {
310 return NGHTTP2_TOKEN_CONNECTION;
314 if (lstreq("user-agen", name, 9)) {
315 return NGHTTP2_TOKEN_USER_AGENT;
319 if (lstreq(":authorit", name, 9)) {
320 return NGHTTP2_TOKEN__AUTHORITY;
328 if (lstreq("retry-afte", name, 10)) {
329 return NGHTTP2_TOKEN_RETRY_AFTER;
337 if (lstreq("content-typ", name, 11)) {
338 return NGHTTP2_TOKEN_CONTENT_TYPE;
342 if (lstreq("max-forward", name, 11)) {
343 return NGHTTP2_TOKEN_MAX_FORWARDS;
351 if (lstreq("last-modifie", name, 12)) {
352 return NGHTTP2_TOKEN_LAST_MODIFIED;
356 if (lstreq("content-rang", name, 12)) {
357 return NGHTTP2_TOKEN_CONTENT_RANGE;
361 if (lstreq("if-none-matc", name, 12)) {
362 return NGHTTP2_TOKEN_IF_NONE_MATCH;
366 if (lstreq("cache-contro", name, 12)) {
367 return NGHTTP2_TOKEN_CACHE_CONTROL;
371 if (lstreq("authorizatio", name, 12)) {
372 return NGHTTP2_TOKEN_AUTHORIZATION;
376 if (lstreq("accept-range", name, 12)) {
377 return NGHTTP2_TOKEN_ACCEPT_RANGES;
385 if (lstreq("content-lengt", name, 13)) {
386 return NGHTTP2_TOKEN_CONTENT_LENGTH;
390 if (lstreq("accept-charse", name, 13)) {
391 return NGHTTP2_TOKEN_ACCEPT_CHARSET;
399 if (lstreq("accept-languag", name, 14)) {
400 return NGHTTP2_TOKEN_ACCEPT_LANGUAGE;
404 if (lstreq("accept-encodin", name, 14)) {
405 return NGHTTP2_TOKEN_ACCEPT_ENCODING;
413 if (lstreq("content-languag", name, 15)) {
414 return NGHTTP2_TOKEN_CONTENT_LANGUAGE;
416 if (lstreq("www-authenticat", name, 15)) {
417 return NGHTTP2_TOKEN_WWW_AUTHENTICATE;
421 if (lstreq("content-encodin", name, 15)) {
422 return NGHTTP2_TOKEN_CONTENT_ENCODING;
426 if (lstreq("content-locatio", name, 15)) {
427 return NGHTTP2_TOKEN_CONTENT_LOCATION;
429 if (lstreq("proxy-connectio", name, 15)) {
430 return NGHTTP2_TOKEN_PROXY_CONNECTION;
438 if (lstreq("if-modified-sinc", name, 16)) {
439 return NGHTTP2_TOKEN_IF_MODIFIED_SINCE;
443 if (lstreq("transfer-encodin", name, 16)) {
444 return NGHTTP2_TOKEN_TRANSFER_ENCODING;
452 if (lstreq("proxy-authenticat", name, 17)) {
453 return NGHTTP2_TOKEN_PROXY_AUTHENTICATE;
461 if (lstreq("if-unmodified-sinc", name, 18)) {
462 return NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE;
466 if (lstreq("content-dispositio", name, 18)) {
467 return NGHTTP2_TOKEN_CONTENT_DISPOSITION;
469 if (lstreq("proxy-authorizatio", name, 18)) {
470 return NGHTTP2_TOKEN_PROXY_AUTHORIZATION;
478 if (lstreq("strict-transport-securit", name, 24)) {
479 return NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY;
487 if (lstreq("access-control-allow-origi", name, 26)) {
488 return NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN;
497 int nghttp2_hd_entry_init(nghttp2_hd_entry *ent, uint8_t flags, uint8_t *name,
498 size_t namelen, uint8_t *value, size_t valuelen,
499 int token, nghttp2_mem *mem) {
502 /* Since nghttp2_hd_entry is used for indexing, ent->nv.flags always
503 NGHTTP2_NV_FLAG_NONE */
504 ent->nv.flags = NGHTTP2_NV_FLAG_NONE;
506 if ((flags & NGHTTP2_HD_FLAG_NAME_ALLOC) &&
507 (flags & NGHTTP2_HD_FLAG_NAME_GIFT) == 0) {
509 flags &= ~NGHTTP2_HD_FLAG_NAME_ALLOC;
510 ent->nv.name = (uint8_t *)"";
512 /* copy including terminating NULL byte */
513 ent->nv.name = nghttp2_memdup(name, namelen + 1, mem);
514 if (ent->nv.name == NULL) {
515 rv = NGHTTP2_ERR_NOMEM;
522 if ((flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) &&
523 (flags & NGHTTP2_HD_FLAG_VALUE_GIFT) == 0) {
525 flags &= ~NGHTTP2_HD_FLAG_VALUE_ALLOC;
526 ent->nv.value = (uint8_t *)"";
528 /* copy including terminating NULL byte */
529 ent->nv.value = nghttp2_memdup(value, valuelen + 1, mem);
530 if (ent->nv.value == NULL) {
531 rv = NGHTTP2_ERR_NOMEM;
536 ent->nv.value = value;
538 ent->nv.namelen = namelen;
539 ent->nv.valuelen = valuelen;
547 if (flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
548 nghttp2_mem_free(mem, ent->nv.name);
554 void nghttp2_hd_entry_free(nghttp2_hd_entry *ent, nghttp2_mem *mem) {
555 assert(ent->ref == 0);
556 if (ent->flags & NGHTTP2_HD_FLAG_NAME_ALLOC) {
557 nghttp2_mem_free(mem, ent->nv.name);
559 if (ent->flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) {
560 nghttp2_mem_free(mem, ent->nv.value);
564 static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
567 for (size = 1; size < bufsize; size <<= 1)
569 ringbuf->buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
570 if (ringbuf->buffer == NULL) {
571 return NGHTTP2_ERR_NOMEM;
573 ringbuf->mask = size - 1;
579 static nghttp2_hd_entry *hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
581 assert(idx < ringbuf->len);
582 return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
585 static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
589 nghttp2_hd_entry **buffer;
591 if (ringbuf->mask + 1 >= bufsize) {
594 for (size = 1; size < bufsize; size <<= 1)
596 buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
597 if (buffer == NULL) {
598 return NGHTTP2_ERR_NOMEM;
600 for (i = 0; i < ringbuf->len; ++i) {
601 buffer[i] = hd_ringbuf_get(ringbuf, i);
603 nghttp2_mem_free(mem, ringbuf->buffer);
604 ringbuf->buffer = buffer;
605 ringbuf->mask = size - 1;
610 static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf, nghttp2_mem *mem) {
612 if (ringbuf == NULL) {
615 for (i = 0; i < ringbuf->len; ++i) {
616 nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
618 nghttp2_hd_entry_free(ent, mem);
619 nghttp2_mem_free(mem, ent);
621 nghttp2_mem_free(mem, ringbuf->buffer);
624 static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
625 nghttp2_hd_entry *ent, nghttp2_mem *mem) {
628 rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem);
634 ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
640 static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) {
641 assert(ringbuf->len > 0);
645 static int hd_context_init(nghttp2_hd_context *context, nghttp2_mem *mem) {
649 context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
650 rv = hd_ringbuf_init(&context->hd_table, context->hd_table_bufsize_max /
651 NGHTTP2_HD_ENTRY_OVERHEAD,
657 context->hd_table_bufsize = 0;
661 static void hd_context_free(nghttp2_hd_context *context) {
662 hd_ringbuf_free(&context->hd_table, context->mem);
665 int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) {
666 return nghttp2_hd_deflate_init2(
667 deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem);
670 int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
671 size_t deflate_hd_table_bufsize_max,
674 rv = hd_context_init(&deflater->ctx, mem);
679 if (deflate_hd_table_bufsize_max < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
680 deflater->notify_table_size_change = 1;
681 deflater->ctx.hd_table_bufsize_max = deflate_hd_table_bufsize_max;
683 deflater->notify_table_size_change = 0;
686 deflater->deflate_hd_table_bufsize_max = deflate_hd_table_bufsize_max;
687 deflater->min_hd_table_bufsize_max = UINT32_MAX;
692 int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem) {
695 rv = hd_context_init(&inflater->ctx, mem);
700 inflater->settings_hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
702 inflater->ent_keep = NULL;
703 inflater->nv_keep = NULL;
705 inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
706 inflater->state = NGHTTP2_HD_STATE_OPCODE;
708 rv = nghttp2_bufs_init3(&inflater->nvbufs, NGHTTP2_HD_MAX_NV / 8, 8, 1, 0,
715 inflater->huffman_encoded = 0;
719 inflater->newnamelen = 0;
720 inflater->index_required = 0;
721 inflater->no_index = 0;
726 hd_context_free(&inflater->ctx);
731 static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) {
734 mem = inflater->ctx.mem;
735 if (inflater->ent_keep) {
736 if (inflater->ent_keep->ref == 0) {
737 nghttp2_hd_entry_free(inflater->ent_keep, mem);
738 nghttp2_mem_free(mem, inflater->ent_keep);
740 inflater->ent_keep = NULL;
743 nghttp2_mem_free(mem, inflater->nv_keep);
744 inflater->nv_keep = NULL;
747 void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater) {
748 hd_context_free(&deflater->ctx);
751 void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater) {
752 hd_inflate_keep_free(inflater);
753 nghttp2_bufs_free(&inflater->nvbufs);
754 hd_context_free(&inflater->ctx);
757 static size_t entry_room(size_t namelen, size_t valuelen) {
758 return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
761 static int emit_indexed_header(nghttp2_nv *nv_out, int *token_out,
762 nghttp2_hd_entry *ent) {
763 DEBUGF(fprintf(stderr, "inflatehd: header emission: %s: %s\n", ent->nv.name,
765 /* ent->ref may be 0. This happens if the encoder emits literal
766 block larger than header table capacity with indexing. */
768 *token_out = ent->token;
772 static int emit_literal_header(nghttp2_nv *nv_out, int *token_out,
774 DEBUGF(fprintf(stderr, "inflatehd: header emission: %s: %s\n", nv->name,
777 *token_out = lookup_token(nv->name, nv->namelen);
781 static size_t count_encoded_length(size_t n, size_t prefix) {
782 size_t k = (1 << prefix) - 1;
792 for (; n >= 128; n >>= 7, ++len)
798 static size_t encode_length(uint8_t *buf, size_t n, size_t prefix) {
799 size_t k = (1 << prefix) - 1;
800 uint8_t *begin = buf;
812 for (; n >= 128; n >>= 7) {
813 *buf++ = (1 << 7) | (n & 0x7f);
818 return (size_t)(buf - begin);
822 * Decodes |prefix| prefixed integer stored from |in|. The |last|
823 * represents the 1 beyond the last of the valid contiguous memory
824 * region from |in|. The decoded integer must be less than or equal
827 * If the |initial| is nonzero, it is used as a initial value, this
828 * function assumes the |in| starts with intermediate data.
830 * An entire integer is decoded successfully, decoded, the |*final| is
833 * This function stores the decoded integer in |*res| if it succeed,
834 * including partial decoding (in this case, number of shift to make
835 * in the next call will be stored in |*shift_ptr|) and returns number
836 * of bytes processed, or returns -1, indicating decoding error.
838 static ssize_t decode_length(uint32_t *res, size_t *shift_ptr, int *final,
839 uint32_t initial, size_t shift, uint8_t *in,
840 uint8_t *last, size_t prefix) {
841 uint32_t k = (1 << prefix) - 1;
842 uint32_t n = initial;
849 if ((*in & k) != k) {
859 return (ssize_t)(in - start);
863 for (; in != last; ++in, shift += 7) {
864 uint32_t add = *in & 0x7f;
866 if ((UINT32_MAX >> shift) < add) {
867 DEBUGF(fprintf(stderr, "inflate: integer overflow on shift\n"));
873 if (UINT32_MAX - add < n) {
874 DEBUGF(fprintf(stderr, "inflate: integer overflow on addition\n"));
880 if ((*in & (1 << 7)) == 0) {
889 return (ssize_t)(in - start);
894 return (ssize_t)(in + 1 - start);
897 static int emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
903 DEBUGF(fprintf(stderr, "deflatehd: emit table_size=%zu\n", table_size));
905 blocklen = count_encoded_length(table_size, 5);
907 if (sizeof(sb) < blocklen) {
908 return NGHTTP2_ERR_HEADER_COMP;
915 encode_length(bufp, table_size, 5);
917 rv = nghttp2_bufs_add(bufs, sb, blocklen);
925 static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx) {
931 blocklen = count_encoded_length(idx + 1, 7);
933 DEBUGF(fprintf(stderr, "deflatehd: emit indexed index=%zu, %zu bytes\n", idx,
936 if (sizeof(sb) < blocklen) {
937 return NGHTTP2_ERR_HEADER_COMP;
942 encode_length(bufp, idx + 1, 7);
944 rv = nghttp2_bufs_add(bufs, sb, blocklen);
952 static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len) {
960 enclen = nghttp2_hd_huff_encode_count(str, len);
968 blocklen = count_encoded_length(enclen, 7);
970 DEBUGF(fprintf(stderr, "deflatehd: emit string str="));
971 DEBUGF(fwrite(str, len, 1, stderr));
972 DEBUGF(fprintf(stderr, ", length=%zu, huffman=%d, encoded_length=%zu\n", len,
975 if (sizeof(sb) < blocklen) {
976 return NGHTTP2_ERR_HEADER_COMP;
980 *bufp = huffman ? 1 << 7 : 0;
981 encode_length(bufp, enclen, 7);
983 rv = nghttp2_bufs_add(bufs, sb, blocklen);
989 rv = nghttp2_hd_huff_encode(bufs, str, len);
991 assert(enclen == len);
992 rv = nghttp2_bufs_add(bufs, str, len);
998 static uint8_t pack_first_byte(int indexing_mode) {
999 switch (indexing_mode) {
1000 case NGHTTP2_HD_WITH_INDEXING:
1002 case NGHTTP2_HD_WITHOUT_INDEXING:
1004 case NGHTTP2_HD_NEVER_INDEXING:
1009 /* This is required to compile with android NDK r10d +
1014 static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
1015 const nghttp2_nv *nv, int indexing_mode) {
1022 if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
1028 DEBUGF(fprintf(stderr, "deflatehd: emit indname index=%zu, valuelen=%zu, "
1029 "indexing_mode=%d\n",
1030 idx, nv->valuelen, indexing_mode));
1032 blocklen = count_encoded_length(idx + 1, prefixlen);
1034 if (sizeof(sb) < blocklen) {
1035 return NGHTTP2_ERR_HEADER_COMP;
1040 *bufp = pack_first_byte(indexing_mode);
1042 encode_length(bufp, idx + 1, prefixlen);
1044 rv = nghttp2_bufs_add(bufs, sb, blocklen);
1049 rv = emit_string(bufs, nv->value, nv->valuelen);
1057 static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
1058 int indexing_mode) {
1061 DEBUGF(fprintf(stderr, "deflatehd: emit newname namelen=%zu, valuelen=%zu, "
1062 "indexing_mode=%d\n",
1063 nv->namelen, nv->valuelen, indexing_mode));
1065 rv = nghttp2_bufs_addb(bufs, pack_first_byte(indexing_mode));
1070 rv = emit_string(bufs, nv->name, nv->namelen);
1075 rv = emit_string(bufs, nv->value, nv->valuelen);
1083 static nghttp2_hd_entry *add_hd_table_incremental(nghttp2_hd_context *context,
1084 const nghttp2_nv *nv,
1086 uint8_t entry_flags) {
1088 nghttp2_hd_entry *new_ent;
1093 room = entry_room(nv->namelen, nv->valuelen);
1095 while (context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
1096 context->hd_table.len > 0) {
1098 size_t idx = context->hd_table.len - 1;
1099 nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
1101 context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
1103 DEBUGF(fprintf(stderr, "hpack: remove item from header table: %s: %s\n",
1104 ent->nv.name, ent->nv.value));
1106 hd_ringbuf_pop_back(&context->hd_table);
1107 if (--ent->ref == 0) {
1108 nghttp2_hd_entry_free(ent, mem);
1109 nghttp2_mem_free(mem, ent);
1113 new_ent = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry));
1114 if (new_ent == NULL) {
1118 rv = nghttp2_hd_entry_init(new_ent, entry_flags, nv->name, nv->namelen,
1119 nv->value, nv->valuelen, token, mem);
1121 nghttp2_mem_free(mem, new_ent);
1125 if (room > context->hd_table_bufsize_max) {
1126 /* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
1127 immediately evicted. */
1130 rv = hd_ringbuf_push_front(&context->hd_table, new_ent, mem);
1135 if ((entry_flags & NGHTTP2_HD_FLAG_NAME_ALLOC) &&
1136 (entry_flags & NGHTTP2_HD_FLAG_NAME_GIFT)) {
1137 /* nv->name are managed by caller. */
1138 new_ent->nv.name = NULL;
1139 new_ent->nv.namelen = 0;
1141 if ((entry_flags & NGHTTP2_HD_FLAG_VALUE_ALLOC) &&
1142 (entry_flags & NGHTTP2_HD_FLAG_VALUE_GIFT)) {
1143 /* nv->value are managed by caller. */
1144 new_ent->nv.value = NULL;
1145 new_ent->nv.valuelen = 0;
1148 nghttp2_hd_entry_free(new_ent, mem);
1149 nghttp2_mem_free(mem, new_ent);
1154 context->hd_table_bufsize += room;
1159 static int name_eq(const nghttp2_nv *a, const nghttp2_nv *b) {
1160 return a->namelen == b->namelen && memeq(a->name, b->name, a->namelen);
1163 static int value_eq(const nghttp2_nv *a, const nghttp2_nv *b) {
1164 return a->valuelen == b->valuelen && memeq(a->value, b->value, a->valuelen);
1169 /* Nonzero if both name and value are matched. */
1170 uint8_t name_value_match;
1173 static search_result search_static_table(const nghttp2_nv *nv, int token,
1174 int indexing_mode) {
1175 search_result res = {token, 0};
1178 if (indexing_mode == NGHTTP2_HD_NEVER_INDEXING) {
1183 i <= NGHTTP2_TOKEN_WWW_AUTHENTICATE && static_table[i].token == token;
1185 if (value_eq(&static_table[i].nv, nv)) {
1187 res.name_value_match = 1;
1194 static search_result search_hd_table(nghttp2_hd_context *context,
1195 const nghttp2_nv *nv, int token,
1196 int indexing_mode) {
1197 search_result res = {-1, 0};
1200 if (token >= 0 && token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
1201 res = search_static_table(nv, token, indexing_mode);
1202 if (res.name_value_match) {
1207 for (i = 0; i < context->hd_table.len; ++i) {
1208 nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, i);
1209 if (ent->token != token || (token == -1 && !name_eq(&ent->nv, nv))) {
1213 if (res.index == -1) {
1214 res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
1217 if (indexing_mode != NGHTTP2_HD_NEVER_INDEXING && value_eq(&ent->nv, nv)) {
1218 res.index = (ssize_t)(i + NGHTTP2_STATIC_TABLE_LENGTH);
1219 res.name_value_match = 1;
1227 static void hd_context_shrink_table_size(nghttp2_hd_context *context) {
1232 while (context->hd_table_bufsize > context->hd_table_bufsize_max &&
1233 context->hd_table.len > 0) {
1234 size_t idx = context->hd_table.len - 1;
1235 nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
1236 context->hd_table_bufsize -= entry_room(ent->nv.namelen, ent->nv.valuelen);
1237 hd_ringbuf_pop_back(&context->hd_table);
1238 if (--ent->ref == 0) {
1239 nghttp2_hd_entry_free(ent, mem);
1240 nghttp2_mem_free(mem, ent);
1245 int nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
1246 size_t settings_hd_table_bufsize_max) {
1247 size_t next_bufsize = nghttp2_min(settings_hd_table_bufsize_max,
1248 deflater->deflate_hd_table_bufsize_max);
1250 deflater->ctx.hd_table_bufsize_max = next_bufsize;
1252 deflater->min_hd_table_bufsize_max =
1253 nghttp2_min(deflater->min_hd_table_bufsize_max, next_bufsize);
1255 deflater->notify_table_size_change = 1;
1257 hd_context_shrink_table_size(&deflater->ctx);
1261 int nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
1262 size_t settings_hd_table_bufsize_max) {
1263 inflater->settings_hd_table_bufsize_max = settings_hd_table_bufsize_max;
1264 inflater->ctx.hd_table_bufsize_max = settings_hd_table_bufsize_max;
1265 hd_context_shrink_table_size(&inflater->ctx);
1269 #define INDEX_RANGE_VALID(context, idx) \
1270 ((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
1272 static size_t get_max_index(nghttp2_hd_context *context) {
1273 return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH - 1;
1276 nghttp2_hd_entry *nghttp2_hd_table_get(nghttp2_hd_context *context,
1278 assert(INDEX_RANGE_VALID(context, idx));
1279 if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
1280 return hd_ringbuf_get(&context->hd_table,
1281 idx - NGHTTP2_STATIC_TABLE_LENGTH);
1283 return &static_table[idx];
1287 static int hd_deflate_decide_indexing(nghttp2_hd_deflater *deflater,
1288 const nghttp2_nv *nv, int token) {
1289 if (token == NGHTTP2_TOKEN__PATH || token == NGHTTP2_TOKEN_AGE ||
1290 token == NGHTTP2_TOKEN_CONTENT_LENGTH || token == NGHTTP2_TOKEN_ETAG ||
1291 token == NGHTTP2_TOKEN_IF_MODIFIED_SINCE ||
1292 token == NGHTTP2_TOKEN_IF_NONE_MATCH || token == NGHTTP2_TOKEN_LOCATION ||
1293 token == NGHTTP2_TOKEN_SET_COOKIE ||
1294 entry_room(nv->namelen, nv->valuelen) >
1295 deflater->ctx.hd_table_bufsize_max * 3 / 4) {
1296 return NGHTTP2_HD_WITHOUT_INDEXING;
1299 return NGHTTP2_HD_WITH_INDEXING;
1302 static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
1303 const nghttp2_nv *nv) {
1311 DEBUGF(fprintf(stderr, "deflatehd: deflating %s: %s\n", nv->name, nv->value));
1313 mem = deflater->ctx.mem;
1315 token = lookup_token(nv->name, nv->namelen);
1317 /* Don't index authorization header field since it may contain low
1318 entropy secret data (e.g., id/password). Also cookie header
1319 field with less than 20 bytes value is also never indexed. This
1320 is the same criteria used in Firefox codebase. */
1322 token == NGHTTP2_TOKEN_AUTHORIZATION ||
1323 (token == NGHTTP2_TOKEN_COOKIE && nv->valuelen < 20) ||
1324 (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX)
1325 ? NGHTTP2_HD_NEVER_INDEXING
1326 : hd_deflate_decide_indexing(deflater, nv, token);
1328 res = search_hd_table(&deflater->ctx, nv, token, indexing_mode);
1332 if (res.name_value_match) {
1334 DEBUGF(fprintf(stderr, "deflatehd: name/value match index=%zd\n", idx));
1336 rv = emit_indexed_block(bufs, idx);
1344 if (res.index != -1) {
1345 DEBUGF(fprintf(stderr, "deflatehd: name match index=%zd\n", res.index));
1348 if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
1349 nghttp2_hd_entry *new_ent;
1350 if (idx != -1 && idx < (ssize_t)NGHTTP2_STATIC_TABLE_LENGTH) {
1351 nghttp2_nv nv_indname;
1353 nv_indname.name = nghttp2_hd_table_get(&deflater->ctx, idx)->nv.name;
1354 new_ent = add_hd_table_incremental(&deflater->ctx, &nv_indname, token,
1355 NGHTTP2_HD_FLAG_VALUE_ALLOC);
1357 new_ent = add_hd_table_incremental(&deflater->ctx, nv, token,
1358 NGHTTP2_HD_FLAG_NAME_ALLOC |
1359 NGHTTP2_HD_FLAG_VALUE_ALLOC);
1362 return NGHTTP2_ERR_HEADER_COMP;
1364 if (new_ent->ref == 0) {
1365 nghttp2_hd_entry_free(new_ent, mem);
1366 nghttp2_mem_free(mem, new_ent);
1370 rv = emit_newname_block(bufs, nv, indexing_mode);
1372 rv = emit_indname_block(bufs, idx, nv, indexing_mode);
1381 int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
1382 nghttp2_bufs *bufs, const nghttp2_nv *nv,
1387 if (deflater->ctx.bad) {
1388 return NGHTTP2_ERR_HEADER_COMP;
1391 if (deflater->notify_table_size_change) {
1392 size_t min_hd_table_bufsize_max;
1394 min_hd_table_bufsize_max = deflater->min_hd_table_bufsize_max;
1396 deflater->notify_table_size_change = 0;
1397 deflater->min_hd_table_bufsize_max = UINT32_MAX;
1399 if (deflater->ctx.hd_table_bufsize_max > min_hd_table_bufsize_max) {
1401 rv = emit_table_size(bufs, min_hd_table_bufsize_max);
1408 rv = emit_table_size(bufs, deflater->ctx.hd_table_bufsize_max);
1415 for (i = 0; i < nvlen; ++i) {
1416 rv = deflate_nv(deflater, bufs, &nv[i]);
1423 fprintf(stderr, "deflatehd: all input name/value pairs were deflated\n"));
1427 DEBUGF(fprintf(stderr, "deflatehd: error return %d\n", rv));
1429 deflater->ctx.bad = 1;
1433 ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf,
1434 size_t buflen, const nghttp2_nv *nv,
1440 mem = deflater->ctx.mem;
1442 rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen, mem);
1448 rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
1450 buflen = nghttp2_bufs_len(&bufs);
1452 nghttp2_bufs_wrap_free(&bufs);
1454 if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
1455 return NGHTTP2_ERR_INSUFF_BUFSIZE;
1462 return (ssize_t)buflen;
1465 size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater _U_,
1466 const nghttp2_nv *nva, size_t nvlen) {
1470 /* Possible Maximum Header Table Size Change. Encoding (1u << 31) -
1471 1 using 4 bit prefix requires 6 bytes. We may emit this at most
1475 /* Use Literal Header Field without indexing - New Name, since it is
1476 most space consuming format. Also we choose the less one between
1477 non-huffman and huffman, so using literal byte count is
1478 sufficient for upper bound.
1480 Encoding (1u << 31) - 1 using 7 bit prefix requires 6 bytes. We
1481 need 2 of this for |nvlen| header fields. */
1484 for (i = 0; i < nvlen; ++i) {
1485 n += nva[i].namelen + nva[i].valuelen;
1491 int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
1492 size_t deflate_hd_table_bufsize_max) {
1493 return nghttp2_hd_deflate_new2(deflater_ptr, deflate_hd_table_bufsize_max,
1497 int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
1498 size_t deflate_hd_table_bufsize_max,
1501 nghttp2_hd_deflater *deflater;
1504 mem = nghttp2_mem_default();
1507 deflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_deflater));
1509 if (deflater == NULL) {
1510 return NGHTTP2_ERR_NOMEM;
1513 rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max, mem);
1516 nghttp2_mem_free(mem, deflater);
1521 *deflater_ptr = deflater;
1526 void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) {
1529 mem = deflater->ctx.mem;
1531 nghttp2_hd_deflate_free(deflater);
1533 nghttp2_mem_free(mem, deflater);
1536 static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater,
1537 const uint8_t *in) {
1538 inflater->huffman_encoded = (*in & (1 << 7)) != 0;
1542 * Decodes the integer from the range [in, last). The result is
1543 * assigned to |inflater->left|. If the |inflater->left| is 0, then
1544 * it performs variable integer decoding from scratch. Otherwise, it
1545 * uses the |inflater->left| as the initial value and continues to
1546 * decode assuming that [in, last) begins with intermediary sequence.
1548 * This function returns the number of bytes read if it succeeds, or
1549 * one of the following negative error codes:
1551 * NGHTTP2_ERR_HEADER_COMP
1552 * Integer decoding failed
1554 static ssize_t hd_inflate_read_len(nghttp2_hd_inflater *inflater, int *rfin,
1555 uint8_t *in, uint8_t *last, size_t prefix,
1562 rv = decode_length(&out, &inflater->shift, rfin, (uint32_t)inflater->left,
1563 inflater->shift, in, last, prefix);
1566 DEBUGF(fprintf(stderr, "inflatehd: integer decoding failed\n"));
1567 return NGHTTP2_ERR_HEADER_COMP;
1572 stderr, "inflatehd: integer exceeded the maximum value %zu\n", maxlen));
1573 return NGHTTP2_ERR_HEADER_COMP;
1576 inflater->left = out;
1578 DEBUGF(fprintf(stderr, "inflatehd: decoded integer is %u\n", out));
1584 * Reads |inflater->left| bytes from the range [in, last) and performs
1585 * huffman decoding against them and pushes the result into the
1588 * This function returns the number of bytes read if it succeeds, or
1589 * one of the following negative error codes:
1593 * NGHTTP2_ERR_HEADER_COMP
1594 * Huffman decoding failed
1595 * NGHTTP2_ERR_BUFFER_ERROR
1596 * Out of buffer space.
1598 static ssize_t hd_inflate_read_huff(nghttp2_hd_inflater *inflater,
1599 nghttp2_bufs *bufs, uint8_t *in,
1603 if ((size_t)(last - in) >= inflater->left) {
1604 last = in + inflater->left;
1607 readlen = nghttp2_hd_huff_decode(&inflater->huff_decode_ctx, bufs, in,
1611 DEBUGF(fprintf(stderr, "inflatehd: huffman decoding failed\n"));
1614 inflater->left -= (size_t)readlen;
1619 * Reads |inflater->left| bytes from the range [in, last) and copies
1620 * them into the |buffer|.
1622 * This function returns the number of bytes read if it succeeds, or
1623 * one of the following negative error codes:
1627 * NGHTTP2_ERR_HEADER_COMP
1628 * Header decompression failed
1629 * NGHTTP2_ERR_BUFFER_ERROR
1630 * Out of buffer space.
1632 static ssize_t hd_inflate_read(nghttp2_hd_inflater *inflater,
1633 nghttp2_bufs *bufs, uint8_t *in, uint8_t *last) {
1635 size_t len = nghttp2_min((size_t)(last - in), inflater->left);
1636 rv = nghttp2_bufs_add(bufs, in, len);
1640 inflater->left -= len;
1641 return (ssize_t)len;
1645 * Finalize indexed header representation reception. If header is
1646 * emitted, |*nv_out| is filled with that value and 0 is returned. If
1647 * no header is emitted, 1 is returned.
1649 * This function returns either 0 or 1 if it succeeds, or one of the
1650 * following negative error codes:
1655 static int hd_inflate_commit_indexed(nghttp2_hd_inflater *inflater,
1656 nghttp2_nv *nv_out, int *token_out) {
1657 nghttp2_hd_entry *ent = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
1659 emit_indexed_header(nv_out, token_out, ent);
1664 static int hd_inflate_remove_bufs(nghttp2_hd_inflater *inflater, nghttp2_nv *nv,
1671 if (inflater->index_required ||
1672 inflater->nvbufs.head != inflater->nvbufs.cur) {
1674 rv = nghttp2_bufs_remove(&inflater->nvbufs, &buf);
1677 return NGHTTP2_ERR_NOMEM;
1680 nghttp2_bufs_reset(&inflater->nvbufs);
1685 /* we don't use this value, so no need to NULL-terminate */
1690 nv->valuelen = buflen - 1;
1693 nv->namelen = inflater->newnamelen;
1695 nv->value = buf + nv->namelen + 1;
1696 nv->valuelen = buflen - nv->namelen - 2;
1702 /* If we are not going to store header in header table and
1703 name/value are in first chunk, we just refer them from nv,
1704 instead of mallocing another memory. */
1706 pbuf = &inflater->nvbufs.head->buf;
1709 /* we don't use this value, so no need to NULL-terminate */
1713 nv->value = pbuf->pos;
1714 nv->valuelen = nghttp2_buf_len(pbuf) - 1;
1716 nv->name = pbuf->pos;
1717 nv->namelen = inflater->newnamelen;
1719 nv->value = pbuf->pos + nv->namelen + 1;
1720 nv->valuelen = nghttp2_buf_len(pbuf) - nv->namelen - 2;
1723 /* Resetting does not change the content of first buffer */
1724 nghttp2_bufs_reset(&inflater->nvbufs);
1729 static int hd_inflate_remove_bufs_with_name(nghttp2_hd_inflater *inflater,
1731 nghttp2_hd_entry *ent_name) {
1739 mem = inflater->ctx.mem;
1741 /* Allocate buffer including name in ent_name, plus terminating
1743 buflen = ent_name->nv.namelen + 1 + nghttp2_bufs_len(&inflater->nvbufs);
1745 buf = nghttp2_mem_malloc(mem, buflen);
1747 return NGHTTP2_ERR_NOMEM;
1750 /* Copy including terminal NULL */
1751 memcpy(buf, ent_name->nv.name, ent_name->nv.namelen + 1);
1755 nghttp2_bufs_remove_copy(&inflater->nvbufs,
1756 buf + ent_name->nv.namelen + 1);
1757 assert(ent_name->nv.namelen + 1 + rv == buflen);
1759 nghttp2_bufs_reset(&inflater->nvbufs);
1762 nv->namelen = ent_name->nv.namelen;
1764 nv->value = buf + nv->namelen + 1;
1765 nv->valuelen = buflen - nv->namelen - 2;
1771 * Finalize literal header representation - new name- reception. If
1772 * header is emitted, |*nv_out| is filled with that value and 0 is
1775 * This function returns 0 if it succeeds, or one of the following
1776 * negative error codes:
1781 static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
1782 nghttp2_nv *nv_out, int *token_out) {
1787 mem = inflater->ctx.mem;
1789 rv = hd_inflate_remove_bufs(inflater, &nv, 0 /* name and value */);
1791 return NGHTTP2_ERR_NOMEM;
1794 if (inflater->no_index) {
1795 nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
1797 nv.flags = NGHTTP2_NV_FLAG_NONE;
1800 if (inflater->index_required) {
1801 nghttp2_hd_entry *new_ent;
1804 /* nv->value points to the middle of the buffer pointed by
1805 nv->name. So we just need to keep track of nv->name for memory
1807 ent_flags = NGHTTP2_HD_FLAG_NAME_ALLOC | NGHTTP2_HD_FLAG_NAME_GIFT;
1809 new_ent = add_hd_table_incremental(
1810 &inflater->ctx, &nv, lookup_token(nv.name, nv.namelen), ent_flags);
1813 emit_indexed_header(nv_out, token_out, new_ent);
1814 inflater->ent_keep = new_ent;
1819 nghttp2_mem_free(mem, nv.name);
1821 return NGHTTP2_ERR_NOMEM;
1824 emit_literal_header(nv_out, token_out, &nv);
1826 if (nv.name != inflater->nvbufs.head->buf.pos) {
1827 inflater->nv_keep = nv.name;
1834 * Finalize literal header representation - indexed name-
1835 * reception. If header is emitted, |*nv_out| is filled with that
1836 * value and 0 is returned.
1838 * This function returns 0 if it succeeds, or one of the following
1839 * negative error codes:
1844 static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
1845 nghttp2_nv *nv_out, int *token_out) {
1848 nghttp2_hd_entry *ent_name;
1851 mem = inflater->ctx.mem;
1853 if (inflater->no_index) {
1854 nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
1856 nv.flags = NGHTTP2_NV_FLAG_NONE;
1859 ent_name = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
1861 if (inflater->index_required) {
1862 nghttp2_hd_entry *new_ent;
1865 if (inflater->index < NGHTTP2_STATIC_TABLE_LENGTH) {
1866 /* We don't copy name in static table */
1867 rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */);
1869 return NGHTTP2_ERR_NOMEM;
1871 nv.name = ent_name->nv.name;
1872 nv.namelen = ent_name->nv.namelen;
1874 ent_flags = NGHTTP2_HD_FLAG_VALUE_ALLOC | NGHTTP2_HD_FLAG_VALUE_GIFT;
1876 rv = hd_inflate_remove_bufs_with_name(inflater, &nv, ent_name);
1878 return NGHTTP2_ERR_NOMEM;
1880 /* nv->name and nv->value are in the same buffer. */
1881 ent_flags = NGHTTP2_HD_FLAG_NAME_ALLOC | NGHTTP2_HD_FLAG_NAME_GIFT;
1884 new_ent = add_hd_table_incremental(&inflater->ctx, &nv, ent_name->token,
1887 /* At this point, ent_name might be deleted. */
1890 emit_indexed_header(nv_out, token_out, new_ent);
1892 inflater->ent_keep = new_ent;
1897 nghttp2_mem_free(mem, nv.value);
1899 return NGHTTP2_ERR_NOMEM;
1902 rv = hd_inflate_remove_bufs(inflater, &nv, 1 /* value only */);
1904 return NGHTTP2_ERR_NOMEM;
1907 nv.name = ent_name->nv.name;
1908 nv.namelen = ent_name->nv.namelen;
1910 emit_literal_header(nv_out, token_out, &nv);
1912 if (nv.value != inflater->nvbufs.head->buf.pos) {
1913 inflater->nv_keep = nv.value;
1919 ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out,
1920 int *inflate_flags, uint8_t *in, size_t inlen,
1924 return nghttp2_hd_inflate_hd2(inflater, nv_out, inflate_flags, &token, in,
1928 ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
1929 nghttp2_nv *nv_out, int *inflate_flags,
1930 int *token_out, uint8_t *in, size_t inlen,
1933 uint8_t *first = in;
1934 uint8_t *last = in + inlen;
1938 if (inflater->ctx.bad) {
1939 return NGHTTP2_ERR_HEADER_COMP;
1942 DEBUGF(fprintf(stderr, "inflatehd: start state=%d\n", inflater->state));
1943 hd_inflate_keep_free(inflater);
1945 *inflate_flags = NGHTTP2_HD_INFLATE_NONE;
1946 for (; in != last || busy;) {
1948 switch (inflater->state) {
1949 case NGHTTP2_HD_STATE_OPCODE:
1950 if ((*in & 0xe0u) == 0x20u) {
1951 DEBUGF(fprintf(stderr, "inflatehd: header table size change\n"));
1952 inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
1953 inflater->state = NGHTTP2_HD_STATE_READ_TABLE_SIZE;
1954 } else if (*in & 0x80u) {
1955 DEBUGF(fprintf(stderr, "inflatehd: indexed repr\n"));
1956 inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
1957 inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
1959 if (*in == 0x40u || *in == 0 || *in == 0x10u) {
1961 fprintf(stderr, "inflatehd: literal header repr - new name\n"));
1962 inflater->opcode = NGHTTP2_HD_OPCODE_NEWNAME;
1963 inflater->state = NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN;
1965 DEBUGF(fprintf(stderr,
1966 "inflatehd: literal header repr - indexed name\n"));
1967 inflater->opcode = NGHTTP2_HD_OPCODE_INDNAME;
1968 inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
1970 inflater->index_required = (*in & 0x40) != 0;
1971 inflater->no_index = (*in & 0xf0u) == 0x10u;
1972 DEBUGF(fprintf(stderr, "inflatehd: indexing required=%d, no_index=%d\n",
1973 inflater->index_required, inflater->no_index));
1974 if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
1979 inflater->shift = 0;
1981 case NGHTTP2_HD_STATE_READ_TABLE_SIZE:
1983 rv = hd_inflate_read_len(inflater, &rfin, in, last, 5,
1984 inflater->settings_hd_table_bufsize_max);
1992 DEBUGF(fprintf(stderr, "inflatehd: table_size=%zu\n", inflater->left));
1993 inflater->ctx.hd_table_bufsize_max = inflater->left;
1994 hd_context_shrink_table_size(&inflater->ctx);
1995 inflater->state = NGHTTP2_HD_STATE_OPCODE;
1997 case NGHTTP2_HD_STATE_READ_INDEX: {
2000 if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
2002 } else if (inflater->index_required) {
2009 rv = hd_inflate_read_len(inflater, &rfin, in, last, prefixlen,
2010 get_max_index(&inflater->ctx) + 1);
2021 if (inflater->left == 0) {
2022 rv = NGHTTP2_ERR_HEADER_COMP;
2026 DEBUGF(fprintf(stderr, "inflatehd: index=%zu\n", inflater->left));
2027 if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
2028 inflater->index = inflater->left;
2031 rv = hd_inflate_commit_indexed(inflater, nv_out, token_out);
2035 inflater->state = NGHTTP2_HD_STATE_OPCODE;
2036 /* If rv == 1, no header was emitted */
2038 *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2039 return (ssize_t)(in - first);
2042 inflater->index = inflater->left;
2045 inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2049 case NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN:
2050 hd_inflate_set_huffman_encoded(inflater, in);
2051 inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN;
2053 inflater->shift = 0;
2054 DEBUGF(fprintf(stderr, "inflatehd: huffman encoded=%d\n",
2055 inflater->huffman_encoded != 0));
2057 case NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN:
2059 rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
2065 DEBUGF(fprintf(stderr,
2066 "inflatehd: integer not fully decoded. current=%zu\n",
2072 if (inflater->huffman_encoded) {
2073 nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
2075 inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF;
2077 inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAME;
2080 case NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF:
2081 rv = hd_inflate_read_huff(inflater, &inflater->nvbufs, in, last);
2088 DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
2090 if (inflater->left) {
2091 DEBUGF(fprintf(stderr, "inflatehd: still %zu bytes to go\n",
2097 inflater->newnamelen = nghttp2_bufs_len(&inflater->nvbufs);
2099 rv = nghttp2_bufs_addb(&inflater->nvbufs, '\0');
2104 inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2107 case NGHTTP2_HD_STATE_NEWNAME_READ_NAME:
2108 rv = hd_inflate_read(inflater, &inflater->nvbufs, in, last);
2115 DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
2116 if (inflater->left) {
2117 DEBUGF(fprintf(stderr, "inflatehd: still %zu bytes to go\n",
2123 inflater->newnamelen = nghttp2_bufs_len(&inflater->nvbufs);
2125 rv = nghttp2_bufs_addb(&inflater->nvbufs, '\0');
2130 inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
2133 case NGHTTP2_HD_STATE_CHECK_VALUELEN:
2134 hd_inflate_set_huffman_encoded(inflater, in);
2135 inflater->state = NGHTTP2_HD_STATE_READ_VALUELEN;
2137 inflater->shift = 0;
2138 DEBUGF(fprintf(stderr, "inflatehd: huffman encoded=%d\n",
2139 inflater->huffman_encoded != 0));
2141 case NGHTTP2_HD_STATE_READ_VALUELEN:
2143 rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
2154 DEBUGF(fprintf(stderr, "inflatehd: valuelen=%zu\n", inflater->left));
2156 if (inflater->huffman_encoded) {
2157 nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
2159 inflater->state = NGHTTP2_HD_STATE_READ_VALUEHUFF;
2161 inflater->state = NGHTTP2_HD_STATE_READ_VALUE;
2167 case NGHTTP2_HD_STATE_READ_VALUEHUFF:
2168 rv = hd_inflate_read_huff(inflater, &inflater->nvbufs, in, last);
2175 DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
2177 if (inflater->left) {
2178 DEBUGF(fprintf(stderr, "inflatehd: still %zu bytes to go\n",
2184 rv = nghttp2_bufs_addb(&inflater->nvbufs, '\0');
2189 if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
2190 rv = hd_inflate_commit_newname(inflater, nv_out, token_out);
2192 rv = hd_inflate_commit_indname(inflater, nv_out, token_out);
2199 inflater->state = NGHTTP2_HD_STATE_OPCODE;
2200 *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2202 return (ssize_t)(in - first);
2203 case NGHTTP2_HD_STATE_READ_VALUE:
2204 rv = hd_inflate_read(inflater, &inflater->nvbufs, in, last);
2206 DEBUGF(fprintf(stderr, "inflatehd: value read failure %zd: %s\n", rv,
2207 nghttp2_strerror((int)rv)));
2213 DEBUGF(fprintf(stderr, "inflatehd: %zd bytes read\n", rv));
2215 if (inflater->left) {
2216 DEBUGF(fprintf(stderr, "inflatehd: still %zu bytes to go\n",
2221 rv = nghttp2_bufs_addb(&inflater->nvbufs, '\0');
2226 if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
2227 rv = hd_inflate_commit_newname(inflater, nv_out, token_out);
2229 rv = hd_inflate_commit_indname(inflater, nv_out, token_out);
2236 inflater->state = NGHTTP2_HD_STATE_OPCODE;
2237 *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
2239 return (ssize_t)(in - first);
2245 DEBUGF(fprintf(stderr, "inflatehd: all input bytes were processed\n"));
2248 DEBUGF(fprintf(stderr, "inflatehd: in_final set\n"));
2250 if (inflater->state != NGHTTP2_HD_STATE_OPCODE) {
2251 DEBUGF(fprintf(stderr, "inflatehd: unacceptable state=%d\n",
2253 rv = NGHTTP2_ERR_HEADER_COMP;
2257 *inflate_flags |= NGHTTP2_HD_INFLATE_FINAL;
2259 return (ssize_t)(in - first);
2262 if (in_final && inflater->state != NGHTTP2_HD_STATE_OPCODE) {
2263 DEBUGF(fprintf(stderr, "inflatehd: input ended prematurely\n"));
2265 rv = NGHTTP2_ERR_HEADER_COMP;
2269 return (ssize_t)(in - first);
2272 DEBUGF(fprintf(stderr, "inflatehd: error return %zd\n", rv));
2274 inflater->ctx.bad = 1;
2278 int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) {
2279 hd_inflate_keep_free(inflater);
2283 int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) {
2284 return nghttp2_hd_inflate_new2(inflater_ptr, NULL);
2287 int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
2290 nghttp2_hd_inflater *inflater;
2293 mem = nghttp2_mem_default();
2296 inflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_inflater));
2298 if (inflater == NULL) {
2299 return NGHTTP2_ERR_NOMEM;
2302 rv = nghttp2_hd_inflate_init(inflater, mem);
2305 nghttp2_mem_free(mem, inflater);
2310 *inflater_ptr = inflater;
2315 void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) {
2318 mem = inflater->ctx.mem;
2319 nghttp2_hd_inflate_free(inflater);
2321 nghttp2_mem_free(mem, inflater);
2324 int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,
2325 nghttp2_nv *nv, int indexing_mode) {
2327 return emit_indname_block(bufs, idx, nv, indexing_mode);
2330 int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
2331 int indexing_mode) {
2332 return emit_newname_block(bufs, nv, indexing_mode);
2335 int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
2336 return emit_table_size(bufs, table_size);
2339 ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *final,
2340 uint32_t initial, size_t shift, uint8_t *in,
2341 uint8_t *last, size_t prefix) {
2342 return decode_length(res, shift_ptr, final, initial, shift, in, last, prefix);