blob: f034e3d5694d1b592ccd8ec4cee38c2f2d916cf7 [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 }
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200220 else if (fields & H2_PHDR_FND_AUTH) {
221 /* authority is present, let's use the absolute form. We simply
222 * use the trash to concatenate them since all of them MUST fit
223 * in a bufsize since it's where they come from.
Willy Tarreau92919f72019-10-08 16:53:07 +0200224 */
225 uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]);
226 istcat(&uri, ist("://"), trash.size);
227 istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size);
228 if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*")))
229 istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size);
Willy Tarreau1440fe82019-10-08 17:34:50 +0200230 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau30ee1ef2019-10-08 18:33:19 +0200231
232 if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) {
233 /* we don't know if it was originally an absolute or a
234 * relative request because newer versions of HTTP use
235 * the absolute URI format by default, which we call
236 * the normalized URI format internally. This is the
237 * strongly recommended way of sending a request for
238 * a regular client, so we cannot distinguish this
239 * from a request intended for a proxy. For other
240 * schemes however there is no doubt.
241 */
242 flags |= HTX_SL_F_NORMALIZED_URI;
243 }
Willy Tarreau92919f72019-10-08 16:53:07 +0200244 }
245 else {
246 /* usual schemes with or without authority, use origin form */
247 uri = phdr[H2_PHDR_IDX_PATH];
Willy Tarreau1440fe82019-10-08 17:34:50 +0200248 if (fields & H2_PHDR_FND_AUTH)
249 flags |= HTX_SL_F_HAS_AUTHORITY;
Willy Tarreau2be362c2019-10-08 11:59:37 +0200250 }
Willy Tarreau6deb4122018-11-27 15:34:18 +0100251
Willy Tarreau2be362c2019-10-08 11:59:37 +0200252 /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states
253 * that :path must not be empty.
254 */
Willy Tarreau92919f72019-10-08 16:53:07 +0200255 if (!uri.len)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100256 goto fail;
257
Willy Tarreau2be362c2019-10-08 11:59:37 +0200258 /* The final URI must not contain LWS nor CTL characters */
Willy Tarreau92919f72019-10-08 16:53:07 +0200259 for (i = 0; i < uri.len; i++) {
260 unsigned char c = uri.ptr[i];
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100261 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
262 htx->flags |= HTX_FL_PARSING_ERROR;
263 }
264
Willy Tarreau6deb4122018-11-27 15:34:18 +0100265 /* Set HTX start-line flags */
266 flags |= HTX_SL_F_VER_11; // V2 in fact
267 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
268
Willy Tarreau92919f72019-10-08 16:53:07 +0200269 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 +0100270 if (!sl)
271 goto fail;
272
273 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
274 return sl;
275 fail:
276 return NULL;
277}
278
279/* Takes an H2 request present in the headers list <list> terminated by a name
280 * being <NULL,0> and emits the equivalent HTX request according to the rules
281 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
282 * non-zero is returned if some bytes were emitted. In case of error, a
283 * negative error code is returned.
284 *
285 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
286 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
287 * if a body is detected (!ES).
288 *
289 * The headers list <list> must be composed of :
290 * - n.name != NULL, n.len > 0 : literal header name
291 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
292 * among H2_PHDR_IDX_*
293 * - n.name ignored, n.len == 0 : end of list
294 * - in all cases except the end of list, v.name and v.len must designate a
295 * valid value.
296 *
297 * The Cookie header will be reassembled at the end, and for this, the <list>
298 * will be used to create a linked list, so its contents may be destroyed.
299 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100300int 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 +0100301{
302 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
303 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
304 uint32_t idx;
305 int ck, lck; /* cookie index and last cookie index */
306 int phdr;
307 int ret;
308 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200309 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100310 struct htx_sl *sl = NULL;
311 unsigned int sl_flags = 0;
312
313 lck = ck = -1; // no cookie for now
314 fields = 0;
315 for (idx = 0; list[idx].n.len != 0; idx++) {
316 if (!list[idx].n.ptr) {
317 /* this is an indexed pseudo-header */
318 phdr = list[idx].n.len;
319 }
320 else {
321 /* this can be any type of header */
322 /* RFC7540#8.1.2: upper case not allowed in header field names */
323 for (i = 0; i < list[idx].n.len; i++)
324 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
325 goto fail;
326
327 phdr = h2_str_to_phdr(list[idx].n);
328 }
329
330 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
331 /* insert a pseudo header by its index (in phdr) and value (in value) */
332 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
333 if (fields & H2_PHDR_FND_NONE) {
334 /* pseudo header field after regular headers */
335 goto fail;
336 }
337 else {
338 /* repeated pseudo header field */
339 goto fail;
340 }
341 }
342 fields |= 1 << phdr;
343 phdr_val[phdr] = list[idx].v;
344 continue;
345 }
346 else if (phdr != 0) {
347 /* invalid pseudo header -- should never happen here */
348 goto fail;
349 }
350
351 /* regular header field in (name,value) */
352 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
353 /* no more pseudo-headers, time to build the request line */
354 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
355 if (!sl)
356 goto fail;
357 fields |= H2_PHDR_FND_NONE;
358 }
359
360 if (isteq(list[idx].n, ist("host")))
361 fields |= H2_PHDR_FND_HOST;
362
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100363 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100364 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100365 if (ret < 0)
366 goto fail;
367
Willy Tarreau6deb4122018-11-27 15:34:18 +0100368 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100369 if (ret == 0)
370 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100371 }
372
373 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
374 if (isteq(list[idx].n, ist("connection")) ||
375 isteq(list[idx].n, ist("proxy-connection")) ||
376 isteq(list[idx].n, ist("keep-alive")) ||
377 isteq(list[idx].n, ist("upgrade")) ||
378 isteq(list[idx].n, ist("transfer-encoding")))
379 goto fail;
380
381 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
382 goto fail;
383
384 /* cookie requires special processing at the end */
385 if (isteq(list[idx].n, ist("cookie"))) {
386 list[idx].n.len = -1;
387
388 if (ck < 0)
389 ck = idx;
390 else
391 list[lck].n.len = idx;
392
393 lck = idx;
394 continue;
395 }
396
397 if (!htx_add_header(htx, list[idx].n, list[idx].v))
398 goto fail;
399 }
400
401 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
402 if (fields & H2_PHDR_FND_STAT)
403 goto fail;
404
405 /* Let's dump the request now if not yet emitted. */
406 if (!(fields & H2_PHDR_FND_NONE)) {
407 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
408 if (!sl)
409 goto fail;
410 }
411
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100412 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
413 sl_flags |= HTX_SL_F_BODYLESS;
414
Willy Tarreau6deb4122018-11-27 15:34:18 +0100415 /* update the start line with last detected header info */
416 sl->flags |= sl_flags;
417
418 /* complete with missing Host if needed */
419 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
420 /* missing Host field, use :authority instead */
421 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
422 goto fail;
423 }
424
425 /* now we may have to build a cookie list. We'll dump the values of all
426 * visited headers.
427 */
428 if (ck >= 0) {
429 uint32_t fs; // free space
430 uint32_t bs; // block size
431 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100432 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100433 struct htx_blk *blk;
434
435 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
436 if (!blk)
437 goto fail;
438
Willy Tarreau164e0612018-12-18 11:00:41 +0100439 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100440 fs = htx_free_data_space(htx);
441 bs = htx_get_blksz(blk);
442
443 /* for each extra cookie, we'll extend the cookie's value and
444 * insert "; " before the new value.
445 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100446 fs += tl; // first one is already counted
447 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100448 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100449 tl += vl + 2;
450 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100451 goto fail;
452
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200453 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100454 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
455 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
456 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
457 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100458 }
459
460 }
461
462 /* now send the end of headers marker */
463 htx_add_endof(htx, HTX_BLK_EOH);
464
Christopher Faulet33543e72019-05-15 15:53:20 +0200465 /* Set bytes used in the HTX mesage for the headers now */
466 sl->hdrs_bytes = htx_used_space(htx) - used;
467
Willy Tarreau6deb4122018-11-27 15:34:18 +0100468 ret = 1;
469 return ret;
470
471 fail:
472 return -1;
473}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200474
475/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
476 * <fields> indicates what was found so far. This should be called once at the
477 * detection of the first general header field or at the end of the message if
478 * no general header field was found yet. Returns the created start line on
479 * success, or NULL on failure. Upon success, <msgf> is updated with a few
480 * H2_MSGF_* flags indicating what was found while parsing.
481 */
482static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
483{
484 unsigned int flags = HTX_SL_F_NONE;
485 struct htx_sl *sl;
486 unsigned char h, t, u;
487
488 /* only :status is allowed as a pseudo header */
489 if (!(fields & H2_PHDR_FND_STAT))
490 goto fail;
491
492 if (phdr[H2_PHDR_IDX_STAT].len != 3)
493 goto fail;
494
495 /* Set HTX start-line flags */
496 flags |= HTX_SL_F_VER_11; // V2 in fact
497 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
498
499 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
500 if (!sl)
501 goto fail;
502
503 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
504 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
505 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
506 if (h > 9 || t > 9 || u > 9)
507 goto fail;
508
509 sl->info.res.status = h * 100 + t * 10 + u;
510
Christopher Faulet0b465482019-02-19 15:14:23 +0100511 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
512 * there is no body. So remove the flag H2_MSGF_BODY and add
513 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
514 * expected.
515 */
516 if (sl->info.res.status < 200 &&
517 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
518 *msgf |= H2_MSGF_RSP_1XX;
519 *msgf &= ~H2_MSGF_BODY;
520 }
521
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200522 return sl;
523 fail:
524 return NULL;
525}
526
527/* Takes an H2 response present in the headers list <list> terminated by a name
528 * being <NULL,0> and emits the equivalent HTX response according to the rules
529 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
530 * a positive value is returned if some bytes were emitted. In case of error, a
531 * negative error code is returned.
532 *
533 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
534 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
535 * if a body is detected (!ES).
536 *
537 * The headers list <list> must be composed of :
538 * - n.name != NULL, n.len > 0 : literal header name
539 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
540 * among H2_PHDR_IDX_*
541 * - n.name ignored, n.len == 0 : end of list
542 * - in all cases except the end of list, v.name and v.len must designate a
543 * valid value.
544 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100545int 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 +0200546{
547 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
548 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
549 uint32_t idx;
550 int phdr;
551 int ret;
552 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200553 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200554 struct htx_sl *sl = NULL;
555 unsigned int sl_flags = 0;
556
557 fields = 0;
558 for (idx = 0; list[idx].n.len != 0; idx++) {
559 if (!list[idx].n.ptr) {
560 /* this is an indexed pseudo-header */
561 phdr = list[idx].n.len;
562 }
563 else {
564 /* this can be any type of header */
565 /* RFC7540#8.1.2: upper case not allowed in header field names */
566 for (i = 0; i < list[idx].n.len; i++)
567 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
568 goto fail;
569
570 phdr = h2_str_to_phdr(list[idx].n);
571 }
572
573 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
574 /* insert a pseudo header by its index (in phdr) and value (in value) */
575 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
576 if (fields & H2_PHDR_FND_NONE) {
577 /* pseudo header field after regular headers */
578 goto fail;
579 }
580 else {
581 /* repeated pseudo header field */
582 goto fail;
583 }
584 }
585 fields |= 1 << phdr;
586 phdr_val[phdr] = list[idx].v;
587 continue;
588 }
589 else if (phdr != 0) {
590 /* invalid pseudo header -- should never happen here */
591 goto fail;
592 }
593
594 /* regular header field in (name,value) */
595 if (!(fields & H2_PHDR_FND_NONE)) {
596 /* no more pseudo-headers, time to build the status line */
597 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
598 if (!sl)
599 goto fail;
600 fields |= H2_PHDR_FND_NONE;
601 }
602
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100603 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100604 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100605 if (ret < 0)
606 goto fail;
607
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200608 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100609 if (ret == 0)
610 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200611 }
612
613 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
614 if (isteq(list[idx].n, ist("connection")) ||
615 isteq(list[idx].n, ist("proxy-connection")) ||
616 isteq(list[idx].n, ist("keep-alive")) ||
617 isteq(list[idx].n, ist("upgrade")) ||
618 isteq(list[idx].n, ist("transfer-encoding")))
619 goto fail;
620
621 if (!htx_add_header(htx, list[idx].n, list[idx].v))
622 goto fail;
623 }
624
625 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
626 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
627 goto fail;
628
629 /* Let's dump the request now if not yet emitted. */
630 if (!(fields & H2_PHDR_FND_NONE)) {
631 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
632 if (!sl)
633 goto fail;
634 }
635
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100636 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
637 sl_flags |= HTX_SL_F_BODYLESS;
638
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200639 /* update the start line with last detected header info */
640 sl->flags |= sl_flags;
641
642 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
643 /* FIXME: Do we need to signal anything when we have a body and
644 * no content-length, to have the equivalent of H1's chunked
645 * encoding?
646 */
647 }
648
649 /* now send the end of headers marker */
650 htx_add_endof(htx, HTX_BLK_EOH);
651
Christopher Faulet33543e72019-05-15 15:53:20 +0200652 /* Set bytes used in the HTX mesage for the headers now */
653 sl->hdrs_bytes = htx_used_space(htx) - used;
654
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200655 ret = 1;
656 return ret;
657
658 fail:
659 return -1;
660}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100661
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200662/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
663 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
664 * and a positive value is returned if some bytes were emitted. In case of
665 * error, a negative error code is returned. The caller must have verified that
666 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100667 *
668 * The headers list <list> must be composed of :
669 * - n.name != NULL, n.len > 0 : literal header name
670 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
671 * among H2_PHDR_IDX_* (illegal here)
672 * - n.name ignored, n.len == 0 : end of list
673 * - in all cases except the end of list, v.name and v.len must designate a
674 * valid value.
675 */
676int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
677{
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100678 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100679 int i;
680
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100681 for (idx = 0; list[idx].n.len != 0; idx++) {
682 if (!list[idx].n.ptr) {
683 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
684 goto fail;
685 }
686
687 /* RFC7540#8.1.2: upper case not allowed in header field names */
688 for (i = 0; i < list[idx].n.len; i++)
689 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
690 goto fail;
691
692 if (h2_str_to_phdr(list[idx].n) != 0) {
693 /* This is a pseudo-header (RFC7540#8.1.2.1) */
694 goto fail;
695 }
696
697 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
698 if (isteq(list[idx].n, ist("host")) ||
699 isteq(list[idx].n, ist("content-length")) ||
700 isteq(list[idx].n, ist("connection")) ||
701 isteq(list[idx].n, ist("proxy-connection")) ||
702 isteq(list[idx].n, ist("keep-alive")) ||
703 isteq(list[idx].n, ist("upgrade")) ||
704 isteq(list[idx].n, ist("te")) ||
705 isteq(list[idx].n, ist("transfer-encoding")))
706 goto fail;
707
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200708 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
709 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100710 }
711
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200712 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100713 goto fail;
714
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100715 return 1;
716
717 fail:
718 return -1;
719}