blob: 4fa9bfd08298de56448be19f67850a9625abeaa2 [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
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010028#include <inttypes.h>
Willy Tarreau679790b2017-05-30 19:09:44 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <import/ist.h>
34#include <haproxy/chunk.h>
35#include <haproxy/h2.h>
Willy Tarreaube327fa2020-06-03 09:09:57 +020036#include <haproxy/hpack-dec.h>
37#include <haproxy/hpack-huff.h>
38#include <haproxy/hpack-tbl.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreau679790b2017-05-30 19:09:44 +020040
Willy Tarreau679790b2017-05-30 19:09:44 +020041
42#if defined(DEBUG_HPACK)
43#define hpack_debug_printf printf
Willy Tarreauc775f832017-12-30 13:17:37 +010044#define hpack_debug_hexdump debug_hexdump
Willy Tarreau679790b2017-05-30 19:09:44 +020045#else
46#define hpack_debug_printf(...) do { } while (0)
Willy Tarreauc775f832017-12-30 13:17:37 +010047#define hpack_debug_hexdump(...) do { } while (0)
Willy Tarreau679790b2017-05-30 19:09:44 +020048#endif
49
50/* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included).
51 * returns the 32-bit value on success after updating raw_in and len_in. Forces
52 * len_in to (uint32_t)-1 on truncated input.
53 */
54static uint32_t get_var_int(const uint8_t **raw_in, uint32_t *len_in, int b)
55{
56 uint32_t ret = 0;
57 int len = *len_in;
58 const uint8_t *raw = *raw_in;
59 uint8_t shift = 0;
60
61 len--;
62 ret = *(raw++) & ((1 << b) - 1);
63 if (ret != (uint32_t)((1 << b) - 1))
64 goto end;
65
Willy Tarreau077d3662020-02-05 15:28:55 +010066 while (len && (*raw & 128)) {
Willy Tarreau679790b2017-05-30 19:09:44 +020067 ret += ((uint32_t)(*raw++) & 127) << shift;
68 shift += 7;
69 len--;
70 }
71
72 /* last 7 bits */
73 if (!len)
74 goto too_short;
75 len--;
76 ret += ((uint32_t)(*raw++) & 127) << shift;
77
78 end:
79 *raw_in = raw;
80 *len_in = len;
81 return ret;
82
83 too_short:
84 *len_in = (uint32_t)-1;
85 return 0;
86}
87
Willy Tarreau59a10fb2017-11-21 20:03:02 +010088/* returns the pseudo-header <idx> corresponds to among the following values :
89 * - 0 = unknown, the header's string needs to be used instead
90 * - 1 = ":authority"
91 * - 2 = ":method"
92 * - 3 = ":path"
93 * - 4 = ":scheme"
94 * - 5 = ":status"
Willy Tarreau679790b2017-05-30 19:09:44 +020095 */
96static inline int hpack_idx_to_phdr(uint32_t idx)
97{
98 if (idx > 14)
99 return 0;
100
101 idx >>= 1;
102 idx <<= 2;
103 return (0x55554321U >> idx) & 0xF;
104}
105
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100106/* If <idx> designates a static header, returns <in>. Otherwise allocates some
107 * room from chunk <store> to duplicate <in> into it and returns the string
108 * allocated there. In case of allocation failure, returns a string whose
109 * pointer is NULL.
Willy Tarreau679790b2017-05-30 19:09:44 +0200110 */
Willy Tarreau7f2a44d2018-09-17 14:07:33 +0200111static inline struct ist hpack_alloc_string(struct buffer *store, uint32_t idx,
Willy Tarreau83061a82018-07-13 11:56:34 +0200112 struct ist in)
Willy Tarreau679790b2017-05-30 19:09:44 +0200113{
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100114 struct ist out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200115
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100116 if (idx < HPACK_SHT_SIZE)
117 return in;
Willy Tarreau679790b2017-05-30 19:09:44 +0200118
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100119 out.len = in.len;
120 out.ptr = chunk_newstr(store);
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100121 if (unlikely(!isttest(out)))
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100122 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200123
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200124 if (unlikely(store->data + out.len > store->size)) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100125 out.ptr = NULL;
126 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200127 }
128
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200129 store->data += out.len;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100130 memcpy(out.ptr, in.ptr, out.len);
131 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200132}
133
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100134/* decode an HPACK frame starting at <raw> for <len> bytes, using the dynamic
135 * headers table <dht>, produces the output into list <list> of <list_size>
136 * entries max, and uses pre-allocated buffer <tmp> for temporary storage (some
137 * list elements will point to it). Some <list> name entries may be made of a
138 * NULL pointer and a len, in which case they will designate a pseudo header
139 * index according to the values returned by hpack_idx_to_phdr() above. The
140 * number of <list> entries used is returned on success, or <0 on failure, with
141 * the opposite one of the HPACK_ERR_* codes. A last element is always zeroed
142 * and is not counted in the number of returned entries. This way the caller
143 * can use list[].n.len == 0 as a marker for the end of list.
Willy Tarreau679790b2017-05-30 19:09:44 +0200144 */
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100145int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len,
Willy Tarreau83061a82018-07-13 11:56:34 +0200146 struct http_hdr *list, int list_size,
147 struct buffer *tmp)
Willy Tarreau679790b2017-05-30 19:09:44 +0200148{
149 uint32_t idx;
150 uint32_t nlen;
151 uint32_t vlen;
152 uint8_t huff;
Willy Tarreau679790b2017-05-30 19:09:44 +0200153 struct ist name;
154 struct ist value;
Willy Tarreau679790b2017-05-30 19:09:44 +0200155 int must_index;
156 int ret;
Willy Tarreau679790b2017-05-30 19:09:44 +0200157
Willy Tarreauc775f832017-12-30 13:17:37 +0100158 hpack_debug_hexdump(stderr, "[HPACK-DEC] ", (const char *)raw, 0, len);
159
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100160 chunk_reset(tmp);
161 ret = 0;
Willy Tarreau679790b2017-05-30 19:09:44 +0200162 while (len) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100163 int __maybe_unused code = *raw; /* first byte, only for debugging */
Willy Tarreau679790b2017-05-30 19:09:44 +0200164
165 must_index = 0;
166 if (*raw >= 0x80) {
167 /* indexed header field */
168 if (*raw == 0x80) {
169 hpack_debug_printf("unhandled code 0x%02x (raw=%p, len=%d)\n", *raw, raw, len);
170 ret = -HPACK_ERR_UNKNOWN_OPCODE;
171 goto leave;
172 }
173
174 hpack_debug_printf("%02x: p14: indexed header field : ", code);
175
176 idx = get_var_int(&raw, &len, 7);
177 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100178 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200179 ret = -HPACK_ERR_TRUNCATED;
180 goto leave;
181 }
182
Willy Tarreauc775f832017-12-30 13:17:37 +0100183 hpack_debug_printf(" idx=%u ", idx);
184
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100185 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100186 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100187 ret = -HPACK_ERR_TOO_LARGE;
188 goto leave;
189 }
190
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100191 value = hpack_alloc_string(tmp, idx, hpack_idx_to_value(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100192 if (!isttest(value)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100193 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100194 ret = -HPACK_ERR_TOO_LARGE;
195 goto leave;
196 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200197
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100198 /* here we don't index so we can always keep the pseudo header number */
199 name = ist2(NULL, hpack_idx_to_phdr(idx));
Willy Tarreau679790b2017-05-30 19:09:44 +0200200
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100201 if (!name.len) {
202 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100203 if (!isttest(name)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100204 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100205 ret = -HPACK_ERR_TOO_LARGE;
206 goto leave;
207 }
208 }
209 /* <name> and <value> are now set and point to stable values */
Willy Tarreau679790b2017-05-30 19:09:44 +0200210 }
211 else if (*raw >= 0x20 && *raw <= 0x3f) {
212 /* max dyn table size change */
Willy Tarreauc775f832017-12-30 13:17:37 +0100213 hpack_debug_printf("%02x: p18: dynamic table size update : ", code);
214
Willy Tarreauc611e662017-12-03 18:09:21 +0100215 if (ret) {
216 /* 7541#4.2.1 : DHT size update must only be at the beginning */
Willy Tarreauc775f832017-12-30 13:17:37 +0100217 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreauc611e662017-12-03 18:09:21 +0100218 ret = -HPACK_ERR_TOO_LARGE;
219 goto leave;
220 }
221
Willy Tarreau679790b2017-05-30 19:09:44 +0200222 idx = get_var_int(&raw, &len, 5);
223 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100224 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200225 ret = -HPACK_ERR_TRUNCATED;
226 goto leave;
227 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100228 hpack_debug_printf(" new len=%u\n", idx);
Willy Tarreau1e7d4442019-01-24 10:47:10 +0100229
230 if (idx > dht->size) {
231 hpack_debug_printf("##ERR@%d##\n", __LINE__);
232 ret = -HPACK_ERR_INVALID_ARGUMENT;
233 goto leave;
234 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200235 continue;
236 }
237 else if (!(*raw & (*raw - 0x10))) {
238 /* 0x00, 0x10, and 0x40 (0x20 and 0x80 were already handled above) */
239
240 /* literal header field without/never/with incremental indexing -- literal name */
241 if (*raw == 0x00)
242 hpack_debug_printf("%02x: p17: literal without indexing : ", code);
243 else if (*raw == 0x10)
244 hpack_debug_printf("%02x: p18: literal never indexed : ", code);
245 else if (*raw == 0x40)
246 hpack_debug_printf("%02x: p16: literal with indexing : ", code);
247
248 if (*raw == 0x40)
249 must_index = 1;
250
251 raw++; len--;
252
253 /* retrieve name */
254 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100255 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200256 ret = -HPACK_ERR_TRUNCATED;
257 goto leave;
258 }
259
260 huff = *raw & 0x80;
261 nlen = get_var_int(&raw, &len, 7);
262 if (len == (uint32_t)-1 || len < nlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100263 hpack_debug_printf("##ERR@%d## (truncated): nlen=%d len=%d\n",
264 __LINE__, (int)nlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200265 ret = -HPACK_ERR_TRUNCATED;
266 goto leave;
267 }
268
269 name = ist2(raw, nlen);
270
271 raw += nlen;
272 len -= nlen;
Willy Tarreau679790b2017-05-30 19:09:44 +0200273
274 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100275 char *ntrash = chunk_newstr(tmp);
276 if (!ntrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100277 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100278 ret = -HPACK_ERR_TOO_LARGE;
279 goto leave;
280 }
281
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200282 nlen = huff_dec((const uint8_t *)name.ptr, name.len, ntrash,
283 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200284 if (nlen == (uint32_t)-1) {
285 hpack_debug_printf("2: can't decode huffman.\n");
286 ret = -HPACK_ERR_HUFFMAN;
287 goto leave;
288 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100289 hpack_debug_printf(" [name huff %d->%d] ", (int)name.len, (int)nlen);
290
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200291 tmp->data += nlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100292 name = ist2(ntrash, nlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200293 }
294
295 /* retrieve value */
296 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100297 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200298 ret = -HPACK_ERR_TRUNCATED;
299 goto leave;
300 }
301
302 huff = *raw & 0x80;
303 vlen = get_var_int(&raw, &len, 7);
304 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100305 hpack_debug_printf("##ERR@%d## : vlen=%d len=%d\n",
306 __LINE__, (int)vlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200307 ret = -HPACK_ERR_TRUNCATED;
308 goto leave;
309 }
310
311 value = ist2(raw, vlen);
312 raw += vlen;
313 len -= vlen;
314
315 if (huff) {
316 char *vtrash = chunk_newstr(tmp);
317 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100318 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100319 ret = -HPACK_ERR_TOO_LARGE;
Willy Tarreau679790b2017-05-30 19:09:44 +0200320 goto leave;
321 }
322
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200323 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
324 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200325 if (vlen == (uint32_t)-1) {
326 hpack_debug_printf("3: can't decode huffman.\n");
327 ret = -HPACK_ERR_HUFFMAN;
328 goto leave;
329 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100330 hpack_debug_printf(" [value huff %d->%d] ", (int)value.len, (int)vlen);
331
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200332 tmp->data += vlen; // make room for the value
Willy Tarreau679790b2017-05-30 19:09:44 +0200333 value = ist2(vtrash, vlen);
334 }
335
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100336 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200337 }
338 else {
339 /* 0x01..0x0f : literal header field without indexing -- indexed name */
340 /* 0x11..0x1f : literal header field never indexed -- indexed name */
341 /* 0x41..0x7f : literal header field with incremental indexing -- indexed name */
342
343 if (*raw <= 0x0f)
344 hpack_debug_printf("%02x: p16: literal without indexing -- indexed name : ", code);
345 else if (*raw >= 0x41)
346 hpack_debug_printf("%02x: p15: literal with indexing -- indexed name : ", code);
347 else
348 hpack_debug_printf("%02x: p16: literal never indexed -- indexed name : ", code);
349
350 /* retrieve name index */
351 if (*raw >= 0x41) {
352 must_index = 1;
353 idx = get_var_int(&raw, &len, 6);
354 }
355 else
356 idx = get_var_int(&raw, &len, 4);
357
Willy Tarreauc775f832017-12-30 13:17:37 +0100358 hpack_debug_printf(" idx=%u ", idx);
359
Willy Tarreau679790b2017-05-30 19:09:44 +0200360 if (len == (uint32_t)-1 || !len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100361 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200362 ret = -HPACK_ERR_TRUNCATED;
363 goto leave;
364 }
365
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100366 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100367 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100368 ret = -HPACK_ERR_TOO_LARGE;
369 goto leave;
370 }
371
Willy Tarreau679790b2017-05-30 19:09:44 +0200372 /* retrieve value */
373 huff = *raw & 0x80;
374 vlen = get_var_int(&raw, &len, 7);
375 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100376 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200377 ret = -HPACK_ERR_TRUNCATED;
378 goto leave;
379 }
380
381 value = ist2(raw, vlen);
382 raw += vlen;
383 len -= vlen;
384
385 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100386 char *vtrash = chunk_newstr(tmp);
387 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100388 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100389 ret = -HPACK_ERR_TOO_LARGE;
390 goto leave;
391 }
392
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200393 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
394 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200395 if (vlen == (uint32_t)-1) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100396 hpack_debug_printf("##ERR@%d## can't decode huffman : ilen=%d osize=%d\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200397 __LINE__, (int)value.len,
398 (int)(tmp->size - tmp->data));
Willy Tarreauc775f832017-12-30 13:17:37 +0100399 hpack_debug_hexdump(stderr, "[HUFFMAN] ", value.ptr, 0, value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200400 ret = -HPACK_ERR_HUFFMAN;
401 goto leave;
402 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200403 tmp->data += vlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100404 value = ist2(vtrash, vlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200405 }
406
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100407 name = IST_NULL;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100408 if (!must_index)
409 name.len = hpack_idx_to_phdr(idx);
Willy Tarreau679790b2017-05-30 19:09:44 +0200410
Willy Tarreaubb39b492017-12-30 16:56:28 +0100411 if (!name.len) {
412 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100413 if (!isttest(name)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100414 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaubb39b492017-12-30 16:56:28 +0100415 ret = -HPACK_ERR_TOO_LARGE;
416 goto leave;
417 }
418 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100419 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200420 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200421
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100422 /* here's what we have here :
423 * - name.len > 0
424 * - value is filled with either const data or data allocated from tmp
425 * - name.ptr == NULL && !must_index : known pseudo-header #name.len
426 * - name.ptr != NULL || must_index : general header, unknown pseudo-header or index needed
427 */
428 if (ret >= list_size) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100429 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200430 ret = -HPACK_ERR_TOO_LARGE;
431 goto leave;
432 }
433
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100434 list[ret].n = name;
435 list[ret].v = value;
436 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200437
438 if (must_index && hpack_dht_insert(dht, name, value) < 0) {
439 hpack_debug_printf("failed to find some room in the dynamic table\n");
440 ret = -HPACK_ERR_DHT_INSERT_FAIL;
441 goto leave;
442 }
443
Willy Tarreau679790b2017-05-30 19:09:44 +0200444 hpack_debug_printf("\e[1;34m%s\e[0m: ",
Tim Duesterhused526372020-03-05 17:56:33 +0100445 isttest(name) ? istpad(trash.area, name).ptr : h2_phdr_to_str(name.len));
Willy Tarreau679790b2017-05-30 19:09:44 +0200446
Willy Tarreauc775f832017-12-30 13:17:37 +0100447 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 +0200448 istpad(trash.area, value).ptr, must_index,
449 dht->used,
Willy Tarreauc775f832017-12-30 13:17:37 +0100450 name.ptr, (int)name.len, value.ptr, (int)value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200451 }
452
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100453 if (ret >= list_size) {
Willy Tarreau679790b2017-05-30 19:09:44 +0200454 ret = -HPACK_ERR_TOO_LARGE;
455 goto leave;
456 }
457
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100458 /* put an end marker */
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100459 list[ret].n = list[ret].v = IST_NULL;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100460 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200461
Willy Tarreau679790b2017-05-30 19:09:44 +0200462 leave:
Willy Tarreauc775f832017-12-30 13:17:37 +0100463 hpack_debug_printf("-- done: ret=%d list_size=%d --\n", (int)ret, (int)list_size);
Willy Tarreau679790b2017-05-30 19:09:44 +0200464 return ret;
465}