blob: fa25ac47537390bbb02e64a468424f246ed819aa [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 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
Willy Tarreau077d3662020-02-05 15:28:55 +010068 while (len && (*raw & 128)) {
Willy Tarreau679790b2017-05-30 19:09:44 +020069 ret += ((uint32_t)(*raw++) & 127) << shift;
70 shift += 7;
71 len--;
72 }
73
74 /* last 7 bits */
75 if (!len)
76 goto too_short;
77 len--;
78 ret += ((uint32_t)(*raw++) & 127) << shift;
79
80 end:
81 *raw_in = raw;
82 *len_in = len;
83 return ret;
84
85 too_short:
86 *len_in = (uint32_t)-1;
87 return 0;
88}
89
Willy Tarreau59a10fb2017-11-21 20:03:02 +010090/* returns the pseudo-header <idx> corresponds to among the following values :
91 * - 0 = unknown, the header's string needs to be used instead
92 * - 1 = ":authority"
93 * - 2 = ":method"
94 * - 3 = ":path"
95 * - 4 = ":scheme"
96 * - 5 = ":status"
Willy Tarreau679790b2017-05-30 19:09:44 +020097 */
98static inline int hpack_idx_to_phdr(uint32_t idx)
99{
100 if (idx > 14)
101 return 0;
102
103 idx >>= 1;
104 idx <<= 2;
105 return (0x55554321U >> idx) & 0xF;
106}
107
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100108/* If <idx> designates a static header, returns <in>. Otherwise allocates some
109 * room from chunk <store> to duplicate <in> into it and returns the string
110 * allocated there. In case of allocation failure, returns a string whose
111 * pointer is NULL.
Willy Tarreau679790b2017-05-30 19:09:44 +0200112 */
Willy Tarreau7f2a44d2018-09-17 14:07:33 +0200113static inline struct ist hpack_alloc_string(struct buffer *store, uint32_t idx,
Willy Tarreau83061a82018-07-13 11:56:34 +0200114 struct ist in)
Willy Tarreau679790b2017-05-30 19:09:44 +0200115{
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100116 struct ist out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200117
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100118 if (idx < HPACK_SHT_SIZE)
119 return in;
Willy Tarreau679790b2017-05-30 19:09:44 +0200120
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100121 out.len = in.len;
122 out.ptr = chunk_newstr(store);
123 if (unlikely(!out.ptr))
124 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200125
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200126 if (unlikely(store->data + out.len > store->size)) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100127 out.ptr = NULL;
128 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200129 }
130
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200131 store->data += out.len;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100132 memcpy(out.ptr, in.ptr, out.len);
133 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200134}
135
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100136/* decode an HPACK frame starting at <raw> for <len> bytes, using the dynamic
137 * headers table <dht>, produces the output into list <list> of <list_size>
138 * entries max, and uses pre-allocated buffer <tmp> for temporary storage (some
139 * list elements will point to it). Some <list> name entries may be made of a
140 * NULL pointer and a len, in which case they will designate a pseudo header
141 * index according to the values returned by hpack_idx_to_phdr() above. The
142 * number of <list> entries used is returned on success, or <0 on failure, with
143 * the opposite one of the HPACK_ERR_* codes. A last element is always zeroed
144 * and is not counted in the number of returned entries. This way the caller
145 * can use list[].n.len == 0 as a marker for the end of list.
Willy Tarreau679790b2017-05-30 19:09:44 +0200146 */
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100147int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len,
Willy Tarreau83061a82018-07-13 11:56:34 +0200148 struct http_hdr *list, int list_size,
149 struct buffer *tmp)
Willy Tarreau679790b2017-05-30 19:09:44 +0200150{
151 uint32_t idx;
152 uint32_t nlen;
153 uint32_t vlen;
154 uint8_t huff;
Willy Tarreau679790b2017-05-30 19:09:44 +0200155 struct ist name;
156 struct ist value;
Willy Tarreau679790b2017-05-30 19:09:44 +0200157 int must_index;
158 int ret;
Willy Tarreau679790b2017-05-30 19:09:44 +0200159
Willy Tarreauc775f832017-12-30 13:17:37 +0100160 hpack_debug_hexdump(stderr, "[HPACK-DEC] ", (const char *)raw, 0, len);
161
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100162 chunk_reset(tmp);
163 ret = 0;
Willy Tarreau679790b2017-05-30 19:09:44 +0200164 while (len) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100165 int __maybe_unused code = *raw; /* first byte, only for debugging */
Willy Tarreau679790b2017-05-30 19:09:44 +0200166
167 must_index = 0;
168 if (*raw >= 0x80) {
169 /* indexed header field */
170 if (*raw == 0x80) {
171 hpack_debug_printf("unhandled code 0x%02x (raw=%p, len=%d)\n", *raw, raw, len);
172 ret = -HPACK_ERR_UNKNOWN_OPCODE;
173 goto leave;
174 }
175
176 hpack_debug_printf("%02x: p14: indexed header field : ", code);
177
178 idx = get_var_int(&raw, &len, 7);
179 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100180 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200181 ret = -HPACK_ERR_TRUNCATED;
182 goto leave;
183 }
184
Willy Tarreauc775f832017-12-30 13:17:37 +0100185 hpack_debug_printf(" idx=%u ", idx);
186
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100187 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100188 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100189 ret = -HPACK_ERR_TOO_LARGE;
190 goto leave;
191 }
192
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100193 value = hpack_alloc_string(tmp, idx, hpack_idx_to_value(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100194 if (!isttest(value)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100195 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100196 ret = -HPACK_ERR_TOO_LARGE;
197 goto leave;
198 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200199
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100200 /* here we don't index so we can always keep the pseudo header number */
201 name = ist2(NULL, hpack_idx_to_phdr(idx));
Willy Tarreau679790b2017-05-30 19:09:44 +0200202
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100203 if (!name.len) {
204 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100205 if (!isttest(name)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100206 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100207 ret = -HPACK_ERR_TOO_LARGE;
208 goto leave;
209 }
210 }
211 /* <name> and <value> are now set and point to stable values */
Willy Tarreau679790b2017-05-30 19:09:44 +0200212 }
213 else if (*raw >= 0x20 && *raw <= 0x3f) {
214 /* max dyn table size change */
Willy Tarreauc775f832017-12-30 13:17:37 +0100215 hpack_debug_printf("%02x: p18: dynamic table size update : ", code);
216
Willy Tarreauc611e662017-12-03 18:09:21 +0100217 if (ret) {
218 /* 7541#4.2.1 : DHT size update must only be at the beginning */
Willy Tarreauc775f832017-12-30 13:17:37 +0100219 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreauc611e662017-12-03 18:09:21 +0100220 ret = -HPACK_ERR_TOO_LARGE;
221 goto leave;
222 }
223
Willy Tarreau679790b2017-05-30 19:09:44 +0200224 idx = get_var_int(&raw, &len, 5);
225 if (len == (uint32_t)-1) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100226 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200227 ret = -HPACK_ERR_TRUNCATED;
228 goto leave;
229 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100230 hpack_debug_printf(" new len=%u\n", idx);
Willy Tarreau1e7d4442019-01-24 10:47:10 +0100231
232 if (idx > dht->size) {
233 hpack_debug_printf("##ERR@%d##\n", __LINE__);
234 ret = -HPACK_ERR_INVALID_ARGUMENT;
235 goto leave;
236 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200237 continue;
238 }
239 else if (!(*raw & (*raw - 0x10))) {
240 /* 0x00, 0x10, and 0x40 (0x20 and 0x80 were already handled above) */
241
242 /* literal header field without/never/with incremental indexing -- literal name */
243 if (*raw == 0x00)
244 hpack_debug_printf("%02x: p17: literal without indexing : ", code);
245 else if (*raw == 0x10)
246 hpack_debug_printf("%02x: p18: literal never indexed : ", code);
247 else if (*raw == 0x40)
248 hpack_debug_printf("%02x: p16: literal with indexing : ", code);
249
250 if (*raw == 0x40)
251 must_index = 1;
252
253 raw++; len--;
254
255 /* retrieve name */
256 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100257 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200258 ret = -HPACK_ERR_TRUNCATED;
259 goto leave;
260 }
261
262 huff = *raw & 0x80;
263 nlen = get_var_int(&raw, &len, 7);
264 if (len == (uint32_t)-1 || len < nlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100265 hpack_debug_printf("##ERR@%d## (truncated): nlen=%d len=%d\n",
266 __LINE__, (int)nlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200267 ret = -HPACK_ERR_TRUNCATED;
268 goto leave;
269 }
270
271 name = ist2(raw, nlen);
272
273 raw += nlen;
274 len -= nlen;
Willy Tarreau679790b2017-05-30 19:09:44 +0200275
276 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100277 char *ntrash = chunk_newstr(tmp);
278 if (!ntrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100279 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100280 ret = -HPACK_ERR_TOO_LARGE;
281 goto leave;
282 }
283
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200284 nlen = huff_dec((const uint8_t *)name.ptr, name.len, ntrash,
285 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200286 if (nlen == (uint32_t)-1) {
287 hpack_debug_printf("2: can't decode huffman.\n");
288 ret = -HPACK_ERR_HUFFMAN;
289 goto leave;
290 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100291 hpack_debug_printf(" [name huff %d->%d] ", (int)name.len, (int)nlen);
292
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200293 tmp->data += nlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100294 name = ist2(ntrash, nlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200295 }
296
297 /* retrieve value */
298 if (!len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100299 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200300 ret = -HPACK_ERR_TRUNCATED;
301 goto leave;
302 }
303
304 huff = *raw & 0x80;
305 vlen = get_var_int(&raw, &len, 7);
306 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100307 hpack_debug_printf("##ERR@%d## : vlen=%d len=%d\n",
308 __LINE__, (int)vlen, (int)len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200309 ret = -HPACK_ERR_TRUNCATED;
310 goto leave;
311 }
312
313 value = ist2(raw, vlen);
314 raw += vlen;
315 len -= vlen;
316
317 if (huff) {
318 char *vtrash = chunk_newstr(tmp);
319 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100320 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100321 ret = -HPACK_ERR_TOO_LARGE;
Willy Tarreau679790b2017-05-30 19:09:44 +0200322 goto leave;
323 }
324
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200325 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
326 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200327 if (vlen == (uint32_t)-1) {
328 hpack_debug_printf("3: can't decode huffman.\n");
329 ret = -HPACK_ERR_HUFFMAN;
330 goto leave;
331 }
Willy Tarreauc775f832017-12-30 13:17:37 +0100332 hpack_debug_printf(" [value huff %d->%d] ", (int)value.len, (int)vlen);
333
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200334 tmp->data += vlen; // make room for the value
Willy Tarreau679790b2017-05-30 19:09:44 +0200335 value = ist2(vtrash, vlen);
336 }
337
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100338 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200339 }
340 else {
341 /* 0x01..0x0f : literal header field without indexing -- indexed name */
342 /* 0x11..0x1f : literal header field never indexed -- indexed name */
343 /* 0x41..0x7f : literal header field with incremental indexing -- indexed name */
344
345 if (*raw <= 0x0f)
346 hpack_debug_printf("%02x: p16: literal without indexing -- indexed name : ", code);
347 else if (*raw >= 0x41)
348 hpack_debug_printf("%02x: p15: literal with indexing -- indexed name : ", code);
349 else
350 hpack_debug_printf("%02x: p16: literal never indexed -- indexed name : ", code);
351
352 /* retrieve name index */
353 if (*raw >= 0x41) {
354 must_index = 1;
355 idx = get_var_int(&raw, &len, 6);
356 }
357 else
358 idx = get_var_int(&raw, &len, 4);
359
Willy Tarreauc775f832017-12-30 13:17:37 +0100360 hpack_debug_printf(" idx=%u ", idx);
361
Willy Tarreau679790b2017-05-30 19:09:44 +0200362 if (len == (uint32_t)-1 || !len) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100363 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200364 ret = -HPACK_ERR_TRUNCATED;
365 goto leave;
366 }
367
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100368 if (!hpack_valid_idx(dht, idx)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100369 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaud85ba4e2017-12-03 12:12:17 +0100370 ret = -HPACK_ERR_TOO_LARGE;
371 goto leave;
372 }
373
Willy Tarreau679790b2017-05-30 19:09:44 +0200374 /* retrieve value */
375 huff = *raw & 0x80;
376 vlen = get_var_int(&raw, &len, 7);
377 if (len == (uint32_t)-1 || len < vlen) { // truncated
Willy Tarreauc775f832017-12-30 13:17:37 +0100378 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200379 ret = -HPACK_ERR_TRUNCATED;
380 goto leave;
381 }
382
383 value = ist2(raw, vlen);
384 raw += vlen;
385 len -= vlen;
386
387 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100388 char *vtrash = chunk_newstr(tmp);
389 if (!vtrash) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100390 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100391 ret = -HPACK_ERR_TOO_LARGE;
392 goto leave;
393 }
394
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200395 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash,
396 tmp->size - tmp->data);
Willy Tarreau679790b2017-05-30 19:09:44 +0200397 if (vlen == (uint32_t)-1) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100398 hpack_debug_printf("##ERR@%d## can't decode huffman : ilen=%d osize=%d\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200399 __LINE__, (int)value.len,
400 (int)(tmp->size - tmp->data));
Willy Tarreauc775f832017-12-30 13:17:37 +0100401 hpack_debug_hexdump(stderr, "[HUFFMAN] ", value.ptr, 0, value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200402 ret = -HPACK_ERR_HUFFMAN;
403 goto leave;
404 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200405 tmp->data += vlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100406 value = ist2(vtrash, vlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200407 }
408
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100409 name = IST_NULL;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100410 if (!must_index)
411 name.len = hpack_idx_to_phdr(idx);
Willy Tarreau679790b2017-05-30 19:09:44 +0200412
Willy Tarreaubb39b492017-12-30 16:56:28 +0100413 if (!name.len) {
414 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
Tim Duesterhused526372020-03-05 17:56:33 +0100415 if (!isttest(name)) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100416 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreaubb39b492017-12-30 16:56:28 +0100417 ret = -HPACK_ERR_TOO_LARGE;
418 goto leave;
419 }
420 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100421 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200422 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200423
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100424 /* here's what we have here :
425 * - name.len > 0
426 * - value is filled with either const data or data allocated from tmp
427 * - name.ptr == NULL && !must_index : known pseudo-header #name.len
428 * - name.ptr != NULL || must_index : general header, unknown pseudo-header or index needed
429 */
430 if (ret >= list_size) {
Willy Tarreauc775f832017-12-30 13:17:37 +0100431 hpack_debug_printf("##ERR@%d##\n", __LINE__);
Willy Tarreau679790b2017-05-30 19:09:44 +0200432 ret = -HPACK_ERR_TOO_LARGE;
433 goto leave;
434 }
435
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100436 list[ret].n = name;
437 list[ret].v = value;
438 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200439
440 if (must_index && hpack_dht_insert(dht, name, value) < 0) {
441 hpack_debug_printf("failed to find some room in the dynamic table\n");
442 ret = -HPACK_ERR_DHT_INSERT_FAIL;
443 goto leave;
444 }
445
Willy Tarreau679790b2017-05-30 19:09:44 +0200446 hpack_debug_printf("\e[1;34m%s\e[0m: ",
Tim Duesterhused526372020-03-05 17:56:33 +0100447 isttest(name) ? istpad(trash.area, name).ptr : h2_phdr_to_str(name.len));
Willy Tarreau679790b2017-05-30 19:09:44 +0200448
Willy Tarreauc775f832017-12-30 13:17:37 +0100449 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 +0200450 istpad(trash.area, value).ptr, must_index,
451 dht->used,
Willy Tarreauc775f832017-12-30 13:17:37 +0100452 name.ptr, (int)name.len, value.ptr, (int)value.len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200453 }
454
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100455 if (ret >= list_size) {
Willy Tarreau679790b2017-05-30 19:09:44 +0200456 ret = -HPACK_ERR_TOO_LARGE;
457 goto leave;
458 }
459
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100460 /* put an end marker */
Tim Duesterhus241e29e2020-03-05 17:56:30 +0100461 list[ret].n = list[ret].v = IST_NULL;
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100462 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200463
Willy Tarreau679790b2017-05-30 19:09:44 +0200464 leave:
Willy Tarreauc775f832017-12-30 13:17:37 +0100465 hpack_debug_printf("-- done: ret=%d list_size=%d --\n", (int)ret, (int)list_size);
Willy Tarreau679790b2017-05-30 19:09:44 +0200466 return ret;
467}