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 | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 29 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/global.h> |
Willy Tarreau | bf07314 | 2020-06-03 12:04:01 +0200 | [diff] [blame] | 31 | #include <haproxy/h2.h> |
Willy Tarreau | 0017be0 | 2020-06-02 19:25:28 +0200 | [diff] [blame] | 32 | #include <haproxy/http-hdr-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/http.h> |
Amaury Denoyelle | 0353410 | 2021-07-07 10:49:28 +0200 | [diff] [blame] | 34 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 35 | #include <haproxy/htx.h> |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 36 | #include <import/ist.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 38 | |
Willy Tarreau | 9c84d82 | 2019-01-30 15:09:21 +0100 | [diff] [blame] | 39 | struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = { |
| 40 | [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 41 | [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, }, |
| 42 | [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, }, |
| 43 | [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, }, |
| 44 | [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 45 | [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, }, |
| 46 | [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, }, |
| 47 | [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, }, |
| 48 | [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, }, |
| 49 | [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 50 | }; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 52 | /* Looks into <ist> for forbidden characters for header values (0x00, 0x0A, |
| 53 | * 0x0D), starting at pointer <start> which must be within <ist>. Returns |
| 54 | * non-zero if such a character is found, 0 otherwise. When run on unlikely |
| 55 | * header match, it's recommended to first check for the presence of control |
| 56 | * chars using ist_find_ctl(). |
| 57 | */ |
| 58 | static int has_forbidden_char(const struct ist ist, const char *start) |
| 59 | { |
| 60 | do { |
| 61 | if ((uint8_t)*start <= 0x0d && |
| 62 | (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0))) |
| 63 | return 1; |
| 64 | start++; |
| 65 | } while (start < ist.ptr + ist.len); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 69 | /* Parse the Content-Length header field of an HTTP/2 request. The function |
| 70 | * checks all possible occurrences of a comma-delimited value, and verifies |
| 71 | * if any of them doesn't match a previous value. It returns <0 if a value |
| 72 | * differs, 0 if the whole header can be dropped (i.e. already known), or >0 |
| 73 | * if the value can be indexed (first one). In the last case, the value might |
| 74 | * be adjusted and the caller must only add the updated value. |
| 75 | */ |
| 76 | int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len) |
| 77 | { |
| 78 | char *e, *n; |
| 79 | unsigned long long cl; |
| 80 | int not_first = !!(*msgf & H2_MSGF_BODY_CL); |
| 81 | struct ist word; |
| 82 | |
Willy Tarreau | ba9afd2 | 2023-08-09 08:32:48 +0200 | [diff] [blame] | 83 | word.ptr = value->ptr; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 84 | e = value->ptr + value->len; |
| 85 | |
Willy Tarreau | ba9afd2 | 2023-08-09 08:32:48 +0200 | [diff] [blame] | 86 | while (1) { |
| 87 | if (word.ptr >= e) { |
| 88 | /* empty header or empty value */ |
| 89 | goto fail; |
| 90 | } |
| 91 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 92 | /* skip leading delimiter and blanks */ |
Willy Tarreau | ba9afd2 | 2023-08-09 08:32:48 +0200 | [diff] [blame] | 93 | if (unlikely(HTTP_IS_LWS(*word.ptr))) { |
| 94 | word.ptr++; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 95 | continue; |
Willy Tarreau | ba9afd2 | 2023-08-09 08:32:48 +0200 | [diff] [blame] | 96 | } |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 97 | |
| 98 | /* digits only now */ |
| 99 | for (cl = 0, n = word.ptr; n < e; n++) { |
| 100 | unsigned int c = *n - '0'; |
| 101 | if (unlikely(c > 9)) { |
| 102 | /* non-digit */ |
| 103 | if (unlikely(n == word.ptr)) // spaces only |
| 104 | goto fail; |
| 105 | break; |
| 106 | } |
| 107 | if (unlikely(cl > ULLONG_MAX / 10ULL)) |
| 108 | goto fail; /* multiply overflow */ |
| 109 | cl = cl * 10ULL; |
| 110 | if (unlikely(cl + c < cl)) |
| 111 | goto fail; /* addition overflow */ |
| 112 | cl = cl + c; |
| 113 | } |
| 114 | |
| 115 | /* keep a copy of the exact cleaned value */ |
| 116 | word.len = n - word.ptr; |
| 117 | |
| 118 | /* skip trailing LWS till next comma or EOL */ |
| 119 | for (; n < e; n++) { |
| 120 | if (!HTTP_IS_LWS(*n)) { |
| 121 | if (unlikely(*n != ',')) |
| 122 | goto fail; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* if duplicate, must be equal */ |
| 128 | if (*msgf & H2_MSGF_BODY_CL && cl != *body_len) |
| 129 | goto fail; |
| 130 | |
| 131 | /* OK, store this result as the one to be indexed */ |
| 132 | *msgf |= H2_MSGF_BODY_CL; |
| 133 | *body_len = cl; |
| 134 | *value = word; |
Willy Tarreau | ba9afd2 | 2023-08-09 08:32:48 +0200 | [diff] [blame] | 135 | |
| 136 | /* Now either n==e and we're done, or n points to the comma, |
| 137 | * and we skip it and continue. |
| 138 | */ |
| 139 | if (n++ == e) |
| 140 | break; |
| 141 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 142 | word.ptr = n; |
| 143 | } |
| 144 | /* here we've reached the end with a single value or a series of |
| 145 | * identical values, all matching previous series if any. The last |
| 146 | * parsed value was sent back into <value>. We just have to decide |
| 147 | * if this occurrence has to be indexed (it's the first one) or |
| 148 | * silently skipped (it's not the first one) |
| 149 | */ |
| 150 | return !not_first; |
| 151 | fail: |
| 152 | return -1; |
| 153 | } |
| 154 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 155 | /* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>. |
| 156 | * <fields> indicates what was found so far. This should be called once at the |
| 157 | * detection of the first general header field or at the end of the request if |
| 158 | * no general header field was found yet. Returns the created start line on |
| 159 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 160 | * H2_MSGF_* flags indicating what was found while parsing. |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 161 | * |
| 162 | * The rules below deserve a bit of explanation. There tends to be some |
| 163 | * confusion regarding H2's authority vs the Host header. They are different |
| 164 | * though may sometimes be exchanged. In H2, the request line is broken into : |
| 165 | * - :method |
| 166 | * - :scheme |
| 167 | * - :authority |
| 168 | * - :path |
| 169 | * |
| 170 | * An equivalent HTTP/1.x absolute-form request would then look like : |
| 171 | * <:method> <:scheme>://<:authority><:path> HTTP/x.y |
| 172 | * |
| 173 | * Except for CONNECT which doesn't have scheme nor path and looks like : |
| 174 | * <:method> <:authority> HTTP/x.y |
| 175 | * |
| 176 | * It's worth noting that H2 still supports an encoding to map H1 origin-form |
| 177 | * and asterisk-form requests. These ones do not specify the authority. However |
| 178 | * in H2 they must still specify the scheme, which is not present in H1. Also, |
| 179 | * when encoding an absolute-form H1 request without a path, the path |
| 180 | * automatically becomes "/" except for the OPTIONS method where it |
| 181 | * becomes "*". |
| 182 | * |
| 183 | * As such it is explicitly permitted for an H2 client to send a request |
| 184 | * featuring a Host header and no :authority, though it's not the recommended |
| 185 | * way to use H2 for a client. It is however the only permitted way to encode |
| 186 | * an origin-form H1 request over H2. Thus we need to respect such differences |
| 187 | * as much as possible when re-encoding the H2 request into HTX. |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 188 | */ |
| 189 | static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 190 | { |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 191 | struct ist uri, meth_sl; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 192 | unsigned int flags = HTX_SL_F_NONE; |
| 193 | struct htx_sl *sl; |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 194 | size_t i; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 195 | |
| 196 | if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) { |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 197 | if (fields & H2_PHDR_FND_PROT) { |
| 198 | /* rfc 8441 Extended Connect Protocol |
| 199 | * #4 :scheme and :path must be present, as well as |
| 200 | * :authority like all h2 requests |
| 201 | */ |
| 202 | if (!(fields & H2_PHDR_FND_SCHM)) { |
| 203 | /* missing scheme */ |
| 204 | goto fail; |
| 205 | } |
| 206 | else if (!(fields & H2_PHDR_FND_PATH)) { |
| 207 | /* missing path */ |
| 208 | goto fail; |
| 209 | } |
| 210 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 211 | /* missing authority */ |
| 212 | goto fail; |
| 213 | } |
| 214 | |
| 215 | flags |= HTX_SL_F_HAS_SCHM; |
| 216 | if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http"))) |
| 217 | flags |= HTX_SL_F_SCHM_HTTP; |
| 218 | else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https"))) |
| 219 | flags |= HTX_SL_F_SCHM_HTTPS; |
Willy Tarreau | 9e0c2b5 | 2021-08-10 15:37:34 +0200 | [diff] [blame] | 220 | else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM])) |
| 221 | htx->flags |= HTX_FL_PARSING_ERROR; |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 222 | |
| 223 | meth_sl = ist("GET"); |
| 224 | |
| 225 | *msgf |= H2_MSGF_EXT_CONNECT; |
| 226 | /* no ES on the HEADERS frame but no body either for |
| 227 | * Extended CONNECT */ |
| 228 | *msgf &= ~H2_MSGF_BODY; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 229 | } |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 230 | else { |
| 231 | /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path" |
| 232 | * MUST be omitted ; ":authority" contains the host and port |
| 233 | * to connect to. |
| 234 | */ |
| 235 | if (fields & H2_PHDR_FND_SCHM) { |
| 236 | /* scheme not allowed */ |
| 237 | goto fail; |
| 238 | } |
| 239 | else if (fields & H2_PHDR_FND_PATH) { |
| 240 | /* path not allowed */ |
| 241 | goto fail; |
| 242 | } |
| 243 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 244 | /* missing authority */ |
| 245 | goto fail; |
| 246 | } |
| 247 | |
| 248 | meth_sl = phdr[H2_PHDR_IDX_METH]; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 249 | } |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 250 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 251 | *msgf |= H2_MSGF_BODY_TUNNEL; |
| 252 | } |
| 253 | else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) != |
| 254 | (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) { |
| 255 | /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one |
| 256 | * valid value for the ":method", ":scheme" and ":path" phdr |
| 257 | * unless it is a CONNECT request. |
| 258 | */ |
| 259 | if (!(fields & H2_PHDR_FND_METH)) { |
| 260 | /* missing method */ |
| 261 | goto fail; |
| 262 | } |
| 263 | else if (!(fields & H2_PHDR_FND_SCHM)) { |
| 264 | /* missing scheme */ |
| 265 | goto fail; |
| 266 | } |
| 267 | else { |
| 268 | /* missing path */ |
| 269 | goto fail; |
| 270 | } |
| 271 | } |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 272 | else { /* regular methods */ |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 273 | /* RFC3986#6.2.2.1: scheme is case-insensitive. We need to |
| 274 | * classify the scheme as "present/http", "present/https", |
| 275 | * "present/other", "absent" so as to decide whether or not |
| 276 | * we're facing a normalized URI that will have to be encoded |
| 277 | * in origin or absolute form. Indeed, 7540#8.1.2.3 says that |
| 278 | * clients should use the absolute form, thus we cannot infer |
| 279 | * whether or not the client wanted to use a proxy here. |
| 280 | */ |
| 281 | flags |= HTX_SL_F_HAS_SCHM; |
| 282 | if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http"))) |
| 283 | flags |= HTX_SL_F_SCHM_HTTP; |
| 284 | else if (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("https"))) |
| 285 | flags |= HTX_SL_F_SCHM_HTTPS; |
Willy Tarreau | 9e0c2b5 | 2021-08-10 15:37:34 +0200 | [diff] [blame] | 286 | else if (!http_validate_scheme(phdr[H2_PHDR_IDX_SCHM])) |
| 287 | htx->flags |= HTX_FL_PARSING_ERROR; |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 288 | |
| 289 | meth_sl = phdr[H2_PHDR_IDX_METH]; |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Willy Tarreau | 6c6b9a5 | 2021-08-10 16:30:55 +0200 | [diff] [blame] | 292 | if (fields & H2_PHDR_FND_PATH) { |
| 293 | /* 7540#8.1.2.3: :path must not be empty, and must be either |
| 294 | * '*' or an RFC3986 "path-absolute" starting with a "/" but |
| 295 | * not with "//". |
Willy Tarreau | 512cee8 | 2021-08-19 23:06:58 +0200 | [diff] [blame] | 296 | * However, this "path-absolute" was a mistake which was |
| 297 | * later fixed in http2bis as "absolute-path" to match |
| 298 | * HTTP/1, thus also allowing "//". |
Willy Tarreau | 6c6b9a5 | 2021-08-10 16:30:55 +0200 | [diff] [blame] | 299 | */ |
| 300 | if (unlikely(!phdr[H2_PHDR_IDX_PATH].len)) |
| 301 | goto fail; |
| 302 | else if (unlikely(phdr[H2_PHDR_IDX_PATH].ptr[0] != '/')) { |
| 303 | if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*"))) |
| 304 | goto fail; |
| 305 | } |
Willy Tarreau | 6c6b9a5 | 2021-08-10 16:30:55 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 308 | if (!(flags & HTX_SL_F_HAS_SCHM)) { |
| 309 | /* no scheme, use authority only (CONNECT) */ |
| 310 | uri = phdr[H2_PHDR_IDX_AUTH]; |
Willy Tarreau | 1440fe8 | 2019-10-08 17:34:50 +0200 | [diff] [blame] | 311 | flags |= HTX_SL_F_HAS_AUTHORITY; |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 312 | } |
Willy Tarreau | 30ee1ef | 2019-10-08 18:33:19 +0200 | [diff] [blame] | 313 | else if (fields & H2_PHDR_FND_AUTH) { |
| 314 | /* authority is present, let's use the absolute form. We simply |
| 315 | * use the trash to concatenate them since all of them MUST fit |
| 316 | * in a bufsize since it's where they come from. |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 317 | */ |
| 318 | uri = ist2bin(trash.area, phdr[H2_PHDR_IDX_SCHM]); |
| 319 | istcat(&uri, ist("://"), trash.size); |
| 320 | istcat(&uri, phdr[H2_PHDR_IDX_AUTH], trash.size); |
| 321 | if (!isteq(phdr[H2_PHDR_IDX_PATH], ist("*"))) |
| 322 | istcat(&uri, phdr[H2_PHDR_IDX_PATH], trash.size); |
Willy Tarreau | 1440fe8 | 2019-10-08 17:34:50 +0200 | [diff] [blame] | 323 | flags |= HTX_SL_F_HAS_AUTHORITY; |
Willy Tarreau | 30ee1ef | 2019-10-08 18:33:19 +0200 | [diff] [blame] | 324 | |
| 325 | if (flags & (HTX_SL_F_SCHM_HTTP|HTX_SL_F_SCHM_HTTPS)) { |
| 326 | /* we don't know if it was originally an absolute or a |
| 327 | * relative request because newer versions of HTTP use |
| 328 | * the absolute URI format by default, which we call |
| 329 | * the normalized URI format internally. This is the |
| 330 | * strongly recommended way of sending a request for |
| 331 | * a regular client, so we cannot distinguish this |
| 332 | * from a request intended for a proxy. For other |
| 333 | * schemes however there is no doubt. |
| 334 | */ |
| 335 | flags |= HTX_SL_F_NORMALIZED_URI; |
| 336 | } |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 337 | } |
| 338 | else { |
| 339 | /* usual schemes with or without authority, use origin form */ |
| 340 | uri = phdr[H2_PHDR_IDX_PATH]; |
Willy Tarreau | 1440fe8 | 2019-10-08 17:34:50 +0200 | [diff] [blame] | 341 | if (fields & H2_PHDR_FND_AUTH) |
| 342 | flags |= HTX_SL_F_HAS_AUTHORITY; |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 343 | } |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 344 | |
Willy Tarreau | e9f1f1e | 2021-08-11 11:12:46 +0200 | [diff] [blame] | 345 | /* The method is a non-empty token (RFC7231#4.1) */ |
| 346 | if (!meth_sl.len) |
| 347 | goto fail; |
| 348 | for (i = 0; i < meth_sl.len; i++) { |
| 349 | if (!HTTP_IS_TOKEN(meth_sl.ptr[i])) |
| 350 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 351 | } |
| 352 | |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 353 | /* make sure the final URI isn't empty. Note that 7540#8.1.2.3 states |
| 354 | * that :path must not be empty. |
| 355 | */ |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 356 | if (!uri.len) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 357 | goto fail; |
| 358 | |
Willy Tarreau | 2be362c | 2019-10-08 11:59:37 +0200 | [diff] [blame] | 359 | /* The final URI must not contain LWS nor CTL characters */ |
Willy Tarreau | 92919f7 | 2019-10-08 16:53:07 +0200 | [diff] [blame] | 360 | for (i = 0; i < uri.len; i++) { |
| 361 | unsigned char c = uri.ptr[i]; |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 362 | if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c)) |
| 363 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 364 | } |
| 365 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 366 | /* Set HTX start-line flags */ |
| 367 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 368 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 369 | |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 370 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, meth_sl, uri, ist("HTTP/2.0")); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 371 | if (!sl) |
| 372 | goto fail; |
| 373 | |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 374 | sl->info.req.meth = find_http_meth(meth_sl.ptr, meth_sl.len); |
Christopher Faulet | 7d247f0 | 2020-12-02 14:26:36 +0100 | [diff] [blame] | 375 | if (sl->info.req.meth == HTTP_METH_HEAD) |
| 376 | *msgf |= H2_MSGF_BODYLESS_RSP; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 377 | return sl; |
| 378 | fail: |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 383 | * being <NULL,0> and emits the equivalent HTX request according to the rules |
| 384 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 385 | * non-zero is returned if some bytes were emitted. In case of error, a |
| 386 | * negative error code is returned. |
| 387 | * |
| 388 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 389 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 390 | * if a body is detected (!ES). |
| 391 | * |
| 392 | * The headers list <list> must be composed of : |
| 393 | * - n.name != NULL, n.len > 0 : literal header name |
| 394 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 395 | * among H2_PHDR_IDX_* |
| 396 | * - n.name ignored, n.len == 0 : end of list |
| 397 | * - in all cases except the end of list, v.name and v.len must designate a |
| 398 | * valid value. |
| 399 | * |
| 400 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 401 | * will be used to create a linked list, so its contents may be destroyed. |
Willy Tarreau | f86e994 | 2023-08-08 15:38:28 +0200 | [diff] [blame] | 402 | * |
| 403 | * When <relaxed> is non-nul, some non-dangerous checks will be ignored. This |
| 404 | * is in order to satisfy "option accept-invalid-http-request" for |
| 405 | * interoperability purposes. |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 406 | */ |
Willy Tarreau | f86e994 | 2023-08-08 15:38:28 +0200 | [diff] [blame] | 407 | int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len, int relaxed) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 408 | { |
| 409 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 410 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 411 | uint32_t idx; |
| 412 | int ck, lck; /* cookie index and last cookie index */ |
| 413 | int phdr; |
| 414 | int ret; |
| 415 | int i; |
| 416 | struct htx_sl *sl = NULL; |
| 417 | unsigned int sl_flags = 0; |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 418 | const char *ctl; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 419 | |
| 420 | lck = ck = -1; // no cookie for now |
| 421 | fields = 0; |
| 422 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 423 | if (!list[idx].n.ptr) { |
| 424 | /* this is an indexed pseudo-header */ |
| 425 | phdr = list[idx].n.len; |
| 426 | } |
| 427 | else { |
| 428 | /* this can be any type of header */ |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 429 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 430 | * #10.3: header names must be valid (i.e. match a token). |
| 431 | * For pseudo-headers we check from 2nd char and for other ones |
| 432 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 433 | * the colon. |
| 434 | */ |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 435 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 436 | |
| 437 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 438 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 439 | goto fail; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 442 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
Willy Tarreau | af232e4 | 2023-08-08 15:40:49 +0200 | [diff] [blame] | 443 | * rejecting NUL, CR and LF characters. For :path we reject all CTL |
| 444 | * chars, spaces, and '#'. |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 445 | */ |
Willy Tarreau | af232e4 | 2023-08-08 15:40:49 +0200 | [diff] [blame] | 446 | if (phdr == H2_PHDR_IDX_PATH && !relaxed) { |
| 447 | ctl = ist_find_range(list[idx].v, 0, '#'); |
| 448 | if (unlikely(ctl) && http_path_has_forbidden_char(list[idx].v, ctl)) |
| 449 | goto fail; |
| 450 | } else { |
| 451 | ctl = ist_find_ctl(list[idx].v); |
| 452 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 453 | goto fail; |
| 454 | } |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 455 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 456 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 457 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 458 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 459 | if (fields & H2_PHDR_FND_NONE) { |
| 460 | /* pseudo header field after regular headers */ |
| 461 | goto fail; |
| 462 | } |
| 463 | else { |
| 464 | /* repeated pseudo header field */ |
| 465 | goto fail; |
| 466 | } |
| 467 | } |
| 468 | fields |= 1 << phdr; |
| 469 | phdr_val[phdr] = list[idx].v; |
| 470 | continue; |
| 471 | } |
| 472 | else if (phdr != 0) { |
| 473 | /* invalid pseudo header -- should never happen here */ |
| 474 | goto fail; |
| 475 | } |
| 476 | |
| 477 | /* regular header field in (name,value) */ |
| 478 | if (unlikely(!(fields & H2_PHDR_FND_NONE))) { |
| 479 | /* no more pseudo-headers, time to build the request line */ |
| 480 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 481 | if (!sl) |
| 482 | goto fail; |
| 483 | fields |= H2_PHDR_FND_NONE; |
Willy Tarreau | b4934f0 | 2021-08-11 15:39:13 +0200 | [diff] [blame] | 484 | |
| 485 | /* http2bis draft recommends to drop Host in favor of :authority when |
| 486 | * the latter is present. This is required to make sure there is no |
| 487 | * discrepancy between the authority and the host header, especially |
| 488 | * since routing rules usually involve Host. Here we already know if |
| 489 | * :authority was found so we can emit it right now and mark the host |
| 490 | * as filled so that it's skipped later. |
| 491 | */ |
| 492 | if (fields & H2_PHDR_FND_AUTH) { |
| 493 | if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH])) |
| 494 | goto fail; |
| 495 | fields |= H2_PHDR_FND_HOST; |
| 496 | } |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Willy Tarreau | b4934f0 | 2021-08-11 15:39:13 +0200 | [diff] [blame] | 499 | if (isteq(list[idx].n, ist("host"))) { |
| 500 | if (fields & H2_PHDR_FND_HOST) |
| 501 | continue; |
| 502 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 503 | fields |= H2_PHDR_FND_HOST; |
Willy Tarreau | b4934f0 | 2021-08-11 15:39:13 +0200 | [diff] [blame] | 504 | } |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 505 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 506 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 507 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 508 | if (ret < 0) |
| 509 | goto fail; |
| 510 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 511 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 512 | if (ret == 0) |
| 513 | continue; // skip this duplicate |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* these ones are forbidden in requests (RFC7540#8.1.2.2) */ |
| 517 | if (isteq(list[idx].n, ist("connection")) || |
| 518 | isteq(list[idx].n, ist("proxy-connection")) || |
| 519 | isteq(list[idx].n, ist("keep-alive")) || |
| 520 | isteq(list[idx].n, ist("upgrade")) || |
| 521 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 522 | goto fail; |
| 523 | |
| 524 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 525 | goto fail; |
| 526 | |
| 527 | /* cookie requires special processing at the end */ |
| 528 | if (isteq(list[idx].n, ist("cookie"))) { |
| 529 | list[idx].n.len = -1; |
| 530 | |
| 531 | if (ck < 0) |
| 532 | ck = idx; |
| 533 | else |
| 534 | list[lck].n.len = idx; |
| 535 | |
| 536 | lck = idx; |
| 537 | continue; |
| 538 | } |
| 539 | |
| 540 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 541 | goto fail; |
| 542 | } |
| 543 | |
| 544 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 545 | if (fields & H2_PHDR_FND_STAT) |
| 546 | goto fail; |
| 547 | |
| 548 | /* Let's dump the request now if not yet emitted. */ |
| 549 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 550 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 551 | if (!sl) |
| 552 | goto fail; |
| 553 | } |
| 554 | |
Christopher Faulet | d0db423 | 2021-01-22 11:46:30 +0100 | [diff] [blame] | 555 | if (*msgf & H2_MSGF_BODY_TUNNEL) |
| 556 | *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL); |
| 557 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 558 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) || |
| 559 | (*msgf & H2_MSGF_BODY_TUNNEL)) { |
| 560 | /* Request without body or tunnel requested */ |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 561 | sl_flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 562 | htx->flags |= HTX_FL_EOM; |
| 563 | } |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 564 | |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 565 | if (*msgf & H2_MSGF_EXT_CONNECT) { |
| 566 | if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT])) |
| 567 | goto fail; |
| 568 | if (!htx_add_header(htx, ist("connection"), ist("upgrade"))) |
| 569 | goto fail; |
| 570 | sl_flags |= HTX_SL_F_CONN_UPG; |
| 571 | } |
| 572 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 573 | /* update the start line with last detected header info */ |
| 574 | sl->flags |= sl_flags; |
| 575 | |
Willy Tarreau | b4934f0 | 2021-08-11 15:39:13 +0200 | [diff] [blame] | 576 | /* complete with missing Host if needed (we may validate this test if |
| 577 | * no regular header was found). |
| 578 | */ |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 579 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 580 | /* missing Host field, use :authority instead */ |
| 581 | if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH])) |
| 582 | goto fail; |
| 583 | } |
| 584 | |
| 585 | /* now we may have to build a cookie list. We'll dump the values of all |
| 586 | * visited headers. |
| 587 | */ |
| 588 | if (ck >= 0) { |
| 589 | uint32_t fs; // free space |
| 590 | uint32_t bs; // block size |
| 591 | uint32_t vl; // value len |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 592 | uint32_t tl; // total length |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 593 | struct htx_blk *blk; |
| 594 | |
| 595 | blk = htx_add_header(htx, ist("cookie"), list[ck].v); |
| 596 | if (!blk) |
| 597 | goto fail; |
| 598 | |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 599 | tl = list[ck].v.len; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 600 | fs = htx_free_data_space(htx); |
| 601 | bs = htx_get_blksz(blk); |
| 602 | |
| 603 | /* for each extra cookie, we'll extend the cookie's value and |
| 604 | * insert "; " before the new value. |
| 605 | */ |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 606 | fs += tl; // first one is already counted |
Tim Duesterhus | 1568355 | 2021-03-04 23:50:13 +0100 | [diff] [blame] | 607 | while ((ck = list[ck].n.len) >= 0) { |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 608 | vl = list[ck].v.len; |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 609 | tl += vl + 2; |
| 610 | if (tl > fs) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 611 | goto fail; |
| 612 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 613 | htx_change_blk_value_len(htx, blk, tl); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 614 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';'; |
| 615 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' '; |
| 616 | memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl); |
| 617 | bs += vl + 2; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | } |
| 621 | |
| 622 | /* now send the end of headers marker */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 623 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 624 | goto fail; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 625 | |
Amaury Denoyelle | 0353410 | 2021-07-07 10:49:28 +0200 | [diff] [blame] | 626 | /* proceed to scheme-based normalization on target-URI */ |
| 627 | if (fields & H2_PHDR_FND_SCHM) |
| 628 | http_scheme_based_normalize(htx); |
| 629 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 630 | ret = 1; |
| 631 | return ret; |
| 632 | |
| 633 | fail: |
| 634 | return -1; |
| 635 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 636 | |
| 637 | /* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>. |
| 638 | * <fields> indicates what was found so far. This should be called once at the |
| 639 | * detection of the first general header field or at the end of the message if |
| 640 | * no general header field was found yet. Returns the created start line on |
| 641 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 642 | * H2_MSGF_* flags indicating what was found while parsing. |
| 643 | */ |
| 644 | static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 645 | { |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 646 | unsigned int status, flags = HTX_SL_F_NONE; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 647 | struct htx_sl *sl; |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 648 | struct ist stat; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 649 | |
| 650 | /* only :status is allowed as a pseudo header */ |
| 651 | if (!(fields & H2_PHDR_FND_STAT)) |
| 652 | goto fail; |
| 653 | |
| 654 | if (phdr[H2_PHDR_IDX_STAT].len != 3) |
| 655 | goto fail; |
| 656 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 657 | /* if Extended CONNECT is used, convert status code from 200 to htx 101 |
| 658 | * following rfc 8441 */ |
| 659 | if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) && |
| 660 | isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) { |
| 661 | stat = ist("101"); |
| 662 | status = 101; |
| 663 | } |
| 664 | else { |
| 665 | unsigned char h, t, u; |
| 666 | |
| 667 | stat = phdr[H2_PHDR_IDX_STAT]; |
| 668 | |
| 669 | h = stat.ptr[0] - '0'; |
| 670 | t = stat.ptr[1] - '0'; |
| 671 | u = stat.ptr[2] - '0'; |
| 672 | if (h > 9 || t > 9 || u > 9) |
| 673 | goto fail; |
| 674 | status = h * 100 + t * 10 + u; |
| 675 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 676 | |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 677 | /* 101 responses are not supported in H2, so return a error. |
| 678 | * On 1xx responses there is no ES on the HEADERS frame but there is no |
| 679 | * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to |
| 680 | * notify the decoder another HEADERS frame is expected. |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 681 | * 204/304 response have no body by definition. So remove the flag |
Christopher Faulet | 7d247f0 | 2020-12-02 14:26:36 +0100 | [diff] [blame] | 682 | * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP. |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 683 | * |
| 684 | * Note however that there is a special condition for Extended CONNECT. |
| 685 | * In this case, we explicitly convert it to HTX 101 to mimic |
| 686 | * Get+Upgrade HTTP/1.1 mechanism |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 687 | */ |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 688 | if (status == 101) { |
| 689 | if (!(*msgf & H2_MSGF_EXT_CONNECT)) |
| 690 | goto fail; |
| 691 | } |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 692 | else if (status < 200) { |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 693 | *msgf |= H2_MSGF_RSP_1XX; |
| 694 | *msgf &= ~H2_MSGF_BODY; |
| 695 | } |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 696 | else if (status == 204 || status == 304) { |
Christopher Faulet | 7d247f0 | 2020-12-02 14:26:36 +0100 | [diff] [blame] | 697 | *msgf &= ~H2_MSGF_BODY; |
| 698 | *msgf |= H2_MSGF_BODYLESS_RSP; |
| 699 | } |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 700 | |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 701 | /* Set HTX start-line flags */ |
| 702 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 703 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 704 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 705 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), stat, ist("")); |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 706 | if (!sl) |
| 707 | goto fail; |
| 708 | sl->info.res.status = status; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 709 | return sl; |
| 710 | fail: |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | /* Takes an H2 response present in the headers list <list> terminated by a name |
| 715 | * being <NULL,0> and emits the equivalent HTX response according to the rules |
| 716 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 717 | * a positive value is returned if some bytes were emitted. In case of error, a |
| 718 | * negative error code is returned. |
| 719 | * |
| 720 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 721 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 722 | * if a body is detected (!ES). |
| 723 | * |
| 724 | * The headers list <list> must be composed of : |
| 725 | * - n.name != NULL, n.len > 0 : literal header name |
| 726 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 727 | * among H2_PHDR_IDX_* |
| 728 | * - n.name ignored, n.len == 0 : end of list |
| 729 | * - in all cases except the end of list, v.name and v.len must designate a |
| 730 | * valid value. |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 731 | * |
| 732 | * <upgrade_protocol> is only used if the htx status code is 101 indicating a |
| 733 | * response to an upgrade or h2-equivalent request. |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 734 | */ |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 735 | int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len, char *upgrade_protocol) |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 736 | { |
| 737 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 738 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 739 | uint32_t idx; |
| 740 | int phdr; |
| 741 | int ret; |
| 742 | int i; |
| 743 | struct htx_sl *sl = NULL; |
| 744 | unsigned int sl_flags = 0; |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 745 | const char *ctl; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 746 | |
| 747 | fields = 0; |
| 748 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 749 | if (!list[idx].n.ptr) { |
| 750 | /* this is an indexed pseudo-header */ |
| 751 | phdr = list[idx].n.len; |
| 752 | } |
| 753 | else { |
| 754 | /* this can be any type of header */ |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 755 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 756 | * #10.3: header names must be valid (i.e. match a token). |
| 757 | * For pseudo-headers we check from 2nd char and for other ones |
| 758 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 759 | * the colon. |
| 760 | */ |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 761 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 762 | |
| 763 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 764 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 765 | goto fail; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 768 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 769 | * rejecting NUL, CR and LF characters. |
| 770 | */ |
| 771 | ctl = ist_find_ctl(list[idx].v); |
| 772 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 773 | goto fail; |
| 774 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 775 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 776 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 777 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 778 | if (fields & H2_PHDR_FND_NONE) { |
| 779 | /* pseudo header field after regular headers */ |
| 780 | goto fail; |
| 781 | } |
| 782 | else { |
| 783 | /* repeated pseudo header field */ |
| 784 | goto fail; |
| 785 | } |
| 786 | } |
| 787 | fields |= 1 << phdr; |
| 788 | phdr_val[phdr] = list[idx].v; |
| 789 | continue; |
| 790 | } |
| 791 | else if (phdr != 0) { |
| 792 | /* invalid pseudo header -- should never happen here */ |
| 793 | goto fail; |
| 794 | } |
| 795 | |
| 796 | /* regular header field in (name,value) */ |
| 797 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 798 | /* no more pseudo-headers, time to build the status line */ |
| 799 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 800 | if (!sl) |
| 801 | goto fail; |
| 802 | fields |= H2_PHDR_FND_NONE; |
| 803 | } |
| 804 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 805 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 806 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 807 | if (ret < 0) |
| 808 | goto fail; |
| 809 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 810 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 811 | if (ret == 0) |
| 812 | continue; // skip this duplicate |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* these ones are forbidden in responses (RFC7540#8.1.2.2) */ |
| 816 | if (isteq(list[idx].n, ist("connection")) || |
| 817 | isteq(list[idx].n, ist("proxy-connection")) || |
| 818 | isteq(list[idx].n, ist("keep-alive")) || |
| 819 | isteq(list[idx].n, ist("upgrade")) || |
| 820 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 821 | goto fail; |
| 822 | |
| 823 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 824 | goto fail; |
| 825 | } |
| 826 | |
| 827 | /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */ |
| 828 | if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM)) |
| 829 | goto fail; |
| 830 | |
| 831 | /* Let's dump the request now if not yet emitted. */ |
| 832 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 833 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 834 | if (!sl) |
| 835 | goto fail; |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | if (sl->info.res.status == 101 && upgrade_protocol) { |
| 839 | if (!htx_add_header(htx, ist("connection"), ist("upgrade"))) |
| 840 | goto fail; |
| 841 | if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol))) |
| 842 | goto fail; |
| 843 | sl_flags |= HTX_SL_F_CONN_UPG; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 846 | if ((*msgf & H2_MSGF_BODY_TUNNEL) && |
| 847 | ((sl->info.res.status >= 200 && sl->info.res.status < 300) || sl->info.res.status == 101)) |
Christopher Faulet | d0db423 | 2021-01-22 11:46:30 +0100 | [diff] [blame] | 848 | *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL); |
| 849 | else |
| 850 | *msgf &= ~H2_MSGF_BODY_TUNNEL; |
| 851 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 852 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) || |
| 853 | (*msgf & H2_MSGF_BODY_TUNNEL)) { |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 854 | /* Response without body or tunnel successfully established */ |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 855 | sl_flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 856 | htx->flags |= HTX_FL_EOM; |
| 857 | } |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 858 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 859 | /* update the start line with last detected header info */ |
| 860 | sl->flags |= sl_flags; |
| 861 | |
| 862 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 863 | /* FIXME: Do we need to signal anything when we have a body and |
| 864 | * no content-length, to have the equivalent of H1's chunked |
| 865 | * encoding? |
| 866 | */ |
| 867 | } |
| 868 | |
| 869 | /* now send the end of headers marker */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 870 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 871 | goto fail; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 872 | |
| 873 | ret = 1; |
| 874 | return ret; |
| 875 | |
| 876 | fail: |
| 877 | return -1; |
| 878 | } |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 879 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 880 | /* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits |
| 881 | * the equivalent HTX trailers blocks. The output contents are emitted in <htx>, |
| 882 | * and a positive value is returned if some bytes were emitted. In case of |
| 883 | * error, a negative error code is returned. The caller must have verified that |
| 884 | * the message in the buffer is compatible with receipt of trailers. |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 885 | * |
| 886 | * The headers list <list> must be composed of : |
| 887 | * - n.name != NULL, n.len > 0 : literal header name |
| 888 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 889 | * among H2_PHDR_IDX_* (illegal here) |
| 890 | * - n.name ignored, n.len == 0 : end of list |
| 891 | * - in all cases except the end of list, v.name and v.len must designate a |
| 892 | * valid value. |
| 893 | */ |
| 894 | int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx) |
| 895 | { |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 896 | const char *ctl; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 897 | uint32_t idx; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 898 | int i; |
| 899 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 900 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 901 | if (!list[idx].n.ptr) { |
| 902 | /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */ |
| 903 | goto fail; |
| 904 | } |
| 905 | |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 906 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 907 | * #10.3: header names must be valid (i.e. match a token). This |
| 908 | * also catches pseudo-headers which are forbidden in trailers. |
| 909 | */ |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 910 | for (i = 0; i < list[idx].n.len; i++) |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 911 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 912 | goto fail; |
| 913 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 914 | /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */ |
| 915 | if (isteq(list[idx].n, ist("host")) || |
| 916 | isteq(list[idx].n, ist("content-length")) || |
| 917 | isteq(list[idx].n, ist("connection")) || |
| 918 | isteq(list[idx].n, ist("proxy-connection")) || |
| 919 | isteq(list[idx].n, ist("keep-alive")) || |
| 920 | isteq(list[idx].n, ist("upgrade")) || |
| 921 | isteq(list[idx].n, ist("te")) || |
| 922 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 923 | goto fail; |
| 924 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 925 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 926 | * rejecting NUL, CR and LF characters. |
| 927 | */ |
| 928 | ctl = ist_find_ctl(list[idx].v); |
| 929 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 930 | goto fail; |
| 931 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 932 | if (!htx_add_trailer(htx, list[idx].n, list[idx].v)) |
| 933 | goto fail; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 936 | if (!htx_add_endof(htx, HTX_BLK_EOT)) |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 937 | goto fail; |
| 938 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 939 | return 1; |
| 940 | |
| 941 | fail: |
| 942 | return -1; |
| 943 | } |