blob: 1b784fd4aba2dbad91db156612243e93aa34e5f3 [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
323/* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>.
324 * <fields> indicates what was found so far. This should be called once at the
325 * detection of the first general header field or at the end of the request if
326 * no general header field was found yet. Returns the created start line on
327 * success, or NULL on failure. Upon success, <msgf> is updated with a few
328 * H2_MSGF_* flags indicating what was found while parsing.
329 */
330static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
331{
332 int uri_idx = H2_PHDR_IDX_PATH;
333 unsigned int flags = HTX_SL_F_NONE;
334 struct htx_sl *sl;
335
336 if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) {
337 /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path"
338 * MUST be omitted ; ":authority" contains the host and port
339 * to connect to.
340 */
341 if (fields & H2_PHDR_FND_SCHM) {
342 /* scheme not allowed */
343 goto fail;
344 }
345 else if (fields & H2_PHDR_FND_PATH) {
346 /* path not allowed */
347 goto fail;
348 }
349 else if (!(fields & H2_PHDR_FND_AUTH)) {
350 /* missing authority */
351 goto fail;
352 }
353 // otherwise OK ; let's use the authority instead of the URI
354 uri_idx = H2_PHDR_IDX_AUTH;
355 *msgf |= H2_MSGF_BODY_TUNNEL;
356 }
357 else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) !=
358 (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) {
359 /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one
360 * valid value for the ":method", ":scheme" and ":path" phdr
361 * unless it is a CONNECT request.
362 */
363 if (!(fields & H2_PHDR_FND_METH)) {
364 /* missing method */
365 goto fail;
366 }
367 else if (!(fields & H2_PHDR_FND_SCHM)) {
368 /* missing scheme */
369 goto fail;
370 }
371 else {
372 /* missing path */
373 goto fail;
374 }
375 }
376
377 /* 7540#8.1.2.3: :path must not be empty */
378 if (!phdr[uri_idx].len)
379 goto fail;
380
381 /* Set HTX start-line flags */
382 flags |= HTX_SL_F_VER_11; // V2 in fact
383 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
384
385 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0"));
386 if (!sl)
387 goto fail;
388
389 sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len);
390 return sl;
391 fail:
392 return NULL;
393}
394
395/* Takes an H2 request present in the headers list <list> terminated by a name
396 * being <NULL,0> and emits the equivalent HTX request according to the rules
397 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
398 * non-zero is returned if some bytes were emitted. In case of error, a
399 * negative error code is returned.
400 *
401 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
402 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
403 * if a body is detected (!ES).
404 *
405 * The headers list <list> must be composed of :
406 * - n.name != NULL, n.len > 0 : literal header name
407 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
408 * among H2_PHDR_IDX_*
409 * - n.name ignored, n.len == 0 : end of list
410 * - in all cases except the end of list, v.name and v.len must designate a
411 * valid value.
412 *
413 * The Cookie header will be reassembled at the end, and for this, the <list>
414 * will be used to create a linked list, so its contents may be destroyed.
415 */
416int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
417{
418 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
419 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
420 uint32_t idx;
421 int ck, lck; /* cookie index and last cookie index */
422 int phdr;
423 int ret;
424 int i;
425 struct htx_sl *sl = NULL;
426 unsigned int sl_flags = 0;
427
428 lck = ck = -1; // no cookie for now
429 fields = 0;
430 for (idx = 0; list[idx].n.len != 0; idx++) {
431 if (!list[idx].n.ptr) {
432 /* this is an indexed pseudo-header */
433 phdr = list[idx].n.len;
434 }
435 else {
436 /* this can be any type of header */
437 /* RFC7540#8.1.2: upper case not allowed in header field names */
438 for (i = 0; i < list[idx].n.len; i++)
439 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
440 goto fail;
441
442 phdr = h2_str_to_phdr(list[idx].n);
443 }
444
445 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
446 /* insert a pseudo header by its index (in phdr) and value (in value) */
447 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
448 if (fields & H2_PHDR_FND_NONE) {
449 /* pseudo header field after regular headers */
450 goto fail;
451 }
452 else {
453 /* repeated pseudo header field */
454 goto fail;
455 }
456 }
457 fields |= 1 << phdr;
458 phdr_val[phdr] = list[idx].v;
459 continue;
460 }
461 else if (phdr != 0) {
462 /* invalid pseudo header -- should never happen here */
463 goto fail;
464 }
465
466 /* regular header field in (name,value) */
467 if (unlikely(!(fields & H2_PHDR_FND_NONE))) {
468 /* no more pseudo-headers, time to build the request line */
469 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
470 if (!sl)
471 goto fail;
472 fields |= H2_PHDR_FND_NONE;
473 }
474
475 if (isteq(list[idx].n, ist("host")))
476 fields |= H2_PHDR_FND_HOST;
477
478 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY &&
479 isteq(list[idx].n, ist("content-length"))) {
480 *msgf |= H2_MSGF_BODY_CL;
481 sl_flags |= HTX_SL_F_CLEN;
482 }
483
484 /* these ones are forbidden in requests (RFC7540#8.1.2.2) */
485 if (isteq(list[idx].n, ist("connection")) ||
486 isteq(list[idx].n, ist("proxy-connection")) ||
487 isteq(list[idx].n, ist("keep-alive")) ||
488 isteq(list[idx].n, ist("upgrade")) ||
489 isteq(list[idx].n, ist("transfer-encoding")))
490 goto fail;
491
492 if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers")))
493 goto fail;
494
495 /* cookie requires special processing at the end */
496 if (isteq(list[idx].n, ist("cookie"))) {
497 list[idx].n.len = -1;
498
499 if (ck < 0)
500 ck = idx;
501 else
502 list[lck].n.len = idx;
503
504 lck = idx;
505 continue;
506 }
507
508 if (!htx_add_header(htx, list[idx].n, list[idx].v))
509 goto fail;
510 }
511
512 /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */
513 if (fields & H2_PHDR_FND_STAT)
514 goto fail;
515
516 /* Let's dump the request now if not yet emitted. */
517 if (!(fields & H2_PHDR_FND_NONE)) {
518 sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf);
519 if (!sl)
520 goto fail;
521 }
522
523 /* update the start line with last detected header info */
524 sl->flags |= sl_flags;
525
526 /* complete with missing Host if needed */
527 if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) {
528 /* missing Host field, use :authority instead */
529 if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH]))
530 goto fail;
531 }
532
533 /* now we may have to build a cookie list. We'll dump the values of all
534 * visited headers.
535 */
536 if (ck >= 0) {
537 uint32_t fs; // free space
538 uint32_t bs; // block size
539 uint32_t vl; // value len
540 struct htx_blk *blk;
541
542 blk = htx_add_header(htx, ist("cookie"), list[ck].v);
543 if (!blk)
544 goto fail;
545
546 fs = htx_free_data_space(htx);
547 bs = htx_get_blksz(blk);
548
549 /* for each extra cookie, we'll extend the cookie's value and
550 * insert "; " before the new value.
551 */
552 for ( ; (ck = list[ck].n.len) >= 0 ; ) {
553 vl = list[ck].v.len;
554 if (vl + 2 > fs)
555 goto fail;
556
557 htx_set_blk_value_len(blk, bs + 2 + vl);
558 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';';
559 *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' ';
560 memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl);
561 bs += vl + 2;
562 fs -= vl + 2;
563 }
564
565 }
566
567 /* now send the end of headers marker */
568 htx_add_endof(htx, HTX_BLK_EOH);
569
570 ret = 1;
571 return ret;
572
573 fail:
574 return -1;
575}
Willy Tarreau1329b5b2018-10-08 14:49:20 +0200576
577/* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>.
578 * <fields> indicates what was found so far. This should be called once at the
579 * detection of the first general header field or at the end of the message if
580 * no general header field was found yet. Returns the created start line on
581 * success, or NULL on failure. Upon success, <msgf> is updated with a few
582 * H2_MSGF_* flags indicating what was found while parsing.
583 */
584static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf)
585{
586 unsigned int flags = HTX_SL_F_NONE;
587 struct htx_sl *sl;
588 unsigned char h, t, u;
589
590 /* only :status is allowed as a pseudo header */
591 if (!(fields & H2_PHDR_FND_STAT))
592 goto fail;
593
594 if (phdr[H2_PHDR_IDX_STAT].len != 3)
595 goto fail;
596
597 /* Set HTX start-line flags */
598 flags |= HTX_SL_F_VER_11; // V2 in fact
599 flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2
600
601 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist(""));
602 if (!sl)
603 goto fail;
604
605 h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0';
606 t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0';
607 u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0';
608 if (h > 9 || t > 9 || u > 9)
609 goto fail;
610
611 sl->info.res.status = h * 100 + t * 10 + u;
612
613 return sl;
614 fail:
615 return NULL;
616}
617
618/* Takes an H2 response present in the headers list <list> terminated by a name
619 * being <NULL,0> and emits the equivalent HTX response according to the rules
620 * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and
621 * a positive value is returned if some bytes were emitted. In case of error, a
622 * negative error code is returned.
623 *
624 * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what
625 * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY
626 * if a body is detected (!ES).
627 *
628 * The headers list <list> must be composed of :
629 * - n.name != NULL, n.len > 0 : literal header name
630 * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len>
631 * among H2_PHDR_IDX_*
632 * - n.name ignored, n.len == 0 : end of list
633 * - in all cases except the end of list, v.name and v.len must designate a
634 * valid value.
635 */
636int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf)
637{
638 struct ist phdr_val[H2_PHDR_NUM_ENTRIES];
639 uint32_t fields; /* bit mask of H2_PHDR_FND_* */
640 uint32_t idx;
641 int phdr;
642 int ret;
643 int i;
644 struct htx_sl *sl = NULL;
645 unsigned int sl_flags = 0;
646
647 fields = 0;
648 for (idx = 0; list[idx].n.len != 0; idx++) {
649 if (!list[idx].n.ptr) {
650 /* this is an indexed pseudo-header */
651 phdr = list[idx].n.len;
652 }
653 else {
654 /* this can be any type of header */
655 /* RFC7540#8.1.2: upper case not allowed in header field names */
656 for (i = 0; i < list[idx].n.len; i++)
657 if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A')
658 goto fail;
659
660 phdr = h2_str_to_phdr(list[idx].n);
661 }
662
663 if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) {
664 /* insert a pseudo header by its index (in phdr) and value (in value) */
665 if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) {
666 if (fields & H2_PHDR_FND_NONE) {
667 /* pseudo header field after regular headers */
668 goto fail;
669 }
670 else {
671 /* repeated pseudo header field */
672 goto fail;
673 }
674 }
675 fields |= 1 << phdr;
676 phdr_val[phdr] = list[idx].v;
677 continue;
678 }
679 else if (phdr != 0) {
680 /* invalid pseudo header -- should never happen here */
681 goto fail;
682 }
683
684 /* regular header field in (name,value) */
685 if (!(fields & H2_PHDR_FND_NONE)) {
686 /* no more pseudo-headers, time to build the status line */
687 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
688 if (!sl)
689 goto fail;
690 fields |= H2_PHDR_FND_NONE;
691 }
692
693 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY &&
694 isteq(list[idx].n, ist("content-length"))) {
695 *msgf |= H2_MSGF_BODY_CL;
696 sl_flags |= HTX_SL_F_CLEN;
697 }
698
699 /* these ones are forbidden in responses (RFC7540#8.1.2.2) */
700 if (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("transfer-encoding")))
705 goto fail;
706
707 if (!htx_add_header(htx, list[idx].n, list[idx].v))
708 goto fail;
709 }
710
711 /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */
712 if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM))
713 goto fail;
714
715 /* Let's dump the request now if not yet emitted. */
716 if (!(fields & H2_PHDR_FND_NONE)) {
717 sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf);
718 if (!sl)
719 goto fail;
720 }
721
722 /* update the start line with last detected header info */
723 sl->flags |= sl_flags;
724
725 if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) {
726 /* FIXME: Do we need to signal anything when we have a body and
727 * no content-length, to have the equivalent of H1's chunked
728 * encoding?
729 */
730 }
731
732 /* now send the end of headers marker */
733 htx_add_endof(htx, HTX_BLK_EOH);
734
735 ret = 1;
736 return ret;
737
738 fail:
739 return -1;
740}