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