blob: 4da78c86142c54327fd90d0d1d84495711ec4343 [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 }
Willy Tarreauc48acd12023-08-09 11:02:34 +0200107
108 if (unlikely(!cl && n > word.ptr)) {
109 /* There was a leading zero before this digit,
110 * let's trim it.
111 */
112 word.ptr = n;
113 }
114
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100115 if (unlikely(cl > ULLONG_MAX / 10ULL))
116 goto fail; /* multiply overflow */
117 cl = cl * 10ULL;
118 if (unlikely(cl + c < cl))
119 goto fail; /* addition overflow */
120 cl = cl + c;
121 }
122
123 /* keep a copy of the exact cleaned value */
124 word.len = n - word.ptr;
125
126 /* skip trailing LWS till next comma or EOL */
127 for (; n < e; n++) {
128 if (!HTTP_IS_LWS(*n)) {
129 if (unlikely(*n != ','))
130 goto fail;
131 break;
132 }
133 }
134
135 /* if duplicate, must be equal */
136 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
137 goto fail;
138
139 /* OK, store this result as the one to be indexed */
140 *msgf |= H2_MSGF_BODY_CL;
141 *body_len = cl;
142 *value = word;
Willy Tarreauba9afd22023-08-09 08:32:48 +0200143
144 /* Now either n==e and we're done, or n points to the comma,
145 * and we skip it and continue.
146 */
147 if (n++ == e)
148 break;
149
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100150 word.ptr = n;
151 }
152 /* here we've reached the end with a single value or a series of
153 * identical values, all matching previous series if any. The last
154 * parsed value was sent back into <value>. We just have to decide
155 * if this occurrence has to be indexed (it's the first one) or
156 * silently skipped (it's not the first one)
157 */
158 return !not_first;
159 fail:
160 return -1;
161}
162
Willy Tarreau6deb4122018-11-27 15:34:18 +0100163/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
164 * <fields> indicates what was found so far. This should be called once at the
165 * detection of the first general header field or at the end of the request if
166 * no general header field was found yet. Returns the created start line on
167 * success, or NULL on failure. Upon success, <msgf> is updated with a few
168 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200169 *
170 * The rules below deserve a bit of explanation. There tends to be some
171 * confusion regarding H2's authority vs the Host header. They are different
172 * though may sometimes be exchanged. In H2, the request line is broken into :
173 * - :method
174 * - :scheme
175 * - :authority
176 * - :path
177 *
178 * An equivalent HTTP/1.x absolute-form request would then look like :
179 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
180 *
181 * Except for CONNECT which doesn't have scheme nor path and looks like :
182 * <:method> <:authority> HTTP/x.y
183 *
184 * It's worth noting that H2 still supports an encoding to map H1 origin-form
185 * and asterisk-form requests. These ones do not specify the authority. However
186 * in H2 they must still specify the scheme, which is not present in H1. Also,
187 * when encoding an absolute-form H1 request without a path, the path
188 * automatically becomes "/" except for the OPTIONS method where it
189 * becomes "*".
190 *
191 * As such it is explicitly permitted for an H2 client to send a request
192 * featuring a Host header and no :authority, though it's not the recommended
193 * way to use H2 for a client. It is however the only permitted way to encode
194 * an origin-form H1 request over H2. Thus we need to respect such differences
195 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100196 */
197static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
198{
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100199 struct ist uri, meth_sl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100200 unsigned int flags = HTX_SL_F_NONE;
201 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100202 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100203
204 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100205 if (fields & H2_PHDR_FND_PROT) {
206 /* rfc 8441 Extended Connect Protocol
207 * #4 :scheme and :path must be present, as well as
208 * :authority like all h2 requests
209 */
210 if (!(fields & H2_PHDR_FND_SCHM)) {
211 /* missing scheme */
212 goto fail;
213 }
214 else if (!(fields & H2_PHDR_FND_PATH)) {
215 /* missing path */
216 goto fail;
217 }
218 else if (!(fields & H2_PHDR_FND_AUTH)) {
219 /* missing authority */
220 goto fail;
221 }
222
223 flags |= HTX_SL_F_HAS_SCHM;
224 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
225 flags |= HTX_SL_F_SCHM_HTTP;
226 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
227 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreau9e0c2b52021-08-10 15:37:34 +0200228 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
229 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100230
231 meth_sl = ist("GET");
232
233 *msgf |= H2_MSGF_EXT_CONNECT;
234 /* no ES on the HEADERS frame but no body either for
235 * Extended CONNECT */
236 *msgf &= ~H2_MSGF_BODY;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100237 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100238 else {
239 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
240 * MUST be omitted ; ":authority" contains the host and port
241 * to connect to.
242 */
243 if (fields & H2_PHDR_FND_SCHM) {
244 /* scheme not allowed */
245 goto fail;
246 }
247 else if (fields & H2_PHDR_FND_PATH) {
248 /* path not allowed */
249 goto fail;
250 }
251 else if (!(fields & H2_PHDR_FND_AUTH)) {
252 /* missing authority */
253 goto fail;
254 }
255
256 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau6deb4122018-11-27 15:34:18 +0100257 }
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100258
Willy Tarreau6deb4122018-11-27 15:34:18 +0100259 *msgf |= H2_MSGF_BODY_TUNNEL;
260 }
261 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
262 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
263 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
264 * valid value for the ":method", ":scheme" and ":path" phdr
265 * unless it is a CONNECT request.
266 */
267 if (!(fields & H2_PHDR_FND_METH)) {
268 /* missing method */
269 goto fail;
270 }
271 else if (!(fields & H2_PHDR_FND_SCHM)) {
272 /* missing scheme */
273 goto fail;
274 }
275 else {
276 /* missing path */
277 goto fail;
278 }
279 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200280 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200281 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
282 * classify the scheme as "present/http", "present/https",
283 * "present/other", "absent" so as to decide whether or not
284 * we're facing a normalized URI that will have to be encoded
285 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
286 * clients should use the absolute form, thus we cannot infer
287 * whether or not the client wanted to use a proxy here.
288 */
289 flags |= HTX_SL_F_HAS_SCHM;
290 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
291 flags |= HTX_SL_F_SCHM_HTTP;
292 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
293 flags |= HTX_SL_F_SCHM_HTTPS;
Willy Tarreau9e0c2b52021-08-10 15:37:34 +0200294 else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM]))
295 htx->flags |= HTX_FL_PARSING_ERROR;
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100296
297 meth_sl = phdr[H2_PHDR_IDX_METH];
Willy Tarreau92919f72019-10-08 16:53:07 +0200298 }
299
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200300 if (fields & H2_PHDR_FND_PATH) {
301 /* 7540#8.1.2.3: :path must not be empty, and must be either
302 * '*' or an RFC3986 "path-absolute" starting with a "/" but
303 * not with "//".
Willy Tarreau512cee82021-08-19 23:06:58 +0200304 * However, this "path-absolute" was a mistake which was
305 * later fixed in http2bis as "absolute-path" to match
306 * HTTP/1, thus also allowing "//".
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200307 */
308 if (unlikely(!phdr[H2_PHDR_IDX_PATH].len))
309 goto fail;
310 else if (unlikely(phdr[H2_PHDR_IDX_PATH].ptr[0] != '/')) {
311 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
312 goto fail;
313 }
Willy Tarreau6c6b9a52021-08-10 16:30:55 +0200314 }
315
Willy Tarreau92919f72019-10-08 16:53:07 +0200316 if (!(flags & HTX_SL_F_HAS_SCHM)) {
317 /* no scheme, use authority only (CONNECT) */
318 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200319 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200320 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200321 else if (fields & H2_PHDR_FND_AUTH) {
322 /* authority is present, let's use the absolute form. We simply
323 * use the trash to concatenate them since all of them MUST fit
324 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200325 */
326 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
327 istcat(&uri, ist("://"), trash.size);
328 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
329 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
330 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200331 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200332
333 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
334 /* we don't know if it was originally an absolute or a
335 * relative request because newer versions of HTTP use
336 * the absolute URI format by default, which we call
337 * the normalized URI format internally. This is the
338 * strongly recommended way of sending a request for
339 * a regular client, so we cannot distinguish this
340 * from a request intended for a proxy. For other
341 * schemes however there is no doubt.
342 */
343 flags |= HTX_SL_F_NORMALIZED_URI;
344 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200345 }
346 else {
347 /* usual schemes with or without authority, use origin form */
348 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200349 if (fields & H2_PHDR_FND_AUTH)
350 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200351 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100352
Willy Tarreaue9f1f1e2021-08-11 11:12:46 +0200353 /* The method is a non-empty token (RFC7231#4.1) */
354 if (!meth_sl.len)
355 goto fail;
356 for (i = 0; i < meth_sl.len; i++) {
357 if (!HTTP_IS_TOKEN(meth_sl.ptr[i]))
358 htx->flags |= HTX_FL_PARSING_ERROR;
359 }
360
Willy Tarreau2be362c2019-10-08 11:59:37 +0200361 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
362 * that :path must not be empty.
363 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200364 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100365 goto fail;
366
Willy Tarreau2be362c2019-10-08 11:59:37 +0200367 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200368 for (i = 0; i < uri.len; i++) {
369 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100370 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
371 htx->flags |= HTX_FL_PARSING_ERROR;
372 }
373
Willy Tarreau6deb4122018-11-27 15:34:18 +0100374 /* Set HTX start-line flags */
375 flags |= HTX_SL_F_VER_11; // V2 in fact
376 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
377
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100378 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_sl, uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100379 if (!sl)
380 goto fail;
381
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100382 sl->info.req.meth = find_http_meth(meth_sl.ptr, meth_sl.len);
Christopher Faulet7d247f02020-12-02 14:26:36 +0100383 if (sl->info.req.meth == HTTP_METH_HEAD)
384 *msgf |= H2_MSGF_BODYLESS_RSP;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100385 return sl;
386 fail:
387 return NULL;
388}
389
390/* Takes an H2 request present in the headers list <list> terminated by a name
391 * being <NULL,0> and emits the equivalent HTX request according to the rules
392 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
393 * non-zero is returned if some bytes were emitted. In case of error, a
394 * negative error code is returned.
395 *
396 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
397 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
398 * if a body is detected (!ES).
399 *
400 * The headers list <list> must be composed of :
401 * - n.name != NULL, n.len > 0 : literal header name
402 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
403 * among H2_PHDR_IDX_*
404 * - n.name ignored, n.len == 0 : end of list
405 * - in all cases except the end of list, v.name and v.len must designate a
406 * valid value.
407 *
408 * The Cookie header will be reassembled at the end, and for this, the <list>
409 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf86e9942023-08-08 15:38:28 +0200410 *
411 * When <relaxed> is non-nul, some non-dangerous checks will be ignored. This
412 * is in order to satisfy "option accept-invalid-http-request" for
413 * interoperability purposes.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100414 */
Willy Tarreauf86e9942023-08-08 15:38:28 +0200415int 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 +0100416{
417 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
418 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
419 uint32_t idx;
420 int ck, lck; /* cookie index and last cookie index */
421 int phdr;
422 int ret;
423 int i;
424 struct htx_sl *sl = NULL;
425 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100426 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100427
428 lck = ck = -1; // no cookie for now
429 fields = 0;
430 for (idx = 0; list[idx].n.len != 0; idx++) {
431 if (!list[idx].n.ptr) {
432 /* this is an indexed pseudo-header */
433 phdr = list[idx].n.len;
434 }
435 else {
436 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100437 /* RFC7540#8.1.2: upper case not allowed in header field names.
438 * #10.3: header names must be valid (i.e. match a token).
439 * For pseudo-headers we check from 2nd char and for other ones
440 * from the first char, because HTTP_IS_TOKEN() also excludes
441 * the colon.
442 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100443 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100444
445 for (i = !!phdr; i < list[idx].n.len; i++)
446 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
447 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100448 }
449
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100450 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
Willy Tarreauaf232e42023-08-08 15:40:49 +0200451 * rejecting NUL, CR and LF characters. For :path we reject all CTL
452 * chars, spaces, and '#'.
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100453 */
Willy Tarreauaf232e42023-08-08 15:40:49 +0200454 if (phdr == H2_PHDR_IDX_PATH && !relaxed) {
455 ctl = ist_find_range(list[idx].v, 0, '#');
456 if (unlikely(ctl) && http_path_has_forbidden_char(list[idx].v, ctl))
457 goto fail;
458 } else {
459 ctl = ist_find_ctl(list[idx].v);
460 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
461 goto fail;
462 }
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100463
Willy Tarreau6deb4122018-11-27 15:34:18 +0100464 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
465 /* insert a pseudo header by its index (in phdr) and value (in value) */
466 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
467 if (fields & H2_PHDR_FND_NONE) {
468 /* pseudo header field after regular headers */
469 goto fail;
470 }
471 else {
472 /* repeated pseudo header field */
473 goto fail;
474 }
475 }
476 fields |= 1 << phdr;
477 phdr_val[phdr] = list[idx].v;
478 continue;
479 }
480 else if (phdr != 0) {
481 /* invalid pseudo header -- should never happen here */
482 goto fail;
483 }
484
485 /* regular header field in (name,value) */
486 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
487 /* no more pseudo-headers, time to build the request line */
488 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
489 if (!sl)
490 goto fail;
491 fields |= H2_PHDR_FND_NONE;
Willy Tarreaub4934f02021-08-11 15:39:13 +0200492
493 /* http2bis draft recommends to drop Host in favor of :authority when
494 * the latter is present. This is required to make sure there is no
495 * discrepancy between the authority and the host header, especially
496 * since routing rules usually involve Host. Here we already know if
497 * :authority was found so we can emit it right now and mark the host
498 * as filled so that it's skipped later.
499 */
500 if (fields & H2_PHDR_FND_AUTH) {
501 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
502 goto fail;
503 fields |= H2_PHDR_FND_HOST;
504 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100505 }
506
Willy Tarreaub4934f02021-08-11 15:39:13 +0200507 if (isteq(list[idx].n, ist("host"))) {
508 if (fields & H2_PHDR_FND_HOST)
509 continue;
510
Willy Tarreau6deb4122018-11-27 15:34:18 +0100511 fields |= H2_PHDR_FND_HOST;
Willy Tarreaub4934f02021-08-11 15:39:13 +0200512 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100513
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100514 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100515 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100516 if (ret < 0)
517 goto fail;
518
Willy Tarreau6deb4122018-11-27 15:34:18 +0100519 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100520 if (ret == 0)
521 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100522 }
523
524 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
525 if (isteq(list[idx].n, ist("connection")) ||
526 isteq(list[idx].n, ist("proxy-connection")) ||
527 isteq(list[idx].n, ist("keep-alive")) ||
528 isteq(list[idx].n, ist("upgrade")) ||
529 isteq(list[idx].n, ist("transfer-encoding")))
530 goto fail;
531
532 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
533 goto fail;
534
535 /* cookie requires special processing at the end */
536 if (isteq(list[idx].n, ist("cookie"))) {
537 list[idx].n.len = -1;
538
539 if (ck < 0)
540 ck = idx;
541 else
542 list[lck].n.len = idx;
543
544 lck = idx;
545 continue;
546 }
547
548 if (!htx_add_header(htx, list[idx].n, list[idx].v))
549 goto fail;
550 }
551
552 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
553 if (fields & H2_PHDR_FND_STAT)
554 goto fail;
555
556 /* Let's dump the request now if not yet emitted. */
557 if (!(fields & H2_PHDR_FND_NONE)) {
558 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
559 if (!sl)
560 goto fail;
561 }
562
Christopher Fauletd0db4232021-01-22 11:46:30 +0100563 if (*msgf & H2_MSGF_BODY_TUNNEL)
564 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
565
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100566 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
567 (*msgf & H2_MSGF_BODY_TUNNEL)) {
568 /* Request without body or tunnel requested */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100569 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100570 htx->flags |= HTX_FL_EOM;
571 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100572
Amaury Denoyellec9a0afc2020-12-11 17:53:09 +0100573 if (*msgf & H2_MSGF_EXT_CONNECT) {
574 if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT]))
575 goto fail;
576 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
577 goto fail;
578 sl_flags |= HTX_SL_F_CONN_UPG;
579 }
580
Willy Tarreau6deb4122018-11-27 15:34:18 +0100581 /* update the start line with last detected header info */
582 sl->flags |= sl_flags;
583
Willy Tarreaub4934f02021-08-11 15:39:13 +0200584 /* complete with missing Host if needed (we may validate this test if
585 * no regular header was found).
586 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100587 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
588 /* missing Host field, use :authority instead */
589 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
590 goto fail;
591 }
592
593 /* now we may have to build a cookie list. We'll dump the values of all
594 * visited headers.
595 */
596 if (ck >= 0) {
597 uint32_t fs; // free space
598 uint32_t bs; // block size
599 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100600 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100601 struct htx_blk *blk;
602
603 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
604 if (!blk)
605 goto fail;
606
Willy Tarreau164e0612018-12-18 11:00:41 +0100607 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100608 fs = htx_free_data_space(htx);
609 bs = htx_get_blksz(blk);
610
611 /* for each extra cookie, we'll extend the cookie's value and
612 * insert "; " before the new value.
613 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100614 fs += tl; // first one is already counted
Tim Duesterhus15683552021-03-04 23:50:13 +0100615 while ((ck = list[ck].n.len) >= 0) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100616 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100617 tl += vl + 2;
618 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100619 goto fail;
620
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200621 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100622 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
623 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
624 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
625 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100626 }
627
628 }
629
630 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100631 if (!htx_add_endof(htx, HTX_BLK_EOH))
632 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100633
Amaury Denoyelle03534102021-07-07 10:49:28 +0200634 /* proceed to scheme-based normalization on target-URI */
635 if (fields & H2_PHDR_FND_SCHM)
636 http_scheme_based_normalize(htx);
637
Willy Tarreau6deb4122018-11-27 15:34:18 +0100638 ret = 1;
639 return ret;
640
641 fail:
642 return -1;
643}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200644
645/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
646 * <fields> indicates what was found so far. This should be called once at the
647 * detection of the first general header field or at the end of the message if
648 * no general header field was found yet. Returns the created start line on
649 * success, or NULL on failure. Upon success, <msgf> is updated with a few
650 * H2_MSGF_* flags indicating what was found while parsing.
651 */
652static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
653{
Christopher Faulet89899422020-12-07 18:24:43 +0100654 unsigned int status, flags = HTX_SL_F_NONE;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200655 struct htx_sl *sl;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100656 struct ist stat;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200657
658 /* only :status is allowed as a pseudo header */
659 if (!(fields & H2_PHDR_FND_STAT))
660 goto fail;
661
662 if (phdr[H2_PHDR_IDX_STAT].len != 3)
663 goto fail;
664
Amaury Denoyelle74162742020-12-11 17:53:05 +0100665 /* if Extended CONNECT is used, convert status code from 200 to htx 101
666 * following rfc 8441 */
667 if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) &&
668 isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) {
669 stat = ist("101");
670 status = 101;
671 }
672 else {
673 unsigned char h, t, u;
674
675 stat = phdr[H2_PHDR_IDX_STAT];
676
677 h = stat.ptr[0] - '0';
678 t = stat.ptr[1] - '0';
679 u = stat.ptr[2] - '0';
680 if (h > 9 || t > 9 || u > 9)
681 goto fail;
682 status = h * 100 + t * 10 + u;
683 }
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200684
Christopher Faulet89899422020-12-07 18:24:43 +0100685 /* 101 responses are not supported in H2, so return a error.
686 * On 1xx responses there is no ES on the HEADERS frame but there is no
687 * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to
688 * notify the decoder another HEADERS frame is expected.
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500689 * 204/304 response have no body by definition. So remove the flag
Christopher Faulet7d247f02020-12-02 14:26:36 +0100690 * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100691 *
692 * Note however that there is a special condition for Extended CONNECT.
693 * In this case, we explicitly convert it to HTX 101 to mimic
694 * Get+Upgrade HTTP/1.1 mechanism
Christopher Faulet0b465482019-02-19 15:14:23 +0100695 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100696 if (status == 101) {
697 if (!(*msgf & H2_MSGF_EXT_CONNECT))
698 goto fail;
699 }
Christopher Faulet89899422020-12-07 18:24:43 +0100700 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100701 *msgf |= H2_MSGF_RSP_1XX;
702 *msgf &= ~H2_MSGF_BODY;
703 }
Amaury Denoyelle74162742020-12-11 17:53:05 +0100704 else if (status == 204 || status == 304) {
Christopher Faulet7d247f02020-12-02 14:26:36 +0100705 *msgf &= ~H2_MSGF_BODY;
706 *msgf |= H2_MSGF_BODYLESS_RSP;
707 }
Christopher Faulet0b465482019-02-19 15:14:23 +0100708
Christopher Faulet89899422020-12-07 18:24:43 +0100709 /* Set HTX start-line flags */
710 flags |= HTX_SL_F_VER_11; // V2 in fact
711 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
712
Amaury Denoyelle74162742020-12-11 17:53:05 +0100713 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), stat, ist(""));
Christopher Faulet89899422020-12-07 18:24:43 +0100714 if (!sl)
715 goto fail;
716 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200717 return sl;
718 fail:
719 return NULL;
720}
721
722/* Takes an H2 response present in the headers list <list> terminated by a name
723 * being <NULL,0> and emits the equivalent HTX response according to the rules
724 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
725 * a positive value is returned if some bytes were emitted. In case of error, a
726 * negative error code is returned.
727 *
728 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
729 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
730 * if a body is detected (!ES).
731 *
732 * The headers list <list> must be composed of :
733 * - n.name != NULL, n.len > 0 : literal header name
734 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
735 * among H2_PHDR_IDX_*
736 * - n.name ignored, n.len == 0 : end of list
737 * - in all cases except the end of list, v.name and v.len must designate a
738 * valid value.
Amaury Denoyelle74162742020-12-11 17:53:05 +0100739 *
740 * <upgrade_protocol> is only used if the htx status code is 101 indicating a
741 * response to an upgrade or h2-equivalent request.
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200742 */
Amaury Denoyelle74162742020-12-11 17:53:05 +0100743int 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 +0200744{
745 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
746 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
747 uint32_t idx;
748 int phdr;
749 int ret;
750 int i;
751 struct htx_sl *sl = NULL;
752 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100753 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200754
755 fields = 0;
756 for (idx = 0; list[idx].n.len != 0; idx++) {
757 if (!list[idx].n.ptr) {
758 /* this is an indexed pseudo-header */
759 phdr = list[idx].n.len;
760 }
761 else {
762 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100763 /* RFC7540#8.1.2: upper case not allowed in header field names.
764 * #10.3: header names must be valid (i.e. match a token).
765 * For pseudo-headers we check from 2nd char and for other ones
766 * from the first char, because HTTP_IS_TOKEN() also excludes
767 * the colon.
768 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200769 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100770
771 for (i = !!phdr; i < list[idx].n.len; i++)
772 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
773 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200774 }
775
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100776 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
777 * rejecting NUL, CR and LF characters.
778 */
779 ctl = ist_find_ctl(list[idx].v);
780 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
781 goto fail;
782
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200783 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
784 /* insert a pseudo header by its index (in phdr) and value (in value) */
785 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
786 if (fields & H2_PHDR_FND_NONE) {
787 /* pseudo header field after regular headers */
788 goto fail;
789 }
790 else {
791 /* repeated pseudo header field */
792 goto fail;
793 }
794 }
795 fields |= 1 << phdr;
796 phdr_val[phdr] = list[idx].v;
797 continue;
798 }
799 else if (phdr != 0) {
800 /* invalid pseudo header -- should never happen here */
801 goto fail;
802 }
803
804 /* regular header field in (name,value) */
805 if (!(fields & H2_PHDR_FND_NONE)) {
806 /* no more pseudo-headers, time to build the status line */
807 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
808 if (!sl)
809 goto fail;
810 fields |= H2_PHDR_FND_NONE;
811 }
812
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100813 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100814 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100815 if (ret < 0)
816 goto fail;
817
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200818 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100819 if (ret == 0)
820 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200821 }
822
823 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
824 if (isteq(list[idx].n, ist("connection")) ||
825 isteq(list[idx].n, ist("proxy-connection")) ||
826 isteq(list[idx].n, ist("keep-alive")) ||
827 isteq(list[idx].n, ist("upgrade")) ||
828 isteq(list[idx].n, ist("transfer-encoding")))
829 goto fail;
830
831 if (!htx_add_header(htx, list[idx].n, list[idx].v))
832 goto fail;
833 }
834
835 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
836 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
837 goto fail;
838
839 /* Let's dump the request now if not yet emitted. */
840 if (!(fields & H2_PHDR_FND_NONE)) {
841 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
842 if (!sl)
843 goto fail;
Amaury Denoyelle74162742020-12-11 17:53:05 +0100844 }
845
846 if (sl->info.res.status == 101 && upgrade_protocol) {
847 if (!htx_add_header(htx, ist("connection"), ist("upgrade")))
848 goto fail;
849 if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol)))
850 goto fail;
851 sl_flags |= HTX_SL_F_CONN_UPG;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200852 }
853
Amaury Denoyelle74162742020-12-11 17:53:05 +0100854 if ((*msgf & H2_MSGF_BODY_TUNNEL) &&
855 ((sl->info.res.status >= 200 && sl->info.res.status < 300) || sl->info.res.status == 101))
Christopher Fauletd0db4232021-01-22 11:46:30 +0100856 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
857 else
858 *msgf &= ~H2_MSGF_BODY_TUNNEL;
859
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100860 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
861 (*msgf & H2_MSGF_BODY_TUNNEL)) {
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500862 /* Response without body or tunnel successfully established */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100863 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100864 htx->flags |= HTX_FL_EOM;
865 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100866
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200867 /* update the start line with last detected header info */
868 sl->flags |= sl_flags;
869
870 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
871 /* FIXME: Do we need to signal anything when we have a body and
872 * no content-length, to have the equivalent of H1's chunked
873 * encoding?
874 */
875 }
876
877 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100878 if (!htx_add_endof(htx, HTX_BLK_EOH))
879 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200880
881 ret = 1;
882 return ret;
883
884 fail:
885 return -1;
886}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100887
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200888/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
889 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
890 * and a positive value is returned if some bytes were emitted. In case of
891 * error, a negative error code is returned. The caller must have verified that
892 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100893 *
894 * The headers list <list> must be composed of :
895 * - n.name != NULL, n.len > 0 : literal header name
896 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
897 * among H2_PHDR_IDX_* (illegal here)
898 * - n.name ignored, n.len == 0 : end of list
899 * - in all cases except the end of list, v.name and v.len must designate a
900 * valid value.
901 */
902int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
903{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100904 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100905 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100906 int i;
907
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100908 for (idx = 0; list[idx].n.len != 0; idx++) {
909 if (!list[idx].n.ptr) {
910 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
911 goto fail;
912 }
913
Willy Tarreau146f53a2019-11-24 10:34:39 +0100914 /* RFC7540#8.1.2: upper case not allowed in header field names.
915 * #10.3: header names must be valid (i.e. match a token). This
916 * also catches pseudo-headers which are forbidden in trailers.
917 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100918 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100919 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 +0100920 goto fail;
921
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100922 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
923 if (isteq(list[idx].n, ist("host")) ||
924 isteq(list[idx].n, ist("content-length")) ||
925 isteq(list[idx].n, ist("connection")) ||
926 isteq(list[idx].n, ist("proxy-connection")) ||
927 isteq(list[idx].n, ist("keep-alive")) ||
928 isteq(list[idx].n, ist("upgrade")) ||
929 isteq(list[idx].n, ist("te")) ||
930 isteq(list[idx].n, ist("transfer-encoding")))
931 goto fail;
932
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100933 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
934 * rejecting NUL, CR and LF characters.
935 */
936 ctl = ist_find_ctl(list[idx].v);
937 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
938 goto fail;
939
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200940 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
941 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100942 }
943
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200944 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100945 goto fail;
946
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100947 return 1;
948
949 fail:
950 return -1;
951}