blob: 719b1743b331fe49c07e59e1cd8fc50c8157e33a [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
574 /* 7540#8.1.2.3: :path must not be empty */
575 if (!phdr[uri_idx].len)
576 goto fail;
577
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100578 /* make sure :path doesn't contain LWS nor CTL characters */
579 for (i = 0; i < phdr[uri_idx].len; i++) {
580 unsigned char c = phdr[uri_idx].ptr[i];
581 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
582 htx->flags |= HTX_FL_PARSING_ERROR;
583 }
584
Willy Tarreau6deb4122018-11-27 15:34:18 +0100585 /* Set HTX start-line flags */
586 flags |= HTX_SL_F_VER_11; // V2 in fact
587 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
588
589 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
590 if (!sl)
591 goto fail;
592
593 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 +0200594 sl->flags |= HTX_SL_F_HAS_SCHM;
595 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 +0100596 return sl;
597 fail:
598 return NULL;
599}
600
601/* Takes an H2 request present in the headers list <list> terminated by a name
602 * being <NULL,0> and emits the equivalent HTX request according to the rules
603 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
604 * non-zero is returned if some bytes were emitted. In case of error, a
605 * negative error code is returned.
606 *
607 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
608 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
609 * if a body is detected (!ES).
610 *
611 * The headers list <list> must be composed of :
612 * - n.name != NULL, n.len > 0 : literal header name
613 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
614 * among H2_PHDR_IDX_*
615 * - n.name ignored, n.len == 0 : end of list
616 * - in all cases except the end of list, v.name and v.len must designate a
617 * valid value.
618 *
619 * The Cookie header will be reassembled at the end, and for this, the <list>
620 * will be used to create a linked list, so its contents may be destroyed.
621 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100622int 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 +0100623{
624 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
625 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
626 uint32_t idx;
627 int ck, lck; /* cookie index and last cookie index */
628 int phdr;
629 int ret;
630 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200631 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100632 struct htx_sl *sl = NULL;
633 unsigned int sl_flags = 0;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100634 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100635
636 lck = ck = -1; // no cookie for now
637 fields = 0;
638 for (idx = 0; list[idx].n.len != 0; idx++) {
639 if (!list[idx].n.ptr) {
640 /* this is an indexed pseudo-header */
641 phdr = list[idx].n.len;
642 }
643 else {
644 /* this can be any type of header */
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100645 /* RFC7540#8.1.2: upper case not allowed in header field names.
646 * #10.3: header names must be valid (i.e. match a token).
647 * For pseudo-headers we check from 2nd char and for other ones
648 * from the first char, because HTTP_IS_TOKEN() also excludes
649 * the colon.
650 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100651 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100652
653 for (i = !!phdr; i < list[idx].n.len; i++)
654 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
655 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100656 }
657
Willy Tarreauc36c6782019-11-22 16:02:43 +0100658 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
659 * rejecting NUL, CR and LF characters.
660 */
661 ctl = ist_find_ctl(list[idx].v);
662 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
663 goto fail;
664
Willy Tarreau6deb4122018-11-27 15:34:18 +0100665 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
666 /* insert a pseudo header by its index (in phdr) and value (in value) */
667 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
668 if (fields & H2_PHDR_FND_NONE) {
669 /* pseudo header field after regular headers */
670 goto fail;
671 }
672 else {
673 /* repeated pseudo header field */
674 goto fail;
675 }
676 }
677 fields |= 1 << phdr;
678 phdr_val[phdr] = list[idx].v;
679 continue;
680 }
681 else if (phdr != 0) {
682 /* invalid pseudo header -- should never happen here */
683 goto fail;
684 }
685
686 /* regular header field in (name,value) */
687 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
688 /* no more pseudo-headers, time to build the request line */
689 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
690 if (!sl)
691 goto fail;
692 fields |= H2_PHDR_FND_NONE;
693 }
694
695 if (isteq(list[idx].n, ist("host")))
696 fields |= H2_PHDR_FND_HOST;
697
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100698 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100699 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100700 if (ret < 0)
701 goto fail;
702
Willy Tarreau6deb4122018-11-27 15:34:18 +0100703 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100704 if (ret == 0)
705 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100706 }
707
708 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
709 if (isteq(list[idx].n, ist("connection")) ||
710 isteq(list[idx].n, ist("proxy-connection")) ||
711 isteq(list[idx].n, ist("keep-alive")) ||
712 isteq(list[idx].n, ist("upgrade")) ||
713 isteq(list[idx].n, ist("transfer-encoding")))
714 goto fail;
715
716 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
717 goto fail;
718
719 /* cookie requires special processing at the end */
720 if (isteq(list[idx].n, ist("cookie"))) {
721 list[idx].n.len = -1;
722
723 if (ck < 0)
724 ck = idx;
725 else
726 list[lck].n.len = idx;
727
728 lck = idx;
729 continue;
730 }
731
732 if (!htx_add_header(htx, list[idx].n, list[idx].v))
733 goto fail;
734 }
735
736 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
737 if (fields & H2_PHDR_FND_STAT)
738 goto fail;
739
740 /* Let's dump the request now if not yet emitted. */
741 if (!(fields & H2_PHDR_FND_NONE)) {
742 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
743 if (!sl)
744 goto fail;
745 }
746
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100747 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
748 sl_flags |= HTX_SL_F_BODYLESS;
749
Willy Tarreau6deb4122018-11-27 15:34:18 +0100750 /* update the start line with last detected header info */
751 sl->flags |= sl_flags;
752
753 /* complete with missing Host if needed */
754 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
755 /* missing Host field, use :authority instead */
756 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
757 goto fail;
758 }
759
760 /* now we may have to build a cookie list. We'll dump the values of all
761 * visited headers.
762 */
763 if (ck >= 0) {
764 uint32_t fs; // free space
765 uint32_t bs; // block size
766 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100767 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100768 struct htx_blk *blk;
769
770 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
771 if (!blk)
772 goto fail;
773
Willy Tarreau164e0612018-12-18 11:00:41 +0100774 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100775 fs = htx_free_data_space(htx);
776 bs = htx_get_blksz(blk);
777
778 /* for each extra cookie, we'll extend the cookie's value and
779 * insert "; " before the new value.
780 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100781 fs += tl; // first one is already counted
782 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100783 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100784 tl += vl + 2;
785 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100786 goto fail;
787
Christopher Faulet41dc8432019-06-18 09:49:16 +0200788 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100789 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
790 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
791 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
792 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100793 }
794
795 }
796
797 /* now send the end of headers marker */
798 htx_add_endof(htx, HTX_BLK_EOH);
799
Christopher Faulet33543e72019-05-15 15:53:20 +0200800 /* Set bytes used in the HTX mesage for the headers now */
801 sl->hdrs_bytes = htx_used_space(htx) - used;
802
Willy Tarreau6deb4122018-11-27 15:34:18 +0100803 ret = 1;
804 return ret;
805
806 fail:
807 return -1;
808}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200809
810/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
811 * <fields> indicates what was found so far. This should be called once at the
812 * detection of the first general header field or at the end of the message if
813 * no general header field was found yet. Returns the created start line on
814 * success, or NULL on failure. Upon success, <msgf> is updated with a few
815 * H2_MSGF_* flags indicating what was found while parsing.
816 */
817static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
818{
819 unsigned int flags = HTX_SL_F_NONE;
820 struct htx_sl *sl;
821 unsigned char h, t, u;
822
823 /* only :status is allowed as a pseudo header */
824 if (!(fields & H2_PHDR_FND_STAT))
825 goto fail;
826
827 if (phdr[H2_PHDR_IDX_STAT].len != 3)
828 goto fail;
829
830 /* Set HTX start-line flags */
831 flags |= HTX_SL_F_VER_11; // V2 in fact
832 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
833
834 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
835 if (!sl)
836 goto fail;
837
838 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
839 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
840 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
841 if (h > 9 || t > 9 || u > 9)
842 goto fail;
843
844 sl->info.res.status = h * 100 + t * 10 + u;
845
Christopher Faulet0b465482019-02-19 15:14:23 +0100846 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
847 * there is no body. So remove the flag H2_MSGF_BODY and add
848 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
849 * expected.
850 */
851 if (sl->info.res.status < 200 &&
852 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
853 *msgf |= H2_MSGF_RSP_1XX;
854 *msgf &= ~H2_MSGF_BODY;
855 }
856
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200857 return sl;
858 fail:
859 return NULL;
860}
861
862/* Takes an H2 response present in the headers list <list> terminated by a name
863 * being <NULL,0> and emits the equivalent HTX response according to the rules
864 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
865 * a positive value is returned if some bytes were emitted. In case of error, a
866 * negative error code is returned.
867 *
868 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
869 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
870 * if a body is detected (!ES).
871 *
872 * The headers list <list> must be composed of :
873 * - n.name != NULL, n.len > 0 : literal header name
874 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
875 * among H2_PHDR_IDX_*
876 * - n.name ignored, n.len == 0 : end of list
877 * - in all cases except the end of list, v.name and v.len must designate a
878 * valid value.
879 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100880int 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 +0200881{
882 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
883 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
884 uint32_t idx;
885 int phdr;
886 int ret;
887 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200888 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200889 struct htx_sl *sl = NULL;
890 unsigned int sl_flags = 0;
Willy Tarreauc36c6782019-11-22 16:02:43 +0100891 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200892
893 fields = 0;
894 for (idx = 0; list[idx].n.len != 0; idx++) {
895 if (!list[idx].n.ptr) {
896 /* this is an indexed pseudo-header */
897 phdr = list[idx].n.len;
898 }
899 else {
900 /* this can be any type of header */
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100901 /* RFC7540#8.1.2: upper case not allowed in header field names.
902 * #10.3: header names must be valid (i.e. match a token).
903 * For pseudo-headers we check from 2nd char and for other ones
904 * from the first char, because HTTP_IS_TOKEN() also excludes
905 * the colon.
906 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200907 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau5eaeec52019-11-24 10:34:39 +0100908
909 for (i = !!phdr; i < list[idx].n.len; i++)
910 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
911 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200912 }
913
Willy Tarreauc36c6782019-11-22 16:02:43 +0100914 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
915 * rejecting NUL, CR and LF characters.
916 */
917 ctl = ist_find_ctl(list[idx].v);
918 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
919 goto fail;
920
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200921 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
922 /* insert a pseudo header by its index (in phdr) and value (in value) */
923 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
924 if (fields & H2_PHDR_FND_NONE) {
925 /* pseudo header field after regular headers */
926 goto fail;
927 }
928 else {
929 /* repeated pseudo header field */
930 goto fail;
931 }
932 }
933 fields |= 1 << phdr;
934 phdr_val[phdr] = list[idx].v;
935 continue;
936 }
937 else if (phdr != 0) {
938 /* invalid pseudo header -- should never happen here */
939 goto fail;
940 }
941
942 /* regular header field in (name,value) */
943 if (!(fields & H2_PHDR_FND_NONE)) {
944 /* no more pseudo-headers, time to build the status line */
945 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
946 if (!sl)
947 goto fail;
948 fields |= H2_PHDR_FND_NONE;
949 }
950
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100951 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100952 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100953 if (ret < 0)
954 goto fail;
955
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200956 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100957 if (ret == 0)
958 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200959 }
960
961 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
962 if (isteq(list[idx].n, ist("connection")) ||
963 isteq(list[idx].n, ist("proxy-connection")) ||
964 isteq(list[idx].n, ist("keep-alive")) ||
965 isteq(list[idx].n, ist("upgrade")) ||
966 isteq(list[idx].n, ist("transfer-encoding")))
967 goto fail;
968
969 if (!htx_add_header(htx, list[idx].n, list[idx].v))
970 goto fail;
971 }
972
973 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
974 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
975 goto fail;
976
977 /* Let's dump the request now if not yet emitted. */
978 if (!(fields & H2_PHDR_FND_NONE)) {
979 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
980 if (!sl)
981 goto fail;
982 }
983
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100984 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
985 sl_flags |= HTX_SL_F_BODYLESS;
986
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200987 /* update the start line with last detected header info */
988 sl->flags |= sl_flags;
989
990 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
991 /* FIXME: Do we need to signal anything when we have a body and
992 * no content-length, to have the equivalent of H1's chunked
993 * encoding?
994 */
995 }
996
997 /* now send the end of headers marker */
998 htx_add_endof(htx, HTX_BLK_EOH);
999
Christopher Faulet33543e72019-05-15 15:53:20 +02001000 /* Set bytes used in the HTX mesage for the headers now */
1001 sl->hdrs_bytes = htx_used_space(htx) - used;
1002
Willy Tarreau1329b5b2018-10-08 14:49:20 +02001003 ret = 1;
1004 return ret;
1005
1006 fail:
1007 return -1;
1008}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001009
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001010/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
1011 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
1012 * and a positive value is returned if some bytes were emitted. In case of
1013 * error, a negative error code is returned. The caller must have verified that
1014 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001015 *
1016 * The headers list <list> must be composed of :
1017 * - n.name != NULL, n.len > 0 : literal header name
1018 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
1019 * among H2_PHDR_IDX_* (illegal here)
1020 * - n.name ignored, n.len == 0 : end of list
1021 * - in all cases except the end of list, v.name and v.len must designate a
1022 * valid value.
1023 */
1024int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
1025{
Willy Tarreauc36c6782019-11-22 16:02:43 +01001026 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001027 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001028 int i;
1029
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001030 for (idx = 0; list[idx].n.len != 0; idx++) {
1031 if (!list[idx].n.ptr) {
1032 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
1033 goto fail;
1034 }
1035
Willy Tarreau5eaeec52019-11-24 10:34:39 +01001036 /* RFC7540#8.1.2: upper case not allowed in header field names.
1037 * #10.3: header names must be valid (i.e. match a token). This
1038 * also catches pseudo-headers which are forbidden in trailers.
1039 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001040 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau5eaeec52019-11-24 10:34:39 +01001041 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 +01001042 goto fail;
1043
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001044 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
1045 if (isteq(list[idx].n, ist("host")) ||
1046 isteq(list[idx].n, ist("content-length")) ||
1047 isteq(list[idx].n, ist("connection")) ||
1048 isteq(list[idx].n, ist("proxy-connection")) ||
1049 isteq(list[idx].n, ist("keep-alive")) ||
1050 isteq(list[idx].n, ist("upgrade")) ||
1051 isteq(list[idx].n, ist("te")) ||
1052 isteq(list[idx].n, ist("transfer-encoding")))
1053 goto fail;
1054
Willy Tarreauc36c6782019-11-22 16:02:43 +01001055 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
1056 * rejecting NUL, CR and LF characters.
1057 */
1058 ctl = ist_find_ctl(list[idx].v);
1059 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
1060 goto fail;
1061
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001062 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
1063 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001064 }
1065
Christopher Faulet2d7c5392019-06-03 10:41:26 +02001066 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001067 goto fail;
1068
Willy Tarreau1e1f27c2019-01-03 18:39:54 +01001069 return 1;
1070
1071 fail:
1072 return -1;
1073}