blob: c5307d9a0aca71aa072dc12375bd2c505db7469f [file] [log] [blame]
Willy Tarreauf24ea8e2017-11-21 19:55:27 +01001/*
2 * HTTP/2 protocol processing
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
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 Tarreauf24ea8e2017-11-21 19:55:27 +010029#include <common/config.h>
30#include <common/h2.h>
31#include <common/http-hdr.h>
32#include <common/ist.h>
Willy Tarreau92919f72019-10-08 16:53:07 +020033#include <types/global.h>
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010034
Willy Tarreau9c84d822019-01-30 15:09:21 +010035struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
36 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
37 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
38 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
39 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
40 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
42 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
43 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
44 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
45 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
46};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010047
Willy Tarreau54f53ef2019-11-22 16:02:43 +010048/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
49 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
50 * non-zero if such a character is found, 0 otherwise. When run on unlikely
51 * header match, it's recommended to first check for the presence of control
52 * chars using ist_find_ctl().
53 */
54static int has_forbidden_char(const struct ist ist, const char *start)
55{
56 do {
57 if ((uint8_t)*start <= 0x0d &&
58 (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
59 return 1;
60 start++;
61 } while (start < ist.ptr + ist.len);
62 return 0;
63}
64
Willy Tarreaubeefaee2018-12-19 13:08:08 +010065/* Parse the Content-Length header field of an HTTP/2 request. The function
66 * checks all possible occurrences of a comma-delimited value, and verifies
67 * if any of them doesn't match a previous value. It returns <0 if a value
68 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
69 * if the value can be indexed (first one). In the last case, the value might
70 * be adjusted and the caller must only add the updated value.
71 */
72int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
73{
74 char *e, *n;
75 unsigned long long cl;
76 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
77 struct ist word;
78
79 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
80 e = value->ptr + value->len;
81
82 while (++word.ptr < e) {
83 /* skip leading delimitor and blanks */
84 if (unlikely(HTTP_IS_LWS(*word.ptr)))
85 continue;
86
87 /* digits only now */
88 for (cl = 0, n = word.ptr; n < e; n++) {
89 unsigned int c = *n - '0';
90 if (unlikely(c > 9)) {
91 /* non-digit */
92 if (unlikely(n == word.ptr)) // spaces only
93 goto fail;
94 break;
95 }
96 if (unlikely(cl > ULLONG_MAX / 10ULL))
97 goto fail; /* multiply overflow */
98 cl = cl * 10ULL;
99 if (unlikely(cl + c < cl))
100 goto fail; /* addition overflow */
101 cl = cl + c;
102 }
103
104 /* keep a copy of the exact cleaned value */
105 word.len = n - word.ptr;
106
107 /* skip trailing LWS till next comma or EOL */
108 for (; n < e; n++) {
109 if (!HTTP_IS_LWS(*n)) {
110 if (unlikely(*n != ','))
111 goto fail;
112 break;
113 }
114 }
115
116 /* if duplicate, must be equal */
117 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
118 goto fail;
119
120 /* OK, store this result as the one to be indexed */
121 *msgf |= H2_MSGF_BODY_CL;
122 *body_len = cl;
123 *value = word;
124 word.ptr = n;
125 }
126 /* here we've reached the end with a single value or a series of
127 * identical values, all matching previous series if any. The last
128 * parsed value was sent back into <value>. We just have to decide
129 * if this occurrence has to be indexed (it's the first one) or
130 * silently skipped (it's not the first one)
131 */
132 return !not_first;
133 fail:
134 return -1;
135}
136
Willy Tarreau6deb4122018-11-27 15:34:18 +0100137/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
138 * <fields> indicates what was found so far. This should be called once at the
139 * detection of the first general header field or at the end of the request if
140 * no general header field was found yet. Returns the created start line on
141 * success, or NULL on failure. Upon success, <msgf> is updated with a few
142 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200143 *
144 * The rules below deserve a bit of explanation. There tends to be some
145 * confusion regarding H2's authority vs the Host header. They are different
146 * though may sometimes be exchanged. In H2, the request line is broken into :
147 * - :method
148 * - :scheme
149 * - :authority
150 * - :path
151 *
152 * An equivalent HTTP/1.x absolute-form request would then look like :
153 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
154 *
155 * Except for CONNECT which doesn't have scheme nor path and looks like :
156 * <:method> <:authority> HTTP/x.y
157 *
158 * It's worth noting that H2 still supports an encoding to map H1 origin-form
159 * and asterisk-form requests. These ones do not specify the authority. However
160 * in H2 they must still specify the scheme, which is not present in H1. Also,
161 * when encoding an absolute-form H1 request without a path, the path
162 * automatically becomes "/" except for the OPTIONS method where it
163 * becomes "*".
164 *
165 * As such it is explicitly permitted for an H2 client to send a request
166 * featuring a Host header and no :authority, though it's not the recommended
167 * way to use H2 for a client. It is however the only permitted way to encode
168 * an origin-form H1 request over H2. Thus we need to respect such differences
169 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100170 */
171static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
172{
Willy Tarreau92919f72019-10-08 16:53:07 +0200173 struct ist uri;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100174 unsigned int flags = HTX_SL_F_NONE;
175 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100176 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100177
178 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
179 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
180 * MUST be omitted ; ":authority" contains the host and port
181 * to connect to.
182 */
183 if (fields & H2_PHDR_FND_SCHM) {
184 /* scheme not allowed */
185 goto fail;
186 }
187 else if (fields & H2_PHDR_FND_PATH) {
188 /* path not allowed */
189 goto fail;
190 }
191 else if (!(fields & H2_PHDR_FND_AUTH)) {
192 /* missing authority */
193 goto fail;
194 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100195 *msgf |= H2_MSGF_BODY_TUNNEL;
196 }
197 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
198 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
199 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
200 * valid value for the ":method", ":scheme" and ":path" phdr
201 * unless it is a CONNECT request.
202 */
203 if (!(fields & H2_PHDR_FND_METH)) {
204 /* missing method */
205 goto fail;
206 }
207 else if (!(fields & H2_PHDR_FND_SCHM)) {
208 /* missing scheme */
209 goto fail;
210 }
211 else {
212 /* missing path */
213 goto fail;
214 }
215 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200216 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200217 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
218 * classify the scheme as "present/http", "present/https",
219 * "present/other", "absent" so as to decide whether or not
220 * we're facing a normalized URI that will have to be encoded
221 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
222 * clients should use the absolute form, thus we cannot infer
223 * whether or not the client wanted to use a proxy here.
224 */
225 flags |= HTX_SL_F_HAS_SCHM;
226 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
227 flags |= HTX_SL_F_SCHM_HTTP;
228 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
229 flags |= HTX_SL_F_SCHM_HTTPS;
230 }
231
232 if (!(flags & HTX_SL_F_HAS_SCHM)) {
233 /* no scheme, use authority only (CONNECT) */
234 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200235 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200236 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200237 else if (fields & H2_PHDR_FND_AUTH) {
238 /* authority is present, let's use the absolute form. We simply
239 * use the trash to concatenate them since all of them MUST fit
240 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200241 */
242 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
243 istcat(&uri, ist("://"), trash.size);
244 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
245 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
246 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200247 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200248
249 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
250 /* we don't know if it was originally an absolute or a
251 * relative request because newer versions of HTTP use
252 * the absolute URI format by default, which we call
253 * the normalized URI format internally. This is the
254 * strongly recommended way of sending a request for
255 * a regular client, so we cannot distinguish this
256 * from a request intended for a proxy. For other
257 * schemes however there is no doubt.
258 */
259 flags |= HTX_SL_F_NORMALIZED_URI;
260 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200261 }
262 else {
263 /* usual schemes with or without authority, use origin form */
264 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200265 if (fields & H2_PHDR_FND_AUTH)
266 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200267 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100268
Willy Tarreau2be362c2019-10-08 11:59:37 +0200269 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
270 * that :path must not be empty.
271 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200272 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100273 goto fail;
274
Willy Tarreau2be362c2019-10-08 11:59:37 +0200275 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200276 for (i = 0; i < uri.len; i++) {
277 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100278 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
279 htx->flags |= HTX_FL_PARSING_ERROR;
280 }
281
Willy Tarreau6deb4122018-11-27 15:34:18 +0100282 /* Set HTX start-line flags */
283 flags |= HTX_SL_F_VER_11; // V2 in fact
284 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
285
Willy Tarreau92919f72019-10-08 16:53:07 +0200286 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100287 if (!sl)
288 goto fail;
289
290 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
291 return sl;
292 fail:
293 return NULL;
294}
295
296/* Takes an H2 request present in the headers list <list> terminated by a name
297 * being <NULL,0> and emits the equivalent HTX request according to the rules
298 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
299 * non-zero is returned if some bytes were emitted. In case of error, a
300 * negative error code is returned.
301 *
302 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
303 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
304 * if a body is detected (!ES).
305 *
306 * The headers list <list> must be composed of :
307 * - n.name != NULL, n.len > 0 : literal header name
308 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
309 * among H2_PHDR_IDX_*
310 * - n.name ignored, n.len == 0 : end of list
311 * - in all cases except the end of list, v.name and v.len must designate a
312 * valid value.
313 *
314 * The Cookie header will be reassembled at the end, and for this, the <list>
315 * will be used to create a linked list, so its contents may be destroyed.
316 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100317int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100318{
319 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
320 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
321 uint32_t idx;
322 int ck, lck; /* cookie index and last cookie index */
323 int phdr;
324 int ret;
325 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200326 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100327 struct htx_sl *sl = NULL;
328 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100329 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100330
331 lck = ck = -1; // no cookie for now
332 fields = 0;
333 for (idx = 0; list[idx].n.len != 0; idx++) {
334 if (!list[idx].n.ptr) {
335 /* this is an indexed pseudo-header */
336 phdr = list[idx].n.len;
337 }
338 else {
339 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100340 /* RFC7540#8.1.2: upper case not allowed in header field names.
341 * #10.3: header names must be valid (i.e. match a token).
342 * For pseudo-headers we check from 2nd char and for other ones
343 * from the first char, because HTTP_IS_TOKEN() also excludes
344 * the colon.
345 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100346 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100347
348 for (i = !!phdr; i < list[idx].n.len; i++)
349 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
350 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100351 }
352
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100353 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
354 * rejecting NUL, CR and LF characters.
355 */
356 ctl = ist_find_ctl(list[idx].v);
357 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
358 goto fail;
359
Willy Tarreau6deb4122018-11-27 15:34:18 +0100360 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
361 /* insert a pseudo header by its index (in phdr) and value (in value) */
362 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
363 if (fields & H2_PHDR_FND_NONE) {
364 /* pseudo header field after regular headers */
365 goto fail;
366 }
367 else {
368 /* repeated pseudo header field */
369 goto fail;
370 }
371 }
372 fields |= 1 << phdr;
373 phdr_val[phdr] = list[idx].v;
374 continue;
375 }
376 else if (phdr != 0) {
377 /* invalid pseudo header -- should never happen here */
378 goto fail;
379 }
380
381 /* regular header field in (name,value) */
382 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
383 /* no more pseudo-headers, time to build the request line */
384 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
385 if (!sl)
386 goto fail;
387 fields |= H2_PHDR_FND_NONE;
388 }
389
390 if (isteq(list[idx].n, ist("host")))
391 fields |= H2_PHDR_FND_HOST;
392
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100393 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100394 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100395 if (ret < 0)
396 goto fail;
397
Willy Tarreau6deb4122018-11-27 15:34:18 +0100398 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100399 if (ret == 0)
400 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100401 }
402
403 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
404 if (isteq(list[idx].n, ist("connection")) ||
405 isteq(list[idx].n, ist("proxy-connection")) ||
406 isteq(list[idx].n, ist("keep-alive")) ||
407 isteq(list[idx].n, ist("upgrade")) ||
408 isteq(list[idx].n, ist("transfer-encoding")))
409 goto fail;
410
411 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
412 goto fail;
413
414 /* cookie requires special processing at the end */
415 if (isteq(list[idx].n, ist("cookie"))) {
416 list[idx].n.len = -1;
417
418 if (ck < 0)
419 ck = idx;
420 else
421 list[lck].n.len = idx;
422
423 lck = idx;
424 continue;
425 }
426
427 if (!htx_add_header(htx, list[idx].n, list[idx].v))
428 goto fail;
429 }
430
431 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
432 if (fields & H2_PHDR_FND_STAT)
433 goto fail;
434
435 /* Let's dump the request now if not yet emitted. */
436 if (!(fields & H2_PHDR_FND_NONE)) {
437 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
438 if (!sl)
439 goto fail;
440 }
441
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100442 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
443 sl_flags |= HTX_SL_F_BODYLESS;
444
Willy Tarreau6deb4122018-11-27 15:34:18 +0100445 /* update the start line with last detected header info */
446 sl->flags |= sl_flags;
447
448 /* complete with missing Host if needed */
449 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
450 /* missing Host field, use :authority instead */
451 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
452 goto fail;
453 }
454
455 /* now we may have to build a cookie list. We'll dump the values of all
456 * visited headers.
457 */
458 if (ck >= 0) {
459 uint32_t fs; // free space
460 uint32_t bs; // block size
461 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100462 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100463 struct htx_blk *blk;
464
465 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
466 if (!blk)
467 goto fail;
468
Willy Tarreau164e0612018-12-18 11:00:41 +0100469 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100470 fs = htx_free_data_space(htx);
471 bs = htx_get_blksz(blk);
472
473 /* for each extra cookie, we'll extend the cookie's value and
474 * insert "; " before the new value.
475 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100476 fs += tl; // first one is already counted
477 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100478 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100479 tl += vl + 2;
480 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100481 goto fail;
482
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200483 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100484 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
485 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
486 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
487 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100488 }
489
490 }
491
492 /* now send the end of headers marker */
493 htx_add_endof(htx, HTX_BLK_EOH);
494
Christopher Faulet33543e72019-05-15 15:53:20 +0200495 /* Set bytes used in the HTX mesage for the headers now */
496 sl->hdrs_bytes = htx_used_space(htx) - used;
497
Willy Tarreau6deb4122018-11-27 15:34:18 +0100498 ret = 1;
499 return ret;
500
501 fail:
502 return -1;
503}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200504
505/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
506 * <fields> indicates what was found so far. This should be called once at the
507 * detection of the first general header field or at the end of the message if
508 * no general header field was found yet. Returns the created start line on
509 * success, or NULL on failure. Upon success, <msgf> is updated with a few
510 * H2_MSGF_* flags indicating what was found while parsing.
511 */
512static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
513{
514 unsigned int flags = HTX_SL_F_NONE;
515 struct htx_sl *sl;
516 unsigned char h, t, u;
517
518 /* only :status is allowed as a pseudo header */
519 if (!(fields & H2_PHDR_FND_STAT))
520 goto fail;
521
522 if (phdr[H2_PHDR_IDX_STAT].len != 3)
523 goto fail;
524
525 /* Set HTX start-line flags */
526 flags |= HTX_SL_F_VER_11; // V2 in fact
527 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
528
529 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
530 if (!sl)
531 goto fail;
532
533 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
534 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
535 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
536 if (h > 9 || t > 9 || u > 9)
537 goto fail;
538
539 sl->info.res.status = h * 100 + t * 10 + u;
540
Christopher Faulet0b465482019-02-19 15:14:23 +0100541 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
542 * there is no body. So remove the flag H2_MSGF_BODY and add
543 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
544 * expected.
545 */
546 if (sl->info.res.status < 200 &&
547 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
548 *msgf |= H2_MSGF_RSP_1XX;
549 *msgf &= ~H2_MSGF_BODY;
550 }
551
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200552 return sl;
553 fail:
554 return NULL;
555}
556
557/* Takes an H2 response present in the headers list <list> terminated by a name
558 * being <NULL,0> and emits the equivalent HTX response according to the rules
559 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
560 * a positive value is returned if some bytes were emitted. In case of error, a
561 * negative error code is returned.
562 *
563 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
564 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
565 * if a body is detected (!ES).
566 *
567 * The headers list <list> must be composed of :
568 * - n.name != NULL, n.len > 0 : literal header name
569 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
570 * among H2_PHDR_IDX_*
571 * - n.name ignored, n.len == 0 : end of list
572 * - in all cases except the end of list, v.name and v.len must designate a
573 * valid value.
574 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100575int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len)
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200576{
577 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
578 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
579 uint32_t idx;
580 int phdr;
581 int ret;
582 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200583 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200584 struct htx_sl *sl = NULL;
585 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100586 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200587
588 fields = 0;
589 for (idx = 0; list[idx].n.len != 0; idx++) {
590 if (!list[idx].n.ptr) {
591 /* this is an indexed pseudo-header */
592 phdr = list[idx].n.len;
593 }
594 else {
595 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100596 /* RFC7540#8.1.2: upper case not allowed in header field names.
597 * #10.3: header names must be valid (i.e. match a token).
598 * For pseudo-headers we check from 2nd char and for other ones
599 * from the first char, because HTTP_IS_TOKEN() also excludes
600 * the colon.
601 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200602 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100603
604 for (i = !!phdr; i < list[idx].n.len; i++)
605 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
606 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200607 }
608
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100609 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
610 * rejecting NUL, CR and LF characters.
611 */
612 ctl = ist_find_ctl(list[idx].v);
613 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
614 goto fail;
615
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200616 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
617 /* insert a pseudo header by its index (in phdr) and value (in value) */
618 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
619 if (fields & H2_PHDR_FND_NONE) {
620 /* pseudo header field after regular headers */
621 goto fail;
622 }
623 else {
624 /* repeated pseudo header field */
625 goto fail;
626 }
627 }
628 fields |= 1 << phdr;
629 phdr_val[phdr] = list[idx].v;
630 continue;
631 }
632 else if (phdr != 0) {
633 /* invalid pseudo header -- should never happen here */
634 goto fail;
635 }
636
637 /* regular header field in (name,value) */
638 if (!(fields & H2_PHDR_FND_NONE)) {
639 /* no more pseudo-headers, time to build the status line */
640 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
641 if (!sl)
642 goto fail;
643 fields |= H2_PHDR_FND_NONE;
644 }
645
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100646 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100647 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100648 if (ret < 0)
649 goto fail;
650
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200651 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100652 if (ret == 0)
653 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200654 }
655
656 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
657 if (isteq(list[idx].n, ist("connection")) ||
658 isteq(list[idx].n, ist("proxy-connection")) ||
659 isteq(list[idx].n, ist("keep-alive")) ||
660 isteq(list[idx].n, ist("upgrade")) ||
661 isteq(list[idx].n, ist("transfer-encoding")))
662 goto fail;
663
664 if (!htx_add_header(htx, list[idx].n, list[idx].v))
665 goto fail;
666 }
667
668 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
669 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
670 goto fail;
671
672 /* Let's dump the request now if not yet emitted. */
673 if (!(fields & H2_PHDR_FND_NONE)) {
674 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
675 if (!sl)
676 goto fail;
677 }
678
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100679 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
680 sl_flags |= HTX_SL_F_BODYLESS;
681
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200682 /* update the start line with last detected header info */
683 sl->flags |= sl_flags;
684
685 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
686 /* FIXME: Do we need to signal anything when we have a body and
687 * no content-length, to have the equivalent of H1's chunked
688 * encoding?
689 */
690 }
691
692 /* now send the end of headers marker */
693 htx_add_endof(htx, HTX_BLK_EOH);
694
Christopher Faulet33543e72019-05-15 15:53:20 +0200695 /* Set bytes used in the HTX mesage for the headers now */
696 sl->hdrs_bytes = htx_used_space(htx) - used;
697
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200698 ret = 1;
699 return ret;
700
701 fail:
702 return -1;
703}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100704
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200705/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
706 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
707 * and a positive value is returned if some bytes were emitted. In case of
708 * error, a negative error code is returned. The caller must have verified that
709 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100710 *
711 * The headers list <list> must be composed of :
712 * - n.name != NULL, n.len > 0 : literal header name
713 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
714 * among H2_PHDR_IDX_* (illegal here)
715 * - n.name ignored, n.len == 0 : end of list
716 * - in all cases except the end of list, v.name and v.len must designate a
717 * valid value.
718 */
719int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
720{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100721 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100722 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100723 int i;
724
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100725 for (idx = 0; list[idx].n.len != 0; idx++) {
726 if (!list[idx].n.ptr) {
727 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
728 goto fail;
729 }
730
Willy Tarreau146f53a2019-11-24 10:34:39 +0100731 /* RFC7540#8.1.2: upper case not allowed in header field names.
732 * #10.3: header names must be valid (i.e. match a token). This
733 * also catches pseudo-headers which are forbidden in trailers.
734 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100735 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100736 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100737 goto fail;
738
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100739 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
740 if (isteq(list[idx].n, ist("host")) ||
741 isteq(list[idx].n, ist("content-length")) ||
742 isteq(list[idx].n, ist("connection")) ||
743 isteq(list[idx].n, ist("proxy-connection")) ||
744 isteq(list[idx].n, ist("keep-alive")) ||
745 isteq(list[idx].n, ist("upgrade")) ||
746 isteq(list[idx].n, ist("te")) ||
747 isteq(list[idx].n, ist("transfer-encoding")))
748 goto fail;
749
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100750 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
751 * rejecting NUL, CR and LF characters.
752 */
753 ctl = ist_find_ctl(list[idx].v);
754 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
755 goto fail;
756
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200757 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
758 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100759 }
760
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200761 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100762 goto fail;
763
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100764 return 1;
765
766 fail:
767 return -1;
768}