blob: 67a443661c3ed683ed4e3cb541f90f8a345eb610 [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>
Amaury Denoyelle4ca0f362021-07-07 10:49:28 +020034#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020035#include <haproxy/htx.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020036#include <import/ist.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010038
Willy Tarreau9c84d822019-01-30 15:09:21 +010039struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
40 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
42 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
43 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
44 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
45 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
46 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
47 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
48 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
49 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
50};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010051
Willy Tarreau6deb4122018-11-27 15:34:18 +010052/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
53 * <fields> indicates what was found so far. This should be called once at the
54 * detection of the first general header field or at the end of the request if
55 * no general header field was found yet. Returns the created start line on
56 * success, or NULL on failure. Upon success, <msgf> is updated with a few
57 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +020058 *
59 * The rules below deserve a bit of explanation. There tends to be some
60 * confusion regarding H2's authority vs the Host header. They are different
61 * though may sometimes be exchanged. In H2, the request line is broken into :
62 * - :method
63 * - :scheme
64 * - :authority
65 * - :path
66 *
67 * An equivalent HTTP/1.x absolute-form request would then look like :
68 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
69 *
70 * Except for CONNECT which doesn't have scheme nor path and looks like :
71 * <:method> <:authority> HTTP/x.y
72 *
73 * It's worth noting that H2 still supports an encoding to map H1 origin-form
74 * and asterisk-form requests. These ones do not specify the authority. However
75 * in H2 they must still specify the scheme, which is not present in H1. Also,
76 * when encoding an absolute-form H1 request without a path, the path
77 * automatically becomes "/" except for the OPTIONS method where it
78 * becomes "*".
79 *
80 * As such it is explicitly permitted for an H2 client to send a request
81 * featuring a Host header and no :authority, though it's not the recommended
82 * way to use H2 for a client. It is however the only permitted way to encode
83 * an origin-form H1 request over H2. Thus we need to respect such differences
84 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +010085 */
86static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
87{
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +010088 struct ist uri, meth_sl;
Willy Tarreau6deb4122018-11-27 15:34:18 +010089 unsigned int flags = HTX_SL_F_NONE;
90 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +010091 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +010092
93 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +010094 if (fields & H2_PHDR_FND_PROT) {
95 /* rfc 8441 Extended Connect Protocol
96 * #4 :scheme and :path must be present, as well as
97 * :authority like all h2 requests
98 */
99 if (!(fields & H2_PHDR_FND_SCHM)) {
100 /* missing scheme */
101 goto fail;
102 }
103 else if (!(fields & H2_PHDR_FND_PATH)) {
104 /* missing path */
105 goto fail;
106 }
107 else if (!(fields & H2_PHDR_FND_AUTH)) {
108 /* missing authority */
109 goto fail;
110 }
111
112 flags |= HTX_SL_F_HAS_SCHM;
113 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
114 flags |= HTX_SL_F_SCHM_HTTP;
115 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
116 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreaua495e0d2021-08-10 15:37:34 +0200117 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
118 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100119
120 meth_sl = ist("GET");
121
122 *msgf |= H2_MSGF_EXT_CONNECT;
123 /* no ES on the HEADERS frame but no body either for
124 * Extended CONNECT */
125 *msgf &= ~H2_MSGF_BODY;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100126 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100127 else {
128 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
129 * MUST be omitted ; ":authority" contains the host and port
130 * to connect to.
131 */
132 if (fields & H2_PHDR_FND_SCHM) {
133 /* scheme not allowed */
134 goto fail;
135 }
136 else if (fields & H2_PHDR_FND_PATH) {
137 /* path not allowed */
138 goto fail;
139 }
140 else if (!(fields & H2_PHDR_FND_AUTH)) {
141 /* missing authority */
142 goto fail;
143 }
144
145 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau6deb4122018-11-27 15:34:18 +0100146 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100147
Willy Tarreau6deb4122018-11-27 15:34:18 +0100148 *msgf |= H2_MSGF_BODY_TUNNEL;
149 }
150 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
151 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
152 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
153 * valid value for the ":method", ":scheme" and ":path" phdr
154 * unless it is a CONNECT request.
155 */
156 if (!(fields & H2_PHDR_FND_METH)) {
157 /* missing method */
158 goto fail;
159 }
160 else if (!(fields & H2_PHDR_FND_SCHM)) {
161 /* missing scheme */
162 goto fail;
163 }
164 else {
165 /* missing path */
166 goto fail;
167 }
168 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200169 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200170 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
171 * classify the scheme as "present/http", "present/https",
172 * "present/other", "absent" so as to decide whether or not
173 * we're facing a normalized URI that will have to be encoded
174 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
175 * clients should use the absolute form, thus we cannot infer
176 * whether or not the client wanted to use a proxy here.
177 */
178 flags |= HTX_SL_F_HAS_SCHM;
179 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
180 flags |= HTX_SL_F_SCHM_HTTP;
181 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
182 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreaua495e0d2021-08-10 15:37:34 +0200183 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
184 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100185
186 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau92919f72019-10-08 16:53:07 +0200187 }
188
Willy Tarreau4b8852c2021-08-10 16:30:55 +0200189 if (fields & H2_PHDR_FND_PATH) {
190 /* 7540#8.1.2.3: :path must not be empty, and must be either
191 * '*' or an RFC3986 "path-absolute" starting with a "/" but
192 * not with "//".
Willy Tarreau46b7dff2021-08-19 23:06:58 +0200193 * However, this "path-absolute" was a mistake which was
194 * later fixed in http2bis as "absolute-path" to match
195 * HTTP/1, thus also allowing "//".
Willy Tarreau4b8852c2021-08-10 16:30:55 +0200196 */
197 if (unlikely(!phdr[H2_PHDR_IDX_PATH].len))
198 goto fail;
199 else if (unlikely(phdr[H2_PHDR_IDX_PATH].ptr[0] != '/')) {
200 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
201 goto fail;
202 }
Willy Tarreau4b8852c2021-08-10 16:30:55 +0200203 }
204
Willy Tarreau92919f72019-10-08 16:53:07 +0200205 if (!(flags & HTX_SL_F_HAS_SCHM)) {
206 /* no scheme, use authority only (CONNECT) */
207 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200208 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200209 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200210 else if (fields & H2_PHDR_FND_AUTH) {
211 /* authority is present, let's use the absolute form. We simply
212 * use the trash to concatenate them since all of them MUST fit
213 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200214 */
215 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
216 istcat(&uri, ist("://"), trash.size);
217 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
218 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
219 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200220 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200221
222 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
223 /* we don't know if it was originally an absolute or a
224 * relative request because newer versions of HTTP use
225 * the absolute URI format by default, which we call
226 * the normalized URI format internally. This is the
227 * strongly recommended way of sending a request for
228 * a regular client, so we cannot distinguish this
229 * from a request intended for a proxy. For other
230 * schemes however there is no doubt.
231 */
232 flags |= HTX_SL_F_NORMALIZED_URI;
233 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200234 }
235 else {
236 /* usual schemes with or without authority, use origin form */
237 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200238 if (fields & H2_PHDR_FND_AUTH)
239 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200240 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100241
Willy Tarreau89265222021-08-11 11:12:46 +0200242 /* The method is a non-empty token (RFC7231#4.1) */
243 if (!meth_sl.len)
244 goto fail;
245 for (i = 0; i < meth_sl.len; i++) {
246 if (!HTTP_IS_TOKEN(meth_sl.ptr[i]))
247 htx->flags |= HTX_FL_PARSING_ERROR;
248 }
249
Willy Tarreau2be362c2019-10-08 11:59:37 +0200250 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
251 * that :path must not be empty.
252 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200253 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100254 goto fail;
255
Willy Tarreau2be362c2019-10-08 11:59:37 +0200256 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200257 for (i = 0; i < uri.len; i++) {
258 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100259 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
260 htx->flags |= HTX_FL_PARSING_ERROR;
261 }
262
Willy Tarreau6deb4122018-11-27 15:34:18 +0100263 /* Set HTX start-line flags */
264 flags |= HTX_SL_F_VER_11; // V2 in fact
265 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
266
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100267 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_sl, uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100268 if (!sl)
269 goto fail;
270
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100271 sl->info.req.meth = find_http_meth(meth_sl.ptr, meth_sl.len);
Christopher Faulet7d247f02020-12-02 14:26:36 +0100272 if (sl->info.req.meth == HTTP_METH_HEAD)
273 *msgf |= H2_MSGF_BODYLESS_RSP;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100274 return sl;
275 fail:
276 return NULL;
277}
278
279/* Takes an H2 request present in the headers list <list> terminated by a name
280 * being <NULL,0> and emits the equivalent HTX request according to the rules
281 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
282 * non-zero is returned if some bytes were emitted. In case of error, a
283 * negative error code is returned.
284 *
285 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
286 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
287 * if a body is detected (!ES).
288 *
289 * The headers list <list> must be composed of :
290 * - n.name != NULL, n.len > 0 : literal header name
291 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
292 * among H2_PHDR_IDX_*
293 * - n.name ignored, n.len == 0 : end of list
294 * - in all cases except the end of list, v.name and v.len must designate a
295 * valid value.
296 *
297 * The Cookie header will be reassembled at the end, and for this, the <list>
298 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreaub6be1a42023-08-08 15:38:28 +0200299 *
300 * When <relaxed> is non-nul, some non-dangerous checks will be ignored. This
301 * is in order to satisfy "option accept-invalid-http-request" for
302 * interoperability purposes.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100303 */
Willy Tarreaub6be1a42023-08-08 15:38:28 +0200304int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len, int relaxed)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100305{
306 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
307 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
308 uint32_t idx;
309 int ck, lck; /* cookie index and last cookie index */
310 int phdr;
311 int ret;
312 int i;
313 struct htx_sl *sl = NULL;
314 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100315 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100316
317 lck = ck = -1; // no cookie for now
318 fields = 0;
319 for (idx = 0; list[idx].n.len != 0; idx++) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100320 if (!isttest(list[idx].n)) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100321 /* this is an indexed pseudo-header */
322 phdr = list[idx].n.len;
323 }
324 else {
325 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100326 /* RFC7540#8.1.2: upper case not allowed in header field names.
327 * #10.3: header names must be valid (i.e. match a token).
328 * For pseudo-headers we check from 2nd char and for other ones
329 * from the first char, because HTTP_IS_TOKEN() also excludes
330 * the colon.
331 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100332 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100333
334 for (i = !!phdr; i < list[idx].n.len; i++)
335 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
336 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100337 }
338
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100339 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
Willy Tarreau462a8602023-08-08 15:40:49 +0200340 * rejecting NUL, CR and LF characters. For :path we reject all CTL
341 * chars, spaces, and '#'.
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100342 */
Willy Tarreau462a8602023-08-08 15:40:49 +0200343 if (phdr == H2_PHDR_IDX_PATH && !relaxed) {
344 ctl = ist_find_range(list[idx].v, 0, '#');
345 if (unlikely(ctl) && http_path_has_forbidden_char(list[idx].v, ctl))
346 goto fail;
347 } else {
348 ctl = ist_find_ctl(list[idx].v);
349 if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl))
350 goto fail;
351 }
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100352
Willy Tarreau6deb4122018-11-27 15:34:18 +0100353 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
354 /* insert a pseudo header by its index (in phdr) and value (in value) */
355 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
356 if (fields & H2_PHDR_FND_NONE) {
357 /* pseudo header field after regular headers */
358 goto fail;
359 }
360 else {
361 /* repeated pseudo header field */
362 goto fail;
363 }
364 }
365 fields |= 1 << phdr;
366 phdr_val[phdr] = list[idx].v;
367 continue;
368 }
369 else if (phdr != 0) {
370 /* invalid pseudo header -- should never happen here */
371 goto fail;
372 }
373
374 /* regular header field in (name,value) */
375 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
376 /* no more pseudo-headers, time to build the request line */
377 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
378 if (!sl)
379 goto fail;
380 fields |= H2_PHDR_FND_NONE;
Willy Tarreaub5d2b9e2021-08-11 15:39:13 +0200381
382 /* http2bis draft recommends to drop Host in favor of :authority when
383 * the latter is present. This is required to make sure there is no
384 * discrepancy between the authority and the host header, especially
385 * since routing rules usually involve Host. Here we already know if
386 * :authority was found so we can emit it right now and mark the host
387 * as filled so that it's skipped later.
388 */
389 if (fields & H2_PHDR_FND_AUTH) {
390 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
391 goto fail;
392 fields |= H2_PHDR_FND_HOST;
393 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100394 }
395
Willy Tarreaub5d2b9e2021-08-11 15:39:13 +0200396 if (isteq(list[idx].n, ist("host"))) {
397 if (fields & H2_PHDR_FND_HOST)
398 continue;
399
Willy Tarreau6deb4122018-11-27 15:34:18 +0100400 fields |= H2_PHDR_FND_HOST;
Willy Tarreaub5d2b9e2021-08-11 15:39:13 +0200401 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100402
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100403 if (isteq(list[idx].n, ist("content-length"))) {
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100404 ret = http_parse_cont_len_header(&list[idx].v, body_len,
405 *msgf & H2_MSGF_BODY_CL);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100406 if (ret < 0)
407 goto fail;
408
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100409 *msgf |= H2_MSGF_BODY_CL;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100410 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100411 if (ret == 0)
412 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100413 }
414
415 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
416 if (isteq(list[idx].n, ist("connection")) ||
417 isteq(list[idx].n, ist("proxy-connection")) ||
418 isteq(list[idx].n, ist("keep-alive")) ||
419 isteq(list[idx].n, ist("upgrade")) ||
420 isteq(list[idx].n, ist("transfer-encoding")))
421 goto fail;
422
423 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
424 goto fail;
425
426 /* cookie requires special processing at the end */
427 if (isteq(list[idx].n, ist("cookie"))) {
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +0200428 http_cookie_register(list, idx, &ck, &lck);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100429 continue;
430 }
431
432 if (!htx_add_header(htx, list[idx].n, list[idx].v))
433 goto fail;
434 }
435
436 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
437 if (fields & H2_PHDR_FND_STAT)
438 goto fail;
439
440 /* Let's dump the request now if not yet emitted. */
441 if (!(fields & H2_PHDR_FND_NONE)) {
442 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
443 if (!sl)
444 goto fail;
445 }
446
Christopher Fauletd0db4232021-01-22 11:46:30 +0100447 if (*msgf & H2_MSGF_BODY_TUNNEL)
448 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
449
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100450 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
451 (*msgf & H2_MSGF_BODY_TUNNEL)) {
452 /* Request without body or tunnel requested */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100453 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100454 htx->flags |= HTX_FL_EOM;
455 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100456
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100457 if (*msgf & H2_MSGF_EXT_CONNECT) {
458 if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT]))
459 goto fail;
460 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
461 goto fail;
462 sl_flags |= HTX_SL_F_CONN_UPG;
463 }
464
Willy Tarreau6deb4122018-11-27 15:34:18 +0100465 /* update the start line with last detected header info */
466 sl->flags |= sl_flags;
467
Willy Tarreaub5d2b9e2021-08-11 15:39:13 +0200468 /* complete with missing Host if needed (we may validate this test if
469 * no regular header was found).
470 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100471 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
472 /* missing Host field, use :authority instead */
473 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
474 goto fail;
475 }
476
477 /* now we may have to build a cookie list. We'll dump the values of all
478 * visited headers.
479 */
480 if (ck >= 0) {
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +0200481 if (http_cookie_merge(htx, list, ck))
Willy Tarreau6deb4122018-11-27 15:34:18 +0100482 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100483 }
484
485 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100486 if (!htx_add_endof(htx, HTX_BLK_EOH))
487 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100488
Amaury Denoyelle4ca0f362021-07-07 10:49:28 +0200489 /* proceed to scheme-based normalization on target-URI */
490 if (fields & H2_PHDR_FND_SCHM)
491 http_scheme_based_normalize(htx);
492
Willy Tarreau6deb4122018-11-27 15:34:18 +0100493 ret = 1;
494 return ret;
495
496 fail:
497 return -1;
498}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200499
500/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
501 * <fields> indicates what was found so far. This should be called once at the
502 * detection of the first general header field or at the end of the message if
503 * no general header field was found yet. Returns the created start line on
504 * success, or NULL on failure. Upon success, <msgf> is updated with a few
505 * H2_MSGF_* flags indicating what was found while parsing.
506 */
507static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
508{
Willy Tarreaud8a44d02022-09-02 11:15:37 +0200509 unsigned int status, flags = HTX_SL_F_IS_RESP;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200510 struct htx_sl *sl;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100511 struct ist stat;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200512
513 /* only :status is allowed as a pseudo header */
514 if (!(fields & H2_PHDR_FND_STAT))
515 goto fail;
516
517 if (phdr[H2_PHDR_IDX_STAT].len != 3)
518 goto fail;
519
Amaury Denoyelle74162742020-12-11 17:53:05 +0100520 /* if Extended CONNECT is used, convert status code from 200 to htx 101
521 * following rfc 8441 */
522 if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) &&
523 isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) {
524 stat = ist("101");
525 status = 101;
526 }
527 else {
528 unsigned char h, t, u;
529
530 stat = phdr[H2_PHDR_IDX_STAT];
531
532 h = stat.ptr[0] - '0';
533 t = stat.ptr[1] - '0';
534 u = stat.ptr[2] - '0';
535 if (h > 9 || t > 9 || u > 9)
536 goto fail;
537 status = h * 100 + t * 10 + u;
538 }
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200539
Christopher Faulet89899422020-12-07 18:24:43 +0100540 /* 101 responses are not supported in H2, so return a error.
541 * On 1xx responses there is no ES on the HEADERS frame but there is no
542 * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to
543 * notify the decoder another HEADERS frame is expected.
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500544 * 204/304 response have no body by definition. So remove the flag
Christopher Faulet7d247f02020-12-02 14:26:36 +0100545 * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100546 *
547 * Note however that there is a special condition for Extended CONNECT.
548 * In this case, we explicitly convert it to HTX 101 to mimic
549 * Get+Upgrade HTTP/1.1 mechanism
Christopher Faulet0b465482019-02-19 15:14:23 +0100550 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100551 if (status == 101) {
552 if (!(*msgf & H2_MSGF_EXT_CONNECT))
553 goto fail;
554 }
Christopher Faulet89899422020-12-07 18:24:43 +0100555 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100556 *msgf |= H2_MSGF_RSP_1XX;
557 *msgf &= ~H2_MSGF_BODY;
558 }
Amaury Denoyelle74162742020-12-11 17:53:05 +0100559 else if (status == 204 || status == 304) {
Christopher Faulet7d247f02020-12-02 14:26:36 +0100560 *msgf &= ~H2_MSGF_BODY;
561 *msgf |= H2_MSGF_BODYLESS_RSP;
562 }
Christopher Faulet0b465482019-02-19 15:14:23 +0100563
Christopher Faulet89899422020-12-07 18:24:43 +0100564 /* Set HTX start-line flags */
565 flags |= HTX_SL_F_VER_11; // V2 in fact
566 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
567
Amaury Denoyelle74162742020-12-11 17:53:05 +0100568 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), stat, ist(""));
Christopher Faulet89899422020-12-07 18:24:43 +0100569 if (!sl)
570 goto fail;
571 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200572 return sl;
573 fail:
574 return NULL;
575}
576
577/* Takes an H2 response present in the headers list <list> terminated by a name
578 * being <NULL,0> and emits the equivalent HTX response according to the rules
579 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
580 * a positive value is returned if some bytes were emitted. In case of error, a
581 * negative error code is returned.
582 *
583 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
584 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
585 * if a body is detected (!ES).
586 *
587 * The headers list <list> must be composed of :
588 * - n.name != NULL, n.len > 0 : literal header name
589 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
590 * among H2_PHDR_IDX_*
591 * - n.name ignored, n.len == 0 : end of list
592 * - in all cases except the end of list, v.name and v.len must designate a
593 * valid value.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100594 *
595 * <upgrade_protocol> is only used if the htx status code is 101 indicating a
596 * response to an upgrade or h2-equivalent request.
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200597 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100598int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len, char *upgrade_protocol)
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200599{
600 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
601 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
602 uint32_t idx;
603 int phdr;
604 int ret;
605 int i;
606 struct htx_sl *sl = NULL;
607 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100608 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200609
610 fields = 0;
611 for (idx = 0; list[idx].n.len != 0; idx++) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100612 if (!isttest(list[idx].n)) {
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200613 /* this is an indexed pseudo-header */
614 phdr = list[idx].n.len;
615 }
616 else {
617 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100618 /* RFC7540#8.1.2: upper case not allowed in header field names.
619 * #10.3: header names must be valid (i.e. match a token).
620 * For pseudo-headers we check from 2nd char and for other ones
621 * from the first char, because HTTP_IS_TOKEN() also excludes
622 * the colon.
623 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200624 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100625
626 for (i = !!phdr; i < list[idx].n.len; i++)
627 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
628 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200629 }
630
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100631 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
632 * rejecting NUL, CR and LF characters.
633 */
634 ctl = ist_find_ctl(list[idx].v);
Willy Tarreau21c4ffd2023-08-08 17:00:50 +0200635 if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl))
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100636 goto fail;
637
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200638 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
639 /* insert a pseudo header by its index (in phdr) and value (in value) */
640 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
641 if (fields & H2_PHDR_FND_NONE) {
642 /* pseudo header field after regular headers */
643 goto fail;
644 }
645 else {
646 /* repeated pseudo header field */
647 goto fail;
648 }
649 }
650 fields |= 1 << phdr;
651 phdr_val[phdr] = list[idx].v;
652 continue;
653 }
654 else if (phdr != 0) {
655 /* invalid pseudo header -- should never happen here */
656 goto fail;
657 }
658
659 /* regular header field in (name,value) */
660 if (!(fields & H2_PHDR_FND_NONE)) {
661 /* no more pseudo-headers, time to build the status line */
662 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
663 if (!sl)
664 goto fail;
665 fields |= H2_PHDR_FND_NONE;
666 }
667
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100668 if (isteq(list[idx].n, ist("content-length"))) {
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100669 ret = http_parse_cont_len_header(&list[idx].v, body_len,
670 *msgf & H2_MSGF_BODY_CL);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100671 if (ret < 0)
672 goto fail;
673
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100674 *msgf |= H2_MSGF_BODY_CL;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200675 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100676 if (ret == 0)
677 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200678 }
679
680 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
681 if (isteq(list[idx].n, ist("connection")) ||
682 isteq(list[idx].n, ist("proxy-connection")) ||
683 isteq(list[idx].n, ist("keep-alive")) ||
684 isteq(list[idx].n, ist("upgrade")) ||
685 isteq(list[idx].n, ist("transfer-encoding")))
686 goto fail;
687
688 if (!htx_add_header(htx, list[idx].n, list[idx].v))
689 goto fail;
690 }
691
692 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
693 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
694 goto fail;
695
696 /* Let's dump the request now if not yet emitted. */
697 if (!(fields & H2_PHDR_FND_NONE)) {
698 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
699 if (!sl)
700 goto fail;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100701 }
702
703 if (sl->info.res.status == 101 && upgrade_protocol) {
704 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
705 goto fail;
706 if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol)))
707 goto fail;
708 sl_flags |= HTX_SL_F_CONN_UPG;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200709 }
710
Amaury Denoyelle74162742020-12-11 17:53:05 +0100711 if ((*msgf & H2_MSGF_BODY_TUNNEL) &&
712 ((sl->info.res.status >= 200 && sl->info.res.status < 300) || sl->info.res.status == 101))
Christopher Fauletd0db4232021-01-22 11:46:30 +0100713 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
714 else
715 *msgf &= ~H2_MSGF_BODY_TUNNEL;
716
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100717 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
718 (*msgf & H2_MSGF_BODY_TUNNEL)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500719 /* Response without body or tunnel successfully established */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100720 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100721 htx->flags |= HTX_FL_EOM;
722 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100723
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200724 /* update the start line with last detected header info */
725 sl->flags |= sl_flags;
726
727 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
728 /* FIXME: Do we need to signal anything when we have a body and
729 * no content-length, to have the equivalent of H1's chunked
730 * encoding?
731 */
732 }
733
734 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100735 if (!htx_add_endof(htx, HTX_BLK_EOH))
736 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200737
738 ret = 1;
739 return ret;
740
741 fail:
742 return -1;
743}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100744
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200745/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
746 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
747 * and a positive value is returned if some bytes were emitted. In case of
748 * error, a negative error code is returned. The caller must have verified that
749 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100750 *
751 * The headers list <list> must be composed of :
752 * - n.name != NULL, n.len > 0 : literal header name
753 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
754 * among H2_PHDR_IDX_* (illegal here)
755 * - n.name ignored, n.len == 0 : end of list
756 * - in all cases except the end of list, v.name and v.len must designate a
757 * valid value.
758 */
759int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
760{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100761 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100762 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100763 int i;
764
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100765 for (idx = 0; list[idx].n.len != 0; idx++) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100766 if (!isttest(list[idx].n)) {
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100767 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
768 goto fail;
769 }
770
Willy Tarreau146f53a2019-11-24 10:34:39 +0100771 /* RFC7540#8.1.2: upper case not allowed in header field names.
772 * #10.3: header names must be valid (i.e. match a token). This
773 * also catches pseudo-headers which are forbidden in trailers.
774 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100775 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100776 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 +0100777 goto fail;
778
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100779 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
780 if (isteq(list[idx].n, ist("host")) ||
781 isteq(list[idx].n, ist("content-length")) ||
782 isteq(list[idx].n, ist("connection")) ||
783 isteq(list[idx].n, ist("proxy-connection")) ||
784 isteq(list[idx].n, ist("keep-alive")) ||
785 isteq(list[idx].n, ist("upgrade")) ||
786 isteq(list[idx].n, ist("te")) ||
787 isteq(list[idx].n, ist("transfer-encoding")))
788 goto fail;
789
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100790 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
791 * rejecting NUL, CR and LF characters.
792 */
793 ctl = ist_find_ctl(list[idx].v);
Willy Tarreau21c4ffd2023-08-08 17:00:50 +0200794 if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl))
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100795 goto fail;
796
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200797 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
798 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100799 }
800
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200801 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100802 goto fail;
803
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100804 return 1;
805
806 fail:
807 return -1;
808}