blob: 1a103e49e52b864d00590705da0f051725eba844 [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
28#include <stdint.h>
29#include <common/config.h>
30#include <common/h2.h>
31#include <common/http-hdr.h>
32#include <common/ist.h>
33
34
35/* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers
36 * stored in <phdr[]>. <fields> indicates what was found so far. This should be
37 * called once at the detection of the first general header field or at the end
38 * of the request if no general header field was found yet. Returns 0 on success
Willy Tarreau174b06a2018-04-25 18:13:58 +020039 * or a negative error code on failure. Upon success, <msgf> is updated with a
40 * few H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010041 */
Willy Tarreau174b06a2018-04-25 18:13:58 +020042static int h2_prepare_h1_reqline(uint32_t fields, struct ist *phdr, char **ptr, char *end, unsigned int *msgf)
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010043{
44 char *out = *ptr;
45 int uri_idx = H2_PHDR_IDX_PATH;
46
47 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
48 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
49 * MUST be omitted ; ":authority" contains the host and port
50 * to connect to.
51 */
52 if (fields & H2_PHDR_FND_SCHM) {
53 /* scheme not allowed */
54 goto fail;
55 }
56 else if (fields & H2_PHDR_FND_PATH) {
57 /* path not allowed */
58 goto fail;
59 }
60 else if (!(fields & H2_PHDR_FND_AUTH)) {
61 /* missing authority */
62 goto fail;
63 }
64 // otherwise OK ; let's use the authority instead of the URI
65 uri_idx = H2_PHDR_IDX_AUTH;
Willy Tarreau174b06a2018-04-25 18:13:58 +020066 *msgf |= H2_MSGF_BODY_TUNNEL;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010067 }
68 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
69 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
70 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
71 * valid value for the ":method", ":scheme" and ":path" phdr
72 * unless it is a CONNECT request.
73 */
74 if (!(fields & H2_PHDR_FND_METH)) {
75 /* missing method */
76 goto fail;
77 }
78 else if (!(fields & H2_PHDR_FND_SCHM)) {
79 /* missing scheme */
80 goto fail;
81 }
82 else {
83 /* missing path */
84 goto fail;
85 }
86 }
87
Willy Tarreaucd4fe172017-12-03 11:51:31 +010088 /* 7540#8.1.2.3: :path must not be empty */
89 if (!phdr[uri_idx].len)
90 goto fail;
91
Willy Tarreau811ad122017-12-03 09:44:50 +010092 if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) {
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010093 /* too large */
94 goto fail;
95 }
96
97 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
98 out += phdr[H2_PHDR_IDX_METH].len;
99 *(out++) = ' ';
100
101 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
102 out += phdr[uri_idx].len;
103 memcpy(out, " HTTP/1.1\r\n", 11);
104 out += 11;
105
106 *ptr = out;
107 return 0;
108 fail:
109 return -1;
110}
111
112/* Takes an H2 request present in the headers list <list> terminated by a name
113 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
114 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
115 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
116 * case of error, a negative error code is returned.
117 *
Willy Tarreau174b06a2018-04-25 18:13:58 +0200118 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
119 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
120 * if a body is detected (!ES).
121 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100122 * The headers list <list> must be composed of :
123 * - n.name != NULL, n.len > 0 : literal header name
124 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
125 * among H2_PHDR_IDX_*
126 * - n.name ignored, n.len == 0 : end of list
127 * - in all cases except the end of list, v.name and v.len must designate a
128 * valid value.
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100129 *
130 * The Cookie header will be reassembled at the end, and for this, the <list>
131 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100132 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100133int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf, unsigned long long *body_len)
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100134{
135 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
136 char *out_end = out + osize;
137 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
138 uint32_t idx;
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100139 int ck, lck; /* cookie index and last cookie index */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100140 int phdr;
141 int ret;
Willy Tarreau637f64d2017-12-03 20:28:13 +0100142 int i;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100143
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100144 lck = ck = -1; // no cookie for now
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100145 fields = 0;
146 for (idx = 0; list[idx].n.len != 0; idx++) {
147 if (!list[idx].n.ptr) {
148 /* this is an indexed pseudo-header */
149 phdr = list[idx].n.len;
150 }
151 else {
152 /* this can be any type of header */
Willy Tarreau637f64d2017-12-03 20:28:13 +0100153 /* RFC7540#8.1.2: upper case not allowed in header field names */
154 for (i = 0; i < list[idx].n.len; i++)
155 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
156 goto fail;
157
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100158 phdr = h2_str_to_phdr(list[idx].n);
159 }
160
161 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
162 /* insert a pseudo header by its index (in phdr) and value (in value) */
163 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
164 if (fields & H2_PHDR_FND_NONE) {
165 /* pseudo header field after regular headers */
166 goto fail;
167 }
168 else {
169 /* repeated pseudo header field */
170 goto fail;
171 }
172 }
173 fields |= 1 << phdr;
174 phdr_val[phdr] = list[idx].v;
175 continue;
176 }
177 else if (phdr != 0) {
178 /* invalid pseudo header -- should never happen here */
179 goto fail;
180 }
181
182 /* regular header field in (name,value) */
183 if (!(fields & H2_PHDR_FND_NONE)) {
184 /* no more pseudo-headers, time to build the request line */
Willy Tarreau174b06a2018-04-25 18:13:58 +0200185 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100186 if (ret != 0)
187 goto leave;
188 fields |= H2_PHDR_FND_NONE;
189 }
190
191 if (isteq(list[idx].n, ist("host")))
192 fields |= H2_PHDR_FND_HOST;
193
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100194 if (isteq(list[idx].n, ist("content-length"))) {
195 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
196 if (ret < 0)
197 goto fail;
198
199 if (ret == 0)
200 continue; // skip this duplicate
201 }
Willy Tarreau174b06a2018-04-25 18:13:58 +0200202
Willy Tarreaufe7c3562017-12-03 20:15:34 +0100203 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
204 if (isteq(list[idx].n, ist("connection")) ||
205 isteq(list[idx].n, ist("proxy-connection")) ||
206 isteq(list[idx].n, ist("keep-alive")) ||
207 isteq(list[idx].n, ist("upgrade")) ||
208 isteq(list[idx].n, ist("transfer-encoding")))
209 goto fail;
210
Willy Tarreaud8d2ac72017-12-03 18:41:31 +0100211 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
212 goto fail;
213
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100214 /* cookie requires special processing at the end */
215 if (isteq(list[idx].n, ist("cookie"))) {
216 list[idx].n.len = -1;
217
218 if (ck < 0)
219 ck = idx;
220 else
221 list[lck].n.len = idx;
222
223 lck = idx;
224 continue;
225 }
226
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100227 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
228 /* too large */
229 goto fail;
230 }
231
232 /* copy "name: value" */
233 memcpy(out, list[idx].n.ptr, list[idx].n.len);
234 out += list[idx].n.len;
235 *(out++) = ':';
236 *(out++) = ' ';
237
238 memcpy(out, list[idx].v.ptr, list[idx].v.len);
239 out += list[idx].v.len;
240 *(out++) = '\r';
241 *(out++) = '\n';
242 }
243
Willy Tarreau52088692017-12-03 20:13:54 +0100244 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
245 if (fields & H2_PHDR_FND_STAT)
246 goto fail;
247
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100248 /* Let's dump the request now if not yet emitted. */
249 if (!(fields & H2_PHDR_FND_NONE)) {
Willy Tarreau174b06a2018-04-25 18:13:58 +0200250 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100251 if (ret != 0)
252 goto leave;
253 }
254
255 /* complete with missing Host if needed */
256 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
257 /* missing Host field, use :authority instead */
258 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
259 /* too large */
260 goto fail;
261 }
262
263 memcpy(out, "host: ", 6);
264 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
265 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
266 *(out++) = '\r';
267 *(out++) = '\n';
268 }
269
Willy Tarreaueba10f22018-04-25 20:44:22 +0200270 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
271 /* add chunked encoding */
272 if (out + 28 > out_end)
273 goto fail;
274 memcpy(out, "transfer-encoding: chunked\r\n", 28);
275 out += 28;
276 }
277
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100278 /* now we may have to build a cookie list. We'll dump the values of all
279 * visited headers.
280 */
281 if (ck >= 0) {
282 if (out + 8 > out_end) {
283 /* too large */
284 goto fail;
285 }
286 memcpy(out, "cookie: ", 8);
287 out += 8;
288
289 do {
290 if (out + list[ck].v.len + 2 > out_end) {
291 /* too large */
292 goto fail;
293 }
294 memcpy(out, list[ck].v.ptr, list[ck].v.len);
295 out += list[ck].v.len;
296 ck = list[ck].n.len;
297
298 if (ck >= 0) {
299 *(out++) = ';';
300 *(out++) = ' ';
301 }
302 } while (ck >= 0);
303
304 if (out + 2 > out_end) {
305 /* too large */
306 goto fail;
307 }
308 *(out++) = '\r';
309 *(out++) = '\n';
310 }
311
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100312 /* And finish */
313 if (out + 2 > out_end) {
314 /* too large */
315 goto fail;
316 }
317
318 *(out++) = '\r';
319 *(out++) = '\n';
320 ret = out + osize - out_end;
321 leave:
322 return ret;
323
324 fail:
325 return -1;
326}
Willy Tarreau6deb4122018-11-27 15:34:18 +0100327
Willy Tarreau9d953e72019-01-03 16:18:14 +0100328/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
329 * emits the equivalent HTTP/1.1 trailers block not including the empty line.
330 * The output contents are emitted in <out> for a max of <osize> bytes, and the
331 * amount of bytes emitted is returned. In case of error, a negative error code
332 * is returned. The caller must have verified that the message in the buffer is
333 * compatible with receipt of trailers.
334 *
335 * The headers list <list> must be composed of :
336 * - n.name != NULL, n.len > 0 : literal header name
337 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
338 * among H2_PHDR_IDX_* (illegal here)
339 * - n.name ignored, n.len == 0 : end of list
340 * - in all cases except the end of list, v.name and v.len must designate a
341 * valid value.
342 */
343int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize)
344{
345 char *out_end = out + osize;
346 uint32_t idx;
347 int i;
348
349 for (idx = 0; list[idx].n.len != 0; idx++) {
350 if (!list[idx].n.ptr) {
351 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
352 goto fail;
353 }
354
355 /* RFC7540#8.1.2: upper case not allowed in header field names */
356 for (i = 0; i < list[idx].n.len; i++)
357 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
358 goto fail;
359
360 if (h2_str_to_phdr(list[idx].n) != 0) {
361 /* This is a pseudo-header (RFC7540#8.1.2.1) */
362 goto fail;
363 }
364
365 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
366 if (isteq(list[idx].n, ist("host")) ||
367 isteq(list[idx].n, ist("content-length")) ||
368 isteq(list[idx].n, ist("connection")) ||
369 isteq(list[idx].n, ist("proxy-connection")) ||
370 isteq(list[idx].n, ist("keep-alive")) ||
371 isteq(list[idx].n, ist("upgrade")) ||
372 isteq(list[idx].n, ist("te")) ||
373 isteq(list[idx].n, ist("transfer-encoding")))
374 goto fail;
375
376 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
377 /* too large */
378 goto fail;
379 }
380
381 /* copy "name: value" */
382 memcpy(out, list[idx].n.ptr, list[idx].n.len);
383 out += list[idx].n.len;
384 *(out++) = ':';
385 *(out++) = ' ';
386
387 memcpy(out, list[idx].v.ptr, list[idx].v.len);
388 out += list[idx].v.len;
389 *(out++) = '\r';
390 *(out++) = '\n';
391 }
392
393 return out + osize - out_end;
394
395 fail:
396 return -1;
397}
398
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100399/* Parse the Content-Length header field of an HTTP/2 request. The function
400 * checks all possible occurrences of a comma-delimited value, and verifies
401 * if any of them doesn't match a previous value. It returns <0 if a value
402 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
403 * if the value can be indexed (first one). In the last case, the value might
404 * be adjusted and the caller must only add the updated value.
405 */
406int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
407{
408 char *e, *n;
409 unsigned long long cl;
410 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
411 struct ist word;
412
413 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
414 e = value->ptr + value->len;
415
416 while (++word.ptr < e) {
417 /* skip leading delimitor and blanks */
418 if (unlikely(HTTP_IS_LWS(*word.ptr)))
419 continue;
420
421 /* digits only now */
422 for (cl = 0, n = word.ptr; n < e; n++) {
423 unsigned int c = *n - '0';
424 if (unlikely(c > 9)) {
425 /* non-digit */
426 if (unlikely(n == word.ptr)) // spaces only
427 goto fail;
428 break;
429 }
430 if (unlikely(cl > ULLONG_MAX / 10ULL))
431 goto fail; /* multiply overflow */
432 cl = cl * 10ULL;
433 if (unlikely(cl + c < cl))
434 goto fail; /* addition overflow */
435 cl = cl + c;
436 }
437
438 /* keep a copy of the exact cleaned value */
439 word.len = n - word.ptr;
440
441 /* skip trailing LWS till next comma or EOL */
442 for (; n < e; n++) {
443 if (!HTTP_IS_LWS(*n)) {
444 if (unlikely(*n != ','))
445 goto fail;
446 break;
447 }
448 }
449
450 /* if duplicate, must be equal */
451 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
452 goto fail;
453
454 /* OK, store this result as the one to be indexed */
455 *msgf |= H2_MSGF_BODY_CL;
456 *body_len = cl;
457 *value = word;
458 word.ptr = n;
459 }
460 /* here we've reached the end with a single value or a series of
461 * identical values, all matching previous series if any. The last
462 * parsed value was sent back into <value>. We just have to decide
463 * if this occurrence has to be indexed (it's the first one) or
464 * silently skipped (it's not the first one)
465 */
466 return !not_first;
467 fail:
468 return -1;
469}
470
Willy Tarreau6deb4122018-11-27 15:34:18 +0100471/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
472 * <fields> indicates what was found so far. This should be called once at the
473 * detection of the first general header field or at the end of the request if
474 * no general header field was found yet. Returns the created start line on
475 * success, or NULL on failure. Upon success, <msgf> is updated with a few
476 * H2_MSGF_* flags indicating what was found while parsing.
477 */
478static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
479{
480 int uri_idx = H2_PHDR_IDX_PATH;
481 unsigned int flags = HTX_SL_F_NONE;
482 struct htx_sl *sl;
483
484 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
485 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
486 * MUST be omitted ; ":authority" contains the host and port
487 * to connect to.
488 */
489 if (fields & H2_PHDR_FND_SCHM) {
490 /* scheme not allowed */
491 goto fail;
492 }
493 else if (fields & H2_PHDR_FND_PATH) {
494 /* path not allowed */
495 goto fail;
496 }
497 else if (!(fields & H2_PHDR_FND_AUTH)) {
498 /* missing authority */
499 goto fail;
500 }
501 // otherwise OK ; let's use the authority instead of the URI
502 uri_idx = H2_PHDR_IDX_AUTH;
503 *msgf |= H2_MSGF_BODY_TUNNEL;
504 }
505 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
506 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
507 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
508 * valid value for the ":method", ":scheme" and ":path" phdr
509 * unless it is a CONNECT request.
510 */
511 if (!(fields & H2_PHDR_FND_METH)) {
512 /* missing method */
513 goto fail;
514 }
515 else if (!(fields & H2_PHDR_FND_SCHM)) {
516 /* missing scheme */
517 goto fail;
518 }
519 else {
520 /* missing path */
521 goto fail;
522 }
523 }
524
525 /* 7540#8.1.2.3: :path must not be empty */
526 if (!phdr[uri_idx].len)
527 goto fail;
528
529 /* Set HTX start-line flags */
530 flags |= HTX_SL_F_VER_11; // V2 in fact
531 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
532
533 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
534 if (!sl)
535 goto fail;
536
537 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
538 return sl;
539 fail:
540 return NULL;
541}
542
543/* Takes an H2 request present in the headers list <list> terminated by a name
544 * being <NULL,0> and emits the equivalent HTX request according to the rules
545 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
546 * non-zero is returned if some bytes were emitted. In case of error, a
547 * negative error code is returned.
548 *
549 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
550 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
551 * if a body is detected (!ES).
552 *
553 * The headers list <list> must be composed of :
554 * - n.name != NULL, n.len > 0 : literal header name
555 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
556 * among H2_PHDR_IDX_*
557 * - n.name ignored, n.len == 0 : end of list
558 * - in all cases except the end of list, v.name and v.len must designate a
559 * valid value.
560 *
561 * The Cookie header will be reassembled at the end, and for this, the <list>
562 * will be used to create a linked list, so its contents may be destroyed.
563 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100564int 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 +0100565{
566 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
567 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
568 uint32_t idx;
569 int ck, lck; /* cookie index and last cookie index */
570 int phdr;
571 int ret;
572 int i;
573 struct htx_sl *sl = NULL;
574 unsigned int sl_flags = 0;
575
576 lck = ck = -1; // no cookie for now
577 fields = 0;
578 for (idx = 0; list[idx].n.len != 0; idx++) {
579 if (!list[idx].n.ptr) {
580 /* this is an indexed pseudo-header */
581 phdr = list[idx].n.len;
582 }
583 else {
584 /* this can be any type of header */
585 /* RFC7540#8.1.2: upper case not allowed in header field names */
586 for (i = 0; i < list[idx].n.len; i++)
587 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
588 goto fail;
589
590 phdr = h2_str_to_phdr(list[idx].n);
591 }
592
593 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
594 /* insert a pseudo header by its index (in phdr) and value (in value) */
595 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
596 if (fields & H2_PHDR_FND_NONE) {
597 /* pseudo header field after regular headers */
598 goto fail;
599 }
600 else {
601 /* repeated pseudo header field */
602 goto fail;
603 }
604 }
605 fields |= 1 << phdr;
606 phdr_val[phdr] = list[idx].v;
607 continue;
608 }
609 else if (phdr != 0) {
610 /* invalid pseudo header -- should never happen here */
611 goto fail;
612 }
613
614 /* regular header field in (name,value) */
615 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
616 /* no more pseudo-headers, time to build the request line */
617 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
618 if (!sl)
619 goto fail;
620 fields |= H2_PHDR_FND_NONE;
621 }
622
623 if (isteq(list[idx].n, ist("host")))
624 fields |= H2_PHDR_FND_HOST;
625
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100626 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100627 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100628 if (ret < 0)
629 goto fail;
630
Willy Tarreau6deb4122018-11-27 15:34:18 +0100631 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100632 if (ret == 0)
633 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100634 }
635
636 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
637 if (isteq(list[idx].n, ist("connection")) ||
638 isteq(list[idx].n, ist("proxy-connection")) ||
639 isteq(list[idx].n, ist("keep-alive")) ||
640 isteq(list[idx].n, ist("upgrade")) ||
641 isteq(list[idx].n, ist("transfer-encoding")))
642 goto fail;
643
644 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
645 goto fail;
646
647 /* cookie requires special processing at the end */
648 if (isteq(list[idx].n, ist("cookie"))) {
649 list[idx].n.len = -1;
650
651 if (ck < 0)
652 ck = idx;
653 else
654 list[lck].n.len = idx;
655
656 lck = idx;
657 continue;
658 }
659
660 if (!htx_add_header(htx, list[idx].n, list[idx].v))
661 goto fail;
662 }
663
664 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
665 if (fields & H2_PHDR_FND_STAT)
666 goto fail;
667
668 /* Let's dump the request now if not yet emitted. */
669 if (!(fields & H2_PHDR_FND_NONE)) {
670 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
671 if (!sl)
672 goto fail;
673 }
674
675 /* update the start line with last detected header info */
676 sl->flags |= sl_flags;
677
678 /* complete with missing Host if needed */
679 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
680 /* missing Host field, use :authority instead */
681 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
682 goto fail;
683 }
684
685 /* now we may have to build a cookie list. We'll dump the values of all
686 * visited headers.
687 */
688 if (ck >= 0) {
689 uint32_t fs; // free space
690 uint32_t bs; // block size
691 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100692 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100693 struct htx_blk *blk;
694
695 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
696 if (!blk)
697 goto fail;
698
Willy Tarreau164e0612018-12-18 11:00:41 +0100699 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100700 fs = htx_free_data_space(htx);
701 bs = htx_get_blksz(blk);
702
703 /* for each extra cookie, we'll extend the cookie's value and
704 * insert "; " before the new value.
705 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100706 fs += tl; // first one is already counted
707 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100708 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100709 tl += vl + 2;
710 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100711 goto fail;
712
Willy Tarreau164e0612018-12-18 11:00:41 +0100713 htx_set_blk_value_len(blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100714 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
715 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
716 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
717 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100718 }
719
720 }
721
722 /* now send the end of headers marker */
723 htx_add_endof(htx, HTX_BLK_EOH);
724
725 ret = 1;
726 return ret;
727
728 fail:
729 return -1;
730}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200731
732/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
733 * <fields> indicates what was found so far. This should be called once at the
734 * detection of the first general header field or at the end of the message if
735 * no general header field was found yet. Returns the created start line on
736 * success, or NULL on failure. Upon success, <msgf> is updated with a few
737 * H2_MSGF_* flags indicating what was found while parsing.
738 */
739static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
740{
741 unsigned int flags = HTX_SL_F_NONE;
742 struct htx_sl *sl;
743 unsigned char h, t, u;
744
745 /* only :status is allowed as a pseudo header */
746 if (!(fields & H2_PHDR_FND_STAT))
747 goto fail;
748
749 if (phdr[H2_PHDR_IDX_STAT].len != 3)
750 goto fail;
751
752 /* Set HTX start-line flags */
753 flags |= HTX_SL_F_VER_11; // V2 in fact
754 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
755
756 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
757 if (!sl)
758 goto fail;
759
760 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
761 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
762 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
763 if (h > 9 || t > 9 || u > 9)
764 goto fail;
765
766 sl->info.res.status = h * 100 + t * 10 + u;
767
768 return sl;
769 fail:
770 return NULL;
771}
772
773/* Takes an H2 response present in the headers list <list> terminated by a name
774 * being <NULL,0> and emits the equivalent HTX response according to the rules
775 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
776 * a positive value is returned if some bytes were emitted. In case of error, a
777 * negative error code is returned.
778 *
779 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
780 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
781 * if a body is detected (!ES).
782 *
783 * The headers list <list> must be composed of :
784 * - n.name != NULL, n.len > 0 : literal header name
785 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
786 * among H2_PHDR_IDX_*
787 * - n.name ignored, n.len == 0 : end of list
788 * - in all cases except the end of list, v.name and v.len must designate a
789 * valid value.
790 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100791int 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 +0200792{
793 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
794 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
795 uint32_t idx;
796 int phdr;
797 int ret;
798 int i;
799 struct htx_sl *sl = NULL;
800 unsigned int sl_flags = 0;
801
802 fields = 0;
803 for (idx = 0; list[idx].n.len != 0; idx++) {
804 if (!list[idx].n.ptr) {
805 /* this is an indexed pseudo-header */
806 phdr = list[idx].n.len;
807 }
808 else {
809 /* this can be any type of header */
810 /* RFC7540#8.1.2: upper case not allowed in header field names */
811 for (i = 0; i < list[idx].n.len; i++)
812 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
813 goto fail;
814
815 phdr = h2_str_to_phdr(list[idx].n);
816 }
817
818 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
819 /* insert a pseudo header by its index (in phdr) and value (in value) */
820 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
821 if (fields & H2_PHDR_FND_NONE) {
822 /* pseudo header field after regular headers */
823 goto fail;
824 }
825 else {
826 /* repeated pseudo header field */
827 goto fail;
828 }
829 }
830 fields |= 1 << phdr;
831 phdr_val[phdr] = list[idx].v;
832 continue;
833 }
834 else if (phdr != 0) {
835 /* invalid pseudo header -- should never happen here */
836 goto fail;
837 }
838
839 /* regular header field in (name,value) */
840 if (!(fields & H2_PHDR_FND_NONE)) {
841 /* no more pseudo-headers, time to build the status line */
842 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
843 if (!sl)
844 goto fail;
845 fields |= H2_PHDR_FND_NONE;
846 }
847
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100848 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100849 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100850 if (ret < 0)
851 goto fail;
852
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200853 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100854 if (ret == 0)
855 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200856 }
857
858 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
859 if (isteq(list[idx].n, ist("connection")) ||
860 isteq(list[idx].n, ist("proxy-connection")) ||
861 isteq(list[idx].n, ist("keep-alive")) ||
862 isteq(list[idx].n, ist("upgrade")) ||
863 isteq(list[idx].n, ist("transfer-encoding")))
864 goto fail;
865
866 if (!htx_add_header(htx, list[idx].n, list[idx].v))
867 goto fail;
868 }
869
870 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
871 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
872 goto fail;
873
874 /* Let's dump the request now if not yet emitted. */
875 if (!(fields & H2_PHDR_FND_NONE)) {
876 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
877 if (!sl)
878 goto fail;
879 }
880
881 /* update the start line with last detected header info */
882 sl->flags |= sl_flags;
883
884 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
885 /* FIXME: Do we need to signal anything when we have a body and
886 * no content-length, to have the equivalent of H1's chunked
887 * encoding?
888 */
889 }
890
891 /* now send the end of headers marker */
892 htx_add_endof(htx, HTX_BLK_EOH);
893
894 ret = 1;
895 return ret;
896
897 fail:
898 return -1;
899}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100900
901/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
902 * emits the equivalent HTX trailers block not including the empty line. The
903 * output contents are emitted in <htx>, and a positive value is returned if
904 * some bytes were emitted. In case of error, a negative error code is
905 * returned. The caller must have verified that the message in the buffer is
906 * compatible with receipt of trailers. Note that for now the HTX trailers
907 * block is in fact an H1 block and it must contain the trailing CRLF.
908 *
909 * The headers list <list> must be composed of :
910 * - n.name != NULL, n.len > 0 : literal header name
911 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
912 * among H2_PHDR_IDX_* (illegal here)
913 * - n.name ignored, n.len == 0 : end of list
914 * - in all cases except the end of list, v.name and v.len must designate a
915 * valid value.
916 */
917int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
918{
919 struct htx_blk *blk;
920 char *out;
921 uint32_t idx;
922 int len;
923 int i;
924
925 len = 2; // CRLF
926 for (idx = 0; list[idx].n.len != 0; idx++) {
927 if (!list[idx].n.ptr) {
928 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
929 goto fail;
930 }
931
932 /* RFC7540#8.1.2: upper case not allowed in header field names */
933 for (i = 0; i < list[idx].n.len; i++)
934 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
935 goto fail;
936
937 if (h2_str_to_phdr(list[idx].n) != 0) {
938 /* This is a pseudo-header (RFC7540#8.1.2.1) */
939 goto fail;
940 }
941
942 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
943 if (isteq(list[idx].n, ist("host")) ||
944 isteq(list[idx].n, ist("content-length")) ||
945 isteq(list[idx].n, ist("connection")) ||
946 isteq(list[idx].n, ist("proxy-connection")) ||
947 isteq(list[idx].n, ist("keep-alive")) ||
948 isteq(list[idx].n, ist("upgrade")) ||
949 isteq(list[idx].n, ist("te")) ||
950 isteq(list[idx].n, ist("transfer-encoding")))
951 goto fail;
952
953 len += list[idx].n.len + 2 + list[idx].v.len + 2;
954 }
955
956 blk = htx_add_blk_type_size(htx, HTX_BLK_TLR, len);
957 if (!blk)
958 goto fail;
959
960 out = htx_get_blk_ptr(htx, blk);
961 for (idx = 0; list[idx].n.len != 0; idx++) {
962 /* copy "name: value" */
963 memcpy(out, list[idx].n.ptr, list[idx].n.len);
964 out += list[idx].n.len;
965 *(out++) = ':';
966 *(out++) = ' ';
967
968 memcpy(out, list[idx].v.ptr, list[idx].v.len);
969 out += list[idx].v.len;
970 *(out++) = '\r';
971 *(out++) = '\n';
972 }
973 *(out++) = '\r';
974 *(out++) = '\n';
975
976 return 1;
977
978 fail:
979 return -1;
980}