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 | |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 28 | #include <inttypes.h> |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 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 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 47 | /* Parse the Content-Length header field of an HTTP/2 request. The function |
| 48 | * checks all possible occurrences of a comma-delimited value, and verifies |
| 49 | * if any of them doesn't match a previous value. It returns <0 if a value |
| 50 | * differs, 0 if the whole header can be dropped (i.e. already known), or >0 |
| 51 | * if the value can be indexed (first one). In the last case, the value might |
| 52 | * be adjusted and the caller must only add the updated value. |
| 53 | */ |
| 54 | int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len) |
| 55 | { |
| 56 | char *e, *n; |
| 57 | unsigned long long cl; |
| 58 | int not_first = !!(*msgf & H2_MSGF_BODY_CL); |
| 59 | struct ist word; |
| 60 | |
| 61 | word.ptr = value->ptr - 1; // -1 for next loop's pre-increment |
| 62 | e = value->ptr + value->len; |
| 63 | |
| 64 | while (++word.ptr < e) { |
| 65 | /* skip leading delimitor and blanks */ |
| 66 | if (unlikely(HTTP_IS_LWS(*word.ptr))) |
| 67 | continue; |
| 68 | |
| 69 | /* digits only now */ |
| 70 | for (cl = 0, n = word.ptr; n < e; n++) { |
| 71 | unsigned int c = *n - '0'; |
| 72 | if (unlikely(c > 9)) { |
| 73 | /* non-digit */ |
| 74 | if (unlikely(n == word.ptr)) // spaces only |
| 75 | goto fail; |
| 76 | break; |
| 77 | } |
| 78 | if (unlikely(cl > ULLONG_MAX / 10ULL)) |
| 79 | goto fail; /* multiply overflow */ |
| 80 | cl = cl * 10ULL; |
| 81 | if (unlikely(cl + c < cl)) |
| 82 | goto fail; /* addition overflow */ |
| 83 | cl = cl + c; |
| 84 | } |
| 85 | |
| 86 | /* keep a copy of the exact cleaned value */ |
| 87 | word.len = n - word.ptr; |
| 88 | |
| 89 | /* skip trailing LWS till next comma or EOL */ |
| 90 | for (; n < e; n++) { |
| 91 | if (!HTTP_IS_LWS(*n)) { |
| 92 | if (unlikely(*n != ',')) |
| 93 | goto fail; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* if duplicate, must be equal */ |
| 99 | if (*msgf & H2_MSGF_BODY_CL && cl != *body_len) |
| 100 | goto fail; |
| 101 | |
| 102 | /* OK, store this result as the one to be indexed */ |
| 103 | *msgf |= H2_MSGF_BODY_CL; |
| 104 | *body_len = cl; |
| 105 | *value = word; |
| 106 | word.ptr = n; |
| 107 | } |
| 108 | /* here we've reached the end with a single value or a series of |
| 109 | * identical values, all matching previous series if any. The last |
| 110 | * parsed value was sent back into <value>. We just have to decide |
| 111 | * if this occurrence has to be indexed (it's the first one) or |
| 112 | * silently skipped (it's not the first one) |
| 113 | */ |
| 114 | return !not_first; |
| 115 | fail: |
| 116 | return -1; |
| 117 | } |
| 118 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 119 | /* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>. |
| 120 | * <fields> indicates what was found so far. This should be called once at the |
| 121 | * detection of the first general header field or at the end of the request if |
| 122 | * no general header field was found yet. Returns the created start line on |
| 123 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 124 | * H2_MSGF_* flags indicating what was found while parsing. |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 125 | * |
| 126 | * The rules below deserve a bit of explanation. There tends to be some |
| 127 | * confusion regarding H2's authority vs the Host header. They are different |
| 128 | * though may sometimes be exchanged. In H2, the request line is broken into : |
| 129 | * - :method |
| 130 | * - :scheme |
| 131 | * - :authority |
| 132 | * - :path |
| 133 | * |
| 134 | * An equivalent HTTP/1.x absolute-form request would then look like : |
| 135 | * <:method> <:scheme>://<:authority><:path> HTTP/x.y |
| 136 | * |
| 137 | * Except for CONNECT which doesn't have scheme nor path and looks like : |
| 138 | * <:method> <:authority> HTTP/x.y |
| 139 | * |
| 140 | * It's worth noting that H2 still supports an encoding to map H1 origin-form |
| 141 | * and asterisk-form requests. These ones do not specify the authority. However |
| 142 | * in H2 they must still specify the scheme, which is not present in H1. Also, |
| 143 | * when encoding an absolute-form H1 request without a path, the path |
| 144 | * automatically becomes "/" except for the OPTIONS method where it |
| 145 | * becomes "*". |
| 146 | * |
| 147 | * As such it is explicitly permitted for an H2 client to send a request |
| 148 | * featuring a Host header and no :authority, though it's not the recommended |
| 149 | * way to use H2 for a client. It is however the only permitted way to encode |
| 150 | * an origin-form H1 request over H2. Thus we need to respect such differences |
| 151 | * as much as possible when re-encoding the H2 request into HTX. |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 152 | */ |
| 153 | static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 154 | { |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 155 | int uri_idx; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 156 | unsigned int flags = HTX_SL_F_NONE; |
| 157 | struct htx_sl *sl; |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 158 | size_t i; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 159 | |
| 160 | if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) { |
| 161 | /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path" |
| 162 | * MUST be omitted ; ":authority" contains the host and port |
| 163 | * to connect to. |
| 164 | */ |
| 165 | if (fields & H2_PHDR_FND_SCHM) { |
| 166 | /* scheme not allowed */ |
| 167 | goto fail; |
| 168 | } |
| 169 | else if (fields & H2_PHDR_FND_PATH) { |
| 170 | /* path not allowed */ |
| 171 | goto fail; |
| 172 | } |
| 173 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 174 | /* missing authority */ |
| 175 | goto fail; |
| 176 | } |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 177 | // otherwise OK ; the URI is only made of the authority here |
| 178 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 179 | uri_idx = H2_PHDR_IDX_AUTH; |
| 180 | *msgf |= H2_MSGF_BODY_TUNNEL; |
| 181 | } |
| 182 | else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) != |
| 183 | (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) { |
| 184 | /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one |
| 185 | * valid value for the ":method", ":scheme" and ":path" phdr |
| 186 | * unless it is a CONNECT request. |
| 187 | */ |
| 188 | if (!(fields & H2_PHDR_FND_METH)) { |
| 189 | /* missing method */ |
| 190 | goto fail; |
| 191 | } |
| 192 | else if (!(fields & H2_PHDR_FND_SCHM)) { |
| 193 | /* missing scheme */ |
| 194 | goto fail; |
| 195 | } |
| 196 | else { |
| 197 | /* missing path */ |
| 198 | goto fail; |
| 199 | } |
| 200 | } |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 201 | else { /* regular methods */ |
| 202 | /* origin-form requests are made only of the path */ |
| 203 | uri_idx = H2_PHDR_IDX_PATH; |
| 204 | } |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 205 | |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 206 | /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states |
| 207 | * that :path must not be empty. |
| 208 | */ |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 209 | if (!phdr[uri_idx].len) |
| 210 | goto fail; |
| 211 | |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 212 | /* The final URI must not contain LWS nor CTL characters */ |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 213 | for (i = 0; i < phdr[uri_idx].len; i++) { |
| 214 | unsigned char c = phdr[uri_idx].ptr[i]; |
| 215 | if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c)) |
| 216 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 217 | } |
| 218 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 219 | /* Set HTX start-line flags */ |
| 220 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 221 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 222 | |
| 223 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0")); |
| 224 | if (!sl) |
| 225 | goto fail; |
| 226 | |
| 227 | sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len); |
Christopher Faulet | a9a5c04 | 2019-06-14 10:25:47 +0200 | [diff] [blame] | 228 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 229 | sl->flags |= (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")) ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 230 | return sl; |
| 231 | fail: |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 236 | * being <NULL,0> and emits the equivalent HTX request according to the rules |
| 237 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 238 | * non-zero is returned if some bytes were emitted. In case of error, a |
| 239 | * negative error code is returned. |
| 240 | * |
| 241 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 242 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 243 | * if a body is detected (!ES). |
| 244 | * |
| 245 | * The headers list <list> must be composed of : |
| 246 | * - n.name != NULL, n.len > 0 : literal header name |
| 247 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 248 | * among H2_PHDR_IDX_* |
| 249 | * - n.name ignored, n.len == 0 : end of list |
| 250 | * - in all cases except the end of list, v.name and v.len must designate a |
| 251 | * valid value. |
| 252 | * |
| 253 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 254 | * will be used to create a linked list, so its contents may be destroyed. |
| 255 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 256 | 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] | 257 | { |
| 258 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 259 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 260 | uint32_t idx; |
| 261 | int ck, lck; /* cookie index and last cookie index */ |
| 262 | int phdr; |
| 263 | int ret; |
| 264 | int i; |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 265 | uint32_t used = htx_used_space(htx); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 266 | struct htx_sl *sl = NULL; |
| 267 | unsigned int sl_flags = 0; |
| 268 | |
| 269 | lck = ck = -1; // no cookie for now |
| 270 | fields = 0; |
| 271 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 272 | if (!list[idx].n.ptr) { |
| 273 | /* this is an indexed pseudo-header */ |
| 274 | phdr = list[idx].n.len; |
| 275 | } |
| 276 | else { |
| 277 | /* this can be any type of header */ |
| 278 | /* RFC7540#8.1.2: upper case not allowed in header field names */ |
| 279 | for (i = 0; i < list[idx].n.len; i++) |
| 280 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A') |
| 281 | goto fail; |
| 282 | |
| 283 | phdr = h2_str_to_phdr(list[idx].n); |
| 284 | } |
| 285 | |
| 286 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 287 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 288 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 289 | if (fields & H2_PHDR_FND_NONE) { |
| 290 | /* pseudo header field after regular headers */ |
| 291 | goto fail; |
| 292 | } |
| 293 | else { |
| 294 | /* repeated pseudo header field */ |
| 295 | goto fail; |
| 296 | } |
| 297 | } |
| 298 | fields |= 1 << phdr; |
| 299 | phdr_val[phdr] = list[idx].v; |
| 300 | continue; |
| 301 | } |
| 302 | else if (phdr != 0) { |
| 303 | /* invalid pseudo header -- should never happen here */ |
| 304 | goto fail; |
| 305 | } |
| 306 | |
| 307 | /* regular header field in (name,value) */ |
| 308 | if (unlikely(!(fields & H2_PHDR_FND_NONE))) { |
| 309 | /* no more pseudo-headers, time to build the request line */ |
| 310 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 311 | if (!sl) |
| 312 | goto fail; |
| 313 | fields |= H2_PHDR_FND_NONE; |
| 314 | } |
| 315 | |
| 316 | if (isteq(list[idx].n, ist("host"))) |
| 317 | fields |= H2_PHDR_FND_HOST; |
| 318 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 319 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 320 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 321 | if (ret < 0) |
| 322 | goto fail; |
| 323 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 324 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 325 | if (ret == 0) |
| 326 | continue; // skip this duplicate |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* these ones are forbidden in requests (RFC7540#8.1.2.2) */ |
| 330 | if (isteq(list[idx].n, ist("connection")) || |
| 331 | isteq(list[idx].n, ist("proxy-connection")) || |
| 332 | isteq(list[idx].n, ist("keep-alive")) || |
| 333 | isteq(list[idx].n, ist("upgrade")) || |
| 334 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 335 | goto fail; |
| 336 | |
| 337 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 338 | goto fail; |
| 339 | |
| 340 | /* cookie requires special processing at the end */ |
| 341 | if (isteq(list[idx].n, ist("cookie"))) { |
| 342 | list[idx].n.len = -1; |
| 343 | |
| 344 | if (ck < 0) |
| 345 | ck = idx; |
| 346 | else |
| 347 | list[lck].n.len = idx; |
| 348 | |
| 349 | lck = idx; |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 354 | goto fail; |
| 355 | } |
| 356 | |
| 357 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 358 | if (fields & H2_PHDR_FND_STAT) |
| 359 | goto fail; |
| 360 | |
| 361 | /* Let's dump the request now if not yet emitted. */ |
| 362 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 363 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 364 | if (!sl) |
| 365 | goto fail; |
| 366 | } |
| 367 | |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 368 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0)) |
| 369 | sl_flags |= HTX_SL_F_BODYLESS; |
| 370 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 371 | /* update the start line with last detected header info */ |
| 372 | sl->flags |= sl_flags; |
| 373 | |
| 374 | /* complete with missing Host if needed */ |
| 375 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 376 | /* missing Host field, use :authority instead */ |
| 377 | if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH])) |
| 378 | goto fail; |
| 379 | } |
| 380 | |
| 381 | /* now we may have to build a cookie list. We'll dump the values of all |
| 382 | * visited headers. |
| 383 | */ |
| 384 | if (ck >= 0) { |
| 385 | uint32_t fs; // free space |
| 386 | uint32_t bs; // block size |
| 387 | uint32_t vl; // value len |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 388 | uint32_t tl; // total length |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 389 | struct htx_blk *blk; |
| 390 | |
| 391 | blk = htx_add_header(htx, ist("cookie"), list[ck].v); |
| 392 | if (!blk) |
| 393 | goto fail; |
| 394 | |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 395 | tl = list[ck].v.len; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 396 | fs = htx_free_data_space(htx); |
| 397 | bs = htx_get_blksz(blk); |
| 398 | |
| 399 | /* for each extra cookie, we'll extend the cookie's value and |
| 400 | * insert "; " before the new value. |
| 401 | */ |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 402 | fs += tl; // first one is already counted |
| 403 | for (; (ck = list[ck].n.len) >= 0 ; ) { |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 404 | vl = list[ck].v.len; |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 405 | tl += vl + 2; |
| 406 | if (tl > fs) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 407 | goto fail; |
| 408 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 409 | htx_change_blk_value_len(htx, blk, tl); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 410 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';'; |
| 411 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' '; |
| 412 | memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl); |
| 413 | bs += vl + 2; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | } |
| 417 | |
| 418 | /* now send the end of headers marker */ |
| 419 | htx_add_endof(htx, HTX_BLK_EOH); |
| 420 | |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 421 | /* Set bytes used in the HTX mesage for the headers now */ |
| 422 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 423 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 424 | ret = 1; |
| 425 | return ret; |
| 426 | |
| 427 | fail: |
| 428 | return -1; |
| 429 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 430 | |
| 431 | /* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>. |
| 432 | * <fields> indicates what was found so far. This should be called once at the |
| 433 | * detection of the first general header field or at the end of the message if |
| 434 | * no general header field was found yet. Returns the created start line on |
| 435 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 436 | * H2_MSGF_* flags indicating what was found while parsing. |
| 437 | */ |
| 438 | static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 439 | { |
| 440 | unsigned int flags = HTX_SL_F_NONE; |
| 441 | struct htx_sl *sl; |
| 442 | unsigned char h, t, u; |
| 443 | |
| 444 | /* only :status is allowed as a pseudo header */ |
| 445 | if (!(fields & H2_PHDR_FND_STAT)) |
| 446 | goto fail; |
| 447 | |
| 448 | if (phdr[H2_PHDR_IDX_STAT].len != 3) |
| 449 | goto fail; |
| 450 | |
| 451 | /* Set HTX start-line flags */ |
| 452 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 453 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 454 | |
| 455 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist("")); |
| 456 | if (!sl) |
| 457 | goto fail; |
| 458 | |
| 459 | h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0'; |
| 460 | t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0'; |
| 461 | u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0'; |
| 462 | if (h > 9 || t > 9 || u > 9) |
| 463 | goto fail; |
| 464 | |
| 465 | sl->info.res.status = h * 100 + t * 10 + u; |
| 466 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 467 | /* On 1xx responses (except 101) there is no ES on the HEADERS frame but |
| 468 | * there is no body. So remove the flag H2_MSGF_BODY and add |
| 469 | * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is |
| 470 | * expected. |
| 471 | */ |
| 472 | if (sl->info.res.status < 200 && |
| 473 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) { |
| 474 | *msgf |= H2_MSGF_RSP_1XX; |
| 475 | *msgf &= ~H2_MSGF_BODY; |
| 476 | } |
| 477 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 478 | return sl; |
| 479 | fail: |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | /* Takes an H2 response present in the headers list <list> terminated by a name |
| 484 | * being <NULL,0> and emits the equivalent HTX response according to the rules |
| 485 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 486 | * a positive value is returned if some bytes were emitted. In case of error, a |
| 487 | * negative error code is returned. |
| 488 | * |
| 489 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 490 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 491 | * if a body is detected (!ES). |
| 492 | * |
| 493 | * The headers list <list> must be composed of : |
| 494 | * - n.name != NULL, n.len > 0 : literal header name |
| 495 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 496 | * among H2_PHDR_IDX_* |
| 497 | * - n.name ignored, n.len == 0 : end of list |
| 498 | * - in all cases except the end of list, v.name and v.len must designate a |
| 499 | * valid value. |
| 500 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 501 | 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] | 502 | { |
| 503 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 504 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 505 | uint32_t idx; |
| 506 | int phdr; |
| 507 | int ret; |
| 508 | int i; |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 509 | uint32_t used = htx_used_space(htx); |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 510 | struct htx_sl *sl = NULL; |
| 511 | unsigned int sl_flags = 0; |
| 512 | |
| 513 | fields = 0; |
| 514 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 515 | if (!list[idx].n.ptr) { |
| 516 | /* this is an indexed pseudo-header */ |
| 517 | phdr = list[idx].n.len; |
| 518 | } |
| 519 | else { |
| 520 | /* this can be any type of header */ |
| 521 | /* RFC7540#8.1.2: upper case not allowed in header field names */ |
| 522 | for (i = 0; i < list[idx].n.len; i++) |
| 523 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A') |
| 524 | goto fail; |
| 525 | |
| 526 | phdr = h2_str_to_phdr(list[idx].n); |
| 527 | } |
| 528 | |
| 529 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 530 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 531 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 532 | if (fields & H2_PHDR_FND_NONE) { |
| 533 | /* pseudo header field after regular headers */ |
| 534 | goto fail; |
| 535 | } |
| 536 | else { |
| 537 | /* repeated pseudo header field */ |
| 538 | goto fail; |
| 539 | } |
| 540 | } |
| 541 | fields |= 1 << phdr; |
| 542 | phdr_val[phdr] = list[idx].v; |
| 543 | continue; |
| 544 | } |
| 545 | else if (phdr != 0) { |
| 546 | /* invalid pseudo header -- should never happen here */ |
| 547 | goto fail; |
| 548 | } |
| 549 | |
| 550 | /* regular header field in (name,value) */ |
| 551 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 552 | /* no more pseudo-headers, time to build the status line */ |
| 553 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 554 | if (!sl) |
| 555 | goto fail; |
| 556 | fields |= H2_PHDR_FND_NONE; |
| 557 | } |
| 558 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 559 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 560 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 561 | if (ret < 0) |
| 562 | goto fail; |
| 563 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 564 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 565 | if (ret == 0) |
| 566 | continue; // skip this duplicate |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* these ones are forbidden in responses (RFC7540#8.1.2.2) */ |
| 570 | if (isteq(list[idx].n, ist("connection")) || |
| 571 | isteq(list[idx].n, ist("proxy-connection")) || |
| 572 | isteq(list[idx].n, ist("keep-alive")) || |
| 573 | isteq(list[idx].n, ist("upgrade")) || |
| 574 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 575 | goto fail; |
| 576 | |
| 577 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 578 | goto fail; |
| 579 | } |
| 580 | |
| 581 | /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */ |
| 582 | if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM)) |
| 583 | goto fail; |
| 584 | |
| 585 | /* Let's dump the request now if not yet emitted. */ |
| 586 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 587 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 588 | if (!sl) |
| 589 | goto fail; |
| 590 | } |
| 591 | |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 592 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0)) |
| 593 | sl_flags |= HTX_SL_F_BODYLESS; |
| 594 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 595 | /* update the start line with last detected header info */ |
| 596 | sl->flags |= sl_flags; |
| 597 | |
| 598 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 599 | /* FIXME: Do we need to signal anything when we have a body and |
| 600 | * no content-length, to have the equivalent of H1's chunked |
| 601 | * encoding? |
| 602 | */ |
| 603 | } |
| 604 | |
| 605 | /* now send the end of headers marker */ |
| 606 | htx_add_endof(htx, HTX_BLK_EOH); |
| 607 | |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 608 | /* Set bytes used in the HTX mesage for the headers now */ |
| 609 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 610 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 611 | ret = 1; |
| 612 | return ret; |
| 613 | |
| 614 | fail: |
| 615 | return -1; |
| 616 | } |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 617 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 618 | /* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits |
| 619 | * the equivalent HTX trailers blocks. The output contents are emitted in <htx>, |
| 620 | * and a positive value is returned if some bytes were emitted. In case of |
| 621 | * error, a negative error code is returned. The caller must have verified that |
| 622 | * the message in the buffer is compatible with receipt of trailers. |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 623 | * |
| 624 | * The headers list <list> must be composed of : |
| 625 | * - n.name != NULL, n.len > 0 : literal header name |
| 626 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 627 | * among H2_PHDR_IDX_* (illegal here) |
| 628 | * - n.name ignored, n.len == 0 : end of list |
| 629 | * - in all cases except the end of list, v.name and v.len must designate a |
| 630 | * valid value. |
| 631 | */ |
| 632 | int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx) |
| 633 | { |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 634 | uint32_t idx; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 635 | int i; |
| 636 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 637 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 638 | if (!list[idx].n.ptr) { |
| 639 | /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */ |
| 640 | goto fail; |
| 641 | } |
| 642 | |
| 643 | /* RFC7540#8.1.2: upper case not allowed in header field names */ |
| 644 | for (i = 0; i < list[idx].n.len; i++) |
| 645 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A') |
| 646 | goto fail; |
| 647 | |
| 648 | if (h2_str_to_phdr(list[idx].n) != 0) { |
| 649 | /* This is a pseudo-header (RFC7540#8.1.2.1) */ |
| 650 | goto fail; |
| 651 | } |
| 652 | |
| 653 | /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */ |
| 654 | if (isteq(list[idx].n, ist("host")) || |
| 655 | isteq(list[idx].n, ist("content-length")) || |
| 656 | isteq(list[idx].n, ist("connection")) || |
| 657 | isteq(list[idx].n, ist("proxy-connection")) || |
| 658 | isteq(list[idx].n, ist("keep-alive")) || |
| 659 | isteq(list[idx].n, ist("upgrade")) || |
| 660 | isteq(list[idx].n, ist("te")) || |
| 661 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 662 | goto fail; |
| 663 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 664 | if (!htx_add_trailer(htx, list[idx].n, list[idx].v)) |
| 665 | goto fail; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 668 | if (!htx_add_endof(htx, HTX_BLK_EOT)) |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 669 | goto fail; |
| 670 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 671 | return 1; |
| 672 | |
| 673 | fail: |
| 674 | return -1; |
| 675 | } |