blob: e179f29d58f71ebd6e1729d63a96f30b20825676 [file] [log] [blame]
Willy Tarreau679790b2017-05-30 19:09:44 +02001/*
2 * HPACK decompressor (RFC7541)
3 *
4 * Copyright (C) 2014-2017 Willy Tarreau <willy@haproxy.org>
5 * Copyright (C) 2017 HAProxy Technologies
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
Willy Tarreauc775f832017-12-30 13:17:37 +010033#include <common/standard.h>
Willy Tarreau679790b2017-05-30 19:09:44 +020034#include <common/hpack-dec.h>
35#include <common/hpack-huff.h>
36#include <common/hpack-tbl.h>
Willy Tarreau59a10fb2017-11-21 20:03:02 +010037#include <common/chunk.h>
Willy Tarreauc775f832017-12-30 13:17:37 +010038#include <common/h2.h>
Willy Tarreau679790b2017-05-30 19:09:44 +020039#include <common/ist.h>
40
41#include <types/global.h>
42
Willy Tarreau679790b2017-05-30 19:09:44 +020043
44#if defined(DEBUG_HPACK)
45#define hpack_debug_printf printf
Willy Tarreauc775f832017-12-30 13:17:37 +010046#define hpack_debug_hexdump debug_hexdump
Willy Tarreau679790b2017-05-30 19:09:44 +020047#else
48#define hpack_debug_printf(...) do { } while (0)
Willy Tarreauc775f832017-12-30 13:17:37 +010049#define hpack_debug_hexdump(...) do { } while (0)
Willy Tarreau679790b2017-05-30 19:09:44 +020050#endif
51
52/* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included).
53 * returns the 32-bit value on success after updating raw_in and len_in. Forces
54 * len_in to (uint32_t)-1 on truncated input.
55 */
56static uint32_t get_var_int(const uint8_t **raw_in, uint32_t *len_in, int b)
57{
58 uint32_t ret = 0;
59 int len = *len_in;
60 const uint8_t *raw = *raw_in;
61 uint8_t shift = 0;
62
63 len--;
64 ret = *(raw++) & ((1 << b) - 1);
65 if (ret != (uint32_t)((1 << b) - 1))
66 goto end;
67
68 while (1) {
69 if (!len)
70 goto too_short;
71 if (!(*raw & 128))
72 break;
73 ret += ((uint32_t)(*raw++) & 127) << shift;
74 shift += 7;
75 len--;
76 }
77
78 /* last 7 bits */
79 if (!len)
80 goto too_short;
81 len--;
82 ret += ((uint32_t)(*raw++) & 127) << shift;
83
84 end:
85 *raw_in = raw;
86 *len_in = len;
87 return ret;
88
89 too_short:
90 *len_in = (uint32_t)-1;
91 return 0;
92}
93
Willy Tarreau59a10fb2017-11-21 20:03:02 +010094/* returns the pseudo-header <idx> corresponds to among the following values :
95 * - 0 = unknown, the header's string needs to be used instead
96 * - 1 = ":authority"
97 * - 2 = ":method"
98 * - 3 = ":path"
99 * - 4 = ":scheme"
100 * - 5 = ":status"
Willy Tarreau679790b2017-05-30 19:09:44 +0200101 */
102static inline int hpack_idx_to_phdr(uint32_t idx)
103{
104 if (idx > 14)
105 return 0;
106
107 idx >>= 1;
108 idx <<= 2;
109 return (0x55554321U >> idx) & 0xF;
110}
111
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100112/* If <idx> designates a static header, returns <in>. Otherwise allocates some
113 * room from chunk <store> to duplicate <in> into it and returns the string
114 * allocated there. In case of allocation failure, returns a string whose
115 * pointer is NULL.
Willy Tarreau679790b2017-05-30 19:09:44 +0200116 */
Willy Tarreau7f2a44d2018-09-17 14:07:33 +0200117static inline struct ist hpack_alloc_string(struct buffer *store, uint32_t idx,
Willy Tarreau83061a82018-07-13 11:56:34 +0200118 struct ist in)
Willy Tarreau679790b2017-05-30 19:09:44 +0200119{
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100120 struct ist out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200121
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100122 if (idx < HPACK_SHT_SIZE)
123 return in;
Willy Tarreau679790b2017-05-30 19:09:44 +0200124
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100125 out.len = in.len;
126 out.ptr = chunk_newstr(store);
127 if (unlikely(!out.ptr))
128 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200129
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200130 if (unlikely(store->data + out.len > store->size)) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100131 out.ptr = NULL;
132 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200133 }
134
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200135 store->data += out.len;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100136 memcpy(out.ptr, in.ptr, out.len);
137 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200138}
139
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100140/* decode an HPACK frame starting at <raw> for <len> bytes, using the dynamic
141 * headers table <dht>, produces the output into list <list> of <list_size>
142 * entries max, and uses pre-allocated buffer <tmp> for temporary storage (some
143 * list elements will point to it). Some <list> name entries may be made of a
144 * NULL pointer and a len, in which case they will designate a pseudo header
145 * index according to the values returned by hpack_idx_to_phdr() above. The
146 * number of <list> entries used is returned on success, or <0 on failure, with
147 * the opposite one of the HPACK_ERR_* codes. A last element is always zeroed
148 * and is not counted in the number of returned entries. This way the caller
149 * can use list[].n.len == 0 as a marker for the end of list.
Willy Tarreau679790b2017-05-30 19:09:44 +0200150 */
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100151int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len,
Willy Tarreau83061a82018-07-13 11:56:34 +0200152 struct http_hdr *list, int list_size,
153 struct buffer *tmp)
Willy Tarreau679790b2017-05-30 19:09:44 +0200154{
155 uint32_t idx;
156 uint32_t nlen;
157 uint32_t vlen;
158 uint8_t huff;
Willy Tarreau679790b2017-05-30 19:09:44 +0200159 struct ist name;
160 struct ist value;
Willy Tarreau679790b2017-05-30 19:09:44 +0200161 int must_index;
162 int ret;
Willy Tarreau679790b2017-05-30 19:09:44 +0200163
Willy Tarreauc775f832017-12-30 13:17:37 +0100164 hpack_debug_hexdump(stderr, "[HPACK-DEC] ", (const char *)raw, 0, len);
165
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100166 chunk_reset(tmp);
167 ret = 0;
Willy Tarreau679790b2017-05-30 19:09:44 +0200168 while (len) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100169 int __maybe_unused code = *raw; /* first byte, only for debugging */
Willy Tarreau679790b2017-05-30 19:09:44 +0200170
171 must_index = 0;
172 if (*raw >= 0x80) {
173 /* indexed header field */
174 if (*raw == 0x80) {
175 hpack_debug_printf("unhandled code 0x%02x (raw=%p, len=%d)\n", *raw, raw, len);
176 ret = -HPACK_ERR_UNKNOWN_OPCODE;
177 goto leave;
178 }
179
180 hpack_debug_printf("%02x: p14: indexed header field : ", code);
181
182 idx = get_var_int(&raw, &len, 7);
183 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100184 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200185 ret = -HPACK_ERR_TRUNCATED;
186 goto leave;
187 }
188
Willy Tarreauc775f832017-12-30 13:17:37 +0100189 hpack_debug_printf(" idx=%u ", idx);
190
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100191 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100192 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100193 ret = -HPACK_ERR_TOO_LARGE;
194 goto leave;
195 }
196
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100197 value = hpack_alloc_string(tmp, idx, hpack_idx_to_value(dht, idx));
198 if (!value.ptr) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100199 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100200 ret = -HPACK_ERR_TOO_LARGE;
201 goto leave;
202 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200203
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100204 /* here we don't index so we can always keep the pseudo header number */
205 name = ist2(NULL, hpack_idx_to_phdr(idx));
Willy Tarreau679790b2017-05-30 19:09:44 +0200206
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100207 if (!name.len) {
208 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
209 if (!name.ptr) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100210 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100211 ret = -HPACK_ERR_TOO_LARGE;
212 goto leave;
213 }
214 }
215 /* <name> and <value> are now set and point to stable values */
Willy Tarreau679790b2017-05-30 19:09:44 +0200216 }
217 else if (*raw >= 0x20 && *raw <= 0x3f) {
218 /* max dyn table size change */
Willy Tarreauc775f832017-12-30 13:17:37 +0100219 hpack_debug_printf("%02x: p18: dynamic table size update : ", code);
220
Willy Tarreauc611e662017-12-03 18:09:21 +0100221 if (ret) {
222 /* 7541#4.2.1 : DHT size update must only be at the beginning */
Willy Tarreauc775f832017-12-30 13:17:37 +0100223 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreauc611e662017-12-03 18:09:21 +0100224 ret = -HPACK_ERR_TOO_LARGE;
225 goto leave;
226 }
227
Willy Tarreau679790b2017-05-30 19:09:44 +0200228 idx = get_var_int(&raw, &len, 5);
229 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100230 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200231 ret = -HPACK_ERR_TRUNCATED;
232 goto leave;
233 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100234 hpack_debug_printf(" new len=%u\n", idx);
Willy Tarreau1e7d4442019-01-24 10:47:10 +0100235
236 if (idx > dht->size) {
237 hpack_debug_printf("##ERR@%d##\n", __LINE__);
238 ret = -HPACK_ERR_INVALID_ARGUMENT;
239 goto leave;
240 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200241 continue;
242 }
243 else if (!(*raw & (*raw - 0x10))) {
244 /* 0x00, 0x10, and 0x40 (0x20 and 0x80 were already handled above) */
245
246 /* literal header field without/never/with incremental indexing -- literal name */
247 if (*raw == 0x00)
248 hpack_debug_printf("%02x: p17: literal without indexing : ", code);
249 else if (*raw == 0x10)
250 hpack_debug_printf("%02x: p18: literal never indexed : ", code);
251 else if (*raw == 0x40)
252 hpack_debug_printf("%02x: p16: literal with indexing : ", code);
253
254 if (*raw == 0x40)
255 must_index = 1;
256
257 raw++; len--;
258
259 /* retrieve name */
260 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100261 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200262 ret = -HPACK_ERR_TRUNCATED;
263 goto leave;
264 }
265
266 huff = *raw & 0x80;
267 nlen = get_var_int(&raw, &len, 7);
268 if (len == (uint32_t)-1 || len < nlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100269 hpack_debug_printf("##ERR@%d## (truncated): nlen=%d len=%d\n",
270 __LINE__, (int)nlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200271 ret = -HPACK_ERR_TRUNCATED;
272 goto leave;
273 }
274
275 name = ist2(raw, nlen);
276
277 raw += nlen;
278 len -= nlen;
Willy Tarreau679790b2017-05-30 19:09:44 +0200279
280 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100281 char *ntrash = chunk_newstr(tmp);
282 if (!ntrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100283 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100284 ret = -HPACK_ERR_TOO_LARGE;
285 goto leave;
286 }
287
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200288 nlen = huff_dec((const uint8_t *)name.ptr, name.len, ntrash,
289 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200290 if (nlen == (uint32_t)-1) {
291 hpack_debug_printf("2: can't decode huffman.\n");
292 ret = -HPACK_ERR_HUFFMAN;
293 goto leave;
294 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100295 hpack_debug_printf(" [name huff %d->%d] ", (int)name.len, (int)nlen);
296
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200297 tmp->data += nlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100298 name = ist2(ntrash, nlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200299 }
300
301 /* retrieve value */
302 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100303 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200304 ret = -HPACK_ERR_TRUNCATED;
305 goto leave;
306 }
307
308 huff = *raw & 0x80;
309 vlen = get_var_int(&raw, &len, 7);
310 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100311 hpack_debug_printf("##ERR@%d## : vlen=%d len=%d\n",
312 __LINE__, (int)vlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200313 ret = -HPACK_ERR_TRUNCATED;
314 goto leave;
315 }
316
317 value = ist2(raw, vlen);
318 raw += vlen;
319 len -= vlen;
320
321 if (huff) {
322 char *vtrash = chunk_newstr(tmp);
323 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100324 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100325 ret = -HPACK_ERR_TOO_LARGE;
Willy Tarreau679790b2017-05-30 19:09:44 +0200326 goto leave;
327 }
328
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200329 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
330 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200331 if (vlen == (uint32_t)-1) {
332 hpack_debug_printf("3: can't decode huffman.\n");
333 ret = -HPACK_ERR_HUFFMAN;
334 goto leave;
335 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100336 hpack_debug_printf(" [value huff %d->%d] ", (int)value.len, (int)vlen);
337
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200338 tmp->data += vlen; // make room for the value
Willy Tarreau679790b2017-05-30 19:09:44 +0200339 value = ist2(vtrash, vlen);
340 }
341
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100342 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200343 }
344 else {
345 /* 0x01..0x0f : literal header field without indexing -- indexed name */
346 /* 0x11..0x1f : literal header field never indexed -- indexed name */
347 /* 0x41..0x7f : literal header field with incremental indexing -- indexed name */
348
349 if (*raw <= 0x0f)
350 hpack_debug_printf("%02x: p16: literal without indexing -- indexed name : ", code);
351 else if (*raw >= 0x41)
352 hpack_debug_printf("%02x: p15: literal with indexing -- indexed name : ", code);
353 else
354 hpack_debug_printf("%02x: p16: literal never indexed -- indexed name : ", code);
355
356 /* retrieve name index */
357 if (*raw >= 0x41) {
358 must_index = 1;
359 idx = get_var_int(&raw, &len, 6);
360 }
361 else
362 idx = get_var_int(&raw, &len, 4);
363
Willy Tarreauc775f832017-12-30 13:17:37 +0100364 hpack_debug_printf(" idx=%u ", idx);
365
Willy Tarreau679790b2017-05-30 19:09:44 +0200366 if (len == (uint32_t)-1 || !len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100367 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200368 ret = -HPACK_ERR_TRUNCATED;
369 goto leave;
370 }
371
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100372 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100373 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100374 ret = -HPACK_ERR_TOO_LARGE;
375 goto leave;
376 }
377
Willy Tarreau679790b2017-05-30 19:09:44 +0200378 /* retrieve value */
379 huff = *raw & 0x80;
380 vlen = get_var_int(&raw, &len, 7);
381 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100382 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200383 ret = -HPACK_ERR_TRUNCATED;
384 goto leave;
385 }
386
387 value = ist2(raw, vlen);
388 raw += vlen;
389 len -= vlen;
390
391 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100392 char *vtrash = chunk_newstr(tmp);
393 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100394 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100395 ret = -HPACK_ERR_TOO_LARGE;
396 goto leave;
397 }
398
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200399 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
400 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200401 if (vlen == (uint32_t)-1) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100402 hpack_debug_printf("##ERR@%d## can't decode huffman : ilen=%d osize=%d\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200403 __LINE__, (int)value.len,
404 (int)(tmp->size - tmp->data));
Willy Tarreauc775f832017-12-30 13:17:37 +0100405 hpack_debug_hexdump(stderr, "[HUFFMAN] ", value.ptr, 0, value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200406 ret = -HPACK_ERR_HUFFMAN;
407 goto leave;
408 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 tmp->data += vlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100410 value = ist2(vtrash, vlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200411 }
412
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100413 name = ist2(NULL, 0);
414 if (!must_index)
415 name.len = hpack_idx_to_phdr(idx);
Willy Tarreau679790b2017-05-30 19:09:44 +0200416
Willy Tarreaubb39b492017-12-30 16:56:28 +0100417 if (!name.len) {
418 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
419 if (!name.ptr) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100420 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaubb39b492017-12-30 16:56:28 +0100421 ret = -HPACK_ERR_TOO_LARGE;
422 goto leave;
423 }
424 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100425 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200426 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200427
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100428 /* here's what we have here :
429 * - name.len > 0
430 * - value is filled with either const data or data allocated from tmp
431 * - name.ptr == NULL && !must_index : known pseudo-header #name.len
432 * - name.ptr != NULL || must_index : general header, unknown pseudo-header or index needed
433 */
434 if (ret >= list_size) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100435 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200436 ret = -HPACK_ERR_TOO_LARGE;
437 goto leave;
438 }
439
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100440 list[ret].n = name;
441 list[ret].v = value;
442 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200443
444 if (must_index && hpack_dht_insert(dht, name, value) < 0) {
445 hpack_debug_printf("failed to find some room in the dynamic table\n");
446 ret = -HPACK_ERR_DHT_INSERT_FAIL;
447 goto leave;
448 }
449
Willy Tarreau679790b2017-05-30 19:09:44 +0200450 hpack_debug_printf("\e[1;34m%s\e[0m: ",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200451 name.ptr ? istpad(trash.area, name).ptr : h2_phdr_to_str(name.len));
Willy Tarreau679790b2017-05-30 19:09:44 +0200452
Willy Tarreauc775f832017-12-30 13:17:37 +0100453 hpack_debug_printf("\e[1;35m%s\e[0m [mustidx=%d, used=%d] [n=(%p,%d) v=(%p,%d)]\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200454 istpad(trash.area, value).ptr, must_index,
455 dht->used,
Willy Tarreauc775f832017-12-30 13:17:37 +0100456 name.ptr, (int)name.len, value.ptr, (int)value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200457 }
458
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100459 if (ret >= list_size) {
Willy Tarreau679790b2017-05-30 19:09:44 +0200460 ret = -HPACK_ERR_TOO_LARGE;
461 goto leave;
462 }
463
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100464 /* put an end marker */
465 list[ret].n = list[ret].v = ist2(NULL, 0);
466 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200467
Willy Tarreau679790b2017-05-30 19:09:44 +0200468 leave:
Willy Tarreauc775f832017-12-30 13:17:37 +0100469 hpack_debug_printf("-- done: ret=%d list_size=%d --\n", (int)ret, (int)list_size);
Willy Tarreau679790b2017-05-30 19:09:44 +0200470 return ret;
471}