blob: 66846108653484823d013bda1a12692b2a48f538 [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 Faulet44af3cf2019-02-18 10:12:56 +0100448 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
449 sl_flags |= HTX_SL_F_BODYLESS;
450
Willy Tarreau6deb4122018-11-27 15:34:18 +0100451 /* update the start line with last detected header info */
452 sl->flags |= sl_flags;
453
454 /* complete with missing Host if needed */
455 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
456 /* missing Host field, use :authority instead */
457 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
458 goto fail;
459 }
460
461 /* now we may have to build a cookie list. We'll dump the values of all
462 * visited headers.
463 */
464 if (ck >= 0) {
465 uint32_t fs; // free space
466 uint32_t bs; // block size
467 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100468 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100469 struct htx_blk *blk;
470
471 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
472 if (!blk)
473 goto fail;
474
Willy Tarreau164e0612018-12-18 11:00:41 +0100475 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100476 fs = htx_free_data_space(htx);
477 bs = htx_get_blksz(blk);
478
479 /* for each extra cookie, we'll extend the cookie's value and
480 * insert "; " before the new value.
481 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100482 fs += tl; // first one is already counted
483 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100484 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100485 tl += vl + 2;
486 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100487 goto fail;
488
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200489 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100490 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
491 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
492 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
493 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100494 }
495
496 }
497
498 /* now send the end of headers marker */
499 htx_add_endof(htx, HTX_BLK_EOH);
500
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500501 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200502 sl->hdrs_bytes = htx_used_space(htx) - used;
503
Willy Tarreau6deb4122018-11-27 15:34:18 +0100504 ret = 1;
505 return ret;
506
507 fail:
508 return -1;
509}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200510
511/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
512 * <fields> indicates what was found so far. This should be called once at the
513 * detection of the first general header field or at the end of the message if
514 * no general header field was found yet. Returns the created start line on
515 * success, or NULL on failure. Upon success, <msgf> is updated with a few
516 * H2_MSGF_* flags indicating what was found while parsing.
517 */
518static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
519{
520 unsigned int flags = HTX_SL_F_NONE;
521 struct htx_sl *sl;
522 unsigned char h, t, u;
523
524 /* only :status is allowed as a pseudo header */
525 if (!(fields & H2_PHDR_FND_STAT))
526 goto fail;
527
528 if (phdr[H2_PHDR_IDX_STAT].len != 3)
529 goto fail;
530
531 /* Set HTX start-line flags */
532 flags |= HTX_SL_F_VER_11; // V2 in fact
533 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
534
535 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
536 if (!sl)
537 goto fail;
538
539 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
540 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
541 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
542 if (h > 9 || t > 9 || u > 9)
543 goto fail;
544
545 sl->info.res.status = h * 100 + t * 10 + u;
546
Christopher Faulet0b465482019-02-19 15:14:23 +0100547 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
548 * there is no body. So remove the flag H2_MSGF_BODY and add
549 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
550 * expected.
551 */
552 if (sl->info.res.status < 200 &&
553 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
554 *msgf |= H2_MSGF_RSP_1XX;
555 *msgf &= ~H2_MSGF_BODY;
556 }
557
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200558 return sl;
559 fail:
560 return NULL;
561}
562
563/* Takes an H2 response present in the headers list <list> terminated by a name
564 * being <NULL,0> and emits the equivalent HTX response according to the rules
565 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
566 * a positive value is returned if some bytes were emitted. In case of error, a
567 * negative error code is returned.
568 *
569 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
570 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
571 * if a body is detected (!ES).
572 *
573 * The headers list <list> must be composed of :
574 * - n.name != NULL, n.len > 0 : literal header name
575 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
576 * among H2_PHDR_IDX_*
577 * - n.name ignored, n.len == 0 : end of list
578 * - in all cases except the end of list, v.name and v.len must designate a
579 * valid value.
580 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100581int 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 +0200582{
583 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
584 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
585 uint32_t idx;
586 int phdr;
587 int ret;
588 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200589 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200590 struct htx_sl *sl = NULL;
591 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100592 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200593
594 fields = 0;
595 for (idx = 0; list[idx].n.len != 0; idx++) {
596 if (!list[idx].n.ptr) {
597 /* this is an indexed pseudo-header */
598 phdr = list[idx].n.len;
599 }
600 else {
601 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100602 /* RFC7540#8.1.2: upper case not allowed in header field names.
603 * #10.3: header names must be valid (i.e. match a token).
604 * For pseudo-headers we check from 2nd char and for other ones
605 * from the first char, because HTTP_IS_TOKEN() also excludes
606 * the colon.
607 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200608 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100609
610 for (i = !!phdr; i < list[idx].n.len; i++)
611 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
612 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200613 }
614
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100615 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
616 * rejecting NUL, CR and LF characters.
617 */
618 ctl = ist_find_ctl(list[idx].v);
619 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
620 goto fail;
621
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200622 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
623 /* insert a pseudo header by its index (in phdr) and value (in value) */
624 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
625 if (fields & H2_PHDR_FND_NONE) {
626 /* pseudo header field after regular headers */
627 goto fail;
628 }
629 else {
630 /* repeated pseudo header field */
631 goto fail;
632 }
633 }
634 fields |= 1 << phdr;
635 phdr_val[phdr] = list[idx].v;
636 continue;
637 }
638 else if (phdr != 0) {
639 /* invalid pseudo header -- should never happen here */
640 goto fail;
641 }
642
643 /* regular header field in (name,value) */
644 if (!(fields & H2_PHDR_FND_NONE)) {
645 /* no more pseudo-headers, time to build the status line */
646 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
647 if (!sl)
648 goto fail;
649 fields |= H2_PHDR_FND_NONE;
650 }
651
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100652 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100653 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100654 if (ret < 0)
655 goto fail;
656
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200657 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100658 if (ret == 0)
659 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200660 }
661
662 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
663 if (isteq(list[idx].n, ist("connection")) ||
664 isteq(list[idx].n, ist("proxy-connection")) ||
665 isteq(list[idx].n, ist("keep-alive")) ||
666 isteq(list[idx].n, ist("upgrade")) ||
667 isteq(list[idx].n, ist("transfer-encoding")))
668 goto fail;
669
670 if (!htx_add_header(htx, list[idx].n, list[idx].v))
671 goto fail;
672 }
673
674 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
675 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
676 goto fail;
677
678 /* Let's dump the request now if not yet emitted. */
679 if (!(fields & H2_PHDR_FND_NONE)) {
680 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
681 if (!sl)
682 goto fail;
683 }
684
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100685 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
686 sl_flags |= HTX_SL_F_BODYLESS;
687
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200688 /* update the start line with last detected header info */
689 sl->flags |= sl_flags;
690
691 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
692 /* FIXME: Do we need to signal anything when we have a body and
693 * no content-length, to have the equivalent of H1's chunked
694 * encoding?
695 */
696 }
697
698 /* now send the end of headers marker */
699 htx_add_endof(htx, HTX_BLK_EOH);
700
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500701 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200702 sl->hdrs_bytes = htx_used_space(htx) - used;
703
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200704 ret = 1;
705 return ret;
706
707 fail:
708 return -1;
709}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100710
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200711/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
712 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
713 * and a positive value is returned if some bytes were emitted. In case of
714 * error, a negative error code is returned. The caller must have verified that
715 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100716 *
717 * The headers list <list> must be composed of :
718 * - n.name != NULL, n.len > 0 : literal header name
719 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
720 * among H2_PHDR_IDX_* (illegal here)
721 * - n.name ignored, n.len == 0 : end of list
722 * - in all cases except the end of list, v.name and v.len must designate a
723 * valid value.
724 */
725int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
726{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100727 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100728 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100729 int i;
730
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100731 for (idx = 0; list[idx].n.len != 0; idx++) {
732 if (!list[idx].n.ptr) {
733 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
734 goto fail;
735 }
736
Willy Tarreau146f53a2019-11-24 10:34:39 +0100737 /* RFC7540#8.1.2: upper case not allowed in header field names.
738 * #10.3: header names must be valid (i.e. match a token). This
739 * also catches pseudo-headers which are forbidden in trailers.
740 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100741 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100742 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 +0100743 goto fail;
744
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100745 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
746 if (isteq(list[idx].n, ist("host")) ||
747 isteq(list[idx].n, ist("content-length")) ||
748 isteq(list[idx].n, ist("connection")) ||
749 isteq(list[idx].n, ist("proxy-connection")) ||
750 isteq(list[idx].n, ist("keep-alive")) ||
751 isteq(list[idx].n, ist("upgrade")) ||
752 isteq(list[idx].n, ist("te")) ||
753 isteq(list[idx].n, ist("transfer-encoding")))
754 goto fail;
755
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100756 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
757 * rejecting NUL, CR and LF characters.
758 */
759 ctl = ist_find_ctl(list[idx].v);
760 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
761 goto fail;
762
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200763 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
764 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100765 }
766
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200767 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100768 goto fail;
769
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100770 return 1;
771
772 fail:
773 return -1;
774}