blob: a4578f468d0afe32b31092b7e40b829f8f5ba14b [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
Willy Tarreau9c84d822019-01-30 15:09:21 +010034struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
35 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
36 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
37 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
38 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
39 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
40 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
42 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
43 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
44 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
45};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010046
47/* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers
48 * stored in <phdr[]>. <fields> indicates what was found so far. This should be
49 * called once at the detection of the first general header field or at the end
50 * of the request if no general header field was found yet. Returns 0 on success
Willy Tarreau174b06a2018-04-25 18:13:58 +020051 * or a negative error code on failure. Upon success, <msgf> is updated with a
52 * few H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010053 */
Willy Tarreau174b06a2018-04-25 18:13:58 +020054static 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 +010055{
56 char *out = *ptr;
57 int uri_idx = H2_PHDR_IDX_PATH;
58
59 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
60 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
61 * MUST be omitted ; ":authority" contains the host and port
62 * to connect to.
63 */
64 if (fields & H2_PHDR_FND_SCHM) {
65 /* scheme not allowed */
66 goto fail;
67 }
68 else if (fields & H2_PHDR_FND_PATH) {
69 /* path not allowed */
70 goto fail;
71 }
72 else if (!(fields & H2_PHDR_FND_AUTH)) {
73 /* missing authority */
74 goto fail;
75 }
76 // otherwise OK ; let's use the authority instead of the URI
77 uri_idx = H2_PHDR_IDX_AUTH;
Willy Tarreau174b06a2018-04-25 18:13:58 +020078 *msgf |= H2_MSGF_BODY_TUNNEL;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010079 }
80 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
81 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
82 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
83 * valid value for the ":method", ":scheme" and ":path" phdr
84 * unless it is a CONNECT request.
85 */
86 if (!(fields & H2_PHDR_FND_METH)) {
87 /* missing method */
88 goto fail;
89 }
90 else if (!(fields & H2_PHDR_FND_SCHM)) {
91 /* missing scheme */
92 goto fail;
93 }
94 else {
95 /* missing path */
96 goto fail;
97 }
98 }
99
Willy Tarreaucd4fe172017-12-03 11:51:31 +0100100 /* 7540#8.1.2.3: :path must not be empty */
101 if (!phdr[uri_idx].len)
102 goto fail;
103
Willy Tarreau811ad122017-12-03 09:44:50 +0100104 if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) {
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100105 /* too large */
106 goto fail;
107 }
108
109 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
110 out += phdr[H2_PHDR_IDX_METH].len;
111 *(out++) = ' ';
112
113 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
114 out += phdr[uri_idx].len;
115 memcpy(out, " HTTP/1.1\r\n", 11);
116 out += 11;
117
118 *ptr = out;
119 return 0;
120 fail:
121 return -1;
122}
123
124/* Takes an H2 request present in the headers list <list> terminated by a name
125 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
126 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
127 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
128 * case of error, a negative error code is returned.
129 *
Willy Tarreau174b06a2018-04-25 18:13:58 +0200130 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
131 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
132 * if a body is detected (!ES).
133 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100134 * The headers list <list> must be composed of :
135 * - n.name != NULL, n.len > 0 : literal header name
136 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
137 * among H2_PHDR_IDX_*
138 * - n.name ignored, n.len == 0 : end of list
139 * - in all cases except the end of list, v.name and v.len must designate a
140 * valid value.
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100141 *
142 * The Cookie header will be reassembled at the end, and for this, the <list>
143 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100144 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100145int 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 +0100146{
147 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
148 char *out_end = out + osize;
149 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
150 uint32_t idx;
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100151 int ck, lck; /* cookie index and last cookie index */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100152 int phdr;
153 int ret;
Willy Tarreau637f64d2017-12-03 20:28:13 +0100154 int i;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100155
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100156 lck = ck = -1; // no cookie for now
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100157 fields = 0;
158 for (idx = 0; list[idx].n.len != 0; idx++) {
159 if (!list[idx].n.ptr) {
160 /* this is an indexed pseudo-header */
161 phdr = list[idx].n.len;
162 }
163 else {
164 /* this can be any type of header */
Willy Tarreau637f64d2017-12-03 20:28:13 +0100165 /* RFC7540#8.1.2: upper case not allowed in header field names */
166 for (i = 0; i < list[idx].n.len; i++)
167 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
168 goto fail;
169
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100170 phdr = h2_str_to_phdr(list[idx].n);
171 }
172
173 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
174 /* insert a pseudo header by its index (in phdr) and value (in value) */
175 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
176 if (fields & H2_PHDR_FND_NONE) {
177 /* pseudo header field after regular headers */
178 goto fail;
179 }
180 else {
181 /* repeated pseudo header field */
182 goto fail;
183 }
184 }
185 fields |= 1 << phdr;
186 phdr_val[phdr] = list[idx].v;
187 continue;
188 }
189 else if (phdr != 0) {
190 /* invalid pseudo header -- should never happen here */
191 goto fail;
192 }
193
194 /* regular header field in (name,value) */
195 if (!(fields & H2_PHDR_FND_NONE)) {
196 /* no more pseudo-headers, time to build the request line */
Willy Tarreau174b06a2018-04-25 18:13:58 +0200197 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100198 if (ret != 0)
199 goto leave;
200 fields |= H2_PHDR_FND_NONE;
201 }
202
203 if (isteq(list[idx].n, ist("host")))
204 fields |= H2_PHDR_FND_HOST;
205
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100206 if (isteq(list[idx].n, ist("content-length"))) {
207 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
208 if (ret < 0)
209 goto fail;
210
211 if (ret == 0)
212 continue; // skip this duplicate
213 }
Willy Tarreau174b06a2018-04-25 18:13:58 +0200214
Willy Tarreaufe7c3562017-12-03 20:15:34 +0100215 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
216 if (isteq(list[idx].n, ist("connection")) ||
217 isteq(list[idx].n, ist("proxy-connection")) ||
218 isteq(list[idx].n, ist("keep-alive")) ||
219 isteq(list[idx].n, ist("upgrade")) ||
220 isteq(list[idx].n, ist("transfer-encoding")))
221 goto fail;
222
Willy Tarreaud8d2ac72017-12-03 18:41:31 +0100223 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
224 goto fail;
225
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100226 /* cookie requires special processing at the end */
227 if (isteq(list[idx].n, ist("cookie"))) {
228 list[idx].n.len = -1;
229
230 if (ck < 0)
231 ck = idx;
232 else
233 list[lck].n.len = idx;
234
235 lck = idx;
236 continue;
237 }
238
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100239 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
240 /* too large */
241 goto fail;
242 }
243
244 /* copy "name: value" */
245 memcpy(out, list[idx].n.ptr, list[idx].n.len);
246 out += list[idx].n.len;
247 *(out++) = ':';
248 *(out++) = ' ';
249
250 memcpy(out, list[idx].v.ptr, list[idx].v.len);
251 out += list[idx].v.len;
252 *(out++) = '\r';
253 *(out++) = '\n';
254 }
255
Willy Tarreau52088692017-12-03 20:13:54 +0100256 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
257 if (fields & H2_PHDR_FND_STAT)
258 goto fail;
259
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100260 /* Let's dump the request now if not yet emitted. */
261 if (!(fields & H2_PHDR_FND_NONE)) {
Willy Tarreau174b06a2018-04-25 18:13:58 +0200262 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100263 if (ret != 0)
264 goto leave;
265 }
266
267 /* complete with missing Host if needed */
268 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
269 /* missing Host field, use :authority instead */
270 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
271 /* too large */
272 goto fail;
273 }
274
275 memcpy(out, "host: ", 6);
276 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
277 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
278 *(out++) = '\r';
279 *(out++) = '\n';
280 }
281
Willy Tarreaueba10f22018-04-25 20:44:22 +0200282 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
283 /* add chunked encoding */
284 if (out + 28 > out_end)
285 goto fail;
286 memcpy(out, "transfer-encoding: chunked\r\n", 28);
287 out += 28;
288 }
289
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100290 /* now we may have to build a cookie list. We'll dump the values of all
291 * visited headers.
292 */
293 if (ck >= 0) {
294 if (out + 8 > out_end) {
295 /* too large */
296 goto fail;
297 }
298 memcpy(out, "cookie: ", 8);
299 out += 8;
300
301 do {
302 if (out + list[ck].v.len + 2 > out_end) {
303 /* too large */
304 goto fail;
305 }
306 memcpy(out, list[ck].v.ptr, list[ck].v.len);
307 out += list[ck].v.len;
308 ck = list[ck].n.len;
309
310 if (ck >= 0) {
311 *(out++) = ';';
312 *(out++) = ' ';
313 }
314 } while (ck >= 0);
315
316 if (out + 2 > out_end) {
317 /* too large */
318 goto fail;
319 }
320 *(out++) = '\r';
321 *(out++) = '\n';
322 }
323
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100324 /* And finish */
325 if (out + 2 > out_end) {
326 /* too large */
327 goto fail;
328 }
329
330 *(out++) = '\r';
331 *(out++) = '\n';
332 ret = out + osize - out_end;
333 leave:
334 return ret;
335
336 fail:
337 return -1;
338}
Willy Tarreau6deb4122018-11-27 15:34:18 +0100339
Willy Tarreau9d953e72019-01-03 16:18:14 +0100340/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
341 * emits the equivalent HTTP/1.1 trailers block not including the empty line.
342 * The output contents are emitted in <out> for a max of <osize> bytes, and the
343 * amount of bytes emitted is returned. In case of error, a negative error code
344 * is returned. The caller must have verified that the message in the buffer is
345 * compatible with receipt of trailers.
346 *
347 * The headers list <list> must be composed of :
348 * - n.name != NULL, n.len > 0 : literal header name
349 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
350 * among H2_PHDR_IDX_* (illegal here)
351 * - n.name ignored, n.len == 0 : end of list
352 * - in all cases except the end of list, v.name and v.len must designate a
353 * valid value.
354 */
355int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize)
356{
357 char *out_end = out + osize;
358 uint32_t idx;
359 int i;
360
361 for (idx = 0; list[idx].n.len != 0; idx++) {
362 if (!list[idx].n.ptr) {
363 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
364 goto fail;
365 }
366
367 /* RFC7540#8.1.2: upper case not allowed in header field names */
368 for (i = 0; i < list[idx].n.len; i++)
369 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
370 goto fail;
371
372 if (h2_str_to_phdr(list[idx].n) != 0) {
373 /* This is a pseudo-header (RFC7540#8.1.2.1) */
374 goto fail;
375 }
376
377 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
378 if (isteq(list[idx].n, ist("host")) ||
379 isteq(list[idx].n, ist("content-length")) ||
380 isteq(list[idx].n, ist("connection")) ||
381 isteq(list[idx].n, ist("proxy-connection")) ||
382 isteq(list[idx].n, ist("keep-alive")) ||
383 isteq(list[idx].n, ist("upgrade")) ||
384 isteq(list[idx].n, ist("te")) ||
385 isteq(list[idx].n, ist("transfer-encoding")))
386 goto fail;
387
388 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
389 /* too large */
390 goto fail;
391 }
392
393 /* copy "name: value" */
394 memcpy(out, list[idx].n.ptr, list[idx].n.len);
395 out += list[idx].n.len;
396 *(out++) = ':';
397 *(out++) = ' ';
398
399 memcpy(out, list[idx].v.ptr, list[idx].v.len);
400 out += list[idx].v.len;
401 *(out++) = '\r';
402 *(out++) = '\n';
403 }
404
405 return out + osize - out_end;
406
407 fail:
408 return -1;
409}
410
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100411/* Parse the Content-Length header field of an HTTP/2 request. The function
412 * checks all possible occurrences of a comma-delimited value, and verifies
413 * if any of them doesn't match a previous value. It returns <0 if a value
414 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
415 * if the value can be indexed (first one). In the last case, the value might
416 * be adjusted and the caller must only add the updated value.
417 */
418int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
419{
420 char *e, *n;
421 unsigned long long cl;
422 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
423 struct ist word;
424
425 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
426 e = value->ptr + value->len;
427
428 while (++word.ptr < e) {
429 /* skip leading delimitor and blanks */
430 if (unlikely(HTTP_IS_LWS(*word.ptr)))
431 continue;
432
433 /* digits only now */
434 for (cl = 0, n = word.ptr; n < e; n++) {
435 unsigned int c = *n - '0';
436 if (unlikely(c > 9)) {
437 /* non-digit */
438 if (unlikely(n == word.ptr)) // spaces only
439 goto fail;
440 break;
441 }
442 if (unlikely(cl > ULLONG_MAX / 10ULL))
443 goto fail; /* multiply overflow */
444 cl = cl * 10ULL;
445 if (unlikely(cl + c < cl))
446 goto fail; /* addition overflow */
447 cl = cl + c;
448 }
449
450 /* keep a copy of the exact cleaned value */
451 word.len = n - word.ptr;
452
453 /* skip trailing LWS till next comma or EOL */
454 for (; n < e; n++) {
455 if (!HTTP_IS_LWS(*n)) {
456 if (unlikely(*n != ','))
457 goto fail;
458 break;
459 }
460 }
461
462 /* if duplicate, must be equal */
463 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
464 goto fail;
465
466 /* OK, store this result as the one to be indexed */
467 *msgf |= H2_MSGF_BODY_CL;
468 *body_len = cl;
469 *value = word;
470 word.ptr = n;
471 }
472 /* here we've reached the end with a single value or a series of
473 * identical values, all matching previous series if any. The last
474 * parsed value was sent back into <value>. We just have to decide
475 * if this occurrence has to be indexed (it's the first one) or
476 * silently skipped (it's not the first one)
477 */
478 return !not_first;
479 fail:
480 return -1;
481}
482
Willy Tarreau6deb4122018-11-27 15:34:18 +0100483/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
484 * <fields> indicates what was found so far. This should be called once at the
485 * detection of the first general header field or at the end of the request if
486 * no general header field was found yet. Returns the created start line on
487 * success, or NULL on failure. Upon success, <msgf> is updated with a few
488 * H2_MSGF_* flags indicating what was found while parsing.
489 */
490static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
491{
492 int uri_idx = H2_PHDR_IDX_PATH;
493 unsigned int flags = HTX_SL_F_NONE;
494 struct htx_sl *sl;
495
496 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
497 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
498 * MUST be omitted ; ":authority" contains the host and port
499 * to connect to.
500 */
501 if (fields & H2_PHDR_FND_SCHM) {
502 /* scheme not allowed */
503 goto fail;
504 }
505 else if (fields & H2_PHDR_FND_PATH) {
506 /* path not allowed */
507 goto fail;
508 }
509 else if (!(fields & H2_PHDR_FND_AUTH)) {
510 /* missing authority */
511 goto fail;
512 }
513 // otherwise OK ; let's use the authority instead of the URI
514 uri_idx = H2_PHDR_IDX_AUTH;
515 *msgf |= H2_MSGF_BODY_TUNNEL;
516 }
517 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
518 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
519 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
520 * valid value for the ":method", ":scheme" and ":path" phdr
521 * unless it is a CONNECT request.
522 */
523 if (!(fields & H2_PHDR_FND_METH)) {
524 /* missing method */
525 goto fail;
526 }
527 else if (!(fields & H2_PHDR_FND_SCHM)) {
528 /* missing scheme */
529 goto fail;
530 }
531 else {
532 /* missing path */
533 goto fail;
534 }
535 }
536
537 /* 7540#8.1.2.3: :path must not be empty */
538 if (!phdr[uri_idx].len)
539 goto fail;
540
541 /* Set HTX start-line flags */
542 flags |= HTX_SL_F_VER_11; // V2 in fact
543 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
544
545 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
546 if (!sl)
547 goto fail;
548
549 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
550 return sl;
551 fail:
552 return NULL;
553}
554
555/* Takes an H2 request present in the headers list <list> terminated by a name
556 * being <NULL,0> and emits the equivalent HTX request according to the rules
557 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
558 * non-zero is returned if some bytes were emitted. In case of error, a
559 * negative error code is returned.
560 *
561 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
562 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
563 * if a body is detected (!ES).
564 *
565 * The headers list <list> must be composed of :
566 * - n.name != NULL, n.len > 0 : literal header name
567 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
568 * among H2_PHDR_IDX_*
569 * - n.name ignored, n.len == 0 : end of list
570 * - in all cases except the end of list, v.name and v.len must designate a
571 * valid value.
572 *
573 * The Cookie header will be reassembled at the end, and for this, the <list>
574 * will be used to create a linked list, so its contents may be destroyed.
575 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100576int 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 +0100577{
578 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
579 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
580 uint32_t idx;
581 int ck, lck; /* cookie index and last cookie index */
582 int phdr;
583 int ret;
584 int i;
585 struct htx_sl *sl = NULL;
586 unsigned int sl_flags = 0;
587
588 lck = ck = -1; // no cookie for now
589 fields = 0;
590 for (idx = 0; list[idx].n.len != 0; idx++) {
591 if (!list[idx].n.ptr) {
592 /* this is an indexed pseudo-header */
593 phdr = list[idx].n.len;
594 }
595 else {
596 /* this can be any type of header */
597 /* RFC7540#8.1.2: upper case not allowed in header field names */
598 for (i = 0; i < list[idx].n.len; i++)
599 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
600 goto fail;
601
602 phdr = h2_str_to_phdr(list[idx].n);
603 }
604
605 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
606 /* insert a pseudo header by its index (in phdr) and value (in value) */
607 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
608 if (fields & H2_PHDR_FND_NONE) {
609 /* pseudo header field after regular headers */
610 goto fail;
611 }
612 else {
613 /* repeated pseudo header field */
614 goto fail;
615 }
616 }
617 fields |= 1 << phdr;
618 phdr_val[phdr] = list[idx].v;
619 continue;
620 }
621 else if (phdr != 0) {
622 /* invalid pseudo header -- should never happen here */
623 goto fail;
624 }
625
626 /* regular header field in (name,value) */
627 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
628 /* no more pseudo-headers, time to build the request line */
629 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
630 if (!sl)
631 goto fail;
632 fields |= H2_PHDR_FND_NONE;
633 }
634
635 if (isteq(list[idx].n, ist("host")))
636 fields |= H2_PHDR_FND_HOST;
637
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100638 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100639 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100640 if (ret < 0)
641 goto fail;
642
Willy Tarreau6deb4122018-11-27 15:34:18 +0100643 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100644 if (ret == 0)
645 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100646 }
647
648 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
649 if (isteq(list[idx].n, ist("connection")) ||
650 isteq(list[idx].n, ist("proxy-connection")) ||
651 isteq(list[idx].n, ist("keep-alive")) ||
652 isteq(list[idx].n, ist("upgrade")) ||
653 isteq(list[idx].n, ist("transfer-encoding")))
654 goto fail;
655
656 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
657 goto fail;
658
659 /* cookie requires special processing at the end */
660 if (isteq(list[idx].n, ist("cookie"))) {
661 list[idx].n.len = -1;
662
663 if (ck < 0)
664 ck = idx;
665 else
666 list[lck].n.len = idx;
667
668 lck = idx;
669 continue;
670 }
671
672 if (!htx_add_header(htx, list[idx].n, list[idx].v))
673 goto fail;
674 }
675
676 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
677 if (fields & H2_PHDR_FND_STAT)
678 goto fail;
679
680 /* Let's dump the request now if not yet emitted. */
681 if (!(fields & H2_PHDR_FND_NONE)) {
682 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
683 if (!sl)
684 goto fail;
685 }
686
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100687 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
688 sl_flags |= HTX_SL_F_BODYLESS;
689
Willy Tarreau6deb4122018-11-27 15:34:18 +0100690 /* update the start line with last detected header info */
691 sl->flags |= sl_flags;
692
693 /* complete with missing Host if needed */
694 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
695 /* missing Host field, use :authority instead */
696 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
697 goto fail;
698 }
699
700 /* now we may have to build a cookie list. We'll dump the values of all
701 * visited headers.
702 */
703 if (ck >= 0) {
704 uint32_t fs; // free space
705 uint32_t bs; // block size
706 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100707 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100708 struct htx_blk *blk;
709
710 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
711 if (!blk)
712 goto fail;
713
Willy Tarreau164e0612018-12-18 11:00:41 +0100714 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100715 fs = htx_free_data_space(htx);
716 bs = htx_get_blksz(blk);
717
718 /* for each extra cookie, we'll extend the cookie's value and
719 * insert "; " before the new value.
720 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100721 fs += tl; // first one is already counted
722 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100723 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100724 tl += vl + 2;
725 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100726 goto fail;
727
Willy Tarreau164e0612018-12-18 11:00:41 +0100728 htx_set_blk_value_len(blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100729 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
730 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
731 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
732 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100733 }
734
735 }
736
737 /* now send the end of headers marker */
738 htx_add_endof(htx, HTX_BLK_EOH);
739
740 ret = 1;
741 return ret;
742
743 fail:
744 return -1;
745}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200746
747/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
748 * <fields> indicates what was found so far. This should be called once at the
749 * detection of the first general header field or at the end of the message if
750 * no general header field was found yet. Returns the created start line on
751 * success, or NULL on failure. Upon success, <msgf> is updated with a few
752 * H2_MSGF_* flags indicating what was found while parsing.
753 */
754static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
755{
756 unsigned int flags = HTX_SL_F_NONE;
757 struct htx_sl *sl;
758 unsigned char h, t, u;
759
760 /* only :status is allowed as a pseudo header */
761 if (!(fields & H2_PHDR_FND_STAT))
762 goto fail;
763
764 if (phdr[H2_PHDR_IDX_STAT].len != 3)
765 goto fail;
766
767 /* Set HTX start-line flags */
768 flags |= HTX_SL_F_VER_11; // V2 in fact
769 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
770
771 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
772 if (!sl)
773 goto fail;
774
775 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
776 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
777 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
778 if (h > 9 || t > 9 || u > 9)
779 goto fail;
780
781 sl->info.res.status = h * 100 + t * 10 + u;
782
Christopher Faulet0b465482019-02-19 15:14:23 +0100783 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
784 * there is no body. So remove the flag H2_MSGF_BODY and add
785 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
786 * expected.
787 */
788 if (sl->info.res.status < 200 &&
789 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
790 *msgf |= H2_MSGF_RSP_1XX;
791 *msgf &= ~H2_MSGF_BODY;
792 }
793
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200794 return sl;
795 fail:
796 return NULL;
797}
798
799/* Takes an H2 response present in the headers list <list> terminated by a name
800 * being <NULL,0> and emits the equivalent HTX response according to the rules
801 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
802 * a positive value is returned if some bytes were emitted. In case of error, a
803 * negative error code is returned.
804 *
805 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
806 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
807 * if a body is detected (!ES).
808 *
809 * The headers list <list> must be composed of :
810 * - n.name != NULL, n.len > 0 : literal header name
811 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
812 * among H2_PHDR_IDX_*
813 * - n.name ignored, n.len == 0 : end of list
814 * - in all cases except the end of list, v.name and v.len must designate a
815 * valid value.
816 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100817int 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 +0200818{
819 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
820 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
821 uint32_t idx;
822 int phdr;
823 int ret;
824 int i;
825 struct htx_sl *sl = NULL;
826 unsigned int sl_flags = 0;
827
828 fields = 0;
829 for (idx = 0; list[idx].n.len != 0; idx++) {
830 if (!list[idx].n.ptr) {
831 /* this is an indexed pseudo-header */
832 phdr = list[idx].n.len;
833 }
834 else {
835 /* this can be any type of header */
836 /* RFC7540#8.1.2: upper case not allowed in header field names */
837 for (i = 0; i < list[idx].n.len; i++)
838 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
839 goto fail;
840
841 phdr = h2_str_to_phdr(list[idx].n);
842 }
843
844 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
845 /* insert a pseudo header by its index (in phdr) and value (in value) */
846 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
847 if (fields & H2_PHDR_FND_NONE) {
848 /* pseudo header field after regular headers */
849 goto fail;
850 }
851 else {
852 /* repeated pseudo header field */
853 goto fail;
854 }
855 }
856 fields |= 1 << phdr;
857 phdr_val[phdr] = list[idx].v;
858 continue;
859 }
860 else if (phdr != 0) {
861 /* invalid pseudo header -- should never happen here */
862 goto fail;
863 }
864
865 /* regular header field in (name,value) */
866 if (!(fields & H2_PHDR_FND_NONE)) {
867 /* no more pseudo-headers, time to build the status line */
868 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
869 if (!sl)
870 goto fail;
871 fields |= H2_PHDR_FND_NONE;
872 }
873
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100874 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100875 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100876 if (ret < 0)
877 goto fail;
878
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200879 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100880 if (ret == 0)
881 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200882 }
883
884 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
885 if (isteq(list[idx].n, ist("connection")) ||
886 isteq(list[idx].n, ist("proxy-connection")) ||
887 isteq(list[idx].n, ist("keep-alive")) ||
888 isteq(list[idx].n, ist("upgrade")) ||
889 isteq(list[idx].n, ist("transfer-encoding")))
890 goto fail;
891
892 if (!htx_add_header(htx, list[idx].n, list[idx].v))
893 goto fail;
894 }
895
896 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
897 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
898 goto fail;
899
900 /* Let's dump the request now if not yet emitted. */
901 if (!(fields & H2_PHDR_FND_NONE)) {
902 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
903 if (!sl)
904 goto fail;
905 }
906
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100907 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
908 sl_flags |= HTX_SL_F_BODYLESS;
909
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200910 /* update the start line with last detected header info */
911 sl->flags |= sl_flags;
912
913 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
914 /* FIXME: Do we need to signal anything when we have a body and
915 * no content-length, to have the equivalent of H1's chunked
916 * encoding?
917 */
918 }
919
920 /* now send the end of headers marker */
921 htx_add_endof(htx, HTX_BLK_EOH);
922
923 ret = 1;
924 return ret;
925
926 fail:
927 return -1;
928}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100929
930/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
931 * emits the equivalent HTX trailers block not including the empty line. The
932 * output contents are emitted in <htx>, and a positive value is returned if
933 * some bytes were emitted. In case of error, a negative error code is
934 * returned. The caller must have verified that the message in the buffer is
935 * compatible with receipt of trailers. Note that for now the HTX trailers
936 * block is in fact an H1 block and it must contain the trailing CRLF.
937 *
938 * The headers list <list> must be composed of :
939 * - n.name != NULL, n.len > 0 : literal header name
940 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
941 * among H2_PHDR_IDX_* (illegal here)
942 * - n.name ignored, n.len == 0 : end of list
943 * - in all cases except the end of list, v.name and v.len must designate a
944 * valid value.
945 */
946int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
947{
948 struct htx_blk *blk;
949 char *out;
950 uint32_t idx;
951 int len;
952 int i;
953
954 len = 2; // CRLF
955 for (idx = 0; list[idx].n.len != 0; idx++) {
956 if (!list[idx].n.ptr) {
957 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
958 goto fail;
959 }
960
961 /* RFC7540#8.1.2: upper case not allowed in header field names */
962 for (i = 0; i < list[idx].n.len; i++)
963 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
964 goto fail;
965
966 if (h2_str_to_phdr(list[idx].n) != 0) {
967 /* This is a pseudo-header (RFC7540#8.1.2.1) */
968 goto fail;
969 }
970
971 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
972 if (isteq(list[idx].n, ist("host")) ||
973 isteq(list[idx].n, ist("content-length")) ||
974 isteq(list[idx].n, ist("connection")) ||
975 isteq(list[idx].n, ist("proxy-connection")) ||
976 isteq(list[idx].n, ist("keep-alive")) ||
977 isteq(list[idx].n, ist("upgrade")) ||
978 isteq(list[idx].n, ist("te")) ||
979 isteq(list[idx].n, ist("transfer-encoding")))
980 goto fail;
981
982 len += list[idx].n.len + 2 + list[idx].v.len + 2;
983 }
984
985 blk = htx_add_blk_type_size(htx, HTX_BLK_TLR, len);
986 if (!blk)
987 goto fail;
988
989 out = htx_get_blk_ptr(htx, blk);
990 for (idx = 0; list[idx].n.len != 0; idx++) {
991 /* copy "name: value" */
992 memcpy(out, list[idx].n.ptr, list[idx].n.len);
993 out += list[idx].n.len;
994 *(out++) = ':';
995 *(out++) = ' ';
996
997 memcpy(out, list[idx].v.ptr, list[idx].v.len);
998 out += list[idx].v.len;
999 *(out++) = '\r';
1000 *(out++) = '\n';
1001 }
1002 *(out++) = '\r';
1003 *(out++) = '\n';
1004
1005 return 1;
1006
1007 fail:
1008 return -1;
1009}