blob: 2d9410d827bf7471f069640dd2869e3bd8249d53 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/global.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020031#include <haproxy/h2.h>
Willy Tarreau0017be02020-06-02 19:25:28 +020032#include <haproxy/http-hdr-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/http.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020034#include <haproxy/htx.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020035#include <import/ist.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010037
Willy Tarreau9c84d822019-01-30 15:09:21 +010038struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
39 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
40 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
42 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
43 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
44 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
45 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
46 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
47 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
48 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
49};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010050
Willy Tarreau54f53ef2019-11-22 16:02:43 +010051/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
52 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
53 * non-zero if such a character is found, 0 otherwise. When run on unlikely
54 * header match, it's recommended to first check for the presence of control
55 * chars using ist_find_ctl().
56 */
57static int has_forbidden_char(const struct ist ist, const char *start)
58{
59 do {
60 if ((uint8_t)*start <= 0x0d &&
61 (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
62 return 1;
63 start++;
64 } while (start < ist.ptr + ist.len);
65 return 0;
66}
67
Willy Tarreaubeefaee2018-12-19 13:08:08 +010068/* Parse the Content-Length header field of an HTTP/2 request. The function
69 * checks all possible occurrences of a comma-delimited value, and verifies
70 * if any of them doesn't match a previous value. It returns <0 if a value
71 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
72 * if the value can be indexed (first one). In the last case, the value might
73 * be adjusted and the caller must only add the updated value.
74 */
75int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
76{
77 char *e, *n;
78 unsigned long long cl;
79 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
80 struct ist word;
81
82 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
83 e = value->ptr + value->len;
84
85 while (++word.ptr < e) {
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +050086 /* skip leading delimiter and blanks */
Willy Tarreaubeefaee2018-12-19 13:08:08 +010087 if (unlikely(HTTP_IS_LWS(*word.ptr)))
88 continue;
89
90 /* digits only now */
91 for (cl = 0, n = word.ptr; n < e; n++) {
92 unsigned int c = *n - '0';
93 if (unlikely(c > 9)) {
94 /* non-digit */
95 if (unlikely(n == word.ptr)) // spaces only
96 goto fail;
97 break;
98 }
99 if (unlikely(cl > ULLONG_MAX / 10ULL))
100 goto fail; /* multiply overflow */
101 cl = cl * 10ULL;
102 if (unlikely(cl + c < cl))
103 goto fail; /* addition overflow */
104 cl = cl + c;
105 }
106
107 /* keep a copy of the exact cleaned value */
108 word.len = n - word.ptr;
109
110 /* skip trailing LWS till next comma or EOL */
111 for (; n < e; n++) {
112 if (!HTTP_IS_LWS(*n)) {
113 if (unlikely(*n != ','))
114 goto fail;
115 break;
116 }
117 }
118
119 /* if duplicate, must be equal */
120 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
121 goto fail;
122
123 /* OK, store this result as the one to be indexed */
124 *msgf |= H2_MSGF_BODY_CL;
125 *body_len = cl;
126 *value = word;
127 word.ptr = n;
128 }
129 /* here we've reached the end with a single value or a series of
130 * identical values, all matching previous series if any. The last
131 * parsed value was sent back into <value>. We just have to decide
132 * if this occurrence has to be indexed (it's the first one) or
133 * silently skipped (it's not the first one)
134 */
135 return !not_first;
136 fail:
137 return -1;
138}
139
Willy Tarreau6deb4122018-11-27 15:34:18 +0100140/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
141 * <fields> indicates what was found so far. This should be called once at the
142 * detection of the first general header field or at the end of the request if
143 * no general header field was found yet. Returns the created start line on
144 * success, or NULL on failure. Upon success, <msgf> is updated with a few
145 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200146 *
147 * The rules below deserve a bit of explanation. There tends to be some
148 * confusion regarding H2's authority vs the Host header. They are different
149 * though may sometimes be exchanged. In H2, the request line is broken into :
150 * - :method
151 * - :scheme
152 * - :authority
153 * - :path
154 *
155 * An equivalent HTTP/1.x absolute-form request would then look like :
156 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
157 *
158 * Except for CONNECT which doesn't have scheme nor path and looks like :
159 * <:method> <:authority> HTTP/x.y
160 *
161 * It's worth noting that H2 still supports an encoding to map H1 origin-form
162 * and asterisk-form requests. These ones do not specify the authority. However
163 * in H2 they must still specify the scheme, which is not present in H1. Also,
164 * when encoding an absolute-form H1 request without a path, the path
165 * automatically becomes "/" except for the OPTIONS method where it
166 * becomes "*".
167 *
168 * As such it is explicitly permitted for an H2 client to send a request
169 * featuring a Host header and no :authority, though it's not the recommended
170 * way to use H2 for a client. It is however the only permitted way to encode
171 * an origin-form H1 request over H2. Thus we need to respect such differences
172 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100173 */
174static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
175{
Willy Tarreau92919f72019-10-08 16:53:07 +0200176 struct ist uri;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100177 unsigned int flags = HTX_SL_F_NONE;
178 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100179 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100180
181 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
182 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
183 * MUST be omitted ; ":authority" contains the host and port
184 * to connect to.
185 */
186 if (fields & H2_PHDR_FND_SCHM) {
187 /* scheme not allowed */
188 goto fail;
189 }
190 else if (fields & H2_PHDR_FND_PATH) {
191 /* path not allowed */
192 goto fail;
193 }
194 else if (!(fields & H2_PHDR_FND_AUTH)) {
195 /* missing authority */
196 goto fail;
197 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100198 *msgf |= H2_MSGF_BODY_TUNNEL;
199 }
200 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
201 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
202 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
203 * valid value for the ":method", ":scheme" and ":path" phdr
204 * unless it is a CONNECT request.
205 */
206 if (!(fields & H2_PHDR_FND_METH)) {
207 /* missing method */
208 goto fail;
209 }
210 else if (!(fields & H2_PHDR_FND_SCHM)) {
211 /* missing scheme */
212 goto fail;
213 }
214 else {
215 /* missing path */
216 goto fail;
217 }
218 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200219 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200220 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
221 * classify the scheme as "present/http", "present/https",
222 * "present/other", "absent" so as to decide whether or not
223 * we're facing a normalized URI that will have to be encoded
224 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
225 * clients should use the absolute form, thus we cannot infer
226 * whether or not the client wanted to use a proxy here.
227 */
228 flags |= HTX_SL_F_HAS_SCHM;
229 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
230 flags |= HTX_SL_F_SCHM_HTTP;
231 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
232 flags |= HTX_SL_F_SCHM_HTTPS;
233 }
234
235 if (!(flags & HTX_SL_F_HAS_SCHM)) {
236 /* no scheme, use authority only (CONNECT) */
237 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200238 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200239 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200240 else if (fields & H2_PHDR_FND_AUTH) {
241 /* authority is present, let's use the absolute form. We simply
242 * use the trash to concatenate them since all of them MUST fit
243 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200244 */
Willy Tarreaufd2658c2020-02-26 13:51:38 +0100245 if (unlikely(!phdr[H2_PHDR_IDX_PATH].len))
246 goto fail; // 7540#8.1.2.3: :path must not be empty
247
Willy Tarreau92919f72019-10-08 16:53:07 +0200248 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
249 istcat(&uri, ist("://"), trash.size);
250 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
251 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
252 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200253 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200254
255 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
256 /* we don't know if it was originally an absolute or a
257 * relative request because newer versions of HTTP use
258 * the absolute URI format by default, which we call
259 * the normalized URI format internally. This is the
260 * strongly recommended way of sending a request for
261 * a regular client, so we cannot distinguish this
262 * from a request intended for a proxy. For other
263 * schemes however there is no doubt.
264 */
265 flags |= HTX_SL_F_NORMALIZED_URI;
266 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200267 }
268 else {
269 /* usual schemes with or without authority, use origin form */
270 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200271 if (fields & H2_PHDR_FND_AUTH)
272 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200273 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100274
Willy Tarreau2be362c2019-10-08 11:59:37 +0200275 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
276 * that :path must not be empty.
277 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200278 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100279 goto fail;
280
Willy Tarreau2be362c2019-10-08 11:59:37 +0200281 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200282 for (i = 0; i < uri.len; i++) {
283 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100284 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
285 htx->flags |= HTX_FL_PARSING_ERROR;
286 }
287
Willy Tarreau6deb4122018-11-27 15:34:18 +0100288 /* Set HTX start-line flags */
289 flags |= HTX_SL_F_VER_11; // V2 in fact
290 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
291
Willy Tarreau92919f72019-10-08 16:53:07 +0200292 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100293 if (!sl)
294 goto fail;
295
296 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
297 return sl;
298 fail:
299 return NULL;
300}
301
302/* Takes an H2 request present in the headers list <list> terminated by a name
303 * being <NULL,0> and emits the equivalent HTX request according to the rules
304 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
305 * non-zero is returned if some bytes were emitted. In case of error, a
306 * negative error code is returned.
307 *
308 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
309 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
310 * if a body is detected (!ES).
311 *
312 * The headers list <list> must be composed of :
313 * - n.name != NULL, n.len > 0 : literal header name
314 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
315 * among H2_PHDR_IDX_*
316 * - n.name ignored, n.len == 0 : end of list
317 * - in all cases except the end of list, v.name and v.len must designate a
318 * valid value.
319 *
320 * The Cookie header will be reassembled at the end, and for this, the <list>
321 * will be used to create a linked list, so its contents may be destroyed.
322 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100323int 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 +0100324{
325 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
326 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
327 uint32_t idx;
328 int ck, lck; /* cookie index and last cookie index */
329 int phdr;
330 int ret;
331 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200332 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100333 struct htx_sl *sl = NULL;
334 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100335 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100336
337 lck = ck = -1; // no cookie for now
338 fields = 0;
339 for (idx = 0; list[idx].n.len != 0; idx++) {
340 if (!list[idx].n.ptr) {
341 /* this is an indexed pseudo-header */
342 phdr = list[idx].n.len;
343 }
344 else {
345 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100346 /* RFC7540#8.1.2: upper case not allowed in header field names.
347 * #10.3: header names must be valid (i.e. match a token).
348 * For pseudo-headers we check from 2nd char and for other ones
349 * from the first char, because HTTP_IS_TOKEN() also excludes
350 * the colon.
351 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100352 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100353
354 for (i = !!phdr; i < list[idx].n.len; i++)
355 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
356 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100357 }
358
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100359 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
360 * rejecting NUL, CR and LF characters.
361 */
362 ctl = ist_find_ctl(list[idx].v);
363 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
364 goto fail;
365
Willy Tarreau6deb4122018-11-27 15:34:18 +0100366 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
367 /* insert a pseudo header by its index (in phdr) and value (in value) */
368 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
369 if (fields & H2_PHDR_FND_NONE) {
370 /* pseudo header field after regular headers */
371 goto fail;
372 }
373 else {
374 /* repeated pseudo header field */
375 goto fail;
376 }
377 }
378 fields |= 1 << phdr;
379 phdr_val[phdr] = list[idx].v;
380 continue;
381 }
382 else if (phdr != 0) {
383 /* invalid pseudo header -- should never happen here */
384 goto fail;
385 }
386
387 /* regular header field in (name,value) */
388 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
389 /* no more pseudo-headers, time to build the request line */
390 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
391 if (!sl)
392 goto fail;
393 fields |= H2_PHDR_FND_NONE;
394 }
395
396 if (isteq(list[idx].n, ist("host")))
397 fields |= H2_PHDR_FND_HOST;
398
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100399 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100400 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100401 if (ret < 0)
402 goto fail;
403
Willy Tarreau6deb4122018-11-27 15:34:18 +0100404 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100405 if (ret == 0)
406 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100407 }
408
409 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
410 if (isteq(list[idx].n, ist("connection")) ||
411 isteq(list[idx].n, ist("proxy-connection")) ||
412 isteq(list[idx].n, ist("keep-alive")) ||
413 isteq(list[idx].n, ist("upgrade")) ||
414 isteq(list[idx].n, ist("transfer-encoding")))
415 goto fail;
416
417 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
418 goto fail;
419
420 /* cookie requires special processing at the end */
421 if (isteq(list[idx].n, ist("cookie"))) {
422 list[idx].n.len = -1;
423
424 if (ck < 0)
425 ck = idx;
426 else
427 list[lck].n.len = idx;
428
429 lck = idx;
430 continue;
431 }
432
433 if (!htx_add_header(htx, list[idx].n, list[idx].v))
434 goto fail;
435 }
436
437 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
438 if (fields & H2_PHDR_FND_STAT)
439 goto fail;
440
441 /* Let's dump the request now if not yet emitted. */
442 if (!(fields & H2_PHDR_FND_NONE)) {
443 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
444 if (!sl)
445 goto fail;
446 }
447
Christopher Fauletd0db4232021-01-22 11:46:30 +0100448 if (*msgf & H2_MSGF_BODY_TUNNEL)
449 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
450
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100451 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
452 sl_flags |= HTX_SL_F_BODYLESS;
453
Willy Tarreau6deb4122018-11-27 15:34:18 +0100454 /* update the start line with last detected header info */
455 sl->flags |= sl_flags;
456
457 /* complete with missing Host if needed */
458 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
459 /* missing Host field, use :authority instead */
460 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
461 goto fail;
462 }
463
464 /* now we may have to build a cookie list. We'll dump the values of all
465 * visited headers.
466 */
467 if (ck >= 0) {
468 uint32_t fs; // free space
469 uint32_t bs; // block size
470 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100471 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100472 struct htx_blk *blk;
473
474 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
475 if (!blk)
476 goto fail;
477
Willy Tarreau164e0612018-12-18 11:00:41 +0100478 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100479 fs = htx_free_data_space(htx);
480 bs = htx_get_blksz(blk);
481
482 /* for each extra cookie, we'll extend the cookie's value and
483 * insert "; " before the new value.
484 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100485 fs += tl; // first one is already counted
486 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100487 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100488 tl += vl + 2;
489 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100490 goto fail;
491
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200492 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100493 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
494 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
495 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
496 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100497 }
498
499 }
500
501 /* now send the end of headers marker */
502 htx_add_endof(htx, HTX_BLK_EOH);
503
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500504 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200505 sl->hdrs_bytes = htx_used_space(htx) - used;
506
Willy Tarreau6deb4122018-11-27 15:34:18 +0100507 ret = 1;
508 return ret;
509
510 fail:
511 return -1;
512}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200513
514/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
515 * <fields> indicates what was found so far. This should be called once at the
516 * detection of the first general header field or at the end of the message if
517 * no general header field was found yet. Returns the created start line on
518 * success, or NULL on failure. Upon success, <msgf> is updated with a few
519 * H2_MSGF_* flags indicating what was found while parsing.
520 */
521static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
522{
Christopher Faulet89899422020-12-07 18:24:43 +0100523 unsigned int status, flags = HTX_SL_F_NONE;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200524 struct htx_sl *sl;
525 unsigned char h, t, u;
526
527 /* only :status is allowed as a pseudo header */
528 if (!(fields & H2_PHDR_FND_STAT))
529 goto fail;
530
531 if (phdr[H2_PHDR_IDX_STAT].len != 3)
532 goto fail;
533
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200534 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
535 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
536 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
537 if (h > 9 || t > 9 || u > 9)
538 goto fail;
Christopher Faulet89899422020-12-07 18:24:43 +0100539 status = h * 100 + t * 10 + u;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200540
Christopher Faulet89899422020-12-07 18:24:43 +0100541 /* 101 responses are not supported in H2, so return a error.
542 * On 1xx responses there is no ES on the HEADERS frame but there is no
543 * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to
544 * notify the decoder another HEADERS frame is expected.
Christopher Faulet0b465482019-02-19 15:14:23 +0100545 */
Christopher Faulet89899422020-12-07 18:24:43 +0100546 if (status == 101)
547 goto fail;
548 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100549 *msgf |= H2_MSGF_RSP_1XX;
550 *msgf &= ~H2_MSGF_BODY;
551 }
552
Christopher Faulet89899422020-12-07 18:24:43 +0100553 /* Set HTX start-line flags */
554 flags |= HTX_SL_F_VER_11; // V2 in fact
555 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
556
557 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
558 if (!sl)
559 goto fail;
560 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200561 return sl;
562 fail:
563 return NULL;
564}
565
566/* Takes an H2 response present in the headers list <list> terminated by a name
567 * being <NULL,0> and emits the equivalent HTX response according to the rules
568 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
569 * a positive value is returned if some bytes were emitted. In case of error, a
570 * negative error code is returned.
571 *
572 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
573 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
574 * if a body is detected (!ES).
575 *
576 * The headers list <list> must be composed of :
577 * - n.name != NULL, n.len > 0 : literal header name
578 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
579 * among H2_PHDR_IDX_*
580 * - n.name ignored, n.len == 0 : end of list
581 * - in all cases except the end of list, v.name and v.len must designate a
582 * valid value.
583 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100584int 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 +0200585{
586 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
587 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
588 uint32_t idx;
589 int phdr;
590 int ret;
591 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200592 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200593 struct htx_sl *sl = NULL;
594 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100595 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200596
597 fields = 0;
598 for (idx = 0; list[idx].n.len != 0; idx++) {
599 if (!list[idx].n.ptr) {
600 /* this is an indexed pseudo-header */
601 phdr = list[idx].n.len;
602 }
603 else {
604 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100605 /* RFC7540#8.1.2: upper case not allowed in header field names.
606 * #10.3: header names must be valid (i.e. match a token).
607 * For pseudo-headers we check from 2nd char and for other ones
608 * from the first char, because HTTP_IS_TOKEN() also excludes
609 * the colon.
610 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200611 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100612
613 for (i = !!phdr; i < list[idx].n.len; i++)
614 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
615 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200616 }
617
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100618 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
619 * rejecting NUL, CR and LF characters.
620 */
621 ctl = ist_find_ctl(list[idx].v);
622 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
623 goto fail;
624
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200625 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
626 /* insert a pseudo header by its index (in phdr) and value (in value) */
627 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
628 if (fields & H2_PHDR_FND_NONE) {
629 /* pseudo header field after regular headers */
630 goto fail;
631 }
632 else {
633 /* repeated pseudo header field */
634 goto fail;
635 }
636 }
637 fields |= 1 << phdr;
638 phdr_val[phdr] = list[idx].v;
639 continue;
640 }
641 else if (phdr != 0) {
642 /* invalid pseudo header -- should never happen here */
643 goto fail;
644 }
645
646 /* regular header field in (name,value) */
647 if (!(fields & H2_PHDR_FND_NONE)) {
648 /* no more pseudo-headers, time to build the status line */
649 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
650 if (!sl)
651 goto fail;
652 fields |= H2_PHDR_FND_NONE;
653 }
654
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100655 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100656 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100657 if (ret < 0)
658 goto fail;
659
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200660 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100661 if (ret == 0)
662 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200663 }
664
665 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
666 if (isteq(list[idx].n, ist("connection")) ||
667 isteq(list[idx].n, ist("proxy-connection")) ||
668 isteq(list[idx].n, ist("keep-alive")) ||
669 isteq(list[idx].n, ist("upgrade")) ||
670 isteq(list[idx].n, ist("transfer-encoding")))
671 goto fail;
672
673 if (!htx_add_header(htx, list[idx].n, list[idx].v))
674 goto fail;
675 }
676
677 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
678 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
679 goto fail;
680
681 /* Let's dump the request now if not yet emitted. */
682 if (!(fields & H2_PHDR_FND_NONE)) {
683 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
684 if (!sl)
685 goto fail;
686 }
687
Christopher Fauletd0db4232021-01-22 11:46:30 +0100688 if ((*msgf & H2_MSGF_BODY_TUNNEL) && sl->info.res.status >= 200 && sl->info.res.status < 300)
689 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
690 else
691 *msgf &= ~H2_MSGF_BODY_TUNNEL;
692
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100693 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
694 sl_flags |= HTX_SL_F_BODYLESS;
695
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200696 /* update the start line with last detected header info */
697 sl->flags |= sl_flags;
698
699 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
700 /* FIXME: Do we need to signal anything when we have a body and
701 * no content-length, to have the equivalent of H1's chunked
702 * encoding?
703 */
704 }
705
706 /* now send the end of headers marker */
707 htx_add_endof(htx, HTX_BLK_EOH);
708
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500709 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200710 sl->hdrs_bytes = htx_used_space(htx) - used;
711
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200712 ret = 1;
713 return ret;
714
715 fail:
716 return -1;
717}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100718
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200719/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
720 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
721 * and a positive value is returned if some bytes were emitted. In case of
722 * error, a negative error code is returned. The caller must have verified that
723 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100724 *
725 * The headers list <list> must be composed of :
726 * - n.name != NULL, n.len > 0 : literal header name
727 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
728 * among H2_PHDR_IDX_* (illegal here)
729 * - n.name ignored, n.len == 0 : end of list
730 * - in all cases except the end of list, v.name and v.len must designate a
731 * valid value.
732 */
733int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
734{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100735 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100736 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100737 int i;
738
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100739 for (idx = 0; list[idx].n.len != 0; idx++) {
740 if (!list[idx].n.ptr) {
741 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
742 goto fail;
743 }
744
Willy Tarreau146f53a2019-11-24 10:34:39 +0100745 /* RFC7540#8.1.2: upper case not allowed in header field names.
746 * #10.3: header names must be valid (i.e. match a token). This
747 * also catches pseudo-headers which are forbidden in trailers.
748 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100749 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100750 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 +0100751 goto fail;
752
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100753 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
754 if (isteq(list[idx].n, ist("host")) ||
755 isteq(list[idx].n, ist("content-length")) ||
756 isteq(list[idx].n, ist("connection")) ||
757 isteq(list[idx].n, ist("proxy-connection")) ||
758 isteq(list[idx].n, ist("keep-alive")) ||
759 isteq(list[idx].n, ist("upgrade")) ||
760 isteq(list[idx].n, ist("te")) ||
761 isteq(list[idx].n, ist("transfer-encoding")))
762 goto fail;
763
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100764 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
765 * rejecting NUL, CR and LF characters.
766 */
767 ctl = ist_find_ctl(list[idx].v);
768 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
769 goto fail;
770
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200771 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
772 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100773 }
774
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200775 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100776 goto fail;
777
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100778 return 1;
779
780 fail:
781 return -1;
782}