blob: 7c6cbeeac05eb96c1a016a406033abfbdad9b2e1 [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 Tarreaubeefaee2018-12-19 13:08:08 +0100323/* Parse the Content-Length header field of an HTTP/2 request. The function
324 * checks all possible occurrences of a comma-delimited value, and verifies
325 * if any of them doesn't match a previous value. It returns <0 if a value
326 * differs, 0 if the whole header can be dropped (i.e. already known), or >0
327 * if the value can be indexed (first one). In the last case, the value might
328 * be adjusted and the caller must only add the updated value.
329 */
330int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len)
331{
332 char *e, *n;
333 unsigned long long cl;
334 int not_first = !!(*msgf & H2_MSGF_BODY_CL);
335 struct ist word;
336
337 word.ptr = value->ptr - 1; // -1 for next loop's pre-increment
338 e = value->ptr + value->len;
339
340 while (++word.ptr < e) {
341 /* skip leading delimitor and blanks */
342 if (unlikely(HTTP_IS_LWS(*word.ptr)))
343 continue;
344
345 /* digits only now */
346 for (cl = 0, n = word.ptr; n < e; n++) {
347 unsigned int c = *n - '0';
348 if (unlikely(c > 9)) {
349 /* non-digit */
350 if (unlikely(n == word.ptr)) // spaces only
351 goto fail;
352 break;
353 }
354 if (unlikely(cl > ULLONG_MAX / 10ULL))
355 goto fail; /* multiply overflow */
356 cl = cl * 10ULL;
357 if (unlikely(cl + c < cl))
358 goto fail; /* addition overflow */
359 cl = cl + c;
360 }
361
362 /* keep a copy of the exact cleaned value */
363 word.len = n - word.ptr;
364
365 /* skip trailing LWS till next comma or EOL */
366 for (; n < e; n++) {
367 if (!HTTP_IS_LWS(*n)) {
368 if (unlikely(*n != ','))
369 goto fail;
370 break;
371 }
372 }
373
374 /* if duplicate, must be equal */
375 if (*msgf & H2_MSGF_BODY_CL && cl != *body_len)
376 goto fail;
377
378 /* OK, store this result as the one to be indexed */
379 *msgf |= H2_MSGF_BODY_CL;
380 *body_len = cl;
381 *value = word;
382 word.ptr = n;
383 }
384 /* here we've reached the end with a single value or a series of
385 * identical values, all matching previous series if any. The last
386 * parsed value was sent back into <value>. We just have to decide
387 * if this occurrence has to be indexed (it's the first one) or
388 * silently skipped (it's not the first one)
389 */
390 return !not_first;
391 fail:
392 return -1;
393}
394
Willy Tarreau6deb4122018-11-27 15:34:18 +0100395/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
396 * <fields> indicates what was found so far. This should be called once at the
397 * detection of the first general header field or at the end of the request if
398 * no general header field was found yet. Returns the created start line on
399 * success, or NULL on failure. Upon success, <msgf> is updated with a few
400 * H2_MSGF_* flags indicating what was found while parsing.
401 */
402static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
403{
404 int uri_idx = H2_PHDR_IDX_PATH;
405 unsigned int flags = HTX_SL_F_NONE;
406 struct htx_sl *sl;
407
408 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
409 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
410 * MUST be omitted ; ":authority" contains the host and port
411 * to connect to.
412 */
413 if (fields & H2_PHDR_FND_SCHM) {
414 /* scheme not allowed */
415 goto fail;
416 }
417 else if (fields & H2_PHDR_FND_PATH) {
418 /* path not allowed */
419 goto fail;
420 }
421 else if (!(fields & H2_PHDR_FND_AUTH)) {
422 /* missing authority */
423 goto fail;
424 }
425 // otherwise OK ; let's use the authority instead of the URI
426 uri_idx = H2_PHDR_IDX_AUTH;
427 *msgf |= H2_MSGF_BODY_TUNNEL;
428 }
429 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
430 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
431 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
432 * valid value for the ":method", ":scheme" and ":path" phdr
433 * unless it is a CONNECT request.
434 */
435 if (!(fields & H2_PHDR_FND_METH)) {
436 /* missing method */
437 goto fail;
438 }
439 else if (!(fields & H2_PHDR_FND_SCHM)) {
440 /* missing scheme */
441 goto fail;
442 }
443 else {
444 /* missing path */
445 goto fail;
446 }
447 }
448
449 /* 7540#8.1.2.3: :path must not be empty */
450 if (!phdr[uri_idx].len)
451 goto fail;
452
453 /* Set HTX start-line flags */
454 flags |= HTX_SL_F_VER_11; // V2 in fact
455 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
456
457 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
458 if (!sl)
459 goto fail;
460
461 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
462 return sl;
463 fail:
464 return NULL;
465}
466
467/* Takes an H2 request present in the headers list <list> terminated by a name
468 * being <NULL,0> and emits the equivalent HTX request according to the rules
469 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
470 * non-zero is returned if some bytes were emitted. In case of error, a
471 * negative error code is returned.
472 *
473 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
474 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
475 * if a body is detected (!ES).
476 *
477 * The headers list <list> must be composed of :
478 * - n.name != NULL, n.len > 0 : literal header name
479 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
480 * among H2_PHDR_IDX_*
481 * - n.name ignored, n.len == 0 : end of list
482 * - in all cases except the end of list, v.name and v.len must designate a
483 * valid value.
484 *
485 * The Cookie header will be reassembled at the end, and for this, the <list>
486 * will be used to create a linked list, so its contents may be destroyed.
487 */
488int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
489{
490 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
491 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
492 uint32_t idx;
493 int ck, lck; /* cookie index and last cookie index */
494 int phdr;
495 int ret;
496 int i;
497 struct htx_sl *sl = NULL;
498 unsigned int sl_flags = 0;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100499 unsigned long long body_len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100500
501 lck = ck = -1; // no cookie for now
502 fields = 0;
503 for (idx = 0; list[idx].n.len != 0; idx++) {
504 if (!list[idx].n.ptr) {
505 /* this is an indexed pseudo-header */
506 phdr = list[idx].n.len;
507 }
508 else {
509 /* this can be any type of header */
510 /* RFC7540#8.1.2: upper case not allowed in header field names */
511 for (i = 0; i < list[idx].n.len; i++)
512 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
513 goto fail;
514
515 phdr = h2_str_to_phdr(list[idx].n);
516 }
517
518 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
519 /* insert a pseudo header by its index (in phdr) and value (in value) */
520 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
521 if (fields & H2_PHDR_FND_NONE) {
522 /* pseudo header field after regular headers */
523 goto fail;
524 }
525 else {
526 /* repeated pseudo header field */
527 goto fail;
528 }
529 }
530 fields |= 1 << phdr;
531 phdr_val[phdr] = list[idx].v;
532 continue;
533 }
534 else if (phdr != 0) {
535 /* invalid pseudo header -- should never happen here */
536 goto fail;
537 }
538
539 /* regular header field in (name,value) */
540 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
541 /* no more pseudo-headers, time to build the request line */
542 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
543 if (!sl)
544 goto fail;
545 fields |= H2_PHDR_FND_NONE;
546 }
547
548 if (isteq(list[idx].n, ist("host")))
549 fields |= H2_PHDR_FND_HOST;
550
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100551 if (isteq(list[idx].n, ist("content-length"))) {
552 ret = h2_parse_cont_len_header(msgf, &list[idx].v, &body_len);
553 if (ret < 0)
554 goto fail;
555
Willy Tarreau6deb4122018-11-27 15:34:18 +0100556 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100557 if (ret == 0)
558 continue; // skip this duplicate
Willy Tarreau6deb4122018-11-27 15:34:18 +0100559 }
560
561 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
562 if (isteq(list[idx].n, ist("connection")) ||
563 isteq(list[idx].n, ist("proxy-connection")) ||
564 isteq(list[idx].n, ist("keep-alive")) ||
565 isteq(list[idx].n, ist("upgrade")) ||
566 isteq(list[idx].n, ist("transfer-encoding")))
567 goto fail;
568
569 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
570 goto fail;
571
572 /* cookie requires special processing at the end */
573 if (isteq(list[idx].n, ist("cookie"))) {
574 list[idx].n.len = -1;
575
576 if (ck < 0)
577 ck = idx;
578 else
579 list[lck].n.len = idx;
580
581 lck = idx;
582 continue;
583 }
584
585 if (!htx_add_header(htx, list[idx].n, list[idx].v))
586 goto fail;
587 }
588
589 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
590 if (fields & H2_PHDR_FND_STAT)
591 goto fail;
592
593 /* Let's dump the request now if not yet emitted. */
594 if (!(fields & H2_PHDR_FND_NONE)) {
595 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
596 if (!sl)
597 goto fail;
598 }
599
600 /* update the start line with last detected header info */
601 sl->flags |= sl_flags;
602
603 /* complete with missing Host if needed */
604 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
605 /* missing Host field, use :authority instead */
606 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
607 goto fail;
608 }
609
610 /* now we may have to build a cookie list. We'll dump the values of all
611 * visited headers.
612 */
613 if (ck >= 0) {
614 uint32_t fs; // free space
615 uint32_t bs; // block size
616 uint32_t vl; // value len
Willy Tarreau164e0612018-12-18 11:00:41 +0100617 uint32_t tl; // total length
Willy Tarreau6deb4122018-11-27 15:34:18 +0100618 struct htx_blk *blk;
619
620 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
621 if (!blk)
622 goto fail;
623
Willy Tarreau164e0612018-12-18 11:00:41 +0100624 tl = list[ck].v.len;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100625 fs = htx_free_data_space(htx);
626 bs = htx_get_blksz(blk);
627
628 /* for each extra cookie, we'll extend the cookie's value and
629 * insert "; " before the new value.
630 */
Willy Tarreau164e0612018-12-18 11:00:41 +0100631 fs += tl; // first one is already counted
632 for (; (ck = list[ck].n.len) >= 0 ; ) {
Willy Tarreau6deb4122018-11-27 15:34:18 +0100633 vl = list[ck].v.len;
Willy Tarreau164e0612018-12-18 11:00:41 +0100634 tl += vl + 2;
635 if (tl > fs)
Willy Tarreau6deb4122018-11-27 15:34:18 +0100636 goto fail;
637
Willy Tarreau164e0612018-12-18 11:00:41 +0100638 htx_set_blk_value_len(blk, tl);
Willy Tarreau6deb4122018-11-27 15:34:18 +0100639 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
640 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
641 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
642 bs += vl + 2;
Willy Tarreau6deb4122018-11-27 15:34:18 +0100643 }
644
645 }
646
647 /* now send the end of headers marker */
648 htx_add_endof(htx, HTX_BLK_EOH);
649
650 ret = 1;
651 return ret;
652
653 fail:
654 return -1;
655}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200656
657/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
658 * <fields> indicates what was found so far. This should be called once at the
659 * detection of the first general header field or at the end of the message if
660 * no general header field was found yet. Returns the created start line on
661 * success, or NULL on failure. Upon success, <msgf> is updated with a few
662 * H2_MSGF_* flags indicating what was found while parsing.
663 */
664static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
665{
666 unsigned int flags = HTX_SL_F_NONE;
667 struct htx_sl *sl;
668 unsigned char h, t, u;
669
670 /* only :status is allowed as a pseudo header */
671 if (!(fields & H2_PHDR_FND_STAT))
672 goto fail;
673
674 if (phdr[H2_PHDR_IDX_STAT].len != 3)
675 goto fail;
676
677 /* Set HTX start-line flags */
678 flags |= HTX_SL_F_VER_11; // V2 in fact
679 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
680
681 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
682 if (!sl)
683 goto fail;
684
685 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
686 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
687 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
688 if (h > 9 || t > 9 || u > 9)
689 goto fail;
690
691 sl->info.res.status = h * 100 + t * 10 + u;
692
693 return sl;
694 fail:
695 return NULL;
696}
697
698/* Takes an H2 response present in the headers list <list> terminated by a name
699 * being <NULL,0> and emits the equivalent HTX response according to the rules
700 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
701 * a positive value is returned if some bytes were emitted. In case of error, a
702 * negative error code is returned.
703 *
704 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
705 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
706 * if a body is detected (!ES).
707 *
708 * The headers list <list> must be composed of :
709 * - n.name != NULL, n.len > 0 : literal header name
710 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
711 * among H2_PHDR_IDX_*
712 * - n.name ignored, n.len == 0 : end of list
713 * - in all cases except the end of list, v.name and v.len must designate a
714 * valid value.
715 */
716int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
717{
718 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
719 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
720 uint32_t idx;
721 int phdr;
722 int ret;
723 int i;
724 struct htx_sl *sl = NULL;
725 unsigned int sl_flags = 0;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100726 unsigned long long body_len;
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200727
728 fields = 0;
729 for (idx = 0; list[idx].n.len != 0; idx++) {
730 if (!list[idx].n.ptr) {
731 /* this is an indexed pseudo-header */
732 phdr = list[idx].n.len;
733 }
734 else {
735 /* this can be any type of header */
736 /* RFC7540#8.1.2: upper case not allowed in header field names */
737 for (i = 0; i < list[idx].n.len; i++)
738 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
739 goto fail;
740
741 phdr = h2_str_to_phdr(list[idx].n);
742 }
743
744 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
745 /* insert a pseudo header by its index (in phdr) and value (in value) */
746 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
747 if (fields & H2_PHDR_FND_NONE) {
748 /* pseudo header field after regular headers */
749 goto fail;
750 }
751 else {
752 /* repeated pseudo header field */
753 goto fail;
754 }
755 }
756 fields |= 1 << phdr;
757 phdr_val[phdr] = list[idx].v;
758 continue;
759 }
760 else if (phdr != 0) {
761 /* invalid pseudo header -- should never happen here */
762 goto fail;
763 }
764
765 /* regular header field in (name,value) */
766 if (!(fields & H2_PHDR_FND_NONE)) {
767 /* no more pseudo-headers, time to build the status line */
768 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
769 if (!sl)
770 goto fail;
771 fields |= H2_PHDR_FND_NONE;
772 }
773
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100774 if (isteq(list[idx].n, ist("content-length"))) {
775 ret = h2_parse_cont_len_header(msgf, &list[idx].v, &body_len);
776 if (ret < 0)
777 goto fail;
778
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200779 sl_flags |= HTX_SL_F_CLEN;
Willy Tarreaubeefaee2018-12-19 13:08:08 +0100780 if (ret == 0)
781 continue; // skip this duplicate
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200782 }
783
784 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
785 if (isteq(list[idx].n, ist("connection")) ||
786 isteq(list[idx].n, ist("proxy-connection")) ||
787 isteq(list[idx].n, ist("keep-alive")) ||
788 isteq(list[idx].n, ist("upgrade")) ||
789 isteq(list[idx].n, ist("transfer-encoding")))
790 goto fail;
791
792 if (!htx_add_header(htx, list[idx].n, list[idx].v))
793 goto fail;
794 }
795
796 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
797 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
798 goto fail;
799
800 /* Let's dump the request now if not yet emitted. */
801 if (!(fields & H2_PHDR_FND_NONE)) {
802 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
803 if (!sl)
804 goto fail;
805 }
806
807 /* update the start line with last detected header info */
808 sl->flags |= sl_flags;
809
810 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
811 /* FIXME: Do we need to signal anything when we have a body and
812 * no content-length, to have the equivalent of H1's chunked
813 * encoding?
814 */
815 }
816
817 /* now send the end of headers marker */
818 htx_add_endof(htx, HTX_BLK_EOH);
819
820 ret = 1;
821 return ret;
822
823 fail:
824 return -1;
825}