blob: 6ec69c4346c546629975e84213ea0f91efef3b8c [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>
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;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100495 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100496
497 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
498 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
499 * MUST be omitted ; ":authority" contains the host and port
500 * to connect to.
501 */
502 if (fields & H2_PHDR_FND_SCHM) {
503 /* scheme not allowed */
504 goto fail;
505 }
506 else if (fields & H2_PHDR_FND_PATH) {
507 /* path not allowed */
508 goto fail;
509 }
510 else if (!(fields & H2_PHDR_FND_AUTH)) {
511 /* missing authority */
512 goto fail;
513 }
514 // otherwise OK ; let's use the authority instead of the URI
515 uri_idx = H2_PHDR_IDX_AUTH;
516 *msgf |= H2_MSGF_BODY_TUNNEL;
517 }
518 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
519 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
520 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
521 * valid value for the ":method", ":scheme" and ":path" phdr
522 * unless it is a CONNECT request.
523 */
524 if (!(fields & H2_PHDR_FND_METH)) {
525 /* missing method */
526 goto fail;
527 }
528 else if (!(fields & H2_PHDR_FND_SCHM)) {
529 /* missing scheme */
530 goto fail;
531 }
532 else {
533 /* missing path */
534 goto fail;
535 }
536 }
537
538 /* 7540#8.1.2.3: :path must not be empty */
539 if (!phdr[uri_idx].len)
540 goto fail;
541
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100542 /* make sure :path doesn't contain LWS nor CTL characters */
543 for (i = 0; i < phdr[uri_idx].len; i++) {
544 unsigned char c = phdr[uri_idx].ptr[i];
545 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
546 htx->flags |= HTX_FL_PARSING_ERROR;
547 }
548
Willy Tarreau6deb4122018-11-27 15:34:18 +0100549 /* Set HTX start-line flags */
550 flags |= HTX_SL_F_VER_11; // V2 in fact
551 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
552
553 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
554 if (!sl)
555 goto fail;
556
557 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
558 return sl;
559 fail:
560 return NULL;
561}
562
563/* Takes an H2 request present in the headers list <list> terminated by a name
564 * being <NULL,0> and emits the equivalent HTX request according to the rules
565 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
566 * non-zero is returned if some bytes were emitted. In case of error, a
567 * negative error code is returned.
568 *
569 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
570 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
571 * if a body is detected (!ES).
572 *
573 * The headers list <list> must be composed of :
574 * - n.name != NULL, n.len > 0 : literal header name
575 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
576 * among H2_PHDR_IDX_*
577 * - n.name ignored, n.len == 0 : end of list
578 * - in all cases except the end of list, v.name and v.len must designate a
579 * valid value.
580 *
581 * The Cookie header will be reassembled at the end, and for this, the <list>
582 * will be used to create a linked list, so its contents may be destroyed.
583 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100584int 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 +0100585{
586 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
587 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
588 uint32_t idx;
589 int ck, lck; /* cookie index and last cookie index */
590 int phdr;
591 int ret;
592 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200593 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100594 struct htx_sl *sl = NULL;
595 unsigned int sl_flags = 0;
596
597 lck = ck = -1; // no cookie for now
598 fields = 0;
599 for (idx = 0; list[idx].n.len != 0; idx++) {
600 if (!list[idx].n.ptr) {
601 /* this is an indexed pseudo-header */
602 phdr = list[idx].n.len;
603 }
604 else {
605 /* this can be any type of header */
606 /* RFC7540#8.1.2: upper case not allowed in header field names */
607 for (i = 0; i < list[idx].n.len; i++)
608 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
609 goto fail;
610
611 phdr = h2_str_to_phdr(list[idx].n);
612 }
613
614 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
615 /* insert a pseudo header by its index (in phdr) and value (in value) */
616 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
617 if (fields & H2_PHDR_FND_NONE) {
618 /* pseudo header field after regular headers */
619 goto fail;
620 }
621 else {
622 /* repeated pseudo header field */
623 goto fail;
624 }
625 }
626 fields |= 1 << phdr;
627 phdr_val[phdr] = list[idx].v;
628 continue;
629 }
630 else if (phdr != 0) {
631 /* invalid pseudo header -- should never happen here */
632 goto fail;
633 }
634
635 /* regular header field in (name,value) */
636 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
637 /* no more pseudo-headers, time to build the request line */
638 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
639 if (!sl)
640 goto fail;
641 fields |= H2_PHDR_FND_NONE;
642 }
643
644 if (isteq(list[idx].n, ist("host")))
645 fields |= H2_PHDR_FND_HOST;
646
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100647 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100648 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100649 if (ret < 0)
650 goto fail;
651
Willy Tarreau6deb4122018-11-27 15:34:18 +0100652 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100653 if (ret == 0)
654 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100655 }
656
657 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
658 if (isteq(list[idx].n, ist("connection")) ||
659 isteq(list[idx].n, ist("proxy-connection")) ||
660 isteq(list[idx].n, ist("keep-alive")) ||
661 isteq(list[idx].n, ist("upgrade")) ||
662 isteq(list[idx].n, ist("transfer-encoding")))
663 goto fail;
664
665 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
666 goto fail;
667
668 /* cookie requires special processing at the end */
669 if (isteq(list[idx].n, ist("cookie"))) {
670 list[idx].n.len = -1;
671
672 if (ck < 0)
673 ck = idx;
674 else
675 list[lck].n.len = idx;
676
677 lck = idx;
678 continue;
679 }
680
681 if (!htx_add_header(htx, list[idx].n, list[idx].v))
682 goto fail;
683 }
684
685 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
686 if (fields & H2_PHDR_FND_STAT)
687 goto fail;
688
689 /* Let's dump the request now if not yet emitted. */
690 if (!(fields & H2_PHDR_FND_NONE)) {
691 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
692 if (!sl)
693 goto fail;
694 }
695
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100696 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
697 sl_flags |= HTX_SL_F_BODYLESS;
698
Willy Tarreau6deb4122018-11-27 15:34:18 +0100699 /* update the start line with last detected header info */
700 sl->flags |= sl_flags;
701
702 /* complete with missing Host if needed */
703 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
704 /* missing Host field, use :authority instead */
705 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
706 goto fail;
707 }
708
709 /* now we may have to build a cookie list. We'll dump the values of all
710 * visited headers.
711 */
712 if (ck >= 0) {
713 uint32_t fs; // free space
714 uint32_t bs; // block size
715 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100716 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100717 struct htx_blk *blk;
718
719 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
720 if (!blk)
721 goto fail;
722
Willy Tarreau164e0612018-12-18 11:00:41 +0100723 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100724 fs = htx_free_data_space(htx);
725 bs = htx_get_blksz(blk);
726
727 /* for each extra cookie, we'll extend the cookie's value and
728 * insert "; " before the new value.
729 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100730 fs += tl; // first one is already counted
731 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100732 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100733 tl += vl + 2;
734 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100735 goto fail;
736
Willy Tarreau164e0612018-12-18 11:00:41 +0100737 htx_set_blk_value_len(blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100738 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
739 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
740 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
741 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100742 }
743
744 }
745
746 /* now send the end of headers marker */
747 htx_add_endof(htx, HTX_BLK_EOH);
748
Christopher Faulet33543e72019-05-15 15:53:20 +0200749 /* Set bytes used in the HTX mesage for the headers now */
750 sl->hdrs_bytes = htx_used_space(htx) - used;
751
Willy Tarreau6deb4122018-11-27 15:34:18 +0100752 ret = 1;
753 return ret;
754
755 fail:
756 return -1;
757}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200758
759/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
760 * <fields> indicates what was found so far. This should be called once at the
761 * detection of the first general header field or at the end of the message if
762 * no general header field was found yet. Returns the created start line on
763 * success, or NULL on failure. Upon success, <msgf> is updated with a few
764 * H2_MSGF_* flags indicating what was found while parsing.
765 */
766static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
767{
768 unsigned int flags = HTX_SL_F_NONE;
769 struct htx_sl *sl;
770 unsigned char h, t, u;
771
772 /* only :status is allowed as a pseudo header */
773 if (!(fields & H2_PHDR_FND_STAT))
774 goto fail;
775
776 if (phdr[H2_PHDR_IDX_STAT].len != 3)
777 goto fail;
778
779 /* Set HTX start-line flags */
780 flags |= HTX_SL_F_VER_11; // V2 in fact
781 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
782
783 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
784 if (!sl)
785 goto fail;
786
787 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
788 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
789 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
790 if (h > 9 || t > 9 || u > 9)
791 goto fail;
792
793 sl->info.res.status = h * 100 + t * 10 + u;
794
Christopher Faulet0b465482019-02-19 15:14:23 +0100795 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
796 * there is no body. So remove the flag H2_MSGF_BODY and add
797 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
798 * expected.
799 */
800 if (sl->info.res.status < 200 &&
801 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
802 *msgf |= H2_MSGF_RSP_1XX;
803 *msgf &= ~H2_MSGF_BODY;
804 }
805
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200806 return sl;
807 fail:
808 return NULL;
809}
810
811/* Takes an H2 response present in the headers list <list> terminated by a name
812 * being <NULL,0> and emits the equivalent HTX response according to the rules
813 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
814 * a positive value is returned if some bytes were emitted. In case of error, a
815 * negative error code is returned.
816 *
817 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
818 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
819 * if a body is detected (!ES).
820 *
821 * The headers list <list> must be composed of :
822 * - n.name != NULL, n.len > 0 : literal header name
823 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
824 * among H2_PHDR_IDX_*
825 * - n.name ignored, n.len == 0 : end of list
826 * - in all cases except the end of list, v.name and v.len must designate a
827 * valid value.
828 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100829int 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 +0200830{
831 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
832 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
833 uint32_t idx;
834 int phdr;
835 int ret;
836 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200837 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200838 struct htx_sl *sl = NULL;
839 unsigned int sl_flags = 0;
840
841 fields = 0;
842 for (idx = 0; list[idx].n.len != 0; idx++) {
843 if (!list[idx].n.ptr) {
844 /* this is an indexed pseudo-header */
845 phdr = list[idx].n.len;
846 }
847 else {
848 /* this can be any type of header */
849 /* RFC7540#8.1.2: upper case not allowed in header field names */
850 for (i = 0; i < list[idx].n.len; i++)
851 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
852 goto fail;
853
854 phdr = h2_str_to_phdr(list[idx].n);
855 }
856
857 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
858 /* insert a pseudo header by its index (in phdr) and value (in value) */
859 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
860 if (fields & H2_PHDR_FND_NONE) {
861 /* pseudo header field after regular headers */
862 goto fail;
863 }
864 else {
865 /* repeated pseudo header field */
866 goto fail;
867 }
868 }
869 fields |= 1 << phdr;
870 phdr_val[phdr] = list[idx].v;
871 continue;
872 }
873 else if (phdr != 0) {
874 /* invalid pseudo header -- should never happen here */
875 goto fail;
876 }
877
878 /* regular header field in (name,value) */
879 if (!(fields & H2_PHDR_FND_NONE)) {
880 /* no more pseudo-headers, time to build the status line */
881 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
882 if (!sl)
883 goto fail;
884 fields |= H2_PHDR_FND_NONE;
885 }
886
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100887 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100888 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100889 if (ret < 0)
890 goto fail;
891
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200892 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100893 if (ret == 0)
894 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200895 }
896
897 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
898 if (isteq(list[idx].n, ist("connection")) ||
899 isteq(list[idx].n, ist("proxy-connection")) ||
900 isteq(list[idx].n, ist("keep-alive")) ||
901 isteq(list[idx].n, ist("upgrade")) ||
902 isteq(list[idx].n, ist("transfer-encoding")))
903 goto fail;
904
905 if (!htx_add_header(htx, list[idx].n, list[idx].v))
906 goto fail;
907 }
908
909 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
910 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
911 goto fail;
912
913 /* Let's dump the request now if not yet emitted. */
914 if (!(fields & H2_PHDR_FND_NONE)) {
915 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
916 if (!sl)
917 goto fail;
918 }
919
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100920 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
921 sl_flags |= HTX_SL_F_BODYLESS;
922
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200923 /* update the start line with last detected header info */
924 sl->flags |= sl_flags;
925
926 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
927 /* FIXME: Do we need to signal anything when we have a body and
928 * no content-length, to have the equivalent of H1's chunked
929 * encoding?
930 */
931 }
932
933 /* now send the end of headers marker */
934 htx_add_endof(htx, HTX_BLK_EOH);
935
Christopher Faulet33543e72019-05-15 15:53:20 +0200936 /* Set bytes used in the HTX mesage for the headers now */
937 sl->hdrs_bytes = htx_used_space(htx) - used;
938
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200939 ret = 1;
940 return ret;
941
942 fail:
943 return -1;
944}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100945
946/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
947 * emits the equivalent HTX trailers block not including the empty line. The
948 * output contents are emitted in <htx>, and a positive value is returned if
949 * some bytes were emitted. In case of error, a negative error code is
950 * returned. The caller must have verified that the message in the buffer is
951 * compatible with receipt of trailers. Note that for now the HTX trailers
952 * block is in fact an H1 block and it must contain the trailing CRLF.
953 *
954 * The headers list <list> must be composed of :
955 * - n.name != NULL, n.len > 0 : literal header name
956 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
957 * among H2_PHDR_IDX_* (illegal here)
958 * - n.name ignored, n.len == 0 : end of list
959 * - in all cases except the end of list, v.name and v.len must designate a
960 * valid value.
961 */
962int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
963{
964 struct htx_blk *blk;
965 char *out;
966 uint32_t idx;
967 int len;
968 int i;
969
970 len = 2; // CRLF
971 for (idx = 0; list[idx].n.len != 0; idx++) {
972 if (!list[idx].n.ptr) {
973 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
974 goto fail;
975 }
976
977 /* RFC7540#8.1.2: upper case not allowed in header field names */
978 for (i = 0; i < list[idx].n.len; i++)
979 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
980 goto fail;
981
982 if (h2_str_to_phdr(list[idx].n) != 0) {
983 /* This is a pseudo-header (RFC7540#8.1.2.1) */
984 goto fail;
985 }
986
987 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
988 if (isteq(list[idx].n, ist("host")) ||
989 isteq(list[idx].n, ist("content-length")) ||
990 isteq(list[idx].n, ist("connection")) ||
991 isteq(list[idx].n, ist("proxy-connection")) ||
992 isteq(list[idx].n, ist("keep-alive")) ||
993 isteq(list[idx].n, ist("upgrade")) ||
994 isteq(list[idx].n, ist("te")) ||
995 isteq(list[idx].n, ist("transfer-encoding")))
996 goto fail;
997
998 len += list[idx].n.len + 2 + list[idx].v.len + 2;
999 }
1000
1001 blk = htx_add_blk_type_size(htx, HTX_BLK_TLR, len);
1002 if (!blk)
1003 goto fail;
1004
1005 out = htx_get_blk_ptr(htx, blk);
1006 for (idx = 0; list[idx].n.len != 0; idx++) {
1007 /* copy "name: value" */
1008 memcpy(out, list[idx].n.ptr, list[idx].n.len);
1009 out += list[idx].n.len;
1010 *(out++) = ':';
1011 *(out++) = ' ';
1012
1013 memcpy(out, list[idx].v.ptr, list[idx].v.len);
1014 out += list[idx].v.len;
1015 *(out++) = '\r';
1016 *(out++) = '\n';
1017 }
1018 *(out++) = '\r';
1019 *(out++) = '\n';
1020
1021 return 1;
1022
1023 fail:
1024 return -1;
1025}