blob: a4279d32447464176d29b0d669e5c419589921b1 [file] [log] [blame]
Willy Tarreauf24ea8e2017-11-21 19:55:27 +01001/*
2 * HTTP/2 protocol processing
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 * Copyright (C) 2017 HAProxy Technologies
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010028#include <inttypes.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/global.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020031#include <haproxy/h2.h>
Willy Tarreau0017be02020-06-02 19:25:28 +020032#include <haproxy/http-hdr-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/http.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020034#include <haproxy/htx.h>
Willy Tarreaueb6f7012020-05-27 16:21:26 +020035#include <import/ist.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010037
Willy Tarreau9c84d822019-01-30 15:09:21 +010038struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
39 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
40 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
42 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
43 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
44 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
45 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
46 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
47 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
48 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
49};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010050
Willy Tarreau54f53ef2019-11-22 16:02:43 +010051/* Looks into <ist> for forbidden characters for header values (0x00, 0x0A,
52 * 0x0D), starting at pointer <start> which must be within <ist>. Returns
53 * non-zero if such a character is found, 0 otherwise. When run on unlikely
54 * header match, it's recommended to first check for the presence of control
55 * chars using ist_find_ctl().
56 */
57static int has_forbidden_char(const struct ist ist, const char *start)
58{
59 do {
60 if ((uint8_t)*start <= 0x0d &&
61 (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0)))
62 return 1;
63 start++;
64 } while (start < ist.ptr + ist.len);
65 return 0;
66}
67
Willy Tarreaubeefaee2018-12-19 13:08:08 +010068/* Parse the Content-Length header field of an HTTP/2 request. The function
69 * checks all possible occurrences of a comma-delimited value, and verifies
70 * if any of them doesn't match a previous value. It returns <0 if a value
71 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
72 * if the value can be indexed (first one). In the last case, the value might
73 * be adjusted and the caller must only add the updated value.
74 */
75int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
76{
77 char *e, *n;
78 unsigned long long cl;
79 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
80 struct ist word;
81
82 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
83 e = value->ptr + value->len;
84
85 while (++word.ptr < e) {
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +050086 /* skip leading delimiter and blanks */
Willy Tarreaubeefaee2018-12-19 13:08:08 +010087 if (unlikely(HTTP_IS_LWS(*word.ptr)))
88 continue;
89
90 /* digits only now */
91 for (cl = 0, n = word.ptr; n < e; n++) {
92 unsigned int c = *n - '0';
93 if (unlikely(c > 9)) {
94 /* non-digit */
95 if (unlikely(n == word.ptr)) // spaces only
96 goto fail;
97 break;
98 }
99 if (unlikely(cl > ULLONG_MAX / 10ULL))
100 goto fail; /* multiply overflow */
101 cl = cl * 10ULL;
102 if (unlikely(cl + c < cl))
103 goto fail; /* addition overflow */
104 cl = cl + c;
105 }
106
107 /* keep a copy of the exact cleaned value */
108 word.len = n - word.ptr;
109
110 /* skip trailing LWS till next comma or EOL */
111 for (; n < e; n++) {
112 if (!HTTP_IS_LWS(*n)) {
113 if (unlikely(*n != ','))
114 goto fail;
115 break;
116 }
117 }
118
119 /* if duplicate, must be equal */
120 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
121 goto fail;
122
123 /* OK, store this result as the one to be indexed */
124 *msgf |= H2_MSGF_BODY_CL;
125 *body_len = cl;
126 *value = word;
127 word.ptr = n;
128 }
129 /* here we've reached the end with a single value or a series of
130 * identical values, all matching previous series if any. The last
131 * parsed value was sent back into <value>. We just have to decide
132 * if this occurrence has to be indexed (it's the first one) or
133 * silently skipped (it's not the first one)
134 */
135 return !not_first;
136 fail:
137 return -1;
138}
139
Willy Tarreau6deb4122018-11-27 15:34:18 +0100140/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
141 * <fields> indicates what was found so far. This should be called once at the
142 * detection of the first general header field or at the end of the request if
143 * no general header field was found yet. Returns the created start line on
144 * success, or NULL on failure. Upon success, <msgf> is updated with a few
145 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200146 *
147 * The rules below deserve a bit of explanation. There tends to be some
148 * confusion regarding H2's authority vs the Host header. They are different
149 * though may sometimes be exchanged. In H2, the request line is broken into :
150 * - :method
151 * - :scheme
152 * - :authority
153 * - :path
154 *
155 * An equivalent HTTP/1.x absolute-form request would then look like :
156 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
157 *
158 * Except for CONNECT which doesn't have scheme nor path and looks like :
159 * <:method> <:authority> HTTP/x.y
160 *
161 * It's worth noting that H2 still supports an encoding to map H1 origin-form
162 * and asterisk-form requests. These ones do not specify the authority. However
163 * in H2 they must still specify the scheme, which is not present in H1. Also,
164 * when encoding an absolute-form H1 request without a path, the path
165 * automatically becomes "/" except for the OPTIONS method where it
166 * becomes "*".
167 *
168 * As such it is explicitly permitted for an H2 client to send a request
169 * featuring a Host header and no :authority, though it's not the recommended
170 * way to use H2 for a client. It is however the only permitted way to encode
171 * an origin-form H1 request over H2. Thus we need to respect such differences
172 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100173 */
174static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
175{
Willy Tarreau92919f72019-10-08 16:53:07 +0200176 struct ist uri;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100177 unsigned int flags = HTX_SL_F_NONE;
178 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100179 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100180
181 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
182 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
183 * MUST be omitted ; ":authority" contains the host and port
184 * to connect to.
185 */
186 if (fields & H2_PHDR_FND_SCHM) {
187 /* scheme not allowed */
188 goto fail;
189 }
190 else if (fields & H2_PHDR_FND_PATH) {
191 /* path not allowed */
192 goto fail;
193 }
194 else if (!(fields & H2_PHDR_FND_AUTH)) {
195 /* missing authority */
196 goto fail;
197 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100198 *msgf |= H2_MSGF_BODY_TUNNEL;
199 }
200 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
201 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
202 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
203 * valid value for the ":method", ":scheme" and ":path" phdr
204 * unless it is a CONNECT request.
205 */
206 if (!(fields & H2_PHDR_FND_METH)) {
207 /* missing method */
208 goto fail;
209 }
210 else if (!(fields & H2_PHDR_FND_SCHM)) {
211 /* missing scheme */
212 goto fail;
213 }
214 else {
215 /* missing path */
216 goto fail;
217 }
218 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200219 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200220 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
221 * classify the scheme as "present/http", "present/https",
222 * "present/other", "absent" so as to decide whether or not
223 * we're facing a normalized URI that will have to be encoded
224 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
225 * clients should use the absolute form, thus we cannot infer
226 * whether or not the client wanted to use a proxy here.
227 */
228 flags |= HTX_SL_F_HAS_SCHM;
229 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
230 flags |= HTX_SL_F_SCHM_HTTP;
231 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
232 flags |= HTX_SL_F_SCHM_HTTPS;
233 }
234
235 if (!(flags & HTX_SL_F_HAS_SCHM)) {
236 /* no scheme, use authority only (CONNECT) */
237 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200238 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200239 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200240 else if (fields & H2_PHDR_FND_AUTH) {
241 /* authority is present, let's use the absolute form. We simply
242 * use the trash to concatenate them since all of them MUST fit
243 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200244 */
Willy Tarreaufd2658c2020-02-26 13:51:38 +0100245 if (unlikely(!phdr[H2_PHDR_IDX_PATH].len))
246 goto fail; // 7540#8.1.2.3: :path must not be empty
247
Willy Tarreau92919f72019-10-08 16:53:07 +0200248 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
249 istcat(&uri, ist("://"), trash.size);
250 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
251 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
252 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200253 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200254
255 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
256 /* we don't know if it was originally an absolute or a
257 * relative request because newer versions of HTTP use
258 * the absolute URI format by default, which we call
259 * the normalized URI format internally. This is the
260 * strongly recommended way of sending a request for
261 * a regular client, so we cannot distinguish this
262 * from a request intended for a proxy. For other
263 * schemes however there is no doubt.
264 */
265 flags |= HTX_SL_F_NORMALIZED_URI;
266 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200267 }
268 else {
269 /* usual schemes with or without authority, use origin form */
270 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200271 if (fields & H2_PHDR_FND_AUTH)
272 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200273 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100274
Willy Tarreau2be362c2019-10-08 11:59:37 +0200275 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
276 * that :path must not be empty.
277 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200278 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100279 goto fail;
280
Willy Tarreau2be362c2019-10-08 11:59:37 +0200281 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200282 for (i = 0; i < uri.len; i++) {
283 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100284 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
285 htx->flags |= HTX_FL_PARSING_ERROR;
286 }
287
Willy Tarreau6deb4122018-11-27 15:34:18 +0100288 /* Set HTX start-line flags */
289 flags |= HTX_SL_F_VER_11; // V2 in fact
290 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
291
Willy Tarreau92919f72019-10-08 16:53:07 +0200292 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], uri, ist("HTTP/2.0"));
Willy Tarreau6deb4122018-11-27 15:34:18 +0100293 if (!sl)
294 goto fail;
295
296 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
Christopher Faulet7d247f02020-12-02 14:26:36 +0100297 if (sl->info.req.meth == HTTP_METH_HEAD)
298 *msgf |= H2_MSGF_BODYLESS_RSP;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100299 return sl;
300 fail:
301 return NULL;
302}
303
304/* Takes an H2 request present in the headers list <list> terminated by a name
305 * being <NULL,0> and emits the equivalent HTX request according to the rules
306 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
307 * non-zero is returned if some bytes were emitted. In case of error, a
308 * negative error code is returned.
309 *
310 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
311 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
312 * if a body is detected (!ES).
313 *
314 * The headers list <list> must be composed of :
315 * - n.name != NULL, n.len > 0 : literal header name
316 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
317 * among H2_PHDR_IDX_*
318 * - n.name ignored, n.len == 0 : end of list
319 * - in all cases except the end of list, v.name and v.len must designate a
320 * valid value.
321 *
322 * The Cookie header will be reassembled at the end, and for this, the <list>
323 * will be used to create a linked list, so its contents may be destroyed.
324 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100325int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100326{
327 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
328 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
329 uint32_t idx;
330 int ck, lck; /* cookie index and last cookie index */
331 int phdr;
332 int ret;
333 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200334 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100335 struct htx_sl *sl = NULL;
336 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100337 const char *ctl;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100338
339 lck = ck = -1; // no cookie for now
340 fields = 0;
341 for (idx = 0; list[idx].n.len != 0; idx++) {
342 if (!list[idx].n.ptr) {
343 /* this is an indexed pseudo-header */
344 phdr = list[idx].n.len;
345 }
346 else {
347 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100348 /* RFC7540#8.1.2: upper case not allowed in header field names.
349 * #10.3: header names must be valid (i.e. match a token).
350 * For pseudo-headers we check from 2nd char and for other ones
351 * from the first char, because HTTP_IS_TOKEN() also excludes
352 * the colon.
353 */
Willy Tarreau6deb4122018-11-27 15:34:18 +0100354 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100355
356 for (i = !!phdr; i < list[idx].n.len; i++)
357 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
358 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100359 }
360
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100361 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
362 * rejecting NUL, CR and LF characters.
363 */
364 ctl = ist_find_ctl(list[idx].v);
365 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
366 goto fail;
367
Willy Tarreau6deb4122018-11-27 15:34:18 +0100368 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
369 /* insert a pseudo header by its index (in phdr) and value (in value) */
370 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
371 if (fields & H2_PHDR_FND_NONE) {
372 /* pseudo header field after regular headers */
373 goto fail;
374 }
375 else {
376 /* repeated pseudo header field */
377 goto fail;
378 }
379 }
380 fields |= 1 << phdr;
381 phdr_val[phdr] = list[idx].v;
382 continue;
383 }
384 else if (phdr != 0) {
385 /* invalid pseudo header -- should never happen here */
386 goto fail;
387 }
388
389 /* regular header field in (name,value) */
390 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
391 /* no more pseudo-headers, time to build the request line */
392 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
393 if (!sl)
394 goto fail;
395 fields |= H2_PHDR_FND_NONE;
396 }
397
398 if (isteq(list[idx].n, ist("host")))
399 fields |= H2_PHDR_FND_HOST;
400
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100401 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100402 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100403 if (ret < 0)
404 goto fail;
405
Willy Tarreau6deb4122018-11-27 15:34:18 +0100406 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100407 if (ret == 0)
408 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100409 }
410
411 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
412 if (isteq(list[idx].n, ist("connection")) ||
413 isteq(list[idx].n, ist("proxy-connection")) ||
414 isteq(list[idx].n, ist("keep-alive")) ||
415 isteq(list[idx].n, ist("upgrade")) ||
416 isteq(list[idx].n, ist("transfer-encoding")))
417 goto fail;
418
419 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
420 goto fail;
421
422 /* cookie requires special processing at the end */
423 if (isteq(list[idx].n, ist("cookie"))) {
424 list[idx].n.len = -1;
425
426 if (ck < 0)
427 ck = idx;
428 else
429 list[lck].n.len = idx;
430
431 lck = idx;
432 continue;
433 }
434
435 if (!htx_add_header(htx, list[idx].n, list[idx].v))
436 goto fail;
437 }
438
439 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
440 if (fields & H2_PHDR_FND_STAT)
441 goto fail;
442
443 /* Let's dump the request now if not yet emitted. */
444 if (!(fields & H2_PHDR_FND_NONE)) {
445 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
446 if (!sl)
447 goto fail;
448 }
449
Christopher Fauletd0db4232021-01-22 11:46:30 +0100450 if (*msgf & H2_MSGF_BODY_TUNNEL)
451 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
452
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100453 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
454 (*msgf & H2_MSGF_BODY_TUNNEL)) {
455 /* Request without body or tunnel requested */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100456 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100457 htx->flags |= HTX_FL_EOM;
458 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100459
Willy Tarreau6deb4122018-11-27 15:34:18 +0100460 /* update the start line with last detected header info */
461 sl->flags |= sl_flags;
462
463 /* complete with missing Host if needed */
464 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
465 /* missing Host field, use :authority instead */
466 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
467 goto fail;
468 }
469
470 /* now we may have to build a cookie list. We'll dump the values of all
471 * visited headers.
472 */
473 if (ck >= 0) {
474 uint32_t fs; // free space
475 uint32_t bs; // block size
476 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100477 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100478 struct htx_blk *blk;
479
480 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
481 if (!blk)
482 goto fail;
483
Willy Tarreau164e0612018-12-18 11:00:41 +0100484 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100485 fs = htx_free_data_space(htx);
486 bs = htx_get_blksz(blk);
487
488 /* for each extra cookie, we'll extend the cookie's value and
489 * insert "; " before the new value.
490 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100491 fs += tl; // first one is already counted
492 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100493 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100494 tl += vl + 2;
495 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100496 goto fail;
497
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200498 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100499 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
500 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
501 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
502 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100503 }
504
505 }
506
507 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100508 if (!htx_add_endof(htx, HTX_BLK_EOH))
509 goto fail;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100510
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500511 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200512 sl->hdrs_bytes = htx_used_space(htx) - used;
513
Willy Tarreau6deb4122018-11-27 15:34:18 +0100514 ret = 1;
515 return ret;
516
517 fail:
518 return -1;
519}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200520
521/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
522 * <fields> indicates what was found so far. This should be called once at the
523 * detection of the first general header field or at the end of the message if
524 * no general header field was found yet. Returns the created start line on
525 * success, or NULL on failure. Upon success, <msgf> is updated with a few
526 * H2_MSGF_* flags indicating what was found while parsing.
527 */
528static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
529{
Christopher Faulet89899422020-12-07 18:24:43 +0100530 unsigned int status, flags = HTX_SL_F_NONE;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200531 struct htx_sl *sl;
532 unsigned char h, t, u;
533
534 /* only :status is allowed as a pseudo header */
535 if (!(fields & H2_PHDR_FND_STAT))
536 goto fail;
537
538 if (phdr[H2_PHDR_IDX_STAT].len != 3)
539 goto fail;
540
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200541 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
542 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
543 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
544 if (h > 9 || t > 9 || u > 9)
545 goto fail;
Christopher Faulet89899422020-12-07 18:24:43 +0100546 status = h * 100 + t * 10 + u;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200547
Christopher Faulet89899422020-12-07 18:24:43 +0100548 /* 101 responses are not supported in H2, so return a error.
549 * On 1xx responses there is no ES on the HEADERS frame but there is no
550 * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to
551 * notify the decoder another HEADERS frame is expected.
Christopher Faulet7d247f02020-12-02 14:26:36 +0100552 * 204/304 resposne have no body by definition. So remove the flag
553 * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP.
Christopher Faulet0b465482019-02-19 15:14:23 +0100554 */
Christopher Faulet89899422020-12-07 18:24:43 +0100555 if (status == 101)
556 goto fail;
557 else if (status < 200) {
Christopher Faulet0b465482019-02-19 15:14:23 +0100558 *msgf |= H2_MSGF_RSP_1XX;
559 *msgf &= ~H2_MSGF_BODY;
560 }
Christopher Faulet7d247f02020-12-02 14:26:36 +0100561 else if (sl->info.res.status == 204 || sl->info.res.status == 304) {
562 *msgf &= ~H2_MSGF_BODY;
563 *msgf |= H2_MSGF_BODYLESS_RSP;
564 }
Christopher Faulet0b465482019-02-19 15:14:23 +0100565
Christopher Faulet89899422020-12-07 18:24:43 +0100566 /* Set HTX start-line flags */
567 flags |= HTX_SL_F_VER_11; // V2 in fact
568 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
569
570 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
571 if (!sl)
572 goto fail;
573 sl->info.res.status = status;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200574 return sl;
575 fail:
576 return NULL;
577}
578
579/* Takes an H2 response present in the headers list <list> terminated by a name
580 * being <NULL,0> and emits the equivalent HTX response according to the rules
581 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
582 * a positive value is returned if some bytes were emitted. In case of error, a
583 * negative error code is returned.
584 *
585 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
586 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
587 * if a body is detected (!ES).
588 *
589 * The headers list <list> must be composed of :
590 * - n.name != NULL, n.len > 0 : literal header name
591 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
592 * among H2_PHDR_IDX_*
593 * - n.name ignored, n.len == 0 : end of list
594 * - in all cases except the end of list, v.name and v.len must designate a
595 * valid value.
596 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100597int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len)
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200598{
599 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
600 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
601 uint32_t idx;
602 int phdr;
603 int ret;
604 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200605 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200606 struct htx_sl *sl = NULL;
607 unsigned int sl_flags = 0;
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100608 const char *ctl;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200609
610 fields = 0;
611 for (idx = 0; list[idx].n.len != 0; idx++) {
612 if (!list[idx].n.ptr) {
613 /* this is an indexed pseudo-header */
614 phdr = list[idx].n.len;
615 }
616 else {
617 /* this can be any type of header */
Willy Tarreau146f53a2019-11-24 10:34:39 +0100618 /* RFC7540#8.1.2: upper case not allowed in header field names.
619 * #10.3: header names must be valid (i.e. match a token).
620 * For pseudo-headers we check from 2nd char and for other ones
621 * from the first char, because HTTP_IS_TOKEN() also excludes
622 * the colon.
623 */
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200624 phdr = h2_str_to_phdr(list[idx].n);
Willy Tarreau146f53a2019-11-24 10:34:39 +0100625
626 for (i = !!phdr; i < list[idx].n.len; i++)
627 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i]))
628 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200629 }
630
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100631 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
632 * rejecting NUL, CR and LF characters.
633 */
634 ctl = ist_find_ctl(list[idx].v);
635 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
636 goto fail;
637
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200638 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
639 /* insert a pseudo header by its index (in phdr) and value (in value) */
640 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
641 if (fields & H2_PHDR_FND_NONE) {
642 /* pseudo header field after regular headers */
643 goto fail;
644 }
645 else {
646 /* repeated pseudo header field */
647 goto fail;
648 }
649 }
650 fields |= 1 << phdr;
651 phdr_val[phdr] = list[idx].v;
652 continue;
653 }
654 else if (phdr != 0) {
655 /* invalid pseudo header -- should never happen here */
656 goto fail;
657 }
658
659 /* regular header field in (name,value) */
660 if (!(fields & H2_PHDR_FND_NONE)) {
661 /* no more pseudo-headers, time to build the status line */
662 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
663 if (!sl)
664 goto fail;
665 fields |= H2_PHDR_FND_NONE;
666 }
667
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100668 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100669 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100670 if (ret < 0)
671 goto fail;
672
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200673 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100674 if (ret == 0)
675 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200676 }
677
678 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
679 if (isteq(list[idx].n, ist("connection")) ||
680 isteq(list[idx].n, ist("proxy-connection")) ||
681 isteq(list[idx].n, ist("keep-alive")) ||
682 isteq(list[idx].n, ist("upgrade")) ||
683 isteq(list[idx].n, ist("transfer-encoding")))
684 goto fail;
685
686 if (!htx_add_header(htx, list[idx].n, list[idx].v))
687 goto fail;
688 }
689
690 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
691 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
692 goto fail;
693
694 /* Let's dump the request now if not yet emitted. */
695 if (!(fields & H2_PHDR_FND_NONE)) {
696 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
697 if (!sl)
698 goto fail;
699 }
700
Christopher Fauletd0db4232021-01-22 11:46:30 +0100701 if ((*msgf & H2_MSGF_BODY_TUNNEL) && sl->info.res.status >= 200 && sl->info.res.status < 300)
702 *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL);
703 else
704 *msgf &= ~H2_MSGF_BODY_TUNNEL;
705
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100706 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) ||
707 (*msgf & H2_MSGF_BODY_TUNNEL)) {
708 /* Response without body or tunnel sucessfully established */
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100709 sl_flags |= HTX_SL_F_BODYLESS;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100710 htx->flags |= HTX_FL_EOM;
711 }
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100712
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200713 /* update the start line with last detected header info */
714 sl->flags |= sl_flags;
715
716 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
717 /* FIXME: Do we need to signal anything when we have a body and
718 * no content-length, to have the equivalent of H1's chunked
719 * encoding?
720 */
721 }
722
723 /* now send the end of headers marker */
Christopher Faulet5be651d2021-01-22 15:28:03 +0100724 if (!htx_add_endof(htx, HTX_BLK_EOH))
725 goto fail;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200726
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500727 /* Set bytes used in the HTX message for the headers now */
Christopher Faulet33543e72019-05-15 15:53:20 +0200728 sl->hdrs_bytes = htx_used_space(htx) - used;
729
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200730 ret = 1;
731 return ret;
732
733 fail:
734 return -1;
735}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100736
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200737/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
738 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
739 * and a positive value is returned if some bytes were emitted. In case of
740 * error, a negative error code is returned. The caller must have verified that
741 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100742 *
743 * The headers list <list> must be composed of :
744 * - n.name != NULL, n.len > 0 : literal header name
745 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
746 * among H2_PHDR_IDX_* (illegal here)
747 * - n.name ignored, n.len == 0 : end of list
748 * - in all cases except the end of list, v.name and v.len must designate a
749 * valid value.
750 */
751int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
752{
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100753 const char *ctl;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100754 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100755 int i;
756
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100757 for (idx = 0; list[idx].n.len != 0; idx++) {
758 if (!list[idx].n.ptr) {
759 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
760 goto fail;
761 }
762
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). This
765 * also catches pseudo-headers which are forbidden in trailers.
766 */
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100767 for (i = 0; i < list[idx].n.len; i++)
Willy Tarreau146f53a2019-11-24 10:34:39 +0100768 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 +0100769 goto fail;
770
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100771 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
772 if (isteq(list[idx].n, ist("host")) ||
773 isteq(list[idx].n, ist("content-length")) ||
774 isteq(list[idx].n, ist("connection")) ||
775 isteq(list[idx].n, ist("proxy-connection")) ||
776 isteq(list[idx].n, ist("keep-alive")) ||
777 isteq(list[idx].n, ist("upgrade")) ||
778 isteq(list[idx].n, ist("te")) ||
779 isteq(list[idx].n, ist("transfer-encoding")))
780 goto fail;
781
Willy Tarreau54f53ef2019-11-22 16:02:43 +0100782 /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of
783 * rejecting NUL, CR and LF characters.
784 */
785 ctl = ist_find_ctl(list[idx].v);
786 if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl))
787 goto fail;
788
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200789 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
790 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100791 }
792
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200793 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100794 goto fail;
795
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100796 return 1;
797
798 fail:
799 return -1;
800}