blob: 8c56a0ddb78450bc6231d78d4642f84aab58c165 [file] [log] [blame]
Willy Tarreauce040942017-05-30 18:46:58 +02001/*
2 * HPACK header table management (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 Tarreauce040942017-05-30 18:46:58 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
Willy Tarreaube327fa2020-06-03 09:09:57 +020033#include <haproxy/hpack-huff.h>
34#include <haproxy/hpack-tbl.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020035#include <import/ist.h>
Willy Tarreauce040942017-05-30 18:46:58 +020036
37#include <types/global.h>
38
39/* static header table as in RFC7541 Appendix A. [0] unused. */
40const struct http_hdr hpack_sht[HPACK_SHT_SIZE] = {
41 [ 1] = { .n = IST(":authority"), .v = IST("") },
42 [ 2] = { .n = IST(":method"), .v = IST("GET") },
43 [ 3] = { .n = IST(":method"), .v = IST("POST") },
44 [ 4] = { .n = IST(":path"), .v = IST("/") },
45 [ 5] = { .n = IST(":path"), .v = IST("/index.html") },
46 [ 6] = { .n = IST(":scheme"), .v = IST("http") },
47 [ 7] = { .n = IST(":scheme"), .v = IST("https") },
48 [ 8] = { .n = IST(":status"), .v = IST("200") },
49 [ 9] = { .n = IST(":status"), .v = IST("204") },
50 [10] = { .n = IST(":status"), .v = IST("206") },
51 [11] = { .n = IST(":status"), .v = IST("304") },
52 [12] = { .n = IST(":status"), .v = IST("400") },
53 [13] = { .n = IST(":status"), .v = IST("404") },
54 [14] = { .n = IST(":status"), .v = IST("500") },
55 [15] = { .n = IST("accept-charset"), .v = IST("") },
56 [16] = { .n = IST("accept-encoding"), .v = IST("gzip, deflate") },
57 [17] = { .n = IST("accept-language"), .v = IST("") },
58 [18] = { .n = IST("accept-ranges"), .v = IST("") },
59 [19] = { .n = IST("accept"), .v = IST("") },
60 [20] = { .n = IST("access-control-allow-origin"), .v = IST("") },
61 [21] = { .n = IST("age"), .v = IST("") },
62 [22] = { .n = IST("allow"), .v = IST("") },
63 [23] = { .n = IST("authorization"), .v = IST("") },
64 [24] = { .n = IST("cache-control"), .v = IST("") },
65 [25] = { .n = IST("content-disposition"), .v = IST("") },
66 [26] = { .n = IST("content-encoding"), .v = IST("") },
67 [27] = { .n = IST("content-language"), .v = IST("") },
68 [28] = { .n = IST("content-length"), .v = IST("") },
69 [29] = { .n = IST("content-location"), .v = IST("") },
70 [30] = { .n = IST("content-range"), .v = IST("") },
71 [31] = { .n = IST("content-type") , .v = IST("") },
72 [32] = { .n = IST("cookie"), .v = IST("") },
73 [33] = { .n = IST("date"), .v = IST("") },
74 [34] = { .n = IST("etag"), .v = IST("") },
75 [35] = { .n = IST("expect"), .v = IST("") },
76 [36] = { .n = IST("expires"), .v = IST("") },
77 [37] = { .n = IST("from"), .v = IST("") },
78 [38] = { .n = IST("host"), .v = IST("") },
79 [39] = { .n = IST("if-match"), .v = IST("") },
80 [40] = { .n = IST("if-modified-since"), .v = IST("") },
81 [41] = { .n = IST("if-none-match"), .v = IST("") },
82 [42] = { .n = IST("if-range"), .v = IST("") },
83 [43] = { .n = IST("if-unmodified-since"), .v = IST("") },
84 [44] = { .n = IST("last-modified"), .v = IST("") },
85 [45] = { .n = IST("link"), .v = IST("") },
86 [46] = { .n = IST("location"), .v = IST("") },
87 [47] = { .n = IST("max-forwards"), .v = IST("") },
88 [48] = { .n = IST("proxy-authenticate"), .v = IST("") },
89 [49] = { .n = IST("proxy-authorization"), .v = IST("") },
90 [50] = { .n = IST("range"), .v = IST("") },
91 [51] = { .n = IST("referer"), .v = IST("") },
92 [52] = { .n = IST("refresh"), .v = IST("") },
93 [53] = { .n = IST("retry-after"), .v = IST("") },
94 [54] = { .n = IST("server"), .v = IST("") },
95 [55] = { .n = IST("set-cookie"), .v = IST("") },
96 [56] = { .n = IST("strict-transport-security"), .v = IST("") },
97 [57] = { .n = IST("transfer-encoding"), .v = IST("") },
98 [58] = { .n = IST("user-agent"), .v = IST("") },
99 [59] = { .n = IST("vary"), .v = IST("") },
100 [60] = { .n = IST("via"), .v = IST("") },
101 [61] = { .n = IST("www-authenticate"), .v = IST("") },
102};
103
Willy Tarreau2bdcc702020-05-19 11:31:11 +0200104struct pool_head *pool_head_hpack_tbl = NULL;
105
Willy Tarreauce040942017-05-30 18:46:58 +0200106/* returns the slot number of the oldest entry (tail). Must not be used on an
107 * empty table.
108 */
109static inline unsigned int hpack_dht_get_tail(const struct hpack_dht *dht)
110{
111 return ((dht->head + 1U < dht->used) ? dht->wrap : 0) + dht->head + 1U - dht->used;
112}
113
114#ifdef DEBUG_HPACK
115/* dump the whole dynamic header table */
Willy Tarreau4f034362017-12-30 17:08:46 +0100116static void hpack_dht_dump(FILE *out, const struct hpack_dht *dht)
Willy Tarreauce040942017-05-30 18:46:58 +0200117{
Willy Tarreau7f2a44d2018-09-17 14:07:33 +0200118 unsigned int i;
Willy Tarreauce040942017-05-30 18:46:58 +0200119 unsigned int slot;
120 char name[4096], value[4096];
121
122 for (i = HPACK_SHT_SIZE; i < HPACK_SHT_SIZE + dht->used; i++) {
123 slot = (hpack_get_dte(dht, i - HPACK_SHT_SIZE + 1) - dht->dte);
Willy Tarreau4f034362017-12-30 17:08:46 +0100124 fprintf(out, "idx=%d slot=%u name=<%s> value=<%s> addr=%u-%u\n",
Willy Tarreauce040942017-05-30 18:46:58 +0200125 i, slot,
126 istpad(name, hpack_idx_to_name(dht, i)).ptr,
127 istpad(value, hpack_idx_to_value(dht, i)).ptr,
128 dht->dte[slot].addr, dht->dte[slot].addr+dht->dte[slot].nlen+dht->dte[slot].vlen-1);
129 }
130}
131
132/* check for the whole dynamic header table consistency, abort on failures */
133static void hpack_dht_check_consistency(const struct hpack_dht *dht)
134{
135 unsigned slot = hpack_dht_get_tail(dht);
136 unsigned used2 = dht->used;
137 unsigned total = 0;
138
139 if (!dht->used)
140 return;
141
142 if (dht->front >= dht->wrap)
143 abort();
144
145 if (dht->used > dht->wrap)
146 abort();
147
148 if (dht->head >= dht->wrap)
149 abort();
150
151 while (used2--) {
152 total += dht->dte[slot].nlen + dht->dte[slot].vlen;
153 slot++;
154 if (slot >= dht->wrap)
155 slot = 0;
156 }
157
158 if (total != dht->total) {
159 fprintf(stderr, "%d: total=%u dht=%u\n", __LINE__, total, dht->total);
160 abort();
161 }
162}
163#endif // DEBUG_HPACK
164
165/* rebuild a new dynamic header table from <dht> with an unwrapped index and
166 * contents at the end. The new table is returned, the caller must not use the
167 * previous one anymore. NULL may be returned if no table could be allocated.
168 */
169static struct hpack_dht *hpack_dht_defrag(struct hpack_dht *dht)
170{
171 struct hpack_dht *alt_dht;
172 uint16_t old, new;
173 uint32_t addr;
174
175 /* Note: for small tables we could use alloca() instead but
176 * portability especially for large tables can be problematic.
177 */
Willy Tarreau2bdcc702020-05-19 11:31:11 +0200178 alt_dht = hpack_dht_alloc();
Willy Tarreauce040942017-05-30 18:46:58 +0200179 if (!alt_dht)
180 return NULL;
181
182 alt_dht->total = dht->total;
183 alt_dht->used = dht->used;
184 alt_dht->wrap = dht->used;
185
186 new = 0;
187 addr = alt_dht->size;
188
189 if (dht->used) {
190 /* start from the tail */
191 old = hpack_dht_get_tail(dht);
192 do {
193 alt_dht->dte[new].nlen = dht->dte[old].nlen;
194 alt_dht->dte[new].vlen = dht->dte[old].vlen;
195 addr -= dht->dte[old].nlen + dht->dte[old].vlen;
196 alt_dht->dte[new].addr = addr;
197
198 memcpy((void *)alt_dht + alt_dht->dte[new].addr,
199 (void *)dht + dht->dte[old].addr,
200 dht->dte[old].nlen + dht->dte[old].vlen);
201
202 old++;
203 if (old >= dht->wrap)
204 old = 0;
205 new++;
206 } while (new < dht->used);
207 }
208
209 alt_dht->front = alt_dht->head = new - 1;
210
211 memcpy(dht, alt_dht, dht->size);
212 hpack_dht_free(alt_dht);
213
214 return dht;
215}
216
217/* Purges table dht until a header field of <needed> bytes fits according to
218 * the protocol (adding 32 bytes overhead). Returns non-zero on success, zero
219 * on failure (ie: table empty but still not sufficient). It must only be
220 * called when the table is not large enough to suit the new entry and there
221 * are some entries left. In case of doubt, use dht_make_room() instead.
222 */
223int __hpack_dht_make_room(struct hpack_dht *dht, unsigned int needed)
224{
225 unsigned int used = dht->used;
226 unsigned int wrap = dht->wrap;
227 unsigned int tail;
228
229 do {
230 tail = ((dht->head + 1U < used) ? wrap : 0) + dht->head + 1U - used;
231 dht->total -= dht->dte[tail].nlen + dht->dte[tail].vlen;
232 if (tail == dht->front)
233 dht->front = dht->head;
234 used--;
235 } while (used && used * 32 + dht->total + needed + 32 > dht->size);
236
237 dht->used = used;
238
239 /* realign if empty */
240 if (!used)
241 dht->front = dht->head = 0;
242
243 /* pack the table if it doesn't wrap anymore */
244 if (dht->head + 1U >= used)
245 dht->wrap = dht->head + 1;
246
247 /* no need to check for 'used' here as if it doesn't fit, used==0 */
248 return needed + 32 <= dht->size;
249}
250
251/* tries to insert a new header <name>:<value> in front of the current head. A
252 * negative value is returned on error.
253 */
254int hpack_dht_insert(struct hpack_dht *dht, struct ist name, struct ist value)
255{
256 unsigned int used;
257 unsigned int head;
258 unsigned int prev;
259 unsigned int wrap;
260 unsigned int tail;
261 uint32_t headroom, tailroom;
262
263 if (!hpack_dht_make_room(dht, name.len + value.len))
Willy Tarreau6c71e462017-12-04 17:58:37 +0100264 return 0;
Willy Tarreauce040942017-05-30 18:46:58 +0200265
Willy Tarreauce040942017-05-30 18:46:58 +0200266 /* Now there is enough room in the table, that's guaranteed by the
267 * protocol, but not necessarily where we need it.
268 */
269
Willy Tarreaua7394e12018-03-27 15:06:02 +0200270 used = dht->used;
Willy Tarreauce040942017-05-30 18:46:58 +0200271 if (!used) {
272 /* easy, the table was empty */
273 dht->front = dht->head = 0;
274 dht->wrap = dht->used = 1;
275 dht->total = 0;
276 head = 0;
277 dht->dte[head].addr = dht->size - (name.len + value.len);
278 goto copy;
279 }
280
281 /* compute the new head, used and wrap position */
Willy Tarreaua7394e12018-03-27 15:06:02 +0200282 prev = head = dht->head;
283 wrap = dht->wrap;
284 tail = hpack_dht_get_tail(dht);
285
Willy Tarreauce040942017-05-30 18:46:58 +0200286 used++;
287 head++;
288
289 if (head >= wrap) {
290 /* head is leading the entries, we either need to push the
291 * table further or to loop back to released entries. We could
292 * force to loop back when at least half of the allocatable
293 * entries are free but in practice it never happens.
294 */
295 if ((sizeof(*dht) + (wrap + 1) * sizeof(dht->dte[0]) <= dht->dte[dht->front].addr))
296 wrap++;
297 else if (head >= used) /* there's a hole at the beginning */
298 head = 0;
299 else {
300 /* no more room, head hits tail and the index cannot be
301 * extended, we have to realign the whole table.
302 */
303 if (!hpack_dht_defrag(dht))
304 return -1;
305
306 wrap = dht->wrap + 1;
307 head = dht->head + 1;
308 prev = head - 1;
309 tail = 0;
310 }
311 }
312 else if (used >= wrap) {
313 /* we've hit the tail, we need to reorganize the index so that
314 * the head is at the end (but not necessarily move the data).
315 */
316 if (!hpack_dht_defrag(dht))
317 return -1;
318
319 wrap = dht->wrap + 1;
320 head = dht->head + 1;
321 prev = head - 1;
322 tail = 0;
323 }
324
325 /* Now we have updated head, used and wrap, we know that there is some
326 * available room at least from the protocol's perspective. This space
327 * is split in two areas :
328 *
329 * 1: if the previous head was the front cell, the space between the
330 * end of the index table and the front cell's address.
331 * 2: if the previous head was the front cell, the space between the
332 * end of the tail and the end of the table ; or if the previous
333 * head was not the front cell, the space between the end of the
334 * tail and the head's address.
335 */
336 if (prev == dht->front) {
337 /* the area was contiguous */
338 headroom = dht->dte[dht->front].addr - (sizeof(*dht) + wrap * sizeof(dht->dte[0]));
339 tailroom = dht->size - dht->dte[tail].addr - dht->dte[tail].nlen - dht->dte[tail].vlen;
340 }
341 else {
342 /* it's already wrapped so we can't store anything in the headroom */
343 headroom = 0;
344 tailroom = dht->dte[prev].addr - dht->dte[tail].addr - dht->dte[tail].nlen - dht->dte[tail].vlen;
345 }
346
347 /* We can decide to stop filling the headroom as soon as there's enough
348 * room left in the tail to suit the protocol, but tests show that in
349 * practice it almost never happens in other situations so the extra
350 * test is useless and we simply fill the headroom as long as it's
Willy Tarreau5dfc5d52020-03-29 08:53:31 +0200351 * available and we don't wrap.
Willy Tarreauce040942017-05-30 18:46:58 +0200352 */
Willy Tarreau5dfc5d52020-03-29 08:53:31 +0200353 if (prev == dht->front && headroom >= name.len + value.len) {
Willy Tarreauce040942017-05-30 18:46:58 +0200354 /* install upfront and update ->front */
355 dht->dte[head].addr = dht->dte[dht->front].addr - (name.len + value.len);
356 dht->front = head;
357 }
358 else if (tailroom >= name.len + value.len) {
359 dht->dte[head].addr = dht->dte[tail].addr + dht->dte[tail].nlen + dht->dte[tail].vlen + tailroom - (name.len + value.len);
360 }
361 else {
362 /* need to defragment the table before inserting upfront */
363 dht = hpack_dht_defrag(dht);
364 wrap = dht->wrap + 1;
365 head = dht->head + 1;
366 dht->dte[head].addr = dht->dte[dht->front].addr - (name.len + value.len);
367 dht->front = head;
368 }
369
370 dht->wrap = wrap;
371 dht->head = head;
372 dht->used = used;
373
374 copy:
375 dht->total += name.len + value.len;
376 dht->dte[head].nlen = name.len;
377 dht->dte[head].vlen = value.len;
378
379 memcpy((void *)dht + dht->dte[head].addr, name.ptr, name.len);
380 memcpy((void *)dht + dht->dte[head].addr + name.len, value.ptr, value.len);
381 return 0;
382}