blob: 254d977192117ef6ddf78ce6762cbb96aa16e671 [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 Tarreauf24ea8e2017-11-21 19:55:27 +010029#include <common/config.h>
30#include <common/h2.h>
31#include <common/http-hdr.h>
32#include <common/ist.h>
Willy Tarreau92919f72019-10-08 16:53:07 +020033#include <types/global.h>
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010034
Willy Tarreau9c84d822019-01-30 15:09:21 +010035struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
36 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
37 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
38 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
39 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
40 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
42 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
43 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
44 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
45 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
46};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010047
Willy Tarreaubeefaee2018-12-19 13:08:08 +010048/* Parse the Content-Length header field of an HTTP/2 request. The function
49 * checks all possible occurrences of a comma-delimited value, and verifies
50 * if any of them doesn't match a previous value. It returns <0 if a value
51 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
52 * if the value can be indexed (first one). In the last case, the value might
53 * be adjusted and the caller must only add the updated value.
54 */
55int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
56{
57 char *e, *n;
58 unsigned long long cl;
59 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
60 struct ist word;
61
62 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
63 e = value->ptr + value->len;
64
65 while (++word.ptr < e) {
66 /* skip leading delimitor and blanks */
67 if (unlikely(HTTP_IS_LWS(*word.ptr)))
68 continue;
69
70 /* digits only now */
71 for (cl = 0, n = word.ptr; n < e; n++) {
72 unsigned int c = *n - '0';
73 if (unlikely(c > 9)) {
74 /* non-digit */
75 if (unlikely(n == word.ptr)) // spaces only
76 goto fail;
77 break;
78 }
79 if (unlikely(cl > ULLONG_MAX / 10ULL))
80 goto fail; /* multiply overflow */
81 cl = cl * 10ULL;
82 if (unlikely(cl + c < cl))
83 goto fail; /* addition overflow */
84 cl = cl + c;
85 }
86
87 /* keep a copy of the exact cleaned value */
88 word.len = n - word.ptr;
89
90 /* skip trailing LWS till next comma or EOL */
91 for (; n < e; n++) {
92 if (!HTTP_IS_LWS(*n)) {
93 if (unlikely(*n != ','))
94 goto fail;
95 break;
96 }
97 }
98
99 /* if duplicate, must be equal */
100 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
101 goto fail;
102
103 /* OK, store this result as the one to be indexed */
104 *msgf |= H2_MSGF_BODY_CL;
105 *body_len = cl;
106 *value = word;
107 word.ptr = n;
108 }
109 /* here we've reached the end with a single value or a series of
110 * identical values, all matching previous series if any. The last
111 * parsed value was sent back into <value>. We just have to decide
112 * if this occurrence has to be indexed (it's the first one) or
113 * silently skipped (it's not the first one)
114 */
115 return !not_first;
116 fail:
117 return -1;
118}
119
Willy Tarreau6deb4122018-11-27 15:34:18 +0100120/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
121 * <fields> indicates what was found so far. This should be called once at the
122 * detection of the first general header field or at the end of the request if
123 * no general header field was found yet. Returns the created start line on
124 * success, or NULL on failure. Upon success, <msgf> is updated with a few
125 * H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreau2be362c2019-10-08 11:59:37 +0200126 *
127 * The rules below deserve a bit of explanation. There tends to be some
128 * confusion regarding H2's authority vs the Host header. They are different
129 * though may sometimes be exchanged. In H2, the request line is broken into :
130 * - :method
131 * - :scheme
132 * - :authority
133 * - :path
134 *
135 * An equivalent HTTP/1.x absolute-form request would then look like :
136 * <:method> <:scheme>://<:authority><:path> HTTP/x.y
137 *
138 * Except for CONNECT which doesn't have scheme nor path and looks like :
139 * <:method> <:authority> HTTP/x.y
140 *
141 * It's worth noting that H2 still supports an encoding to map H1 origin-form
142 * and asterisk-form requests. These ones do not specify the authority. However
143 * in H2 they must still specify the scheme, which is not present in H1. Also,
144 * when encoding an absolute-form H1 request without a path, the path
145 * automatically becomes "/" except for the OPTIONS method where it
146 * becomes "*".
147 *
148 * As such it is explicitly permitted for an H2 client to send a request
149 * featuring a Host header and no :authority, though it's not the recommended
150 * way to use H2 for a client. It is however the only permitted way to encode
151 * an origin-form H1 request over H2. Thus we need to respect such differences
152 * as much as possible when re-encoding the H2 request into HTX.
Willy Tarreau6deb4122018-11-27 15:34:18 +0100153 */
154static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
155{
Willy Tarreau92919f72019-10-08 16:53:07 +0200156 struct ist uri;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100157 unsigned int flags = HTX_SL_F_NONE;
158 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100159 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100160
161 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
162 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
163 * MUST be omitted ; ":authority" contains the host and port
164 * to connect to.
165 */
166 if (fields & H2_PHDR_FND_SCHM) {
167 /* scheme not allowed */
168 goto fail;
169 }
170 else if (fields & H2_PHDR_FND_PATH) {
171 /* path not allowed */
172 goto fail;
173 }
174 else if (!(fields & H2_PHDR_FND_AUTH)) {
175 /* missing authority */
176 goto fail;
177 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100178 *msgf |= H2_MSGF_BODY_TUNNEL;
179 }
180 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
181 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
182 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
183 * valid value for the ":method", ":scheme" and ":path" phdr
184 * unless it is a CONNECT request.
185 */
186 if (!(fields & H2_PHDR_FND_METH)) {
187 /* missing method */
188 goto fail;
189 }
190 else if (!(fields & H2_PHDR_FND_SCHM)) {
191 /* missing scheme */
192 goto fail;
193 }
194 else {
195 /* missing path */
196 goto fail;
197 }
198 }
Willy Tarreau2be362c2019-10-08 11:59:37 +0200199 else { /* regular methods */
Willy Tarreau92919f72019-10-08 16:53:07 +0200200 /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to
201 * classify the scheme as "present/http", "present/https",
202 * "present/other", "absent" so as to decide whether or not
203 * we're facing a normalized URI that will have to be encoded
204 * in origin or absolute form. Indeed, 7540#8.1.2.3 says that
205 * clients should use the absolute form, thus we cannot infer
206 * whether or not the client wanted to use a proxy here.
207 */
208 flags |= HTX_SL_F_HAS_SCHM;
209 if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")))
210 flags |= HTX_SL_F_SCHM_HTTP;
211 else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https")))
212 flags |= HTX_SL_F_SCHM_HTTPS;
213 }
214
215 if (!(flags & HTX_SL_F_HAS_SCHM)) {
216 /* no scheme, use authority only (CONNECT) */
217 uri = phdr[H2_PHDR_IDX_AUTH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200218 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200219 }
220 else if (!(flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) && (fields & H2_PHDR_FND_AUTH)) {
221 /* non-http/https scheme + authority, let's use the absolute
222 * form. We simply use the trash to concatenate them since all
223 * of them MUST fit in a bufsize since it's where they come
224 * from.
225 */
226 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
227 istcat(&uri, ist("://"), trash.size);
228 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
229 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
230 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200231 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau92919f72019-10-08 16:53:07 +0200232 }
233 else {
234 /* usual schemes with or without authority, use origin form */
235 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200236 if (fields & H2_PHDR_FND_AUTH)
237 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200238 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100239
Willy Tarreau2be362c2019-10-08 11:59:37 +0200240 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
241 * that :path must not be empty.
242 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200243 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100244 goto fail;
245
Willy Tarreau2be362c2019-10-08 11:59:37 +0200246 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200247 for (i = 0; i < uri.len; i++) {
248 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100249 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
250 htx->flags |= HTX_FL_PARSING_ERROR;
251 }
252
Willy Tarreau6deb4122018-11-27 15:34:18 +0100253 /* Set HTX start-line flags */
254 flags |= HTX_SL_F_VER_11; // V2 in fact
255 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
256
Willy Tarreau92919f72019-10-08 16:53:07 +0200257 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 +0100258 if (!sl)
259 goto fail;
260
261 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
262 return sl;
263 fail:
264 return NULL;
265}
266
267/* Takes an H2 request present in the headers list <list> terminated by a name
268 * being <NULL,0> and emits the equivalent HTX request according to the rules
269 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
270 * non-zero is returned if some bytes were emitted. In case of error, a
271 * negative error code is returned.
272 *
273 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
274 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
275 * if a body is detected (!ES).
276 *
277 * The headers list <list> must be composed of :
278 * - n.name != NULL, n.len > 0 : literal header name
279 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
280 * among H2_PHDR_IDX_*
281 * - n.name ignored, n.len == 0 : end of list
282 * - in all cases except the end of list, v.name and v.len must designate a
283 * valid value.
284 *
285 * The Cookie header will be reassembled at the end, and for this, the <list>
286 * will be used to create a linked list, so its contents may be destroyed.
287 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100288int 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 +0100289{
290 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
291 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
292 uint32_t idx;
293 int ck, lck; /* cookie index and last cookie index */
294 int phdr;
295 int ret;
296 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200297 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100298 struct htx_sl *sl = NULL;
299 unsigned int sl_flags = 0;
300
301 lck = ck = -1; // no cookie for now
302 fields = 0;
303 for (idx = 0; list[idx].n.len != 0; idx++) {
304 if (!list[idx].n.ptr) {
305 /* this is an indexed pseudo-header */
306 phdr = list[idx].n.len;
307 }
308 else {
309 /* this can be any type of header */
310 /* RFC7540#8.1.2: upper case not allowed in header field names */
311 for (i = 0; i < list[idx].n.len; i++)
312 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
313 goto fail;
314
315 phdr = h2_str_to_phdr(list[idx].n);
316 }
317
318 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
319 /* insert a pseudo header by its index (in phdr) and value (in value) */
320 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
321 if (fields & H2_PHDR_FND_NONE) {
322 /* pseudo header field after regular headers */
323 goto fail;
324 }
325 else {
326 /* repeated pseudo header field */
327 goto fail;
328 }
329 }
330 fields |= 1 << phdr;
331 phdr_val[phdr] = list[idx].v;
332 continue;
333 }
334 else if (phdr != 0) {
335 /* invalid pseudo header -- should never happen here */
336 goto fail;
337 }
338
339 /* regular header field in (name,value) */
340 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
341 /* no more pseudo-headers, time to build the request line */
342 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
343 if (!sl)
344 goto fail;
345 fields |= H2_PHDR_FND_NONE;
346 }
347
348 if (isteq(list[idx].n, ist("host")))
349 fields |= H2_PHDR_FND_HOST;
350
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100351 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100352 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100353 if (ret < 0)
354 goto fail;
355
Willy Tarreau6deb4122018-11-27 15:34:18 +0100356 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100357 if (ret == 0)
358 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100359 }
360
361 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
362 if (isteq(list[idx].n, ist("connection")) ||
363 isteq(list[idx].n, ist("proxy-connection")) ||
364 isteq(list[idx].n, ist("keep-alive")) ||
365 isteq(list[idx].n, ist("upgrade")) ||
366 isteq(list[idx].n, ist("transfer-encoding")))
367 goto fail;
368
369 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
370 goto fail;
371
372 /* cookie requires special processing at the end */
373 if (isteq(list[idx].n, ist("cookie"))) {
374 list[idx].n.len = -1;
375
376 if (ck < 0)
377 ck = idx;
378 else
379 list[lck].n.len = idx;
380
381 lck = idx;
382 continue;
383 }
384
385 if (!htx_add_header(htx, list[idx].n, list[idx].v))
386 goto fail;
387 }
388
389 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
390 if (fields & H2_PHDR_FND_STAT)
391 goto fail;
392
393 /* Let's dump the request now if not yet emitted. */
394 if (!(fields & H2_PHDR_FND_NONE)) {
395 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
396 if (!sl)
397 goto fail;
398 }
399
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100400 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
401 sl_flags |= HTX_SL_F_BODYLESS;
402
Willy Tarreau6deb4122018-11-27 15:34:18 +0100403 /* update the start line with last detected header info */
404 sl->flags |= sl_flags;
405
406 /* complete with missing Host if needed */
407 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
408 /* missing Host field, use :authority instead */
409 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
410 goto fail;
411 }
412
413 /* now we may have to build a cookie list. We'll dump the values of all
414 * visited headers.
415 */
416 if (ck >= 0) {
417 uint32_t fs; // free space
418 uint32_t bs; // block size
419 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100420 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100421 struct htx_blk *blk;
422
423 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
424 if (!blk)
425 goto fail;
426
Willy Tarreau164e0612018-12-18 11:00:41 +0100427 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100428 fs = htx_free_data_space(htx);
429 bs = htx_get_blksz(blk);
430
431 /* for each extra cookie, we'll extend the cookie's value and
432 * insert "; " before the new value.
433 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100434 fs += tl; // first one is already counted
435 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100436 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100437 tl += vl + 2;
438 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100439 goto fail;
440
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200441 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100442 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
443 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
444 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
445 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100446 }
447
448 }
449
450 /* now send the end of headers marker */
451 htx_add_endof(htx, HTX_BLK_EOH);
452
Christopher Faulet33543e72019-05-15 15:53:20 +0200453 /* Set bytes used in the HTX mesage for the headers now */
454 sl->hdrs_bytes = htx_used_space(htx) - used;
455
Willy Tarreau6deb4122018-11-27 15:34:18 +0100456 ret = 1;
457 return ret;
458
459 fail:
460 return -1;
461}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200462
463/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
464 * <fields> indicates what was found so far. This should be called once at the
465 * detection of the first general header field or at the end of the message if
466 * no general header field was found yet. Returns the created start line on
467 * success, or NULL on failure. Upon success, <msgf> is updated with a few
468 * H2_MSGF_* flags indicating what was found while parsing.
469 */
470static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
471{
472 unsigned int flags = HTX_SL_F_NONE;
473 struct htx_sl *sl;
474 unsigned char h, t, u;
475
476 /* only :status is allowed as a pseudo header */
477 if (!(fields & H2_PHDR_FND_STAT))
478 goto fail;
479
480 if (phdr[H2_PHDR_IDX_STAT].len != 3)
481 goto fail;
482
483 /* Set HTX start-line flags */
484 flags |= HTX_SL_F_VER_11; // V2 in fact
485 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
486
487 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
488 if (!sl)
489 goto fail;
490
491 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
492 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
493 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
494 if (h > 9 || t > 9 || u > 9)
495 goto fail;
496
497 sl->info.res.status = h * 100 + t * 10 + u;
498
Christopher Faulet0b465482019-02-19 15:14:23 +0100499 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
500 * there is no body. So remove the flag H2_MSGF_BODY and add
501 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
502 * expected.
503 */
504 if (sl->info.res.status < 200 &&
505 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
506 *msgf |= H2_MSGF_RSP_1XX;
507 *msgf &= ~H2_MSGF_BODY;
508 }
509
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200510 return sl;
511 fail:
512 return NULL;
513}
514
515/* Takes an H2 response present in the headers list <list> terminated by a name
516 * being <NULL,0> and emits the equivalent HTX response according to the rules
517 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
518 * a positive value is returned if some bytes were emitted. In case of error, a
519 * negative error code is returned.
520 *
521 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
522 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
523 * if a body is detected (!ES).
524 *
525 * The headers list <list> must be composed of :
526 * - n.name != NULL, n.len > 0 : literal header name
527 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
528 * among H2_PHDR_IDX_*
529 * - n.name ignored, n.len == 0 : end of list
530 * - in all cases except the end of list, v.name and v.len must designate a
531 * valid value.
532 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100533int 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 +0200534{
535 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
536 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
537 uint32_t idx;
538 int phdr;
539 int ret;
540 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200541 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200542 struct htx_sl *sl = NULL;
543 unsigned int sl_flags = 0;
544
545 fields = 0;
546 for (idx = 0; list[idx].n.len != 0; idx++) {
547 if (!list[idx].n.ptr) {
548 /* this is an indexed pseudo-header */
549 phdr = list[idx].n.len;
550 }
551 else {
552 /* this can be any type of header */
553 /* RFC7540#8.1.2: upper case not allowed in header field names */
554 for (i = 0; i < list[idx].n.len; i++)
555 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
556 goto fail;
557
558 phdr = h2_str_to_phdr(list[idx].n);
559 }
560
561 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
562 /* insert a pseudo header by its index (in phdr) and value (in value) */
563 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
564 if (fields & H2_PHDR_FND_NONE) {
565 /* pseudo header field after regular headers */
566 goto fail;
567 }
568 else {
569 /* repeated pseudo header field */
570 goto fail;
571 }
572 }
573 fields |= 1 << phdr;
574 phdr_val[phdr] = list[idx].v;
575 continue;
576 }
577 else if (phdr != 0) {
578 /* invalid pseudo header -- should never happen here */
579 goto fail;
580 }
581
582 /* regular header field in (name,value) */
583 if (!(fields & H2_PHDR_FND_NONE)) {
584 /* no more pseudo-headers, time to build the status line */
585 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
586 if (!sl)
587 goto fail;
588 fields |= H2_PHDR_FND_NONE;
589 }
590
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100591 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100592 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100593 if (ret < 0)
594 goto fail;
595
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200596 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100597 if (ret == 0)
598 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200599 }
600
601 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
602 if (isteq(list[idx].n, ist("connection")) ||
603 isteq(list[idx].n, ist("proxy-connection")) ||
604 isteq(list[idx].n, ist("keep-alive")) ||
605 isteq(list[idx].n, ist("upgrade")) ||
606 isteq(list[idx].n, ist("transfer-encoding")))
607 goto fail;
608
609 if (!htx_add_header(htx, list[idx].n, list[idx].v))
610 goto fail;
611 }
612
613 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
614 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
615 goto fail;
616
617 /* Let's dump the request now if not yet emitted. */
618 if (!(fields & H2_PHDR_FND_NONE)) {
619 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
620 if (!sl)
621 goto fail;
622 }
623
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100624 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
625 sl_flags |= HTX_SL_F_BODYLESS;
626
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200627 /* update the start line with last detected header info */
628 sl->flags |= sl_flags;
629
630 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
631 /* FIXME: Do we need to signal anything when we have a body and
632 * no content-length, to have the equivalent of H1's chunked
633 * encoding?
634 */
635 }
636
637 /* now send the end of headers marker */
638 htx_add_endof(htx, HTX_BLK_EOH);
639
Christopher Faulet33543e72019-05-15 15:53:20 +0200640 /* Set bytes used in the HTX mesage for the headers now */
641 sl->hdrs_bytes = htx_used_space(htx) - used;
642
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200643 ret = 1;
644 return ret;
645
646 fail:
647 return -1;
648}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100649
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200650/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
651 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
652 * and a positive value is returned if some bytes were emitted. In case of
653 * error, a negative error code is returned. The caller must have verified that
654 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100655 *
656 * The headers list <list> must be composed of :
657 * - n.name != NULL, n.len > 0 : literal header name
658 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
659 * among H2_PHDR_IDX_* (illegal here)
660 * - n.name ignored, n.len == 0 : end of list
661 * - in all cases except the end of list, v.name and v.len must designate a
662 * valid value.
663 */
664int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
665{
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100666 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100667 int i;
668
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100669 for (idx = 0; list[idx].n.len != 0; idx++) {
670 if (!list[idx].n.ptr) {
671 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
672 goto fail;
673 }
674
675 /* RFC7540#8.1.2: upper case not allowed in header field names */
676 for (i = 0; i < list[idx].n.len; i++)
677 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
678 goto fail;
679
680 if (h2_str_to_phdr(list[idx].n) != 0) {
681 /* This is a pseudo-header (RFC7540#8.1.2.1) */
682 goto fail;
683 }
684
685 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
686 if (isteq(list[idx].n, ist("host")) ||
687 isteq(list[idx].n, ist("content-length")) ||
688 isteq(list[idx].n, ist("connection")) ||
689 isteq(list[idx].n, ist("proxy-connection")) ||
690 isteq(list[idx].n, ist("keep-alive")) ||
691 isteq(list[idx].n, ist("upgrade")) ||
692 isteq(list[idx].n, ist("te")) ||
693 isteq(list[idx].n, ist("transfer-encoding")))
694 goto fail;
695
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200696 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
697 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100698 }
699
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200700 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100701 goto fail;
702
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100703 return 1;
704
705 fail:
706 return -1;
707}