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