blob: e190c52b546d06b80671bc956815f2aee4097b4a [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 Denoyelle03534102021-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 Tarreau54f53ef2019-11-22 16:02:43 +010052/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
53 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
54 * non-zero if such a character is found, 0 otherwise. When run on unlikely
55 * header match, it's recommended to first check for the presence of control
56 * chars using ist_find_ctl().
57 */
58static int has_forbidden_char(const struct ist ist, const char *start)
59{
60 do {
61 if ((uint8_t)*start <= 0x0d &&
62 (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
63 return 1;
64 start++;
65 } while (start < ist.ptr + ist.len);
66 return 0;
67}
68
Willy Tarreaubeefaee2018-12-19 13:08:08 +010069/* Parse the Content-Length header field of an HTTP/2 request. The function
70 * checks all possible occurrences of a comma-delimited value, and verifies
71 * if any of them doesn't match a previous value. It returns <0 if a value
72 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
73 * if the value can be indexed (first one). In the last case, the value might
74 * be adjusted and the caller must only add the updated value.
75 */
76int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
77{
78 char *e, *n;
79 unsigned long long cl;
80 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
81 struct ist word;
82
Willy Tarreauba9afd22023-08-09 08:32:48 +020083 word.ptr = value->ptr;
Willy Tarreaubeefaee2018-12-19 13:08:08 +010084 e = value->ptr + value->len;
85
Willy Tarreauba9afd22023-08-09 08:32:48 +020086 while (1) {
87 if (word.ptr >= e) {
88 /* empty header or empty value */
89 goto fail;
90 }
91
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +050092 /* skip leading delimiter and blanks */
Willy Tarreauba9afd22023-08-09 08:32:48 +020093 if (unlikely(HTTP_IS_LWS(*word.ptr))) {
94 word.ptr++;
Willy Tarreaubeefaee2018-12-19 13:08:08 +010095 continue;
Willy Tarreauba9afd22023-08-09 08:32:48 +020096 }
Willy Tarreaubeefaee2018-12-19 13:08:08 +010097
98 /* digits only now */
99 for (cl = 0, n = word.ptr; n < e; n++) {
100 unsigned int c = *n - '0';
101 if (unlikely(c > 9)) {
102 /* non-digit */
103 if (unlikely(n == word.ptr)) // spaces only
104 goto fail;
105 break;
106 }
107 if (unlikely(cl > ULLONG_MAX / 10ULL))
108 goto fail; /* multiply overflow */
109 cl = cl * 10ULL;
110 if (unlikely(cl + c < cl))
111 goto fail; /* addition overflow */
112 cl = cl + c;
113 }
114
115 /* keep a copy of the exact cleaned value */
116 word.len = n - word.ptr;
117
118 /* skip trailing LWS till next comma or EOL */
119 for (; n < e; n++) {
120 if (!HTTP_IS_LWS(*n)) {
121 if (unlikely(*n != ','))
122 goto fail;
123 break;
124 }
125 }
126
127 /* if duplicate, must be equal */
128 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
129 goto fail;
130
131 /* OK, store this result as the one to be indexed */
132 *msgf |= H2_MSGF_BODY_CL;
133 *body_len = cl;
134 *value = word;
Willy Tarreauba9afd22023-08-09 08:32:48 +0200135
136 /* Now either n==e and we're done, or n points to the comma,
137 * and we skip it and continue.
138 */
139 if (n++ == e)
140 break;
141
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100142 word.ptr = n;
143 }
144 /* here we've reached the end with a single value or a series of
145 * identical values, all matching previous series if any. The last
146 * parsed value was sent back into <value>. We just have to decide
147 * if this occurrence has to be indexed (it's the first one) or
148 * silently skipped (it's not the first one)
149 */
150 return !not_first;
151 fail:
152 return -1;
153}
154
Willy Tarreau6deb4122018-11-27 15:34:18 +0100155/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
156 * <fields> indicates what was found so far. This should be called once at the
157 * detection of the first general header field or at the end of the request if
158 * no general header field was found yet. Returns the created start line on
159 * success, or NULL on failure. Upon success, <msgf> is updated with a few
160 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200161 *
162 * The rules below deserve a bit of explanation. There tends to be some
163 * confusion regarding H2's authority vs the Host header. They are different
164 * though may sometimes be exchanged. In H2, the request line is broken into :
165 * - :method
166 * - :scheme
167 * - :authority
168 * - :path
169 *
170 * An equivalent HTTP/1.x absolute-form request would then look like :
171 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
172 *
173 * Except for CONNECT which doesn't have scheme nor path and looks like :
174 * <:method> <:authority> HTTP/x.y
175 *
176 * It's worth noting that H2 still supports an encoding to map H1 origin-form
177 * and asterisk-form requests. These ones do not specify the authority. However
178 * in H2 they must still specify the scheme, which is not present in H1. Also,
179 * when encoding an absolute-form H1 request without a path, the path
180 * automatically becomes "/" except for the OPTIONS method where it
181 * becomes "*".
182 *
183 * As such it is explicitly permitted for an H2 client to send a request
184 * featuring a Host header and no :authority, though it's not the recommended
185 * way to use H2 for a client. It is however the only permitted way to encode
186 * an origin-form H1 request over H2. Thus we need to respect such differences
187 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100188 */
189static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
190{
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100191 struct ist uri, meth_sl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100192 unsigned int flags = HTX_SL_F_NONE;
193 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100194 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100195
196 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100197 if (fields & H2_PHDR_FND_PROT) {
198 /* rfc 8441 Extended Connect Protocol
199 * #4 :scheme and :path must be present, as well as
200 * :authority like all h2 requests
201 */
202 if (!(fields & H2_PHDR_FND_SCHM)) {
203 /* missing scheme */
204 goto fail;
205 }
206 else if (!(fields & H2_PHDR_FND_PATH)) {
207 /* missing path */
208 goto fail;
209 }
210 else if (!(fields & H2_PHDR_FND_AUTH)) {
211 /* missing authority */
212 goto fail;
213 }
214
215 flags |= HTX_SL_F_HAS_SCHM;
216 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
217 flags |= HTX_SL_F_SCHM_HTTP;
218 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
219 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreau9e0c2b52021-08-10 15:37:34 +0200220 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
221 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100222
223 meth_sl = ist("GET");
224
225 *msgf |= H2_MSGF_EXT_CONNECT;
226 /* no ES on the HEADERS frame but no body either for
227 * Extended CONNECT */
228 *msgf &= ~H2_MSGF_BODY;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100229 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100230 else {
231 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
232 * MUST be omitted ; ":authority" contains the host and port
233 * to connect to.
234 */
235 if (fields & H2_PHDR_FND_SCHM) {
236 /* scheme not allowed */
237 goto fail;
238 }
239 else if (fields & H2_PHDR_FND_PATH) {
240 /* path not allowed */
241 goto fail;
242 }
243 else if (!(fields & H2_PHDR_FND_AUTH)) {
244 /* missing authority */
245 goto fail;
246 }
247
248 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau6deb4122018-11-27 15:34:18 +0100249 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100250
Willy Tarreau6deb4122018-11-27 15:34:18 +0100251 *msgf |= H2_MSGF_BODY_TUNNEL;
252 }
253 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
254 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
255 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
256 * valid value for the ":method", ":scheme" and ":path" phdr
257 * unless it is a CONNECT request.
258 */
259 if (!(fields & H2_PHDR_FND_METH)) {
260 /* missing method */
261 goto fail;
262 }
263 else if (!(fields & H2_PHDR_FND_SCHM)) {
264 /* missing scheme */
265 goto fail;
266 }
267 else {
268 /* missing path */
269 goto fail;
270 }
271 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200272 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200273 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
274 * classify the scheme as "present/http", "present/https",
275 * "present/other", "absent" so as to decide whether or not
276 * we're facing a normalized URI that will have to be encoded
277 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
278 * clients should use the absolute form, thus we cannot infer
279 * whether or not the client wanted to use a proxy here.
280 */
281 flags |= HTX_SL_F_HAS_SCHM;
282 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
283 flags |= HTX_SL_F_SCHM_HTTP;
284 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
285 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreau9e0c2b52021-08-10 15:37:34 +0200286 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
287 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100288
289 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau92919f72019-10-08 16:53:07 +0200290 }
291
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200292 if (fields & H2_PHDR_FND_PATH) {
293 /* 7540#8.1.2.3: :path must not be empty, and must be either
294 * '*' or an RFC3986 "path-absolute" starting with a "/" but
295 * not with "//".
Willy Tarreau512cee82021-08-19 23:06:58 +0200296 * However, this "path-absolute" was a mistake which was
297 * later fixed in http2bis as "absolute-path" to match
298 * HTTP/1, thus also allowing "//".
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200299 */
300 if (unlikely(!phdr[H2_PHDR_IDX_PATH].len))
301 goto fail;
302 else if (unlikely(phdr[H2_PHDR_IDX_PATH].ptr[0] != '/')) {
303 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
304 goto fail;
305 }
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200306 }
307
Willy Tarreau92919f72019-10-08 16:53:07 +0200308 if (!(flags & HTX_SL_F_HAS_SCHM)) {
309 /* no scheme, use authority only (CONNECT) */
310 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200311 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200312 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200313 else if (fields & H2_PHDR_FND_AUTH) {
314 /* authority is present, let's use the absolute form. We simply
315 * use the trash to concatenate them since all of them MUST fit
316 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200317 */
318 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
319 istcat(&uri, ist("://"), trash.size);
320 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
321 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
322 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200323 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200324
325 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
326 /* we don't know if it was originally an absolute or a
327 * relative request because newer versions of HTTP use
328 * the absolute URI format by default, which we call
329 * the normalized URI format internally. This is the
330 * strongly recommended way of sending a request for
331 * a regular client, so we cannot distinguish this
332 * from a request intended for a proxy. For other
333 * schemes however there is no doubt.
334 */
335 flags |= HTX_SL_F_NORMALIZED_URI;
336 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200337 }
338 else {
339 /* usual schemes with or without authority, use origin form */
340 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200341 if (fields & H2_PHDR_FND_AUTH)
342 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200343 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100344
Willy Tarreaue9f1f1e2021-08-11 11:12:46 +0200345 /* The method is a non-empty token (RFC7231#4.1) */
346 if (!meth_sl.len)
347 goto fail;
348 for (i = 0; i < meth_sl.len; i++) {
349 if (!HTTP_IS_TOKEN(meth_sl.ptr[i]))
350 htx->flags |= HTX_FL_PARSING_ERROR;
351 }
352
Willy Tarreau2be362c2019-10-08 11:59:37 +0200353 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
354 * that :path must not be empty.
355 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200356 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100357 goto fail;
358
Willy Tarreau2be362c2019-10-08 11:59:37 +0200359 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200360 for (i = 0; i < uri.len; i++) {
361 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100362 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
363 htx->flags |= HTX_FL_PARSING_ERROR;
364 }
365
Willy Tarreau6deb4122018-11-27 15:34:18 +0100366 /* Set HTX start-line flags */
367 flags |= HTX_SL_F_VER_11; // V2 in fact
368 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
369
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100370 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_sl, uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100371 if (!sl)
372 goto fail;
373
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100374 sl->info.req.meth = find_http_meth(meth_sl.ptr, meth_sl.len);
Christopher Faulet7d247f02020-12-02 14:26:36 +0100375 if (sl->info.req.meth == HTTP_METH_HEAD)
376 *msgf |= H2_MSGF_BODYLESS_RSP;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100377 return sl;
378 fail:
379 return NULL;
380}
381
382/* Takes an H2 request present in the headers list <list> terminated by a name
383 * being <NULL,0> and emits the equivalent HTX request according to the rules
384 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
385 * non-zero is returned if some bytes were emitted. In case of error, a
386 * negative error code is returned.
387 *
388 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
389 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
390 * if a body is detected (!ES).
391 *
392 * The headers list <list> must be composed of :
393 * - n.name != NULL, n.len > 0 : literal header name
394 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
395 * among H2_PHDR_IDX_*
396 * - n.name ignored, n.len == 0 : end of list
397 * - in all cases except the end of list, v.name and v.len must designate a
398 * valid value.
399 *
400 * The Cookie header will be reassembled at the end, and for this, the <list>
401 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf86e9942023-08-08 15:38:28 +0200402 *
403 * When <relaxed> is non-nul, some non-dangerous checks will be ignored. This
404 * is in order to satisfy "option accept-invalid-http-request" for
405 * interoperability purposes.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100406 */
Willy Tarreauf86e9942023-08-08 15:38:28 +0200407int 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 +0100408{
409 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
410 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
411 uint32_t idx;
412 int ck, lck; /* cookie index and last cookie index */
413 int phdr;
414 int ret;
415 int i;
416 struct htx_sl *sl = NULL;
417 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100418 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100419
420 lck = ck = -1; // no cookie for now
421 fields = 0;
422 for (idx = 0; list[idx].n.len != 0; idx++) {
423 if (!list[idx].n.ptr) {
424 /* this is an indexed pseudo-header */
425 phdr = list[idx].n.len;
426 }
427 else {
428 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100429 /* RFC7540#8.1.2: upper case not allowed in header field names.
430 * #10.3: header names must be valid (i.e. match a token).
431 * For pseudo-headers we check from 2nd char and for other ones
432 * from the first char, because HTTP_IS_TOKEN() also excludes
433 * the colon.
434 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100435 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100436
437 for (i = !!phdr; i < list[idx].n.len; i++)
438 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
439 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100440 }
441
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100442 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
Willy Tarreauaf232e42023-08-08 15:40:49 +0200443 * rejecting NUL, CR and LF characters. For :path we reject all CTL
444 * chars, spaces, and '#'.
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100445 */
Willy Tarreauaf232e42023-08-08 15:40:49 +0200446 if (phdr == H2_PHDR_IDX_PATH && !relaxed) {
447 ctl = ist_find_range(list[idx].v, 0, '#');
448 if (unlikely(ctl) && http_path_has_forbidden_char(list[idx].v, ctl))
449 goto fail;
450 } else {
451 ctl = ist_find_ctl(list[idx].v);
452 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
453 goto fail;
454 }
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100455
Willy Tarreau6deb4122018-11-27 15:34:18 +0100456 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
457 /* insert a pseudo header by its index (in phdr) and value (in value) */
458 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
459 if (fields & H2_PHDR_FND_NONE) {
460 /* pseudo header field after regular headers */
461 goto fail;
462 }
463 else {
464 /* repeated pseudo header field */
465 goto fail;
466 }
467 }
468 fields |= 1 << phdr;
469 phdr_val[phdr] = list[idx].v;
470 continue;
471 }
472 else if (phdr != 0) {
473 /* invalid pseudo header -- should never happen here */
474 goto fail;
475 }
476
477 /* regular header field in (name,value) */
478 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
479 /* no more pseudo-headers, time to build the request line */
480 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
481 if (!sl)
482 goto fail;
483 fields |= H2_PHDR_FND_NONE;
Willy Tarreaub4934f02021-08-11 15:39:13 +0200484
485 /* http2bis draft recommends to drop Host in favor of :authority when
486 * the latter is present. This is required to make sure there is no
487 * discrepancy between the authority and the host header, especially
488 * since routing rules usually involve Host. Here we already know if
489 * :authority was found so we can emit it right now and mark the host
490 * as filled so that it's skipped later.
491 */
492 if (fields & H2_PHDR_FND_AUTH) {
493 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
494 goto fail;
495 fields |= H2_PHDR_FND_HOST;
496 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100497 }
498
Willy Tarreaub4934f02021-08-11 15:39:13 +0200499 if (isteq(list[idx].n, ist("host"))) {
500 if (fields & H2_PHDR_FND_HOST)
501 continue;
502
Willy Tarreau6deb4122018-11-27 15:34:18 +0100503 fields |= H2_PHDR_FND_HOST;
Willy Tarreaub4934f02021-08-11 15:39:13 +0200504 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100505
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100506 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100507 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100508 if (ret < 0)
509 goto fail;
510
Willy Tarreau6deb4122018-11-27 15:34:18 +0100511 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100512 if (ret == 0)
513 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100514 }
515
516 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
517 if (isteq(list[idx].n, ist("connection")) ||
518 isteq(list[idx].n, ist("proxy-connection")) ||
519 isteq(list[idx].n, ist("keep-alive")) ||
520 isteq(list[idx].n, ist("upgrade")) ||
521 isteq(list[idx].n, ist("transfer-encoding")))
522 goto fail;
523
524 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
525 goto fail;
526
527 /* cookie requires special processing at the end */
528 if (isteq(list[idx].n, ist("cookie"))) {
529 list[idx].n.len = -1;
530
531 if (ck < 0)
532 ck = idx;
533 else
534 list[lck].n.len = idx;
535
536 lck = idx;
537 continue;
538 }
539
540 if (!htx_add_header(htx, list[idx].n, list[idx].v))
541 goto fail;
542 }
543
544 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
545 if (fields & H2_PHDR_FND_STAT)
546 goto fail;
547
548 /* Let's dump the request now if not yet emitted. */
549 if (!(fields & H2_PHDR_FND_NONE)) {
550 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
551 if (!sl)
552 goto fail;
553 }
554
Christopher Fauletd0db4232021-01-22 11:46:30 +0100555 if (*msgf & H2_MSGF_BODY_TUNNEL)
556 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
557
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100558 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
559 (*msgf & H2_MSGF_BODY_TUNNEL)) {
560 /* Request without body or tunnel requested */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100561 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100562 htx->flags |= HTX_FL_EOM;
563 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100564
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100565 if (*msgf & H2_MSGF_EXT_CONNECT) {
566 if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT]))
567 goto fail;
568 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
569 goto fail;
570 sl_flags |= HTX_SL_F_CONN_UPG;
571 }
572
Willy Tarreau6deb4122018-11-27 15:34:18 +0100573 /* update the start line with last detected header info */
574 sl->flags |= sl_flags;
575
Willy Tarreaub4934f02021-08-11 15:39:13 +0200576 /* complete with missing Host if needed (we may validate this test if
577 * no regular header was found).
578 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100579 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
580 /* missing Host field, use :authority instead */
581 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
582 goto fail;
583 }
584
585 /* now we may have to build a cookie list. We'll dump the values of all
586 * visited headers.
587 */
588 if (ck >= 0) {
589 uint32_t fs; // free space
590 uint32_t bs; // block size
591 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100592 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100593 struct htx_blk *blk;
594
595 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
596 if (!blk)
597 goto fail;
598
Willy Tarreau164e0612018-12-18 11:00:41 +0100599 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100600 fs = htx_free_data_space(htx);
601 bs = htx_get_blksz(blk);
602
603 /* for each extra cookie, we'll extend the cookie's value and
604 * insert "; " before the new value.
605 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100606 fs += tl; // first one is already counted
Tim Duesterhus15683552021-03-04 23:50:13 +0100607 while ((ck = list[ck].n.len) >= 0) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100608 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100609 tl += vl + 2;
610 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100611 goto fail;
612
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200613 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100614 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
615 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
616 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
617 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100618 }
619
620 }
621
622 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100623 if (!htx_add_endof(htx, HTX_BLK_EOH))
624 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100625
Amaury Denoyelle03534102021-07-07 10:49:28 +0200626 /* proceed to scheme-based normalization on target-URI */
627 if (fields & H2_PHDR_FND_SCHM)
628 http_scheme_based_normalize(htx);
629
Willy Tarreau6deb4122018-11-27 15:34:18 +0100630 ret = 1;
631 return ret;
632
633 fail:
634 return -1;
635}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200636
637/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
638 * <fields> indicates what was found so far. This should be called once at the
639 * detection of the first general header field or at the end of the message if
640 * no general header field was found yet. Returns the created start line on
641 * success, or NULL on failure. Upon success, <msgf> is updated with a few
642 * H2_MSGF_* flags indicating what was found while parsing.
643 */
644static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
645{
Christopher Faulet89899422020-12-07 18:24:43 +0100646 unsigned int status, flags = HTX_SL_F_NONE;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200647 struct htx_sl *sl;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100648 struct ist stat;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200649
650 /* only :status is allowed as a pseudo header */
651 if (!(fields & H2_PHDR_FND_STAT))
652 goto fail;
653
654 if (phdr[H2_PHDR_IDX_STAT].len != 3)
655 goto fail;
656
Amaury Denoyelle74162742020-12-11 17:53:05 +0100657 /* if Extended CONNECT is used, convert status code from 200 to htx 101
658 * following rfc 8441 */
659 if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) &&
660 isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) {
661 stat = ist("101");
662 status = 101;
663 }
664 else {
665 unsigned char h, t, u;
666
667 stat = phdr[H2_PHDR_IDX_STAT];
668
669 h = stat.ptr[0] - '0';
670 t = stat.ptr[1] - '0';
671 u = stat.ptr[2] - '0';
672 if (h > 9 || t > 9 || u > 9)
673 goto fail;
674 status = h * 100 + t * 10 + u;
675 }
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200676
Christopher Faulet89899422020-12-07 18:24:43 +0100677 /* 101 responses are not supported in H2, so return a error.
678 * On 1xx responses there is no ES on the HEADERS frame but there is no
679 * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to
680 * notify the decoder another HEADERS frame is expected.
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500681 * 204/304 response have no body by definition. So remove the flag
Christopher Faulet7d247f02020-12-02 14:26:36 +0100682 * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100683 *
684 * Note however that there is a special condition for Extended CONNECT.
685 * In this case, we explicitly convert it to HTX 101 to mimic
686 * Get+Upgrade HTTP/1.1 mechanism
Christopher Faulet0b465482019-02-19 15:14:23 +0100687 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100688 if (status == 101) {
689 if (!(*msgf & H2_MSGF_EXT_CONNECT))
690 goto fail;
691 }
Christopher Faulet89899422020-12-07 18:24:43 +0100692 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100693 *msgf |= H2_MSGF_RSP_1XX;
694 *msgf &= ~H2_MSGF_BODY;
695 }
Amaury Denoyelle74162742020-12-11 17:53:05 +0100696 else if (status == 204 || status == 304) {
Christopher Faulet7d247f02020-12-02 14:26:36 +0100697 *msgf &= ~H2_MSGF_BODY;
698 *msgf |= H2_MSGF_BODYLESS_RSP;
699 }
Christopher Faulet0b465482019-02-19 15:14:23 +0100700
Christopher Faulet89899422020-12-07 18:24:43 +0100701 /* Set HTX start-line flags */
702 flags |= HTX_SL_F_VER_11; // V2 in fact
703 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
704
Amaury Denoyelle74162742020-12-11 17:53:05 +0100705 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), stat, ist(""));
Christopher Faulet89899422020-12-07 18:24:43 +0100706 if (!sl)
707 goto fail;
708 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200709 return sl;
710 fail:
711 return NULL;
712}
713
714/* Takes an H2 response present in the headers list <list> terminated by a name
715 * being <NULL,0> and emits the equivalent HTX response according to the rules
716 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
717 * a positive value is returned if some bytes were emitted. In case of error, a
718 * negative error code is returned.
719 *
720 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
721 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
722 * if a body is detected (!ES).
723 *
724 * The headers list <list> must be composed of :
725 * - n.name != NULL, n.len > 0 : literal header name
726 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
727 * among H2_PHDR_IDX_*
728 * - n.name ignored, n.len == 0 : end of list
729 * - in all cases except the end of list, v.name and v.len must designate a
730 * valid value.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100731 *
732 * <upgrade_protocol> is only used if the htx status code is 101 indicating a
733 * response to an upgrade or h2-equivalent request.
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200734 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100735int 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 +0200736{
737 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
738 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
739 uint32_t idx;
740 int phdr;
741 int ret;
742 int i;
743 struct htx_sl *sl = NULL;
744 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100745 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200746
747 fields = 0;
748 for (idx = 0; list[idx].n.len != 0; idx++) {
749 if (!list[idx].n.ptr) {
750 /* this is an indexed pseudo-header */
751 phdr = list[idx].n.len;
752 }
753 else {
754 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100755 /* RFC7540#8.1.2: upper case not allowed in header field names.
756 * #10.3: header names must be valid (i.e. match a token).
757 * For pseudo-headers we check from 2nd char and for other ones
758 * from the first char, because HTTP_IS_TOKEN() also excludes
759 * the colon.
760 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200761 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100762
763 for (i = !!phdr; i < list[idx].n.len; i++)
764 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
765 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200766 }
767
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100768 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
769 * rejecting NUL, CR and LF characters.
770 */
771 ctl = ist_find_ctl(list[idx].v);
772 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
773 goto fail;
774
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200775 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
776 /* insert a pseudo header by its index (in phdr) and value (in value) */
777 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
778 if (fields & H2_PHDR_FND_NONE) {
779 /* pseudo header field after regular headers */
780 goto fail;
781 }
782 else {
783 /* repeated pseudo header field */
784 goto fail;
785 }
786 }
787 fields |= 1 << phdr;
788 phdr_val[phdr] = list[idx].v;
789 continue;
790 }
791 else if (phdr != 0) {
792 /* invalid pseudo header -- should never happen here */
793 goto fail;
794 }
795
796 /* regular header field in (name,value) */
797 if (!(fields & H2_PHDR_FND_NONE)) {
798 /* no more pseudo-headers, time to build the status line */
799 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
800 if (!sl)
801 goto fail;
802 fields |= H2_PHDR_FND_NONE;
803 }
804
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100805 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100806 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100807 if (ret < 0)
808 goto fail;
809
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200810 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100811 if (ret == 0)
812 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200813 }
814
815 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
816 if (isteq(list[idx].n, ist("connection")) ||
817 isteq(list[idx].n, ist("proxy-connection")) ||
818 isteq(list[idx].n, ist("keep-alive")) ||
819 isteq(list[idx].n, ist("upgrade")) ||
820 isteq(list[idx].n, ist("transfer-encoding")))
821 goto fail;
822
823 if (!htx_add_header(htx, list[idx].n, list[idx].v))
824 goto fail;
825 }
826
827 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
828 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
829 goto fail;
830
831 /* Let's dump the request now if not yet emitted. */
832 if (!(fields & H2_PHDR_FND_NONE)) {
833 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
834 if (!sl)
835 goto fail;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100836 }
837
838 if (sl->info.res.status == 101 && upgrade_protocol) {
839 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
840 goto fail;
841 if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol)))
842 goto fail;
843 sl_flags |= HTX_SL_F_CONN_UPG;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200844 }
845
Amaury Denoyelle74162742020-12-11 17:53:05 +0100846 if ((*msgf & H2_MSGF_BODY_TUNNEL) &&
847 ((sl->info.res.status >= 200 && sl->info.res.status < 300) || sl->info.res.status == 101))
Christopher Fauletd0db4232021-01-22 11:46:30 +0100848 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
849 else
850 *msgf &= ~H2_MSGF_BODY_TUNNEL;
851
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100852 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
853 (*msgf & H2_MSGF_BODY_TUNNEL)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500854 /* Response without body or tunnel successfully established */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100855 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100856 htx->flags |= HTX_FL_EOM;
857 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100858
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200859 /* update the start line with last detected header info */
860 sl->flags |= sl_flags;
861
862 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
863 /* FIXME: Do we need to signal anything when we have a body and
864 * no content-length, to have the equivalent of H1's chunked
865 * encoding?
866 */
867 }
868
869 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100870 if (!htx_add_endof(htx, HTX_BLK_EOH))
871 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200872
873 ret = 1;
874 return ret;
875
876 fail:
877 return -1;
878}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100879
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200880/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
881 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
882 * and a positive value is returned if some bytes were emitted. In case of
883 * error, a negative error code is returned. The caller must have verified that
884 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100885 *
886 * The headers list <list> must be composed of :
887 * - n.name != NULL, n.len > 0 : literal header name
888 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
889 * among H2_PHDR_IDX_* (illegal here)
890 * - n.name ignored, n.len == 0 : end of list
891 * - in all cases except the end of list, v.name and v.len must designate a
892 * valid value.
893 */
894int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
895{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100896 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100897 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100898 int i;
899
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100900 for (idx = 0; list[idx].n.len != 0; idx++) {
901 if (!list[idx].n.ptr) {
902 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
903 goto fail;
904 }
905
Willy Tarreau146f53a2019-11-24 10:34:39 +0100906 /* RFC7540#8.1.2: upper case not allowed in header field names.
907 * #10.3: header names must be valid (i.e. match a token). This
908 * also catches pseudo-headers which are forbidden in trailers.
909 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100910 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100911 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 +0100912 goto fail;
913
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100914 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
915 if (isteq(list[idx].n, ist("host")) ||
916 isteq(list[idx].n, ist("content-length")) ||
917 isteq(list[idx].n, ist("connection")) ||
918 isteq(list[idx].n, ist("proxy-connection")) ||
919 isteq(list[idx].n, ist("keep-alive")) ||
920 isteq(list[idx].n, ist("upgrade")) ||
921 isteq(list[idx].n, ist("te")) ||
922 isteq(list[idx].n, ist("transfer-encoding")))
923 goto fail;
924
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100925 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
926 * rejecting NUL, CR and LF characters.
927 */
928 ctl = ist_find_ctl(list[idx].v);
929 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
930 goto fail;
931
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200932 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
933 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100934 }
935
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200936 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100937 goto fail;
938
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100939 return 1;
940
941 fail:
942 return -1;
943}