blob: bfc0bdafea230c75bc3c147b7330445aa126e990 [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
Willy Tarreauc36c6782019-11-22 16:02:43 +010047/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
48 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
49 * non-zero if such a character is found, 0 otherwise. When run on unlikely
50 * header match, it's recommended to first check for the presence of control
51 * chars using ist_find_ctl().
52 */
53static int has_forbidden_char(const struct ist ist, const char *start)
54{
55 do {
56 if ((uint8_t)*start <= 0x0d &&
57 (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
58 return 1;
59 start++;
60 } while (start < ist.ptr + ist.len);
61 return 0;
62}
63
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010064/* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers
65 * stored in <phdr[]>. <fields> indicates what was found so far. This should be
66 * called once at the detection of the first general header field or at the end
67 * of the request if no general header field was found yet. Returns 0 on success
Willy Tarreau174b06a2018-04-25 18:13:58 +020068 * or a negative error code on failure. Upon success, <msgf> is updated with a
69 * few H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010070 */
Willy Tarreau174b06a2018-04-25 18:13:58 +020071static 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 +010072{
73 char *out = *ptr;
74 int uri_idx = H2_PHDR_IDX_PATH;
75
76 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
77 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
78 * MUST be omitted ; ":authority" contains the host and port
79 * to connect to.
80 */
81 if (fields & H2_PHDR_FND_SCHM) {
82 /* scheme not allowed */
83 goto fail;
84 }
85 else if (fields & H2_PHDR_FND_PATH) {
86 /* path not allowed */
87 goto fail;
88 }
89 else if (!(fields & H2_PHDR_FND_AUTH)) {
90 /* missing authority */
91 goto fail;
92 }
93 // otherwise OK ; let's use the authority instead of the URI
94 uri_idx = H2_PHDR_IDX_AUTH;
Willy Tarreau174b06a2018-04-25 18:13:58 +020095 *msgf |= H2_MSGF_BODY_TUNNEL;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010096 }
97 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
98 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
99 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
100 * valid value for the ":method", ":scheme" and ":path" phdr
101 * unless it is a CONNECT request.
102 */
103 if (!(fields & H2_PHDR_FND_METH)) {
104 /* missing method */
105 goto fail;
106 }
107 else if (!(fields & H2_PHDR_FND_SCHM)) {
108 /* missing scheme */
109 goto fail;
110 }
111 else {
112 /* missing path */
113 goto fail;
114 }
115 }
116
Willy Tarreaucd4fe172017-12-03 11:51:31 +0100117 /* 7540#8.1.2.3: :path must not be empty */
118 if (!phdr[uri_idx].len)
119 goto fail;
120
Willy Tarreau811ad122017-12-03 09:44:50 +0100121 if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) {
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100122 /* too large */
123 goto fail;
124 }
125
126 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
127 out += phdr[H2_PHDR_IDX_METH].len;
128 *(out++) = ' ';
129
130 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
131 out += phdr[uri_idx].len;
132 memcpy(out, " HTTP/1.1\r\n", 11);
133 out += 11;
134
135 *ptr = out;
136 return 0;
137 fail:
138 return -1;
139}
140
141/* Takes an H2 request present in the headers list <list> terminated by a name
142 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
143 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
144 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
145 * case of error, a negative error code is returned.
146 *
Willy Tarreau174b06a2018-04-25 18:13:58 +0200147 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
148 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
149 * if a body is detected (!ES).
150 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100151 * The headers list <list> must be composed of :
152 * - n.name != NULL, n.len > 0 : literal header name
153 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
154 * among H2_PHDR_IDX_*
155 * - n.name ignored, n.len == 0 : end of list
156 * - in all cases except the end of list, v.name and v.len must designate a
157 * valid value.
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100158 *
159 * The Cookie header will be reassembled at the end, and for this, the <list>
160 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100161 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100162int 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 +0100163{
164 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
165 char *out_end = out + osize;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100166 const char *ctl;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100167 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
168 uint32_t idx;
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100169 int ck, lck; /* cookie index and last cookie index */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100170 int phdr;
171 int ret;
Willy Tarreau637f64d2017-12-03 20:28:13 +0100172 int i;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100173
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100174 lck = ck = -1; // no cookie for now
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100175 fields = 0;
176 for (idx = 0; list[idx].n.len != 0; idx++) {
177 if (!list[idx].n.ptr) {
178 /* this is an indexed pseudo-header */
179 phdr = list[idx].n.len;
180 }
181 else {
182 /* this can be any type of header */
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100183 /* RFC7540#8.1.2: upper case not allowed in header field names.
184 * #10.3: header names must be valid (i.e. match a token).
185 * For pseudo-headers we check from 2nd char and for other ones
186 * from the first char, because HTTP_IS_TOKEN() also excludes
187 * the colon.
188 */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100189 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100190
191 for (i = !!phdr; i < list[idx].n.len; i++)
192 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
193 goto fail;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100194 }
195
Willy Tarreauc36c6782019-11-22 16:02:43 +0100196 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
197 * rejecting NUL, CR and LF characters.
198 */
199 ctl = ist_find_ctl(list[idx].v);
200 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
201 goto fail;
202
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100203 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
204 /* insert a pseudo header by its index (in phdr) and value (in value) */
205 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
206 if (fields & H2_PHDR_FND_NONE) {
207 /* pseudo header field after regular headers */
208 goto fail;
209 }
210 else {
211 /* repeated pseudo header field */
212 goto fail;
213 }
214 }
215 fields |= 1 << phdr;
216 phdr_val[phdr] = list[idx].v;
217 continue;
218 }
219 else if (phdr != 0) {
220 /* invalid pseudo header -- should never happen here */
221 goto fail;
222 }
223
224 /* regular header field in (name,value) */
225 if (!(fields & H2_PHDR_FND_NONE)) {
226 /* no more pseudo-headers, time to build the request line */
Willy Tarreau174b06a2018-04-25 18:13:58 +0200227 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100228 if (ret != 0)
229 goto leave;
230 fields |= H2_PHDR_FND_NONE;
231 }
232
233 if (isteq(list[idx].n, ist("host")))
234 fields |= H2_PHDR_FND_HOST;
235
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100236 if (isteq(list[idx].n, ist("content-length"))) {
237 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
238 if (ret < 0)
239 goto fail;
240
241 if (ret == 0)
242 continue; // skip this duplicate
243 }
Willy Tarreau174b06a2018-04-25 18:13:58 +0200244
Willy Tarreaufe7c3562017-12-03 20:15:34 +0100245 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
246 if (isteq(list[idx].n, ist("connection")) ||
247 isteq(list[idx].n, ist("proxy-connection")) ||
248 isteq(list[idx].n, ist("keep-alive")) ||
249 isteq(list[idx].n, ist("upgrade")) ||
250 isteq(list[idx].n, ist("transfer-encoding")))
251 goto fail;
252
Willy Tarreaud8d2ac72017-12-03 18:41:31 +0100253 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
254 goto fail;
255
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100256 /* cookie requires special processing at the end */
257 if (isteq(list[idx].n, ist("cookie"))) {
258 list[idx].n.len = -1;
259
260 if (ck < 0)
261 ck = idx;
262 else
263 list[lck].n.len = idx;
264
265 lck = idx;
266 continue;
267 }
268
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100269 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
270 /* too large */
271 goto fail;
272 }
273
274 /* copy "name: value" */
275 memcpy(out, list[idx].n.ptr, list[idx].n.len);
276 out += list[idx].n.len;
277 *(out++) = ':';
278 *(out++) = ' ';
279
280 memcpy(out, list[idx].v.ptr, list[idx].v.len);
281 out += list[idx].v.len;
282 *(out++) = '\r';
283 *(out++) = '\n';
284 }
285
Willy Tarreau52088692017-12-03 20:13:54 +0100286 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
287 if (fields & H2_PHDR_FND_STAT)
288 goto fail;
289
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100290 /* Let's dump the request now if not yet emitted. */
291 if (!(fields & H2_PHDR_FND_NONE)) {
Willy Tarreau174b06a2018-04-25 18:13:58 +0200292 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100293 if (ret != 0)
294 goto leave;
295 }
296
297 /* complete with missing Host if needed */
298 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
299 /* missing Host field, use :authority instead */
300 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
301 /* too large */
302 goto fail;
303 }
304
305 memcpy(out, "host: ", 6);
306 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
307 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
308 *(out++) = '\r';
309 *(out++) = '\n';
310 }
311
Willy Tarreaueba10f22018-04-25 20:44:22 +0200312 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
313 /* add chunked encoding */
314 if (out + 28 > out_end)
315 goto fail;
316 memcpy(out, "transfer-encoding: chunked\r\n", 28);
317 out += 28;
318 }
319
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100320 /* now we may have to build a cookie list. We'll dump the values of all
321 * visited headers.
322 */
323 if (ck >= 0) {
324 if (out + 8 > out_end) {
325 /* too large */
326 goto fail;
327 }
328 memcpy(out, "cookie: ", 8);
329 out += 8;
330
331 do {
332 if (out + list[ck].v.len + 2 > out_end) {
333 /* too large */
334 goto fail;
335 }
336 memcpy(out, list[ck].v.ptr, list[ck].v.len);
337 out += list[ck].v.len;
338 ck = list[ck].n.len;
339
340 if (ck >= 0) {
341 *(out++) = ';';
342 *(out++) = ' ';
343 }
344 } while (ck >= 0);
345
346 if (out + 2 > out_end) {
347 /* too large */
348 goto fail;
349 }
350 *(out++) = '\r';
351 *(out++) = '\n';
352 }
353
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100354 /* And finish */
355 if (out + 2 > out_end) {
356 /* too large */
357 goto fail;
358 }
359
360 *(out++) = '\r';
361 *(out++) = '\n';
362 ret = out + osize - out_end;
363 leave:
364 return ret;
365
366 fail:
367 return -1;
368}
Willy Tarreau6deb4122018-11-27 15:34:18 +0100369
Willy Tarreau9d953e72019-01-03 16:18:14 +0100370/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
371 * emits the equivalent HTTP/1.1 trailers block not including the empty line.
372 * The output contents are emitted in <out> for a max of <osize> bytes, and the
373 * amount of bytes emitted is returned. In case of error, a negative error code
374 * is returned. The caller must have verified that the message in the buffer is
375 * compatible with receipt of trailers.
376 *
377 * The headers list <list> must be composed of :
378 * - n.name != NULL, n.len > 0 : literal header name
379 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
380 * among H2_PHDR_IDX_* (illegal here)
381 * - n.name ignored, n.len == 0 : end of list
382 * - in all cases except the end of list, v.name and v.len must designate a
383 * valid value.
384 */
385int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize)
386{
387 char *out_end = out + osize;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100388 const char *ctl;
Willy Tarreau9d953e72019-01-03 16:18:14 +0100389 uint32_t idx;
390 int i;
391
392 for (idx = 0; list[idx].n.len != 0; idx++) {
393 if (!list[idx].n.ptr) {
394 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
395 goto fail;
396 }
397
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100398 /* RFC7540#8.1.2: upper case not allowed in header field names.
399 * #10.3: header names must be valid (i.e. match a token). This
400 * also catches pseudo-headers which are forbidden in trailers.
401 */
Willy Tarreau9d953e72019-01-03 16:18:14 +0100402 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100403 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
Willy Tarreau9d953e72019-01-03 16:18:14 +0100404 goto fail;
405
Willy Tarreau9d953e72019-01-03 16:18:14 +0100406 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
407 if (isteq(list[idx].n, ist("host")) ||
408 isteq(list[idx].n, ist("content-length")) ||
409 isteq(list[idx].n, ist("connection")) ||
410 isteq(list[idx].n, ist("proxy-connection")) ||
411 isteq(list[idx].n, ist("keep-alive")) ||
412 isteq(list[idx].n, ist("upgrade")) ||
413 isteq(list[idx].n, ist("te")) ||
414 isteq(list[idx].n, ist("transfer-encoding")))
415 goto fail;
416
417 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
418 /* too large */
419 goto fail;
420 }
421
Willy Tarreauc36c6782019-11-22 16:02:43 +0100422 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
423 * rejecting NUL, CR and LF characters.
424 */
425 ctl = ist_find_ctl(list[idx].v);
426 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
427 goto fail;
428
Willy Tarreau9d953e72019-01-03 16:18:14 +0100429 /* copy "name: value" */
430 memcpy(out, list[idx].n.ptr, list[idx].n.len);
431 out += list[idx].n.len;
432 *(out++) = ':';
433 *(out++) = ' ';
434
435 memcpy(out, list[idx].v.ptr, list[idx].v.len);
436 out += list[idx].v.len;
437 *(out++) = '\r';
438 *(out++) = '\n';
439 }
440
441 return out + osize - out_end;
442
443 fail:
444 return -1;
445}
446
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100447/* Parse the Content-Length header field of an HTTP/2 request. The function
448 * checks all possible occurrences of a comma-delimited value, and verifies
449 * if any of them doesn't match a previous value. It returns <0 if a value
450 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
451 * if the value can be indexed (first one). In the last case, the value might
452 * be adjusted and the caller must only add the updated value.
453 */
454int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
455{
456 char *e, *n;
457 unsigned long long cl;
458 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
459 struct ist word;
460
461 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
462 e = value->ptr + value->len;
463
464 while (++word.ptr < e) {
465 /* skip leading delimitor and blanks */
466 if (unlikely(HTTP_IS_LWS(*word.ptr)))
467 continue;
468
469 /* digits only now */
470 for (cl = 0, n = word.ptr; n < e; n++) {
471 unsigned int c = *n - '0';
472 if (unlikely(c > 9)) {
473 /* non-digit */
474 if (unlikely(n == word.ptr)) // spaces only
475 goto fail;
476 break;
477 }
478 if (unlikely(cl > ULLONG_MAX / 10ULL))
479 goto fail; /* multiply overflow */
480 cl = cl * 10ULL;
481 if (unlikely(cl + c < cl))
482 goto fail; /* addition overflow */
483 cl = cl + c;
484 }
485
486 /* keep a copy of the exact cleaned value */
487 word.len = n - word.ptr;
488
489 /* skip trailing LWS till next comma or EOL */
490 for (; n < e; n++) {
491 if (!HTTP_IS_LWS(*n)) {
492 if (unlikely(*n != ','))
493 goto fail;
494 break;
495 }
496 }
497
498 /* if duplicate, must be equal */
499 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
500 goto fail;
501
502 /* OK, store this result as the one to be indexed */
503 *msgf |= H2_MSGF_BODY_CL;
504 *body_len = cl;
505 *value = word;
506 word.ptr = n;
507 }
508 /* here we've reached the end with a single value or a series of
509 * identical values, all matching previous series if any. The last
510 * parsed value was sent back into <value>. We just have to decide
511 * if this occurrence has to be indexed (it's the first one) or
512 * silently skipped (it's not the first one)
513 */
514 return !not_first;
515 fail:
516 return -1;
517}
518
Willy Tarreau6deb4122018-11-27 15:34:18 +0100519/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
520 * <fields> indicates what was found so far. This should be called once at the
521 * detection of the first general header field or at the end of the request if
522 * no general header field was found yet. Returns the created start line on
523 * success, or NULL on failure. Upon success, <msgf> is updated with a few
524 * H2_MSGF_* flags indicating what was found while parsing.
525 */
526static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
527{
528 int uri_idx = H2_PHDR_IDX_PATH;
529 unsigned int flags = HTX_SL_F_NONE;
530 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100531 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100532
533 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
534 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
535 * MUST be omitted ; ":authority" contains the host and port
536 * to connect to.
537 */
538 if (fields & H2_PHDR_FND_SCHM) {
539 /* scheme not allowed */
540 goto fail;
541 }
542 else if (fields & H2_PHDR_FND_PATH) {
543 /* path not allowed */
544 goto fail;
545 }
546 else if (!(fields & H2_PHDR_FND_AUTH)) {
547 /* missing authority */
548 goto fail;
549 }
550 // otherwise OK ; let's use the authority instead of the URI
551 uri_idx = H2_PHDR_IDX_AUTH;
552 *msgf |= H2_MSGF_BODY_TUNNEL;
553 }
554 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
555 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
556 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
557 * valid value for the ":method", ":scheme" and ":path" phdr
558 * unless it is a CONNECT request.
559 */
560 if (!(fields & H2_PHDR_FND_METH)) {
561 /* missing method */
562 goto fail;
563 }
564 else if (!(fields & H2_PHDR_FND_SCHM)) {
565 /* missing scheme */
566 goto fail;
567 }
568 else {
569 /* missing path */
570 goto fail;
571 }
572 }
573
Willy Tarreaub2318bc2021-08-11 11:12:46 +0200574 /* The method is a non-empty token (RFC7231#4.1) */
575 if (!phdr[H2_PHDR_IDX_METH].len)
576 goto fail;
577 for (i = 0; i < phdr[H2_PHDR_IDX_METH].len; i++) {
578 if (!HTTP_IS_TOKEN(phdr[H2_PHDR_IDX_METH].ptr[i]))
579 htx->flags |= HTX_FL_PARSING_ERROR;
580 }
581
Willy Tarreau6deb4122018-11-27 15:34:18 +0100582 /* 7540#8.1.2.3: :path must not be empty */
583 if (!phdr[uri_idx].len)
584 goto fail;
585
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100586 /* make sure :path doesn't contain LWS nor CTL characters */
587 for (i = 0; i < phdr[uri_idx].len; i++) {
588 unsigned char c = phdr[uri_idx].ptr[i];
589 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
590 htx->flags |= HTX_FL_PARSING_ERROR;
591 }
592
Willy Tarreau6deb4122018-11-27 15:34:18 +0100593 /* Set HTX start-line flags */
594 flags |= HTX_SL_F_VER_11; // V2 in fact
595 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
596
597 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
598 if (!sl)
599 goto fail;
600
601 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 +0200602 sl->flags |= HTX_SL_F_HAS_SCHM;
603 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 +0100604 return sl;
605 fail:
606 return NULL;
607}
608
609/* Takes an H2 request present in the headers list <list> terminated by a name
610 * being <NULL,0> and emits the equivalent HTX request according to the rules
611 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
612 * non-zero is returned if some bytes were emitted. In case of error, a
613 * negative error code is returned.
614 *
615 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
616 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
617 * if a body is detected (!ES).
618 *
619 * The headers list <list> must be composed of :
620 * - n.name != NULL, n.len > 0 : literal header name
621 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
622 * among H2_PHDR_IDX_*
623 * - n.name ignored, n.len == 0 : end of list
624 * - in all cases except the end of list, v.name and v.len must designate a
625 * valid value.
626 *
627 * The Cookie header will be reassembled at the end, and for this, the <list>
628 * will be used to create a linked list, so its contents may be destroyed.
629 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100630int 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 +0100631{
632 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
633 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
634 uint32_t idx;
635 int ck, lck; /* cookie index and last cookie index */
636 int phdr;
637 int ret;
638 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200639 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100640 struct htx_sl *sl = NULL;
641 unsigned int sl_flags = 0;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100642 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100643
644 lck = ck = -1; // no cookie for now
645 fields = 0;
646 for (idx = 0; list[idx].n.len != 0; idx++) {
647 if (!list[idx].n.ptr) {
648 /* this is an indexed pseudo-header */
649 phdr = list[idx].n.len;
650 }
651 else {
652 /* this can be any type of header */
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100653 /* RFC7540#8.1.2: upper case not allowed in header field names.
654 * #10.3: header names must be valid (i.e. match a token).
655 * For pseudo-headers we check from 2nd char and for other ones
656 * from the first char, because HTTP_IS_TOKEN() also excludes
657 * the colon.
658 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100659 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100660
661 for (i = !!phdr; i < list[idx].n.len; i++)
662 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
663 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100664 }
665
Willy Tarreauc36c6782019-11-22 16:02:43 +0100666 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
667 * rejecting NUL, CR and LF characters.
668 */
669 ctl = ist_find_ctl(list[idx].v);
670 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
671 goto fail;
672
Willy Tarreau6deb4122018-11-27 15:34:18 +0100673 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
674 /* insert a pseudo header by its index (in phdr) and value (in value) */
675 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
676 if (fields & H2_PHDR_FND_NONE) {
677 /* pseudo header field after regular headers */
678 goto fail;
679 }
680 else {
681 /* repeated pseudo header field */
682 goto fail;
683 }
684 }
685 fields |= 1 << phdr;
686 phdr_val[phdr] = list[idx].v;
687 continue;
688 }
689 else if (phdr != 0) {
690 /* invalid pseudo header -- should never happen here */
691 goto fail;
692 }
693
694 /* regular header field in (name,value) */
695 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
696 /* no more pseudo-headers, time to build the request line */
697 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
698 if (!sl)
699 goto fail;
700 fields |= H2_PHDR_FND_NONE;
701 }
702
703 if (isteq(list[idx].n, ist("host")))
704 fields |= H2_PHDR_FND_HOST;
705
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100706 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100707 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100708 if (ret < 0)
709 goto fail;
710
Willy Tarreau6deb4122018-11-27 15:34:18 +0100711 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100712 if (ret == 0)
713 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100714 }
715
716 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
717 if (isteq(list[idx].n, ist("connection")) ||
718 isteq(list[idx].n, ist("proxy-connection")) ||
719 isteq(list[idx].n, ist("keep-alive")) ||
720 isteq(list[idx].n, ist("upgrade")) ||
721 isteq(list[idx].n, ist("transfer-encoding")))
722 goto fail;
723
724 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
725 goto fail;
726
727 /* cookie requires special processing at the end */
728 if (isteq(list[idx].n, ist("cookie"))) {
729 list[idx].n.len = -1;
730
731 if (ck < 0)
732 ck = idx;
733 else
734 list[lck].n.len = idx;
735
736 lck = idx;
737 continue;
738 }
739
740 if (!htx_add_header(htx, list[idx].n, list[idx].v))
741 goto fail;
742 }
743
744 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
745 if (fields & H2_PHDR_FND_STAT)
746 goto fail;
747
748 /* Let's dump the request now if not yet emitted. */
749 if (!(fields & H2_PHDR_FND_NONE)) {
750 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
751 if (!sl)
752 goto fail;
753 }
754
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100755 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
756 sl_flags |= HTX_SL_F_BODYLESS;
757
Willy Tarreau6deb4122018-11-27 15:34:18 +0100758 /* update the start line with last detected header info */
759 sl->flags |= sl_flags;
760
761 /* complete with missing Host if needed */
762 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
763 /* missing Host field, use :authority instead */
764 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
765 goto fail;
766 }
767
768 /* now we may have to build a cookie list. We'll dump the values of all
769 * visited headers.
770 */
771 if (ck >= 0) {
772 uint32_t fs; // free space
773 uint32_t bs; // block size
774 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100775 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100776 struct htx_blk *blk;
777
778 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
779 if (!blk)
780 goto fail;
781
Willy Tarreau164e0612018-12-18 11:00:41 +0100782 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100783 fs = htx_free_data_space(htx);
784 bs = htx_get_blksz(blk);
785
786 /* for each extra cookie, we'll extend the cookie's value and
787 * insert "; " before the new value.
788 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100789 fs += tl; // first one is already counted
790 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100791 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100792 tl += vl + 2;
793 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100794 goto fail;
795
Christopher Faulet41dc8432019-06-18 09:49:16 +0200796 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100797 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
798 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
799 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
800 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100801 }
802
803 }
804
805 /* now send the end of headers marker */
806 htx_add_endof(htx, HTX_BLK_EOH);
807
Christopher Faulet33543e72019-05-15 15:53:20 +0200808 /* Set bytes used in the HTX mesage for the headers now */
809 sl->hdrs_bytes = htx_used_space(htx) - used;
810
Willy Tarreau6deb4122018-11-27 15:34:18 +0100811 ret = 1;
812 return ret;
813
814 fail:
815 return -1;
816}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200817
818/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
819 * <fields> indicates what was found so far. This should be called once at the
820 * detection of the first general header field or at the end of the message if
821 * no general header field was found yet. Returns the created start line on
822 * success, or NULL on failure. Upon success, <msgf> is updated with a few
823 * H2_MSGF_* flags indicating what was found while parsing.
824 */
825static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
826{
827 unsigned int flags = HTX_SL_F_NONE;
828 struct htx_sl *sl;
829 unsigned char h, t, u;
830
831 /* only :status is allowed as a pseudo header */
832 if (!(fields & H2_PHDR_FND_STAT))
833 goto fail;
834
835 if (phdr[H2_PHDR_IDX_STAT].len != 3)
836 goto fail;
837
838 /* Set HTX start-line flags */
839 flags |= HTX_SL_F_VER_11; // V2 in fact
840 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
841
842 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
843 if (!sl)
844 goto fail;
845
846 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
847 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
848 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
849 if (h > 9 || t > 9 || u > 9)
850 goto fail;
851
852 sl->info.res.status = h * 100 + t * 10 + u;
853
Christopher Faulet0b465482019-02-19 15:14:23 +0100854 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
855 * there is no body. So remove the flag H2_MSGF_BODY and add
856 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
857 * expected.
858 */
859 if (sl->info.res.status < 200 &&
860 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
861 *msgf |= H2_MSGF_RSP_1XX;
862 *msgf &= ~H2_MSGF_BODY;
863 }
864
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200865 return sl;
866 fail:
867 return NULL;
868}
869
870/* Takes an H2 response present in the headers list <list> terminated by a name
871 * being <NULL,0> and emits the equivalent HTX response according to the rules
872 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
873 * a positive value is returned if some bytes were emitted. In case of error, a
874 * negative error code is returned.
875 *
876 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
877 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
878 * if a body is detected (!ES).
879 *
880 * The headers list <list> must be composed of :
881 * - n.name != NULL, n.len > 0 : literal header name
882 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
883 * among H2_PHDR_IDX_*
884 * - n.name ignored, n.len == 0 : end of list
885 * - in all cases except the end of list, v.name and v.len must designate a
886 * valid value.
887 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100888int 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 +0200889{
890 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
891 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
892 uint32_t idx;
893 int phdr;
894 int ret;
895 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200896 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200897 struct htx_sl *sl = NULL;
898 unsigned int sl_flags = 0;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100899 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200900
901 fields = 0;
902 for (idx = 0; list[idx].n.len != 0; idx++) {
903 if (!list[idx].n.ptr) {
904 /* this is an indexed pseudo-header */
905 phdr = list[idx].n.len;
906 }
907 else {
908 /* this can be any type of header */
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100909 /* RFC7540#8.1.2: upper case not allowed in header field names.
910 * #10.3: header names must be valid (i.e. match a token).
911 * For pseudo-headers we check from 2nd char and for other ones
912 * from the first char, because HTTP_IS_TOKEN() also excludes
913 * the colon.
914 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200915 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100916
917 for (i = !!phdr; i < list[idx].n.len; i++)
918 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
919 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200920 }
921
Willy Tarreauc36c6782019-11-22 16:02:43 +0100922 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
923 * rejecting NUL, CR and LF characters.
924 */
925 ctl = ist_find_ctl(list[idx].v);
926 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
927 goto fail;
928
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200929 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
930 /* insert a pseudo header by its index (in phdr) and value (in value) */
931 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
932 if (fields & H2_PHDR_FND_NONE) {
933 /* pseudo header field after regular headers */
934 goto fail;
935 }
936 else {
937 /* repeated pseudo header field */
938 goto fail;
939 }
940 }
941 fields |= 1 << phdr;
942 phdr_val[phdr] = list[idx].v;
943 continue;
944 }
945 else if (phdr != 0) {
946 /* invalid pseudo header -- should never happen here */
947 goto fail;
948 }
949
950 /* regular header field in (name,value) */
951 if (!(fields & H2_PHDR_FND_NONE)) {
952 /* no more pseudo-headers, time to build the status line */
953 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
954 if (!sl)
955 goto fail;
956 fields |= H2_PHDR_FND_NONE;
957 }
958
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100959 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100960 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100961 if (ret < 0)
962 goto fail;
963
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200964 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100965 if (ret == 0)
966 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200967 }
968
969 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
970 if (isteq(list[idx].n, ist("connection")) ||
971 isteq(list[idx].n, ist("proxy-connection")) ||
972 isteq(list[idx].n, ist("keep-alive")) ||
973 isteq(list[idx].n, ist("upgrade")) ||
974 isteq(list[idx].n, ist("transfer-encoding")))
975 goto fail;
976
977 if (!htx_add_header(htx, list[idx].n, list[idx].v))
978 goto fail;
979 }
980
981 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
982 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
983 goto fail;
984
985 /* Let's dump the request now if not yet emitted. */
986 if (!(fields & H2_PHDR_FND_NONE)) {
987 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
988 if (!sl)
989 goto fail;
990 }
991
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100992 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
993 sl_flags |= HTX_SL_F_BODYLESS;
994
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200995 /* update the start line with last detected header info */
996 sl->flags |= sl_flags;
997
998 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
999 /* FIXME: Do we need to signal anything when we have a body and
1000 * no content-length, to have the equivalent of H1's chunked
1001 * encoding?
1002 */
1003 }
1004
1005 /* now send the end of headers marker */
1006 htx_add_endof(htx, HTX_BLK_EOH);
1007
Christopher Faulet33543e72019-05-15 15:53:20 +02001008 /* Set bytes used in the HTX mesage for the headers now */
1009 sl->hdrs_bytes = htx_used_space(htx) - used;
1010
Willy Tarreau1329b5b2018-10-08 14:49:20 +02001011 ret = 1;
1012 return ret;
1013
1014 fail:
1015 return -1;
1016}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001017
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001018/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
1019 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
1020 * and a positive value is returned if some bytes were emitted. In case of
1021 * error, a negative error code is returned. The caller must have verified that
1022 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001023 *
1024 * The headers list <list> must be composed of :
1025 * - n.name != NULL, n.len > 0 : literal header name
1026 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
1027 * among H2_PHDR_IDX_* (illegal here)
1028 * - n.name ignored, n.len == 0 : end of list
1029 * - in all cases except the end of list, v.name and v.len must designate a
1030 * valid value.
1031 */
1032int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
1033{
Willy Tarreauc36c6782019-11-22 16:02:43 +01001034 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001035 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001036 int i;
1037
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001038 for (idx = 0; list[idx].n.len != 0; idx++) {
1039 if (!list[idx].n.ptr) {
1040 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
1041 goto fail;
1042 }
1043
Willy Tarreau5eaeec52019-11-24 10:34:39 +01001044 /* RFC7540#8.1.2: upper case not allowed in header field names.
1045 * #10.3: header names must be valid (i.e. match a token). This
1046 * also catches pseudo-headers which are forbidden in trailers.
1047 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001048 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau5eaeec52019-11-24 10:34:39 +01001049 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001050 goto fail;
1051
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001052 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
1053 if (isteq(list[idx].n, ist("host")) ||
1054 isteq(list[idx].n, ist("content-length")) ||
1055 isteq(list[idx].n, ist("connection")) ||
1056 isteq(list[idx].n, ist("proxy-connection")) ||
1057 isteq(list[idx].n, ist("keep-alive")) ||
1058 isteq(list[idx].n, ist("upgrade")) ||
1059 isteq(list[idx].n, ist("te")) ||
1060 isteq(list[idx].n, ist("transfer-encoding")))
1061 goto fail;
1062
Willy Tarreauc36c6782019-11-22 16:02:43 +01001063 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
1064 * rejecting NUL, CR and LF characters.
1065 */
1066 ctl = ist_find_ctl(list[idx].v);
1067 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
1068 goto fail;
1069
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001070 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
1071 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001072 }
1073
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001074 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001075 goto fail;
1076
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001077 return 1;
1078
1079 fail:
1080 return -1;
1081}