blob: d2eb0b34bb8c89d007315329ca5e3bc7ffa5d3e0 [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 Faulet6557acd2024-08-01 16:01:50 +0200454 if (*msgf & H2_MSGF_BODY_TUNNEL)
455 htx->flags |= HTX_FL_EOM;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100456 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100457
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100458 if (*msgf & H2_MSGF_EXT_CONNECT) {
459 if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT]))
460 goto fail;
461 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
462 goto fail;
463 sl_flags |= HTX_SL_F_CONN_UPG;
464 }
465
Willy Tarreau6deb4122018-11-27 15:34:18 +0100466 /* update the start line with last detected header info */
467 sl->flags |= sl_flags;
468
Willy Tarreaub5d2b9e2021-08-11 15:39:13 +0200469 /* complete with missing Host if needed (we may validate this test if
470 * no regular header was found).
471 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100472 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
473 /* missing Host field, use :authority instead */
474 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
475 goto fail;
476 }
477
478 /* now we may have to build a cookie list. We'll dump the values of all
479 * visited headers.
480 */
481 if (ck >= 0) {
Amaury Denoyelle2c5a7ee2022-08-17 16:33:53 +0200482 if (http_cookie_merge(htx, list, ck))
Willy Tarreau6deb4122018-11-27 15:34:18 +0100483 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100484 }
485
486 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100487 if (!htx_add_endof(htx, HTX_BLK_EOH))
488 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100489
Amaury Denoyelle4ca0f362021-07-07 10:49:28 +0200490 /* proceed to scheme-based normalization on target-URI */
491 if (fields & H2_PHDR_FND_SCHM)
492 http_scheme_based_normalize(htx);
493
Willy Tarreau6deb4122018-11-27 15:34:18 +0100494 ret = 1;
495 return ret;
496
497 fail:
498 return -1;
499}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200500
501/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
502 * <fields> indicates what was found so far. This should be called once at the
503 * detection of the first general header field or at the end of the message if
504 * no general header field was found yet. Returns the created start line on
505 * success, or NULL on failure. Upon success, <msgf> is updated with a few
506 * H2_MSGF_* flags indicating what was found while parsing.
507 */
508static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
509{
Willy Tarreaud8a44d02022-09-02 11:15:37 +0200510 unsigned int status, flags = HTX_SL_F_IS_RESP;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200511 struct htx_sl *sl;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100512 struct ist stat;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200513
514 /* only :status is allowed as a pseudo header */
515 if (!(fields & H2_PHDR_FND_STAT))
516 goto fail;
517
518 if (phdr[H2_PHDR_IDX_STAT].len != 3)
519 goto fail;
520
Amaury Denoyelle74162742020-12-11 17:53:05 +0100521 /* if Extended CONNECT is used, convert status code from 200 to htx 101
522 * following rfc 8441 */
523 if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) &&
524 isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) {
525 stat = ist("101");
526 status = 101;
527 }
528 else {
529 unsigned char h, t, u;
530
531 stat = phdr[H2_PHDR_IDX_STAT];
532
533 h = stat.ptr[0] - '0';
534 t = stat.ptr[1] - '0';
535 u = stat.ptr[2] - '0';
536 if (h > 9 || t > 9 || u > 9)
537 goto fail;
538 status = h * 100 + t * 10 + u;
539 }
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.
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500545 * 204/304 response have no body by definition. So remove the flag
Christopher Faulet7d247f02020-12-02 14:26:36 +0100546 * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100547 *
548 * Note however that there is a special condition for Extended CONNECT.
549 * In this case, we explicitly convert it to HTX 101 to mimic
550 * Get+Upgrade HTTP/1.1 mechanism
Christopher Faulet0b465482019-02-19 15:14:23 +0100551 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100552 if (status == 101) {
553 if (!(*msgf & H2_MSGF_EXT_CONNECT))
554 goto fail;
555 }
Christopher Faulet89899422020-12-07 18:24:43 +0100556 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100557 *msgf |= H2_MSGF_RSP_1XX;
558 *msgf &= ~H2_MSGF_BODY;
559 }
Amaury Denoyelle74162742020-12-11 17:53:05 +0100560 else if (status == 204 || status == 304) {
Christopher Faulet7d247f02020-12-02 14:26:36 +0100561 *msgf &= ~H2_MSGF_BODY;
562 *msgf |= H2_MSGF_BODYLESS_RSP;
563 }
Christopher Faulet0b465482019-02-19 15:14:23 +0100564
Christopher Faulet89899422020-12-07 18:24:43 +0100565 /* Set HTX start-line flags */
566 flags |= HTX_SL_F_VER_11; // V2 in fact
567 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
568
Amaury Denoyelle74162742020-12-11 17:53:05 +0100569 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), stat, ist(""));
Christopher Faulet89899422020-12-07 18:24:43 +0100570 if (!sl)
571 goto fail;
572 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200573 return sl;
574 fail:
575 return NULL;
576}
577
578/* Takes an H2 response present in the headers list <list> terminated by a name
579 * being <NULL,0> and emits the equivalent HTX response according to the rules
580 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
581 * a positive value is returned if some bytes were emitted. In case of error, a
582 * negative error code is returned.
583 *
584 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
585 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
586 * if a body is detected (!ES).
587 *
588 * The headers list <list> must be composed of :
589 * - n.name != NULL, n.len > 0 : literal header name
590 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
591 * among H2_PHDR_IDX_*
592 * - n.name ignored, n.len == 0 : end of list
593 * - in all cases except the end of list, v.name and v.len must designate a
594 * valid value.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100595 *
596 * <upgrade_protocol> is only used if the htx status code is 101 indicating a
597 * response to an upgrade or h2-equivalent request.
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200598 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100599int 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 +0200600{
601 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
602 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
603 uint32_t idx;
604 int phdr;
605 int ret;
606 int i;
607 struct htx_sl *sl = NULL;
608 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100609 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200610
611 fields = 0;
612 for (idx = 0; list[idx].n.len != 0; idx++) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100613 if (!isttest(list[idx].n)) {
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200614 /* this is an indexed pseudo-header */
615 phdr = list[idx].n.len;
616 }
617 else {
618 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100619 /* RFC7540#8.1.2: upper case not allowed in header field names.
620 * #10.3: header names must be valid (i.e. match a token).
621 * For pseudo-headers we check from 2nd char and for other ones
622 * from the first char, because HTTP_IS_TOKEN() also excludes
623 * the colon.
624 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200625 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100626
627 for (i = !!phdr; i < list[idx].n.len; i++)
628 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
629 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200630 }
631
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100632 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
633 * rejecting NUL, CR and LF characters.
634 */
635 ctl = ist_find_ctl(list[idx].v);
Willy Tarreau21c4ffd2023-08-08 17:00:50 +0200636 if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl))
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100637 goto fail;
638
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200639 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
640 /* insert a pseudo header by its index (in phdr) and value (in value) */
641 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
642 if (fields & H2_PHDR_FND_NONE) {
643 /* pseudo header field after regular headers */
644 goto fail;
645 }
646 else {
647 /* repeated pseudo header field */
648 goto fail;
649 }
650 }
651 fields |= 1 << phdr;
652 phdr_val[phdr] = list[idx].v;
653 continue;
654 }
655 else if (phdr != 0) {
656 /* invalid pseudo header -- should never happen here */
657 goto fail;
658 }
659
660 /* regular header field in (name,value) */
661 if (!(fields & H2_PHDR_FND_NONE)) {
662 /* no more pseudo-headers, time to build the status line */
663 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
664 if (!sl)
665 goto fail;
666 fields |= H2_PHDR_FND_NONE;
667 }
668
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100669 if (isteq(list[idx].n, ist("content-length"))) {
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100670 ret = http_parse_cont_len_header(&list[idx].v, body_len,
671 *msgf & H2_MSGF_BODY_CL);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100672 if (ret < 0)
673 goto fail;
674
Amaury Denoyelle15f3cc42022-12-08 16:53:58 +0100675 *msgf |= H2_MSGF_BODY_CL;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200676 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100677 if (ret == 0)
678 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200679 }
680
681 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
682 if (isteq(list[idx].n, ist("connection")) ||
683 isteq(list[idx].n, ist("proxy-connection")) ||
684 isteq(list[idx].n, ist("keep-alive")) ||
685 isteq(list[idx].n, ist("upgrade")) ||
686 isteq(list[idx].n, ist("transfer-encoding")))
687 goto fail;
688
689 if (!htx_add_header(htx, list[idx].n, list[idx].v))
690 goto fail;
691 }
692
693 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
694 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
695 goto fail;
696
697 /* Let's dump the request now if not yet emitted. */
698 if (!(fields & H2_PHDR_FND_NONE)) {
699 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
700 if (!sl)
701 goto fail;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100702 }
703
704 if (sl->info.res.status == 101 && upgrade_protocol) {
705 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
706 goto fail;
707 if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol)))
708 goto fail;
709 sl_flags |= HTX_SL_F_CONN_UPG;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200710 }
711
Amaury Denoyelle74162742020-12-11 17:53:05 +0100712 if ((*msgf & H2_MSGF_BODY_TUNNEL) &&
713 ((sl->info.res.status >= 200 && sl->info.res.status < 300) || sl->info.res.status == 101))
Christopher Fauletd0db4232021-01-22 11:46:30 +0100714 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
715 else
716 *msgf &= ~H2_MSGF_BODY_TUNNEL;
717
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100718 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
719 (*msgf & H2_MSGF_BODY_TUNNEL)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500720 /* Response without body or tunnel successfully established */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100721 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Faulet6557acd2024-08-01 16:01:50 +0200722 if (*msgf & H2_MSGF_BODY_TUNNEL)
723 htx->flags |= HTX_FL_EOM;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100724 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100725
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200726 /* update the start line with last detected header info */
727 sl->flags |= sl_flags;
728
729 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
730 /* FIXME: Do we need to signal anything when we have a body and
731 * no content-length, to have the equivalent of H1's chunked
732 * encoding?
733 */
734 }
735
736 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100737 if (!htx_add_endof(htx, HTX_BLK_EOH))
738 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200739
740 ret = 1;
741 return ret;
742
743 fail:
744 return -1;
745}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100746
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200747/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
748 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
749 * and a positive value is returned if some bytes were emitted. In case of
750 * error, a negative error code is returned. The caller must have verified that
751 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100752 *
753 * The headers list <list> must be composed of :
754 * - n.name != NULL, n.len > 0 : literal header name
755 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
756 * among H2_PHDR_IDX_* (illegal here)
757 * - n.name ignored, n.len == 0 : end of list
758 * - in all cases except the end of list, v.name and v.len must designate a
759 * valid value.
760 */
761int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
762{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100763 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100764 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100765 int i;
766
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100767 for (idx = 0; list[idx].n.len != 0; idx++) {
Tim Duesterhus77508502022-03-15 13:11:06 +0100768 if (!isttest(list[idx].n)) {
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100769 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
770 goto fail;
771 }
772
Willy Tarreau146f53a2019-11-24 10:34:39 +0100773 /* RFC7540#8.1.2: upper case not allowed in header field names.
774 * #10.3: header names must be valid (i.e. match a token). This
775 * also catches pseudo-headers which are forbidden in trailers.
776 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100777 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100778 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 +0100779 goto fail;
780
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100781 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
782 if (isteq(list[idx].n, ist("host")) ||
783 isteq(list[idx].n, ist("content-length")) ||
784 isteq(list[idx].n, ist("connection")) ||
785 isteq(list[idx].n, ist("proxy-connection")) ||
786 isteq(list[idx].n, ist("keep-alive")) ||
787 isteq(list[idx].n, ist("upgrade")) ||
788 isteq(list[idx].n, ist("te")) ||
789 isteq(list[idx].n, ist("transfer-encoding")))
790 goto fail;
791
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100792 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
793 * rejecting NUL, CR and LF characters.
794 */
795 ctl = ist_find_ctl(list[idx].v);
Willy Tarreau21c4ffd2023-08-08 17:00:50 +0200796 if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl))
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100797 goto fail;
798
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200799 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
800 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100801 }
802
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200803 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100804 goto fail;
805
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100806 return 1;
807
808 fail:
809 return -1;
810}