blob: 72b2dc6138443aebee7f543b057ab3a9fda16b7d [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
28#include <stdint.h>
29#include <common/config.h>
30#include <common/h2.h>
31#include <common/http-hdr.h>
32#include <common/ist.h>
33
34
35/* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers
36 * stored in <phdr[]>. <fields> indicates what was found so far. This should be
37 * called once at the detection of the first general header field or at the end
38 * of the request if no general header field was found yet. Returns 0 on success
Willy Tarreau174b06a2018-04-25 18:13:58 +020039 * or a negative error code on failure. Upon success, <msgf> is updated with a
40 * few H2_MSGF_* flags indicating what was found while parsing.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010041 */
Willy Tarreau174b06a2018-04-25 18:13:58 +020042static int h2_prepare_h1_reqline(uint32_t fields, struct ist *phdr, char **ptr, char *end, unsigned int *msgf)
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010043{
44 char *out = *ptr;
45 int uri_idx = H2_PHDR_IDX_PATH;
46
47 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
48 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
49 * MUST be omitted ; ":authority" contains the host and port
50 * to connect to.
51 */
52 if (fields & H2_PHDR_FND_SCHM) {
53 /* scheme not allowed */
54 goto fail;
55 }
56 else if (fields & H2_PHDR_FND_PATH) {
57 /* path not allowed */
58 goto fail;
59 }
60 else if (!(fields & H2_PHDR_FND_AUTH)) {
61 /* missing authority */
62 goto fail;
63 }
64 // otherwise OK ; let's use the authority instead of the URI
65 uri_idx = H2_PHDR_IDX_AUTH;
Willy Tarreau174b06a2018-04-25 18:13:58 +020066 *msgf |= H2_MSGF_BODY_TUNNEL;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010067 }
68 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
69 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
70 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
71 * valid value for the ":method", ":scheme" and ":path" phdr
72 * unless it is a CONNECT request.
73 */
74 if (!(fields & H2_PHDR_FND_METH)) {
75 /* missing method */
76 goto fail;
77 }
78 else if (!(fields & H2_PHDR_FND_SCHM)) {
79 /* missing scheme */
80 goto fail;
81 }
82 else {
83 /* missing path */
84 goto fail;
85 }
86 }
87
Willy Tarreaucd4fe172017-12-03 11:51:31 +010088 /* 7540#8.1.2.3: :path must not be empty */
89 if (!phdr[uri_idx].len)
90 goto fail;
91
Willy Tarreau811ad122017-12-03 09:44:50 +010092 if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) {
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010093 /* too large */
94 goto fail;
95 }
96
97 memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
98 out += phdr[H2_PHDR_IDX_METH].len;
99 *(out++) = ' ';
100
101 memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len);
102 out += phdr[uri_idx].len;
103 memcpy(out, " HTTP/1.1\r\n", 11);
104 out += 11;
105
106 *ptr = out;
107 return 0;
108 fail:
109 return -1;
110}
111
112/* Takes an H2 request present in the headers list <list> terminated by a name
113 * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the
114 * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out>
115 * for a max of <osize> bytes, and the amount of bytes emitted is returned. In
116 * case of error, a negative error code is returned.
117 *
Willy Tarreau174b06a2018-04-25 18:13:58 +0200118 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
119 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
120 * if a body is detected (!ES).
121 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100122 * The headers list <list> must be composed of :
123 * - n.name != NULL, n.len > 0 : literal header name
124 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
125 * among H2_PHDR_IDX_*
126 * - n.name ignored, n.len == 0 : end of list
127 * - in all cases except the end of list, v.name and v.len must designate a
128 * valid value.
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100129 *
130 * The Cookie header will be reassembled at the end, and for this, the <list>
131 * will be used to create a linked list, so its contents may be destroyed.
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100132 */
Willy Tarreau174b06a2018-04-25 18:13:58 +0200133int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf)
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100134{
135 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
136 char *out_end = out + osize;
137 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
138 uint32_t idx;
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100139 int ck, lck; /* cookie index and last cookie index */
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100140 int phdr;
141 int ret;
Willy Tarreau637f64d2017-12-03 20:28:13 +0100142 int i;
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100143
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100144 lck = ck = -1; // no cookie for now
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100145 fields = 0;
146 for (idx = 0; list[idx].n.len != 0; idx++) {
147 if (!list[idx].n.ptr) {
148 /* this is an indexed pseudo-header */
149 phdr = list[idx].n.len;
150 }
151 else {
152 /* this can be any type of header */
Willy Tarreau637f64d2017-12-03 20:28:13 +0100153 /* RFC7540#8.1.2: upper case not allowed in header field names */
154 for (i = 0; i < list[idx].n.len; i++)
155 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
156 goto fail;
157
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100158 phdr = h2_str_to_phdr(list[idx].n);
159 }
160
161 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
162 /* insert a pseudo header by its index (in phdr) and value (in value) */
163 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
164 if (fields & H2_PHDR_FND_NONE) {
165 /* pseudo header field after regular headers */
166 goto fail;
167 }
168 else {
169 /* repeated pseudo header field */
170 goto fail;
171 }
172 }
173 fields |= 1 << phdr;
174 phdr_val[phdr] = list[idx].v;
175 continue;
176 }
177 else if (phdr != 0) {
178 /* invalid pseudo header -- should never happen here */
179 goto fail;
180 }
181
182 /* regular header field in (name,value) */
183 if (!(fields & H2_PHDR_FND_NONE)) {
184 /* no more pseudo-headers, time to build the request line */
Willy Tarreau174b06a2018-04-25 18:13:58 +0200185 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100186 if (ret != 0)
187 goto leave;
188 fields |= H2_PHDR_FND_NONE;
189 }
190
191 if (isteq(list[idx].n, ist("host")))
192 fields |= H2_PHDR_FND_HOST;
193
Willy Tarreau174b06a2018-04-25 18:13:58 +0200194 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY &&
195 isteq(list[idx].n, ist("content-length")))
196 *msgf |= H2_MSGF_BODY_CL;
197
Willy Tarreaufe7c3562017-12-03 20:15:34 +0100198 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
199 if (isteq(list[idx].n, ist("connection")) ||
200 isteq(list[idx].n, ist("proxy-connection")) ||
201 isteq(list[idx].n, ist("keep-alive")) ||
202 isteq(list[idx].n, ist("upgrade")) ||
203 isteq(list[idx].n, ist("transfer-encoding")))
204 goto fail;
205
Willy Tarreaud8d2ac72017-12-03 18:41:31 +0100206 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
207 goto fail;
208
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100209 /* cookie requires special processing at the end */
210 if (isteq(list[idx].n, ist("cookie"))) {
211 list[idx].n.len = -1;
212
213 if (ck < 0)
214 ck = idx;
215 else
216 list[lck].n.len = idx;
217
218 lck = idx;
219 continue;
220 }
221
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100222 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
223 /* too large */
224 goto fail;
225 }
226
227 /* copy "name: value" */
228 memcpy(out, list[idx].n.ptr, list[idx].n.len);
229 out += list[idx].n.len;
230 *(out++) = ':';
231 *(out++) = ' ';
232
233 memcpy(out, list[idx].v.ptr, list[idx].v.len);
234 out += list[idx].v.len;
235 *(out++) = '\r';
236 *(out++) = '\n';
237 }
238
Willy Tarreau52088692017-12-03 20:13:54 +0100239 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
240 if (fields & H2_PHDR_FND_STAT)
241 goto fail;
242
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100243 /* Let's dump the request now if not yet emitted. */
244 if (!(fields & H2_PHDR_FND_NONE)) {
Willy Tarreau174b06a2018-04-25 18:13:58 +0200245 ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100246 if (ret != 0)
247 goto leave;
248 }
249
250 /* complete with missing Host if needed */
251 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
252 /* missing Host field, use :authority instead */
253 if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) {
254 /* too large */
255 goto fail;
256 }
257
258 memcpy(out, "host: ", 6);
259 memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len);
260 out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len;
261 *(out++) = '\r';
262 *(out++) = '\n';
263 }
264
Willy Tarreaueba10f22018-04-25 20:44:22 +0200265 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
266 /* add chunked encoding */
267 if (out + 28 > out_end)
268 goto fail;
269 memcpy(out, "transfer-encoding: chunked\r\n", 28);
270 out += 28;
271 }
272
Willy Tarreau2fb986c2017-11-21 21:01:29 +0100273 /* now we may have to build a cookie list. We'll dump the values of all
274 * visited headers.
275 */
276 if (ck >= 0) {
277 if (out + 8 > out_end) {
278 /* too large */
279 goto fail;
280 }
281 memcpy(out, "cookie: ", 8);
282 out += 8;
283
284 do {
285 if (out + list[ck].v.len + 2 > out_end) {
286 /* too large */
287 goto fail;
288 }
289 memcpy(out, list[ck].v.ptr, list[ck].v.len);
290 out += list[ck].v.len;
291 ck = list[ck].n.len;
292
293 if (ck >= 0) {
294 *(out++) = ';';
295 *(out++) = ' ';
296 }
297 } while (ck >= 0);
298
299 if (out + 2 > out_end) {
300 /* too large */
301 goto fail;
302 }
303 *(out++) = '\r';
304 *(out++) = '\n';
305 }
306
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100307 /* And finish */
308 if (out + 2 > out_end) {
309 /* too large */
310 goto fail;
311 }
312
313 *(out++) = '\r';
314 *(out++) = '\n';
315 ret = out + osize - out_end;
316 leave:
317 return ret;
318
319 fail:
320 return -1;
321}
Willy Tarreau6deb4122018-11-27 15:34:18 +0100322
Willy Tarreau9d953e72019-01-03 16:18:14 +0100323/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
324 * emits the equivalent HTTP/1.1 trailers block not including the empty line.
325 * The output contents are emitted in <out> for a max of <osize> bytes, and the
326 * amount of bytes emitted is returned. In case of error, a negative error code
327 * is returned. The caller must have verified that the message in the buffer is
328 * compatible with receipt of trailers.
329 *
330 * The headers list <list> must be composed of :
331 * - n.name != NULL, n.len > 0 : literal header name
332 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
333 * among H2_PHDR_IDX_* (illegal here)
334 * - n.name ignored, n.len == 0 : end of list
335 * - in all cases except the end of list, v.name and v.len must designate a
336 * valid value.
337 */
338int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize)
339{
340 char *out_end = out + osize;
341 uint32_t idx;
342 int i;
343
344 for (idx = 0; list[idx].n.len != 0; idx++) {
345 if (!list[idx].n.ptr) {
346 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
347 goto fail;
348 }
349
350 /* RFC7540#8.1.2: upper case not allowed in header field names */
351 for (i = 0; i < list[idx].n.len; i++)
352 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
353 goto fail;
354
355 if (h2_str_to_phdr(list[idx].n) != 0) {
356 /* This is a pseudo-header (RFC7540#8.1.2.1) */
357 goto fail;
358 }
359
360 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
361 if (isteq(list[idx].n, ist("host")) ||
362 isteq(list[idx].n, ist("content-length")) ||
363 isteq(list[idx].n, ist("connection")) ||
364 isteq(list[idx].n, ist("proxy-connection")) ||
365 isteq(list[idx].n, ist("keep-alive")) ||
366 isteq(list[idx].n, ist("upgrade")) ||
367 isteq(list[idx].n, ist("te")) ||
368 isteq(list[idx].n, ist("transfer-encoding")))
369 goto fail;
370
371 if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) {
372 /* too large */
373 goto fail;
374 }
375
376 /* copy "name: value" */
377 memcpy(out, list[idx].n.ptr, list[idx].n.len);
378 out += list[idx].n.len;
379 *(out++) = ':';
380 *(out++) = ' ';
381
382 memcpy(out, list[idx].v.ptr, list[idx].v.len);
383 out += list[idx].v.len;
384 *(out++) = '\r';
385 *(out++) = '\n';
386 }
387
388 return out + osize - out_end;
389
390 fail:
391 return -1;
392}
393
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100394/* Parse the Content-Length header field of an HTTP/2 request. The function
395 * checks all possible occurrences of a comma-delimited value, and verifies
396 * if any of them doesn't match a previous value. It returns <0 if a value
397 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
398 * if the value can be indexed (first one). In the last case, the value might
399 * be adjusted and the caller must only add the updated value.
400 */
401int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
402{
403 char *e, *n;
404 unsigned long long cl;
405 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
406 struct ist word;
407
408 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
409 e = value->ptr + value->len;
410
411 while (++word.ptr < e) {
412 /* skip leading delimitor and blanks */
413 if (unlikely(HTTP_IS_LWS(*word.ptr)))
414 continue;
415
416 /* digits only now */
417 for (cl = 0, n = word.ptr; n < e; n++) {
418 unsigned int c = *n - '0';
419 if (unlikely(c > 9)) {
420 /* non-digit */
421 if (unlikely(n == word.ptr)) // spaces only
422 goto fail;
423 break;
424 }
425 if (unlikely(cl > ULLONG_MAX / 10ULL))
426 goto fail; /* multiply overflow */
427 cl = cl * 10ULL;
428 if (unlikely(cl + c < cl))
429 goto fail; /* addition overflow */
430 cl = cl + c;
431 }
432
433 /* keep a copy of the exact cleaned value */
434 word.len = n - word.ptr;
435
436 /* skip trailing LWS till next comma or EOL */
437 for (; n < e; n++) {
438 if (!HTTP_IS_LWS(*n)) {
439 if (unlikely(*n != ','))
440 goto fail;
441 break;
442 }
443 }
444
445 /* if duplicate, must be equal */
446 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
447 goto fail;
448
449 /* OK, store this result as the one to be indexed */
450 *msgf |= H2_MSGF_BODY_CL;
451 *body_len = cl;
452 *value = word;
453 word.ptr = n;
454 }
455 /* here we've reached the end with a single value or a series of
456 * identical values, all matching previous series if any. The last
457 * parsed value was sent back into <value>. We just have to decide
458 * if this occurrence has to be indexed (it's the first one) or
459 * silently skipped (it's not the first one)
460 */
461 return !not_first;
462 fail:
463 return -1;
464}
465
Willy Tarreau6deb4122018-11-27 15:34:18 +0100466/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
467 * <fields> indicates what was found so far. This should be called once at the
468 * detection of the first general header field or at the end of the request if
469 * no general header field was found yet. Returns the created start line on
470 * success, or NULL on failure. Upon success, <msgf> is updated with a few
471 * H2_MSGF_* flags indicating what was found while parsing.
472 */
473static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
474{
475 int uri_idx = H2_PHDR_IDX_PATH;
476 unsigned int flags = HTX_SL_F_NONE;
477 struct htx_sl *sl;
478
479 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
480 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
481 * MUST be omitted ; ":authority" contains the host and port
482 * to connect to.
483 */
484 if (fields & H2_PHDR_FND_SCHM) {
485 /* scheme not allowed */
486 goto fail;
487 }
488 else if (fields & H2_PHDR_FND_PATH) {
489 /* path not allowed */
490 goto fail;
491 }
492 else if (!(fields & H2_PHDR_FND_AUTH)) {
493 /* missing authority */
494 goto fail;
495 }
496 // otherwise OK ; let's use the authority instead of the URI
497 uri_idx = H2_PHDR_IDX_AUTH;
498 *msgf |= H2_MSGF_BODY_TUNNEL;
499 }
500 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
501 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
502 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
503 * valid value for the ":method", ":scheme" and ":path" phdr
504 * unless it is a CONNECT request.
505 */
506 if (!(fields & H2_PHDR_FND_METH)) {
507 /* missing method */
508 goto fail;
509 }
510 else if (!(fields & H2_PHDR_FND_SCHM)) {
511 /* missing scheme */
512 goto fail;
513 }
514 else {
515 /* missing path */
516 goto fail;
517 }
518 }
519
520 /* 7540#8.1.2.3: :path must not be empty */
521 if (!phdr[uri_idx].len)
522 goto fail;
523
524 /* Set HTX start-line flags */
525 flags |= HTX_SL_F_VER_11; // V2 in fact
526 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
527
528 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
529 if (!sl)
530 goto fail;
531
532 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
533 return sl;
534 fail:
535 return NULL;
536}
537
538/* Takes an H2 request present in the headers list <list> terminated by a name
539 * being <NULL,0> and emits the equivalent HTX request according to the rules
540 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
541 * non-zero is returned if some bytes were emitted. In case of error, a
542 * negative error code is returned.
543 *
544 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
545 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
546 * if a body is detected (!ES).
547 *
548 * The headers list <list> must be composed of :
549 * - n.name != NULL, n.len > 0 : literal header name
550 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
551 * among H2_PHDR_IDX_*
552 * - n.name ignored, n.len == 0 : end of list
553 * - in all cases except the end of list, v.name and v.len must designate a
554 * valid value.
555 *
556 * The Cookie header will be reassembled at the end, and for this, the <list>
557 * will be used to create a linked list, so its contents may be destroyed.
558 */
559int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
560{
561 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
562 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
563 uint32_t idx;
564 int ck, lck; /* cookie index and last cookie index */
565 int phdr;
566 int ret;
567 int i;
568 struct htx_sl *sl = NULL;
569 unsigned int sl_flags = 0;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100570 unsigned long long body_len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100571
572 lck = ck = -1; // no cookie for now
573 fields = 0;
574 for (idx = 0; list[idx].n.len != 0; idx++) {
575 if (!list[idx].n.ptr) {
576 /* this is an indexed pseudo-header */
577 phdr = list[idx].n.len;
578 }
579 else {
580 /* this can be any type of header */
581 /* RFC7540#8.1.2: upper case not allowed in header field names */
582 for (i = 0; i < list[idx].n.len; i++)
583 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
584 goto fail;
585
586 phdr = h2_str_to_phdr(list[idx].n);
587 }
588
589 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
590 /* insert a pseudo header by its index (in phdr) and value (in value) */
591 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
592 if (fields & H2_PHDR_FND_NONE) {
593 /* pseudo header field after regular headers */
594 goto fail;
595 }
596 else {
597 /* repeated pseudo header field */
598 goto fail;
599 }
600 }
601 fields |= 1 << phdr;
602 phdr_val[phdr] = list[idx].v;
603 continue;
604 }
605 else if (phdr != 0) {
606 /* invalid pseudo header -- should never happen here */
607 goto fail;
608 }
609
610 /* regular header field in (name,value) */
611 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
612 /* no more pseudo-headers, time to build the request line */
613 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
614 if (!sl)
615 goto fail;
616 fields |= H2_PHDR_FND_NONE;
617 }
618
619 if (isteq(list[idx].n, ist("host")))
620 fields |= H2_PHDR_FND_HOST;
621
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100622 if (isteq(list[idx].n, ist("content-length"))) {
623 ret = h2_parse_cont_len_header(msgf, &list[idx].v, &body_len);
624 if (ret < 0)
625 goto fail;
626
Willy Tarreau6deb4122018-11-27 15:34:18 +0100627 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100628 if (ret == 0)
629 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100630 }
631
632 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
633 if (isteq(list[idx].n, ist("connection")) ||
634 isteq(list[idx].n, ist("proxy-connection")) ||
635 isteq(list[idx].n, ist("keep-alive")) ||
636 isteq(list[idx].n, ist("upgrade")) ||
637 isteq(list[idx].n, ist("transfer-encoding")))
638 goto fail;
639
640 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
641 goto fail;
642
643 /* cookie requires special processing at the end */
644 if (isteq(list[idx].n, ist("cookie"))) {
645 list[idx].n.len = -1;
646
647 if (ck < 0)
648 ck = idx;
649 else
650 list[lck].n.len = idx;
651
652 lck = idx;
653 continue;
654 }
655
656 if (!htx_add_header(htx, list[idx].n, list[idx].v))
657 goto fail;
658 }
659
660 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
661 if (fields & H2_PHDR_FND_STAT)
662 goto fail;
663
664 /* Let's dump the request now if not yet emitted. */
665 if (!(fields & H2_PHDR_FND_NONE)) {
666 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
667 if (!sl)
668 goto fail;
669 }
670
671 /* update the start line with last detected header info */
672 sl->flags |= sl_flags;
673
674 /* complete with missing Host if needed */
675 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
676 /* missing Host field, use :authority instead */
677 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
678 goto fail;
679 }
680
681 /* now we may have to build a cookie list. We'll dump the values of all
682 * visited headers.
683 */
684 if (ck >= 0) {
685 uint32_t fs; // free space
686 uint32_t bs; // block size
687 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100688 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100689 struct htx_blk *blk;
690
691 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
692 if (!blk)
693 goto fail;
694
Willy Tarreau164e0612018-12-18 11:00:41 +0100695 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100696 fs = htx_free_data_space(htx);
697 bs = htx_get_blksz(blk);
698
699 /* for each extra cookie, we'll extend the cookie's value and
700 * insert "; " before the new value.
701 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100702 fs += tl; // first one is already counted
703 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100704 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100705 tl += vl + 2;
706 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100707 goto fail;
708
Willy Tarreau164e0612018-12-18 11:00:41 +0100709 htx_set_blk_value_len(blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100710 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
711 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
712 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
713 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100714 }
715
716 }
717
718 /* now send the end of headers marker */
719 htx_add_endof(htx, HTX_BLK_EOH);
720
721 ret = 1;
722 return ret;
723
724 fail:
725 return -1;
726}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200727
728/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
729 * <fields> indicates what was found so far. This should be called once at the
730 * detection of the first general header field or at the end of the message if
731 * no general header field was found yet. Returns the created start line on
732 * success, or NULL on failure. Upon success, <msgf> is updated with a few
733 * H2_MSGF_* flags indicating what was found while parsing.
734 */
735static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
736{
737 unsigned int flags = HTX_SL_F_NONE;
738 struct htx_sl *sl;
739 unsigned char h, t, u;
740
741 /* only :status is allowed as a pseudo header */
742 if (!(fields & H2_PHDR_FND_STAT))
743 goto fail;
744
745 if (phdr[H2_PHDR_IDX_STAT].len != 3)
746 goto fail;
747
748 /* Set HTX start-line flags */
749 flags |= HTX_SL_F_VER_11; // V2 in fact
750 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
751
752 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
753 if (!sl)
754 goto fail;
755
756 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
757 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
758 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
759 if (h > 9 || t > 9 || u > 9)
760 goto fail;
761
762 sl->info.res.status = h * 100 + t * 10 + u;
763
764 return sl;
765 fail:
766 return NULL;
767}
768
769/* Takes an H2 response present in the headers list <list> terminated by a name
770 * being <NULL,0> and emits the equivalent HTX response according to the rules
771 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
772 * a positive value is returned if some bytes were emitted. In case of error, a
773 * negative error code is returned.
774 *
775 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
776 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
777 * if a body is detected (!ES).
778 *
779 * The headers list <list> must be composed of :
780 * - n.name != NULL, n.len > 0 : literal header name
781 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
782 * among H2_PHDR_IDX_*
783 * - n.name ignored, n.len == 0 : end of list
784 * - in all cases except the end of list, v.name and v.len must designate a
785 * valid value.
786 */
787int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
788{
789 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
790 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
791 uint32_t idx;
792 int phdr;
793 int ret;
794 int i;
795 struct htx_sl *sl = NULL;
796 unsigned int sl_flags = 0;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100797 unsigned long long body_len;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200798
799 fields = 0;
800 for (idx = 0; list[idx].n.len != 0; idx++) {
801 if (!list[idx].n.ptr) {
802 /* this is an indexed pseudo-header */
803 phdr = list[idx].n.len;
804 }
805 else {
806 /* this can be any type of header */
807 /* RFC7540#8.1.2: upper case not allowed in header field names */
808 for (i = 0; i < list[idx].n.len; i++)
809 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
810 goto fail;
811
812 phdr = h2_str_to_phdr(list[idx].n);
813 }
814
815 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
816 /* insert a pseudo header by its index (in phdr) and value (in value) */
817 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
818 if (fields & H2_PHDR_FND_NONE) {
819 /* pseudo header field after regular headers */
820 goto fail;
821 }
822 else {
823 /* repeated pseudo header field */
824 goto fail;
825 }
826 }
827 fields |= 1 << phdr;
828 phdr_val[phdr] = list[idx].v;
829 continue;
830 }
831 else if (phdr != 0) {
832 /* invalid pseudo header -- should never happen here */
833 goto fail;
834 }
835
836 /* regular header field in (name,value) */
837 if (!(fields & H2_PHDR_FND_NONE)) {
838 /* no more pseudo-headers, time to build the status line */
839 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
840 if (!sl)
841 goto fail;
842 fields |= H2_PHDR_FND_NONE;
843 }
844
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100845 if (isteq(list[idx].n, ist("content-length"))) {
846 ret = h2_parse_cont_len_header(msgf, &list[idx].v, &body_len);
847 if (ret < 0)
848 goto fail;
849
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200850 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100851 if (ret == 0)
852 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200853 }
854
855 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
856 if (isteq(list[idx].n, ist("connection")) ||
857 isteq(list[idx].n, ist("proxy-connection")) ||
858 isteq(list[idx].n, ist("keep-alive")) ||
859 isteq(list[idx].n, ist("upgrade")) ||
860 isteq(list[idx].n, ist("transfer-encoding")))
861 goto fail;
862
863 if (!htx_add_header(htx, list[idx].n, list[idx].v))
864 goto fail;
865 }
866
867 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
868 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
869 goto fail;
870
871 /* Let's dump the request now if not yet emitted. */
872 if (!(fields & H2_PHDR_FND_NONE)) {
873 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
874 if (!sl)
875 goto fail;
876 }
877
878 /* update the start line with last detected header info */
879 sl->flags |= sl_flags;
880
881 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
882 /* FIXME: Do we need to signal anything when we have a body and
883 * no content-length, to have the equivalent of H1's chunked
884 * encoding?
885 */
886 }
887
888 /* now send the end of headers marker */
889 htx_add_endof(htx, HTX_BLK_EOH);
890
891 ret = 1;
892 return ret;
893
894 fail:
895 return -1;
896}
Willy Tarreau1e1f27c2019-01-03 18:39:54 +0100897
898/* Takes an H2 headers list <list> terminated by a name being <NULL,0> and
899 * emits the equivalent HTX trailers block not including the empty line. The
900 * output contents are emitted in <htx>, and a positive value is returned if
901 * some bytes were emitted. In case of error, a negative error code is
902 * returned. The caller must have verified that the message in the buffer is
903 * compatible with receipt of trailers. Note that for now the HTX trailers
904 * block is in fact an H1 block and it must contain the trailing CRLF.
905 *
906 * The headers list <list> must be composed of :
907 * - n.name != NULL, n.len > 0 : literal header name
908 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
909 * among H2_PHDR_IDX_* (illegal here)
910 * - n.name ignored, n.len == 0 : end of list
911 * - in all cases except the end of list, v.name and v.len must designate a
912 * valid value.
913 */
914int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx)
915{
916 struct htx_blk *blk;
917 char *out;
918 uint32_t idx;
919 int len;
920 int i;
921
922 len = 2; // CRLF
923 for (idx = 0; list[idx].n.len != 0; idx++) {
924 if (!list[idx].n.ptr) {
925 /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */
926 goto fail;
927 }
928
929 /* RFC7540#8.1.2: upper case not allowed in header field names */
930 for (i = 0; i < list[idx].n.len; i++)
931 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
932 goto fail;
933
934 if (h2_str_to_phdr(list[idx].n) != 0) {
935 /* This is a pseudo-header (RFC7540#8.1.2.1) */
936 goto fail;
937 }
938
939 /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */
940 if (isteq(list[idx].n, ist("host")) ||
941 isteq(list[idx].n, ist("content-length")) ||
942 isteq(list[idx].n, ist("connection")) ||
943 isteq(list[idx].n, ist("proxy-connection")) ||
944 isteq(list[idx].n, ist("keep-alive")) ||
945 isteq(list[idx].n, ist("upgrade")) ||
946 isteq(list[idx].n, ist("te")) ||
947 isteq(list[idx].n, ist("transfer-encoding")))
948 goto fail;
949
950 len += list[idx].n.len + 2 + list[idx].v.len + 2;
951 }
952
953 blk = htx_add_blk_type_size(htx, HTX_BLK_TLR, len);
954 if (!blk)
955 goto fail;
956
957 out = htx_get_blk_ptr(htx, blk);
958 for (idx = 0; list[idx].n.len != 0; idx++) {
959 /* copy "name: value" */
960 memcpy(out, list[idx].n.ptr, list[idx].n.len);
961 out += list[idx].n.len;
962 *(out++) = ':';
963 *(out++) = ' ';
964
965 memcpy(out, list[idx].v.ptr, list[idx].v.len);
966 out += list[idx].v.len;
967 *(out++) = '\r';
968 *(out++) = '\n';
969 }
970 *(out++) = '\r';
971 *(out++) = '\n';
972
973 return 1;
974
975 fail:
976 return -1;
977}