blob: 3ab21bc5f083ccf3b180082171dfd13a3e0c6523 [file] [log] [blame]
Willy Tarreau1be4f3d2017-09-21 14:35:57 +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 Tarreau1be4f3d2017-09-21 14:35:57 +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>
Willy Tarreaube327fa2020-06-03 09:09:57 +020034#include <haproxy/hpack-enc.h>
Willy Tarreau0017be02020-06-02 19:25:28 +020035#include <haproxy/http-hdr-t.h>
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020036
Willy Tarreau2c313942018-12-10 15:19:14 +010037/*
38 * HPACK encoding: these tables were generated using gen-enc.c
39 */
40
41/* encoding of stream of compressed headers. This stream is composed of series
42 * of <len:8b> <index:8b> <name:<len>*8b>.
43 */
44const char hpack_enc_stream[666] = {
45 /* 0: */ 0x03, 0x15, 0x61, 0x67, 0x65, 0x03, 0x3c, 0x76,
46 /* 8: */ 0x69, 0x61, 0x04, 0x21, 0x64, 0x61, 0x74, 0x65,
47 /* 16: */ 0x04, 0x26, 0x68, 0x6f, 0x73, 0x74, 0x04, 0x22,
48 /* 24: */ 0x65, 0x74, 0x61, 0x67, 0x04, 0x25, 0x66, 0x72,
49 /* 32: */ 0x6f, 0x6d, 0x04, 0x2d, 0x6c, 0x69, 0x6e, 0x6b,
50 /* 40: */ 0x04, 0x3b, 0x76, 0x61, 0x72, 0x79, 0x05, 0x04,
51 /* 48: */ 0x3a, 0x70, 0x61, 0x74, 0x68, 0x05, 0x16, 0x61,
52 /* 56: */ 0x6c, 0x6c, 0x6f, 0x77, 0x05, 0x32, 0x72, 0x61,
53 /* 64: */ 0x6e, 0x67, 0x65, 0x06, 0x13, 0x61, 0x63, 0x63,
54 /* 72: */ 0x65, 0x70, 0x74, 0x06, 0x36, 0x73, 0x65, 0x72,
55 /* 80: */ 0x76, 0x65, 0x72, 0x06, 0x20, 0x63, 0x6f, 0x6f,
56 /* 88: */ 0x6b, 0x69, 0x65, 0x06, 0x23, 0x65, 0x78, 0x70,
57 /* 96: */ 0x65, 0x63, 0x74, 0x07, 0x33, 0x72, 0x65, 0x66,
58 /* 104: */ 0x65, 0x72, 0x65, 0x72, 0x07, 0x24, 0x65, 0x78,
59 /* 112: */ 0x70, 0x69, 0x72, 0x65, 0x73, 0x07, 0x02, 0x3a,
60 /* 120: */ 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x07, 0x06,
61 /* 128: */ 0x3a, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x65, 0x07,
62 /* 136: */ 0x08, 0x3a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73,
63 /* 144: */ 0x07, 0x34, 0x72, 0x65, 0x66, 0x72, 0x65, 0x73,
64 /* 152: */ 0x68, 0x08, 0x2e, 0x6c, 0x6f, 0x63, 0x61, 0x74,
65 /* 160: */ 0x69, 0x6f, 0x6e, 0x08, 0x27, 0x69, 0x66, 0x2d,
66 /* 168: */ 0x6d, 0x61, 0x74, 0x63, 0x68, 0x08, 0x2a, 0x69,
67 /* 176: */ 0x66, 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x0a,
68 /* 184: */ 0x3a, 0x75, 0x73, 0x65, 0x72, 0x2d, 0x61, 0x67,
69 /* 192: */ 0x65, 0x6e, 0x74, 0x0a, 0x37, 0x73, 0x65, 0x74,
70 /* 200: */ 0x2d, 0x63, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x0a,
71 /* 208: */ 0x01, 0x3a, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72,
72 /* 216: */ 0x69, 0x74, 0x79, 0x0b, 0x35, 0x72, 0x65, 0x74,
73 /* 224: */ 0x72, 0x79, 0x2d, 0x61, 0x66, 0x74, 0x65, 0x72,
74 /* 232: */ 0x0c, 0x1f, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
75 /* 240: */ 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x0c, 0x2f,
76 /* 248: */ 0x6d, 0x61, 0x78, 0x2d, 0x66, 0x6f, 0x72, 0x77,
77 /* 256: */ 0x61, 0x72, 0x64, 0x73, 0x0d, 0x18, 0x63, 0x61,
78 /* 264: */ 0x63, 0x68, 0x65, 0x2d, 0x63, 0x6f, 0x6e, 0x74,
79 /* 272: */ 0x72, 0x6f, 0x6c, 0x0d, 0x2c, 0x6c, 0x61, 0x73,
80 /* 280: */ 0x74, 0x2d, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69,
81 /* 288: */ 0x65, 0x64, 0x0d, 0x12, 0x61, 0x63, 0x63, 0x65,
82 /* 296: */ 0x70, 0x74, 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65,
83 /* 304: */ 0x73, 0x0d, 0x29, 0x69, 0x66, 0x2d, 0x6e, 0x6f,
84 /* 312: */ 0x6e, 0x65, 0x2d, 0x6d, 0x61, 0x74, 0x63, 0x68,
85 /* 320: */ 0x0d, 0x17, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72,
86 /* 328: */ 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x0d,
87 /* 336: */ 0x1e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
88 /* 344: */ 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x0e, 0x1c,
89 /* 352: */ 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
90 /* 360: */ 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x0e, 0x0f,
91 /* 368: */ 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x63,
92 /* 376: */ 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x0f, 0x10,
93 /* 384: */ 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x65,
94 /* 392: */ 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x0f,
95 /* 400: */ 0x11, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
96 /* 408: */ 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,
97 /* 416: */ 0x10, 0x1a, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
98 /* 424: */ 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69,
99 /* 432: */ 0x6e, 0x67, 0x10, 0x1b, 0x63, 0x6f, 0x6e, 0x74,
100 /* 440: */ 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x61, 0x6e, 0x67,
101 /* 448: */ 0x75, 0x61, 0x67, 0x65, 0x10, 0x1d, 0x63, 0x6f,
102 /* 456: */ 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x6f,
103 /* 464: */ 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x10, 0x3d,
104 /* 472: */ 0x77, 0x77, 0x77, 0x2d, 0x61, 0x75, 0x74, 0x68,
105 /* 480: */ 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, 0x65,
106 /* 488: */ 0x11, 0x39, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66,
107 /* 496: */ 0x65, 0x72, 0x2d, 0x65, 0x6e, 0x63, 0x6f, 0x64,
108 /* 504: */ 0x69, 0x6e, 0x67, 0x11, 0x28, 0x69, 0x66, 0x2d,
109 /* 512: */ 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64,
110 /* 520: */ 0x2d, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x12, 0x30,
111 /* 528: */ 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2d, 0x61, 0x75,
112 /* 536: */ 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61,
113 /* 544: */ 0x74, 0x65, 0x13, 0x19, 0x63, 0x6f, 0x6e, 0x74,
114 /* 552: */ 0x65, 0x6e, 0x74, 0x2d, 0x64, 0x69, 0x73, 0x70,
115 /* 560: */ 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x13,
116 /* 568: */ 0x2b, 0x69, 0x66, 0x2d, 0x75, 0x6e, 0x6d, 0x6f,
117 /* 576: */ 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x2d, 0x73,
118 /* 584: */ 0x69, 0x6e, 0x63, 0x65, 0x13, 0x31, 0x70, 0x72,
119 /* 592: */ 0x6f, 0x78, 0x79, 0x2d, 0x61, 0x75, 0x74, 0x68,
120 /* 600: */ 0x6f, 0x72, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f,
121 /* 608: */ 0x6e, 0x19, 0x38, 0x73, 0x74, 0x72, 0x69, 0x63,
122 /* 616: */ 0x74, 0x2d, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70,
123 /* 624: */ 0x6f, 0x72, 0x74, 0x2d, 0x73, 0x65, 0x63, 0x75,
124 /* 632: */ 0x72, 0x69, 0x74, 0x79, 0x1b, 0x14, 0x61, 0x63,
125 /* 640: */ 0x63, 0x65, 0x73, 0x73, 0x2d, 0x63, 0x6f, 0x6e,
126 /* 648: */ 0x74, 0x72, 0x6f, 0x6c, 0x2d, 0x61, 0x6c, 0x6c,
127 /* 656: */ 0x6f, 0x77, 0x2d, 0x6f, 0x72, 0x69, 0x67, 0x69,
128 /* 664: */ 0x6e, 0x00,
129};
130
131/* This points to the first position in table hpack_enc_stream[] of a header
132 * of the same length.
133 */
134const signed short hpack_pos_len[32] = {
135 /* 0: */ -1, -1, -1, 0, 10, 46, 67, 99,
136 /* 8: */ 153, -1, 183, 219, 232, 260, 350, 382,
137 /* 16: */ 416, 488, 526, 546, -1, -1, -1, -1,
138 /* 24: */ -1, 609, -1, 636, -1, -1, -1, -1,
139};
140
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200141/* Tries to encode header whose name is <n> and value <v> into the chunk <out>.
142 * Returns non-zero on success, 0 on failure (buffer full).
143 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200144int hpack_encode_header(struct buffer *out, const struct ist n,
145 const struct ist v)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200146{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200147 int len = out->data;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200148 int size = out->size;
Willy Tarreau2c313942018-12-10 15:19:14 +0100149 int pos;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200150
151 if (len >= size)
152 return 0;
153
Willy Tarreau2c313942018-12-10 15:19:14 +0100154 /* look for the header field <n> in the static table */
155 if (n.len >= sizeof(hpack_pos_len) / sizeof(hpack_pos_len[0]))
156 goto make_literal;
157
158 pos = hpack_pos_len[n.len];
159 if (pos >= 0) {
160 /* At least one header field of this length exist */
161 do {
162 char idx;
163
164 pos++;
165 idx = hpack_enc_stream[pos++];
166 pos += n.len;
167 if (isteq(ist2(&hpack_enc_stream[pos - n.len], n.len), n)) {
168 /* emit literal with indexing (7541#6.2.1) :
169 * [ 0 | 1 | Index (6+) ]
170 */
171 out->area[len++] = idx | 0x40;
172 goto emit_value;
173 }
174 } while ((unsigned char)hpack_enc_stream[pos] == n.len);
175 }
176
177 make_literal:
Willy Tarreau0dc1b842018-12-16 09:38:30 +0100178 if (likely(n.len < 127 && len + 2 + n.len <= size)) {
Willy Tarreau19ed92b2018-12-11 06:42:01 +0100179 out->area[len++] = 0x00; /* literal without indexing -- new name */
180 out->area[len++] = n.len; /* single-byte length encoding */
181 ist2bin(out->area + len, n);
182 len += n.len;
183 }
Willy Tarreaubad0a382018-12-11 07:44:00 +0100184 else if (hpack_len_to_bytes(n.len) &&
185 len + 1 + hpack_len_to_bytes(n.len) + n.len <= size) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200186 out->area[len++] = 0x00; /* literal without indexing -- new name */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200187 len = hpack_encode_len(out->area, len, n.len);
Willy Tarreauac73ae02018-12-11 06:27:06 +0100188 ist2bin(out->area + len, n);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200189 len += n.len;
190 }
191 else {
192 /* header field name too large for the buffer */
193 return 0;
194 }
195
Willy Tarreau2c313942018-12-10 15:19:14 +0100196 emit_value:
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200197 /* copy literal header field value */
Willy Tarreaubad0a382018-12-11 07:44:00 +0100198 if (!hpack_len_to_bytes(v.len) ||
199 len + hpack_len_to_bytes(v.len) + v.len > size) {
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200200 /* header value too large for the buffer */
201 return 0;
202 }
203
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200204 len = hpack_encode_len(out->area, len, v.len);
205 memcpy(out->area + len, v.ptr, v.len);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200206 len += v.len;
207
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 out->data = len;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200209 return 1;
210}