blob: c41da8e5ee116e75e4719709527511c299a3657c [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>
33
Willy Tarreau9c84d822019-01-30 15:09:21 +010034struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = {
35 [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
36 [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, },
37 [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, },
38 [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
39 [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
40 [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, },
41 [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, },
42 [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, },
43 [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, },
44 [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, },
45};
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010046
Willy Tarreaubeefaee2018-12-19 13:08:08 +010047/* Parse the Content-Length header field of an HTTP/2 request. The function
48 * checks all possible occurrences of a comma-delimited value, and verifies
49 * if any of them doesn't match a previous value. It returns <0 if a value
50 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
51 * if the value can be indexed (first one). In the last case, the value might
52 * be adjusted and the caller must only add the updated value.
53 */
54int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
55{
56 char *e, *n;
57 unsigned long long cl;
58 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
59 struct ist word;
60
61 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
62 e = value->ptr + value->len;
63
64 while (++word.ptr < e) {
65 /* skip leading delimitor and blanks */
66 if (unlikely(HTTP_IS_LWS(*word.ptr)))
67 continue;
68
69 /* digits only now */
70 for (cl = 0, n = word.ptr; n < e; n++) {
71 unsigned int c = *n - '0';
72 if (unlikely(c > 9)) {
73 /* non-digit */
74 if (unlikely(n == word.ptr)) // spaces only
75 goto fail;
76 break;
77 }
78 if (unlikely(cl > ULLONG_MAX / 10ULL))
79 goto fail; /* multiply overflow */
80 cl = cl * 10ULL;
81 if (unlikely(cl + c < cl))
82 goto fail; /* addition overflow */
83 cl = cl + c;
84 }
85
86 /* keep a copy of the exact cleaned value */
87 word.len = n - word.ptr;
88
89 /* skip trailing LWS till next comma or EOL */
90 for (; n < e; n++) {
91 if (!HTTP_IS_LWS(*n)) {
92 if (unlikely(*n != ','))
93 goto fail;
94 break;
95 }
96 }
97
98 /* if duplicate, must be equal */
99 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
100 goto fail;
101
102 /* OK, store this result as the one to be indexed */
103 *msgf |= H2_MSGF_BODY_CL;
104 *body_len = cl;
105 *value = word;
106 word.ptr = n;
107 }
108 /* here we've reached the end with a single value or a series of
109 * identical values, all matching previous series if any. The last
110 * parsed value was sent back into <value>. We just have to decide
111 * if this occurrence has to be indexed (it's the first one) or
112 * silently skipped (it's not the first one)
113 */
114 return !not_first;
115 fail:
116 return -1;
117}
118
Willy Tarreau6deb4122018-11-27 15:34:18 +0100119/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
120 * <fields> indicates what was found so far. This should be called once at the
121 * detection of the first general header field or at the end of the request if
122 * no general header field was found yet. Returns the created start line on
123 * success, or NULL on failure. Upon success, <msgf> is updated with a few
124 * H2_MSGF_* flags indicating what was found while parsing.
125 */
126static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
127{
128 int uri_idx = H2_PHDR_IDX_PATH;
129 unsigned int flags = HTX_SL_F_NONE;
130 struct htx_sl *sl;
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100131 size_t i;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100132
133 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
134 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
135 * MUST be omitted ; ":authority" contains the host and port
136 * to connect to.
137 */
138 if (fields & H2_PHDR_FND_SCHM) {
139 /* scheme not allowed */
140 goto fail;
141 }
142 else if (fields & H2_PHDR_FND_PATH) {
143 /* path not allowed */
144 goto fail;
145 }
146 else if (!(fields & H2_PHDR_FND_AUTH)) {
147 /* missing authority */
148 goto fail;
149 }
150 // otherwise OK ; let's use the authority instead of the URI
151 uri_idx = H2_PHDR_IDX_AUTH;
152 *msgf |= H2_MSGF_BODY_TUNNEL;
153 }
154 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
155 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
156 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
157 * valid value for the ":method", ":scheme" and ":path" phdr
158 * unless it is a CONNECT request.
159 */
160 if (!(fields & H2_PHDR_FND_METH)) {
161 /* missing method */
162 goto fail;
163 }
164 else if (!(fields & H2_PHDR_FND_SCHM)) {
165 /* missing scheme */
166 goto fail;
167 }
168 else {
169 /* missing path */
170 goto fail;
171 }
172 }
173
174 /* 7540#8.1.2.3: :path must not be empty */
175 if (!phdr[uri_idx].len)
176 goto fail;
177
Willy Tarreau9255e7e2019-03-05 10:47:37 +0100178 /* make sure :path doesn't contain LWS nor CTL characters */
179 for (i = 0; i < phdr[uri_idx].len; i++) {
180 unsigned char c = phdr[uri_idx].ptr[i];
181 if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c))
182 htx->flags |= HTX_FL_PARSING_ERROR;
183 }
184
Willy Tarreau6deb4122018-11-27 15:34:18 +0100185 /* Set HTX start-line flags */
186 flags |= HTX_SL_F_VER_11; // V2 in fact
187 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
188
189 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
190 if (!sl)
191 goto fail;
192
193 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
Christopher Fauleta9a5c042019-06-14 10:25:47 +0200194 sl->flags |= HTX_SL_F_HAS_SCHM;
195 sl->flags |= (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")) ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100196 return sl;
197 fail:
198 return NULL;
199}
200
201/* Takes an H2 request present in the headers list <list> terminated by a name
202 * being <NULL,0> and emits the equivalent HTX request according to the rules
203 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
204 * non-zero is returned if some bytes were emitted. In case of error, a
205 * negative error code is returned.
206 *
207 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
208 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
209 * if a body is detected (!ES).
210 *
211 * The headers list <list> must be composed of :
212 * - n.name != NULL, n.len > 0 : literal header name
213 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
214 * among H2_PHDR_IDX_*
215 * - n.name ignored, n.len == 0 : end of list
216 * - in all cases except the end of list, v.name and v.len must designate a
217 * valid value.
218 *
219 * The Cookie header will be reassembled at the end, and for this, the <list>
220 * will be used to create a linked list, so its contents may be destroyed.
221 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100222int 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 +0100223{
224 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
225 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
226 uint32_t idx;
227 int ck, lck; /* cookie index and last cookie index */
228 int phdr;
229 int ret;
230 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200231 uint32_t used = htx_used_space(htx);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100232 struct htx_sl *sl = NULL;
233 unsigned int sl_flags = 0;
234
235 lck = ck = -1; // no cookie for now
236 fields = 0;
237 for (idx = 0; list[idx].n.len != 0; idx++) {
238 if (!list[idx].n.ptr) {
239 /* this is an indexed pseudo-header */
240 phdr = list[idx].n.len;
241 }
242 else {
243 /* this can be any type of header */
244 /* RFC7540#8.1.2: upper case not allowed in header field names */
245 for (i = 0; i < list[idx].n.len; i++)
246 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
247 goto fail;
248
249 phdr = h2_str_to_phdr(list[idx].n);
250 }
251
252 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
253 /* insert a pseudo header by its index (in phdr) and value (in value) */
254 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
255 if (fields & H2_PHDR_FND_NONE) {
256 /* pseudo header field after regular headers */
257 goto fail;
258 }
259 else {
260 /* repeated pseudo header field */
261 goto fail;
262 }
263 }
264 fields |= 1 << phdr;
265 phdr_val[phdr] = list[idx].v;
266 continue;
267 }
268 else if (phdr != 0) {
269 /* invalid pseudo header -- should never happen here */
270 goto fail;
271 }
272
273 /* regular header field in (name,value) */
274 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
275 /* no more pseudo-headers, time to build the request line */
276 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
277 if (!sl)
278 goto fail;
279 fields |= H2_PHDR_FND_NONE;
280 }
281
282 if (isteq(list[idx].n, ist("host")))
283 fields |= H2_PHDR_FND_HOST;
284
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100285 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100286 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100287 if (ret < 0)
288 goto fail;
289
Willy Tarreau6deb4122018-11-27 15:34:18 +0100290 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100291 if (ret == 0)
292 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100293 }
294
295 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
296 if (isteq(list[idx].n, ist("connection")) ||
297 isteq(list[idx].n, ist("proxy-connection")) ||
298 isteq(list[idx].n, ist("keep-alive")) ||
299 isteq(list[idx].n, ist("upgrade")) ||
300 isteq(list[idx].n, ist("transfer-encoding")))
301 goto fail;
302
303 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
304 goto fail;
305
306 /* cookie requires special processing at the end */
307 if (isteq(list[idx].n, ist("cookie"))) {
308 list[idx].n.len = -1;
309
310 if (ck < 0)
311 ck = idx;
312 else
313 list[lck].n.len = idx;
314
315 lck = idx;
316 continue;
317 }
318
319 if (!htx_add_header(htx, list[idx].n, list[idx].v))
320 goto fail;
321 }
322
323 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
324 if (fields & H2_PHDR_FND_STAT)
325 goto fail;
326
327 /* Let's dump the request now if not yet emitted. */
328 if (!(fields & H2_PHDR_FND_NONE)) {
329 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
330 if (!sl)
331 goto fail;
332 }
333
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100334 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
335 sl_flags |= HTX_SL_F_BODYLESS;
336
Willy Tarreau6deb4122018-11-27 15:34:18 +0100337 /* update the start line with last detected header info */
338 sl->flags |= sl_flags;
339
340 /* complete with missing Host if needed */
341 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
342 /* missing Host field, use :authority instead */
343 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
344 goto fail;
345 }
346
347 /* now we may have to build a cookie list. We'll dump the values of all
348 * visited headers.
349 */
350 if (ck >= 0) {
351 uint32_t fs; // free space
352 uint32_t bs; // block size
353 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100354 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100355 struct htx_blk *blk;
356
357 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
358 if (!blk)
359 goto fail;
360
Willy Tarreau164e0612018-12-18 11:00:41 +0100361 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100362 fs = htx_free_data_space(htx);
363 bs = htx_get_blksz(blk);
364
365 /* for each extra cookie, we'll extend the cookie's value and
366 * insert "; " before the new value.
367 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100368 fs += tl; // first one is already counted
369 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100370 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100371 tl += vl + 2;
372 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100373 goto fail;
374
Christopher Faulet3e2638e2019-06-18 09:49:16 +0200375 htx_change_blk_value_len(htx, blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100376 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
377 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
378 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
379 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100380 }
381
382 }
383
384 /* now send the end of headers marker */
385 htx_add_endof(htx, HTX_BLK_EOH);
386
Christopher Faulet33543e72019-05-15 15:53:20 +0200387 /* Set bytes used in the HTX mesage for the headers now */
388 sl->hdrs_bytes = htx_used_space(htx) - used;
389
Willy Tarreau6deb4122018-11-27 15:34:18 +0100390 ret = 1;
391 return ret;
392
393 fail:
394 return -1;
395}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200396
397/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
398 * <fields> indicates what was found so far. This should be called once at the
399 * detection of the first general header field or at the end of the message if
400 * no general header field was found yet. Returns the created start line on
401 * success, or NULL on failure. Upon success, <msgf> is updated with a few
402 * H2_MSGF_* flags indicating what was found while parsing.
403 */
404static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
405{
406 unsigned int flags = HTX_SL_F_NONE;
407 struct htx_sl *sl;
408 unsigned char h, t, u;
409
410 /* only :status is allowed as a pseudo header */
411 if (!(fields & H2_PHDR_FND_STAT))
412 goto fail;
413
414 if (phdr[H2_PHDR_IDX_STAT].len != 3)
415 goto fail;
416
417 /* Set HTX start-line flags */
418 flags |= HTX_SL_F_VER_11; // V2 in fact
419 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
420
421 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
422 if (!sl)
423 goto fail;
424
425 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
426 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
427 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
428 if (h > 9 || t > 9 || u > 9)
429 goto fail;
430
431 sl->info.res.status = h * 100 + t * 10 + u;
432
Christopher Faulet0b465482019-02-19 15:14:23 +0100433 /* On 1xx responses (except 101) there is no ES on the HEADERS frame but
434 * there is no body. So remove the flag H2_MSGF_BODY and add
435 * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is
436 * expected.
437 */
438 if (sl->info.res.status < 200 &&
439 (sl->info.res.status == 100 || sl->info.res.status >= 102)) {
440 *msgf |= H2_MSGF_RSP_1XX;
441 *msgf &= ~H2_MSGF_BODY;
442 }
443
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200444 return sl;
445 fail:
446 return NULL;
447}
448
449/* Takes an H2 response present in the headers list <list> terminated by a name
450 * being <NULL,0> and emits the equivalent HTX response according to the rules
451 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
452 * a positive value is returned if some bytes were emitted. In case of error, a
453 * negative error code is returned.
454 *
455 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
456 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
457 * if a body is detected (!ES).
458 *
459 * The headers list <list> must be composed of :
460 * - n.name != NULL, n.len > 0 : literal header name
461 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
462 * among H2_PHDR_IDX_*
463 * - n.name ignored, n.len == 0 : end of list
464 * - in all cases except the end of list, v.name and v.len must designate a
465 * valid value.
466 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100467int 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 +0200468{
469 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
470 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
471 uint32_t idx;
472 int phdr;
473 int ret;
474 int i;
Christopher Faulet33543e72019-05-15 15:53:20 +0200475 uint32_t used = htx_used_space(htx);
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200476 struct htx_sl *sl = NULL;
477 unsigned int sl_flags = 0;
478
479 fields = 0;
480 for (idx = 0; list[idx].n.len != 0; idx++) {
481 if (!list[idx].n.ptr) {
482 /* this is an indexed pseudo-header */
483 phdr = list[idx].n.len;
484 }
485 else {
486 /* this can be any type of header */
487 /* RFC7540#8.1.2: upper case not allowed in header field names */
488 for (i = 0; i < list[idx].n.len; i++)
489 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
490 goto fail;
491
492 phdr = h2_str_to_phdr(list[idx].n);
493 }
494
495 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
496 /* insert a pseudo header by its index (in phdr) and value (in value) */
497 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
498 if (fields & H2_PHDR_FND_NONE) {
499 /* pseudo header field after regular headers */
500 goto fail;
501 }
502 else {
503 /* repeated pseudo header field */
504 goto fail;
505 }
506 }
507 fields |= 1 << phdr;
508 phdr_val[phdr] = list[idx].v;
509 continue;
510 }
511 else if (phdr != 0) {
512 /* invalid pseudo header -- should never happen here */
513 goto fail;
514 }
515
516 /* regular header field in (name,value) */
517 if (!(fields & H2_PHDR_FND_NONE)) {
518 /* no more pseudo-headers, time to build the status line */
519 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
520 if (!sl)
521 goto fail;
522 fields |= H2_PHDR_FND_NONE;
523 }
524
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100525 if (isteq(list[idx].n, ist("content-length"))) {
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100526 ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len);
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100527 if (ret < 0)
528 goto fail;
529
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200530 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100531 if (ret == 0)
532 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200533 }
534
535 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
536 if (isteq(list[idx].n, ist("connection")) ||
537 isteq(list[idx].n, ist("proxy-connection")) ||
538 isteq(list[idx].n, ist("keep-alive")) ||
539 isteq(list[idx].n, ist("upgrade")) ||
540 isteq(list[idx].n, ist("transfer-encoding")))
541 goto fail;
542
543 if (!htx_add_header(htx, list[idx].n, list[idx].v))
544 goto fail;
545 }
546
547 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
548 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
549 goto fail;
550
551 /* Let's dump the request now if not yet emitted. */
552 if (!(fields & H2_PHDR_FND_NONE)) {
553 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
554 if (!sl)
555 goto fail;
556 }
557
Christopher Faulet44af3cf2019-02-18 10:12:56 +0100558 if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0))
559 sl_flags |= HTX_SL_F_BODYLESS;
560
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200561 /* update the start line with last detected header info */
562 sl->flags |= sl_flags;
563
564 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
565 /* FIXME: Do we need to signal anything when we have a body and
566 * no content-length, to have the equivalent of H1's chunked
567 * encoding?
568 */
569 }
570
571 /* now send the end of headers marker */
572 htx_add_endof(htx, HTX_BLK_EOH);
573
Christopher Faulet33543e72019-05-15 15:53:20 +0200574 /* Set bytes used in the HTX mesage for the headers now */
575 sl->hdrs_bytes = htx_used_space(htx) - used;
576
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200577 ret = 1;
578 return ret;
579
580 fail:
581 return -1;
582}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100583
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200584/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits
585 * the equivalent HTX trailers blocks. The output contents are emitted in <htx>,
586 * and a positive value is returned if some bytes were emitted. In case of
587 * error, a negative error code is returned. The caller must have verified that
588 * the message in the buffer is compatible with receipt of trailers.
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100589 *
590 * The headers list <list> must be composed of :
591 * - n.name != NULL, n.len > 0 : literal header name
592 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
593 * among H2_PHDR_IDX_* (illegal here)
594 * - n.name ignored, n.len == 0 : end of list
595 * - in all cases except the end of list, v.name and v.len must designate a
596 * valid value.
597 */
598int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
599{
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100600 uint32_t idx;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100601 int i;
602
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100603 for (idx = 0; list[idx].n.len != 0; idx++) {
604 if (!list[idx].n.ptr) {
605 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
606 goto fail;
607 }
608
609 /* RFC7540#8.1.2: upper case not allowed in header field names */
610 for (i = 0; i < list[idx].n.len; i++)
611 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
612 goto fail;
613
614 if (h2_str_to_phdr(list[idx].n) != 0) {
615 /* This is a pseudo-header (RFC7540#8.1.2.1) */
616 goto fail;
617 }
618
619 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
620 if (isteq(list[idx].n, ist("host")) ||
621 isteq(list[idx].n, ist("content-length")) ||
622 isteq(list[idx].n, ist("connection")) ||
623 isteq(list[idx].n, ist("proxy-connection")) ||
624 isteq(list[idx].n, ist("keep-alive")) ||
625 isteq(list[idx].n, ist("upgrade")) ||
626 isteq(list[idx].n, ist("te")) ||
627 isteq(list[idx].n, ist("transfer-encoding")))
628 goto fail;
629
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200630 if (!htx_add_trailer(htx, list[idx].n, list[idx].v))
631 goto fail;
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100632 }
633
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200634 if (!htx_add_endof(htx, HTX_BLK_EOT))
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100635 goto fail;
636
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100637 return 1;
638
639 fail:
640 return -1;
641}