blob: 990d602b1b6c33ac328898ef56aa4f36a75ebb80 [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);
Christopher Fauleta9a5c042019-06-14 10:25:47 +0200558 sl->flags |= HTX_SL_F_HAS_SCHM;
559 sl->flags |= (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")) ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100560 return sl;
561 fail:
562 return NULL;
563}
564
565/* Takes an H2 request present in the headers list <list> terminated by a name
566 * being <NULL,0> and emits the equivalent HTX request according to the rules
567 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
568 * non-zero is returned if some bytes were emitted. In case of error, a
569 * negative error code is returned.
570 *
571 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
572 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
573 * if a body is detected (!ES).
574 *
575 * The headers list <list> must be composed of :
576 * - n.name != NULL, n.len > 0 : literal header name
577 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
578 * among H2_PHDR_IDX_*
579 * - n.name ignored, n.len == 0 : end of list
580 * - in all cases except the end of list, v.name and v.len must designate a
581 * valid value.
582 *
583 * The Cookie header will be reassembled at the end, and for this, the <list>
584 * will be used to create a linked list, so its contents may be destroyed.
585 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100586int 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 +0100587{
588 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
589 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
590 uint32_t idx;
591 int ck, lck; /* cookie index and last cookie index */
592 int phdr;
593 int ret;
594 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200595 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100596 struct htx_sl *sl = NULL;
597 unsigned int sl_flags = 0;
598
599 lck = ck = -1; // no cookie for now
600 fields = 0;
601 for (idx = 0; list[idx].n.len != 0; idx++) {
602 if (!list[idx].n.ptr) {
603 /* this is an indexed pseudo-header */
604 phdr = list[idx].n.len;
605 }
606 else {
607 /* this can be any type of header */
608 /* RFC7540#8.1.2: upper case not allowed in header field names */
609 for (i = 0; i < list[idx].n.len; i++)
610 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
611 goto fail;
612
613 phdr = h2_str_to_phdr(list[idx].n);
614 }
615
616 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
617 /* insert a pseudo header by its index (in phdr) and value (in value) */
618 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
619 if (fields & H2_PHDR_FND_NONE) {
620 /* pseudo header field after regular headers */
621 goto fail;
622 }
623 else {
624 /* repeated pseudo header field */
625 goto fail;
626 }
627 }
628 fields |= 1 << phdr;
629 phdr_val[phdr] = list[idx].v;
630 continue;
631 }
632 else if (phdr != 0) {
633 /* invalid pseudo header -- should never happen here */
634 goto fail;
635 }
636
637 /* regular header field in (name,value) */
638 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
639 /* no more pseudo-headers, time to build the request line */
640 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
641 if (!sl)
642 goto fail;
643 fields |= H2_PHDR_FND_NONE;
644 }
645
646 if (isteq(list[idx].n, ist("host")))
647 fields |= H2_PHDR_FND_HOST;
648
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100649 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100650 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100651 if (ret < 0)
652 goto fail;
653
Willy Tarreau6deb4122018-11-27 15:34:18 +0100654 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100655 if (ret == 0)
656 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100657 }
658
659 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
660 if (isteq(list[idx].n, ist("connection")) ||
661 isteq(list[idx].n, ist("proxy-connection")) ||
662 isteq(list[idx].n, ist("keep-alive")) ||
663 isteq(list[idx].n, ist("upgrade")) ||
664 isteq(list[idx].n, ist("transfer-encoding")))
665 goto fail;
666
667 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
668 goto fail;
669
670 /* cookie requires special processing at the end */
671 if (isteq(list[idx].n, ist("cookie"))) {
672 list[idx].n.len = -1;
673
674 if (ck < 0)
675 ck = idx;
676 else
677 list[lck].n.len = idx;
678
679 lck = idx;
680 continue;
681 }
682
683 if (!htx_add_header(htx, list[idx].n, list[idx].v))
684 goto fail;
685 }
686
687 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
688 if (fields & H2_PHDR_FND_STAT)
689 goto fail;
690
691 /* Let's dump the request now if not yet emitted. */
692 if (!(fields & H2_PHDR_FND_NONE)) {
693 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
694 if (!sl)
695 goto fail;
696 }
697
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100698 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
699 sl_flags |= HTX_SL_F_BODYLESS;
700
Willy Tarreau6deb4122018-11-27 15:34:18 +0100701 /* update the start line with last detected header info */
702 sl->flags |= sl_flags;
703
704 /* complete with missing Host if needed */
705 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
706 /* missing Host field, use :authority instead */
707 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
708 goto fail;
709 }
710
711 /* now we may have to build a cookie list. We'll dump the values of all
712 * visited headers.
713 */
714 if (ck >= 0) {
715 uint32_t fs; // free space
716 uint32_t bs; // block size
717 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100718 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100719 struct htx_blk *blk;
720
721 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
722 if (!blk)
723 goto fail;
724
Willy Tarreau164e0612018-12-18 11:00:41 +0100725 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100726 fs = htx_free_data_space(htx);
727 bs = htx_get_blksz(blk);
728
729 /* for each extra cookie, we'll extend the cookie's value and
730 * insert "; " before the new value.
731 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100732 fs += tl; // first one is already counted
733 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100734 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100735 tl += vl + 2;
736 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100737 goto fail;
738
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200739 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100740 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
741 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
742 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
743 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100744 }
745
746 }
747
748 /* now send the end of headers marker */
749 htx_add_endof(htx, HTX_BLK_EOH);
750
Christopher Faulet33543e72019-05-15 15:53:20 +0200751 /* Set bytes used in the HTX mesage for the headers now */
752 sl->hdrs_bytes = htx_used_space(htx) - used;
753
Willy Tarreau6deb4122018-11-27 15:34:18 +0100754 ret = 1;
755 return ret;
756
757 fail:
758 return -1;
759}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200760
761/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
762 * <fields> indicates what was found so far. This should be called once at the
763 * detection of the first general header field or at the end of the message if
764 * no general header field was found yet. Returns the created start line on
765 * success, or NULL on failure. Upon success, <msgf> is updated with a few
766 * H2_MSGF_* flags indicating what was found while parsing.
767 */
768static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
769{
770 unsigned int flags = HTX_SL_F_NONE;
771 struct htx_sl *sl;
772 unsigned char h, t, u;
773
774 /* only :status is allowed as a pseudo header */
775 if (!(fields & H2_PHDR_FND_STAT))
776 goto fail;
777
778 if (phdr[H2_PHDR_IDX_STAT].len != 3)
779 goto fail;
780
781 /* Set HTX start-line flags */
782 flags |= HTX_SL_F_VER_11; // V2 in fact
783 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
784
785 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
786 if (!sl)
787 goto fail;
788
789 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
790 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
791 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
792 if (h > 9 || t > 9 || u > 9)
793 goto fail;
794
795 sl->info.res.status = h * 100 + t * 10 + u;
796
Christopher Faulet0b465482019-02-19 15:14:23 +0100797 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
798 * there is no body. So remove the flag H2_MSGF_BODY and add
799 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
800 * expected.
801 */
802 if (sl->info.res.status < 200 &&
803 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
804 *msgf |= H2_MSGF_RSP_1XX;
805 *msgf &= ~H2_MSGF_BODY;
806 }
807
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200808 return sl;
809 fail:
810 return NULL;
811}
812
813/* Takes an H2 response present in the headers list <list> terminated by a name
814 * being <NULL,0> and emits the equivalent HTX response according to the rules
815 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
816 * a positive value is returned if some bytes were emitted. In case of error, a
817 * negative error code is returned.
818 *
819 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
820 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
821 * if a body is detected (!ES).
822 *
823 * The headers list <list> must be composed of :
824 * - n.name != NULL, n.len > 0 : literal header name
825 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
826 * among H2_PHDR_IDX_*
827 * - n.name ignored, n.len == 0 : end of list
828 * - in all cases except the end of list, v.name and v.len must designate a
829 * valid value.
830 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100831int 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 +0200832{
833 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
834 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
835 uint32_t idx;
836 int phdr;
837 int ret;
838 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200839 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200840 struct htx_sl *sl = NULL;
841 unsigned int sl_flags = 0;
842
843 fields = 0;
844 for (idx = 0; list[idx].n.len != 0; idx++) {
845 if (!list[idx].n.ptr) {
846 /* this is an indexed pseudo-header */
847 phdr = list[idx].n.len;
848 }
849 else {
850 /* this can be any type of header */
851 /* RFC7540#8.1.2: upper case not allowed in header field names */
852 for (i = 0; i < list[idx].n.len; i++)
853 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
854 goto fail;
855
856 phdr = h2_str_to_phdr(list[idx].n);
857 }
858
859 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
860 /* insert a pseudo header by its index (in phdr) and value (in value) */
861 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
862 if (fields & H2_PHDR_FND_NONE) {
863 /* pseudo header field after regular headers */
864 goto fail;
865 }
866 else {
867 /* repeated pseudo header field */
868 goto fail;
869 }
870 }
871 fields |= 1 << phdr;
872 phdr_val[phdr] = list[idx].v;
873 continue;
874 }
875 else if (phdr != 0) {
876 /* invalid pseudo header -- should never happen here */
877 goto fail;
878 }
879
880 /* regular header field in (name,value) */
881 if (!(fields & H2_PHDR_FND_NONE)) {
882 /* no more pseudo-headers, time to build the status line */
883 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
884 if (!sl)
885 goto fail;
886 fields |= H2_PHDR_FND_NONE;
887 }
888
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100889 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100890 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100891 if (ret < 0)
892 goto fail;
893
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200894 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100895 if (ret == 0)
896 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200897 }
898
899 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
900 if (isteq(list[idx].n, ist("connection")) ||
901 isteq(list[idx].n, ist("proxy-connection")) ||
902 isteq(list[idx].n, ist("keep-alive")) ||
903 isteq(list[idx].n, ist("upgrade")) ||
904 isteq(list[idx].n, ist("transfer-encoding")))
905 goto fail;
906
907 if (!htx_add_header(htx, list[idx].n, list[idx].v))
908 goto fail;
909 }
910
911 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
912 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
913 goto fail;
914
915 /* Let's dump the request now if not yet emitted. */
916 if (!(fields & H2_PHDR_FND_NONE)) {
917 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
918 if (!sl)
919 goto fail;
920 }
921
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100922 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
923 sl_flags |= HTX_SL_F_BODYLESS;
924
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200925 /* update the start line with last detected header info */
926 sl->flags |= sl_flags;
927
928 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
929 /* FIXME: Do we need to signal anything when we have a body and
930 * no content-length, to have the equivalent of H1's chunked
931 * encoding?
932 */
933 }
934
935 /* now send the end of headers marker */
936 htx_add_endof(htx, HTX_BLK_EOH);
937
Christopher Faulet33543e72019-05-15 15:53:20 +0200938 /* Set bytes used in the HTX mesage for the headers now */
939 sl->hdrs_bytes = htx_used_space(htx) - used;
940
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200941 ret = 1;
942 return ret;
943
944 fail:
945 return -1;
946}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100947
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200948/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
949 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
950 * and a positive value is returned if some bytes were emitted. In case of
951 * error, a negative error code is returned. The caller must have verified that
952 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100953 *
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{
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100964 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100965 int i;
966
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100967 for (idx = 0; list[idx].n.len != 0; idx++) {
968 if (!list[idx].n.ptr) {
969 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
970 goto fail;
971 }
972
973 /* RFC7540#8.1.2: upper case not allowed in header field names */
974 for (i = 0; i < list[idx].n.len; i++)
975 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
976 goto fail;
977
978 if (h2_str_to_phdr(list[idx].n) != 0) {
979 /* This is a pseudo-header (RFC7540#8.1.2.1) */
980 goto fail;
981 }
982
983 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
984 if (isteq(list[idx].n, ist("host")) ||
985 isteq(list[idx].n, ist("content-length")) ||
986 isteq(list[idx].n, ist("connection")) ||
987 isteq(list[idx].n, ist("proxy-connection")) ||
988 isteq(list[idx].n, ist("keep-alive")) ||
989 isteq(list[idx].n, ist("upgrade")) ||
990 isteq(list[idx].n, ist("te")) ||
991 isteq(list[idx].n, ist("transfer-encoding")))
992 goto fail;
993
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200994 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
995 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100996 }
997
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200998 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100999 goto fail;
1000
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001001 return 1;
1002
1003 fail:
1004 return -1;
1005}