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