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 | 6557acd | 2024-08-01 16:01:50 +0200 | [diff] [blame] | 454 | if (*msgf & H2_MSGF_BODY_TUNNEL) |
| 455 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 456 | } |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 457 | |
Amaury Denoyelle | c9a0afc | 2020-12-11 17:53:09 +0100 | [diff] [blame] | 458 | if (*msgf & H2_MSGF_EXT_CONNECT) { |
| 459 | if (!htx_add_header(htx, ist("upgrade"), phdr_val[H2_PHDR_IDX_PROT])) |
| 460 | goto fail; |
| 461 | if (!htx_add_header(htx, ist("connection"), ist("upgrade"))) |
| 462 | goto fail; |
| 463 | sl_flags |= HTX_SL_F_CONN_UPG; |
| 464 | } |
| 465 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 466 | /* update the start line with last detected header info */ |
| 467 | sl->flags |= sl_flags; |
| 468 | |
Willy Tarreau | b5d2b9e | 2021-08-11 15:39:13 +0200 | [diff] [blame] | 469 | /* complete with missing Host if needed (we may validate this test if |
| 470 | * no regular header was found). |
| 471 | */ |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 472 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 473 | /* missing Host field, use :authority instead */ |
| 474 | if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH])) |
| 475 | goto fail; |
| 476 | } |
| 477 | |
| 478 | /* now we may have to build a cookie list. We'll dump the values of all |
| 479 | * visited headers. |
| 480 | */ |
| 481 | if (ck >= 0) { |
Amaury Denoyelle | 2c5a7ee | 2022-08-17 16:33:53 +0200 | [diff] [blame] | 482 | if (http_cookie_merge(htx, list, ck)) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 483 | goto fail; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* now send the end of headers marker */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 487 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 488 | goto fail; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 489 | |
Amaury Denoyelle | 4ca0f36 | 2021-07-07 10:49:28 +0200 | [diff] [blame] | 490 | /* proceed to scheme-based normalization on target-URI */ |
| 491 | if (fields & H2_PHDR_FND_SCHM) |
| 492 | http_scheme_based_normalize(htx); |
| 493 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 494 | ret = 1; |
| 495 | return ret; |
| 496 | |
| 497 | fail: |
| 498 | return -1; |
| 499 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 500 | |
| 501 | /* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>. |
| 502 | * <fields> indicates what was found so far. This should be called once at the |
| 503 | * detection of the first general header field or at the end of the message if |
| 504 | * no general header field was found yet. Returns the created start line on |
| 505 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 506 | * H2_MSGF_* flags indicating what was found while parsing. |
| 507 | */ |
| 508 | static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 509 | { |
Willy Tarreau | d8a44d0 | 2022-09-02 11:15:37 +0200 | [diff] [blame] | 510 | unsigned int status, flags = HTX_SL_F_IS_RESP; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 511 | struct htx_sl *sl; |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 512 | struct ist stat; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 513 | |
| 514 | /* only :status is allowed as a pseudo header */ |
| 515 | if (!(fields & H2_PHDR_FND_STAT)) |
| 516 | goto fail; |
| 517 | |
| 518 | if (phdr[H2_PHDR_IDX_STAT].len != 3) |
| 519 | goto fail; |
| 520 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 521 | /* if Extended CONNECT is used, convert status code from 200 to htx 101 |
| 522 | * following rfc 8441 */ |
| 523 | if (unlikely(*msgf & H2_MSGF_EXT_CONNECT) && |
| 524 | isteq(phdr[H2_PHDR_IDX_STAT], ist("200"))) { |
| 525 | stat = ist("101"); |
| 526 | status = 101; |
| 527 | } |
| 528 | else { |
| 529 | unsigned char h, t, u; |
| 530 | |
| 531 | stat = phdr[H2_PHDR_IDX_STAT]; |
| 532 | |
| 533 | h = stat.ptr[0] - '0'; |
| 534 | t = stat.ptr[1] - '0'; |
| 535 | u = stat.ptr[2] - '0'; |
| 536 | if (h > 9 || t > 9 || u > 9) |
| 537 | goto fail; |
| 538 | status = h * 100 + t * 10 + u; |
| 539 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 540 | |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 541 | /* 101 responses are not supported in H2, so return a error. |
| 542 | * On 1xx responses there is no ES on the HEADERS frame but there is no |
| 543 | * body. So remove the flag H2_MSGF_BODY and add H2_MSGF_RSP_1XX to |
| 544 | * notify the decoder another HEADERS frame is expected. |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 545 | * 204/304 response have no body by definition. So remove the flag |
Christopher Faulet | 7d247f0 | 2020-12-02 14:26:36 +0100 | [diff] [blame] | 546 | * H2_MSGF_BODY and set H2_MSGF_BODYLESS_RSP. |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 547 | * |
| 548 | * Note however that there is a special condition for Extended CONNECT. |
| 549 | * In this case, we explicitly convert it to HTX 101 to mimic |
| 550 | * Get+Upgrade HTTP/1.1 mechanism |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 551 | */ |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 552 | if (status == 101) { |
| 553 | if (!(*msgf & H2_MSGF_EXT_CONNECT)) |
| 554 | goto fail; |
| 555 | } |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 556 | else if (status < 200) { |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 557 | *msgf |= H2_MSGF_RSP_1XX; |
| 558 | *msgf &= ~H2_MSGF_BODY; |
| 559 | } |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 560 | else if (status == 204 || status == 304) { |
Christopher Faulet | 7d247f0 | 2020-12-02 14:26:36 +0100 | [diff] [blame] | 561 | *msgf &= ~H2_MSGF_BODY; |
| 562 | *msgf |= H2_MSGF_BODYLESS_RSP; |
| 563 | } |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 564 | |
Christopher Faulet | 8989942 | 2020-12-07 18:24:43 +0100 | [diff] [blame] | 565 | /* Set HTX start-line flags */ |
| 566 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 567 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 568 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 569 | 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] | 570 | if (!sl) |
| 571 | goto fail; |
| 572 | sl->info.res.status = status; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 573 | return sl; |
| 574 | fail: |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | /* Takes an H2 response present in the headers list <list> terminated by a name |
| 579 | * being <NULL,0> and emits the equivalent HTX response according to the rules |
| 580 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 581 | * a positive value is returned if some bytes were emitted. In case of error, a |
| 582 | * negative error code is returned. |
| 583 | * |
| 584 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 585 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 586 | * if a body is detected (!ES). |
| 587 | * |
| 588 | * The headers list <list> must be composed of : |
| 589 | * - n.name != NULL, n.len > 0 : literal header name |
| 590 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 591 | * among H2_PHDR_IDX_* |
| 592 | * - n.name ignored, n.len == 0 : end of list |
| 593 | * - in all cases except the end of list, v.name and v.len must designate a |
| 594 | * valid value. |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 595 | * |
| 596 | * <upgrade_protocol> is only used if the htx status code is 101 indicating a |
| 597 | * response to an upgrade or h2-equivalent request. |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 598 | */ |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 599 | 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] | 600 | { |
| 601 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 602 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 603 | uint32_t idx; |
| 604 | int phdr; |
| 605 | int ret; |
| 606 | int i; |
| 607 | struct htx_sl *sl = NULL; |
| 608 | unsigned int sl_flags = 0; |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 609 | const char *ctl; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 610 | |
| 611 | fields = 0; |
| 612 | for (idx = 0; list[idx].n.len != 0; idx++) { |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 613 | if (!isttest(list[idx].n)) { |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 614 | /* this is an indexed pseudo-header */ |
| 615 | phdr = list[idx].n.len; |
| 616 | } |
| 617 | else { |
| 618 | /* this can be any type of header */ |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 619 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 620 | * #10.3: header names must be valid (i.e. match a token). |
| 621 | * For pseudo-headers we check from 2nd char and for other ones |
| 622 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 623 | * the colon. |
| 624 | */ |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 625 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 626 | |
| 627 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 628 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 629 | goto fail; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 632 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 633 | * rejecting NUL, CR and LF characters. |
| 634 | */ |
| 635 | ctl = ist_find_ctl(list[idx].v); |
Willy Tarreau | 21c4ffd | 2023-08-08 17:00:50 +0200 | [diff] [blame] | 636 | if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl)) |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 637 | goto fail; |
| 638 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 639 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 640 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 641 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 642 | if (fields & H2_PHDR_FND_NONE) { |
| 643 | /* pseudo header field after regular headers */ |
| 644 | goto fail; |
| 645 | } |
| 646 | else { |
| 647 | /* repeated pseudo header field */ |
| 648 | goto fail; |
| 649 | } |
| 650 | } |
| 651 | fields |= 1 << phdr; |
| 652 | phdr_val[phdr] = list[idx].v; |
| 653 | continue; |
| 654 | } |
| 655 | else if (phdr != 0) { |
| 656 | /* invalid pseudo header -- should never happen here */ |
| 657 | goto fail; |
| 658 | } |
| 659 | |
| 660 | /* regular header field in (name,value) */ |
| 661 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 662 | /* no more pseudo-headers, time to build the status line */ |
| 663 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 664 | if (!sl) |
| 665 | goto fail; |
| 666 | fields |= H2_PHDR_FND_NONE; |
| 667 | } |
| 668 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 669 | if (isteq(list[idx].n, ist("content-length"))) { |
Amaury Denoyelle | 15f3cc4 | 2022-12-08 16:53:58 +0100 | [diff] [blame] | 670 | ret = http_parse_cont_len_header(&list[idx].v, body_len, |
| 671 | *msgf & H2_MSGF_BODY_CL); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 672 | if (ret < 0) |
| 673 | goto fail; |
| 674 | |
Amaury Denoyelle | 15f3cc4 | 2022-12-08 16:53:58 +0100 | [diff] [blame] | 675 | *msgf |= H2_MSGF_BODY_CL; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 676 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 677 | if (ret == 0) |
| 678 | continue; // skip this duplicate |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* these ones are forbidden in responses (RFC7540#8.1.2.2) */ |
| 682 | if (isteq(list[idx].n, ist("connection")) || |
| 683 | isteq(list[idx].n, ist("proxy-connection")) || |
| 684 | isteq(list[idx].n, ist("keep-alive")) || |
| 685 | isteq(list[idx].n, ist("upgrade")) || |
| 686 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 687 | goto fail; |
| 688 | |
| 689 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 690 | goto fail; |
| 691 | } |
| 692 | |
| 693 | /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */ |
| 694 | if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM)) |
| 695 | goto fail; |
| 696 | |
| 697 | /* Let's dump the request now if not yet emitted. */ |
| 698 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 699 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 700 | if (!sl) |
| 701 | goto fail; |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | if (sl->info.res.status == 101 && upgrade_protocol) { |
| 705 | if (!htx_add_header(htx, ist("connection"), ist("upgrade"))) |
| 706 | goto fail; |
| 707 | if (!htx_add_header(htx, ist("upgrade"), ist(upgrade_protocol))) |
| 708 | goto fail; |
| 709 | sl_flags |= HTX_SL_F_CONN_UPG; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Amaury Denoyelle | 7416274 | 2020-12-11 17:53:05 +0100 | [diff] [blame] | 712 | if ((*msgf & H2_MSGF_BODY_TUNNEL) && |
| 713 | ((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] | 714 | *msgf &= ~(H2_MSGF_BODY|H2_MSGF_BODY_CL); |
| 715 | else |
| 716 | *msgf &= ~H2_MSGF_BODY_TUNNEL; |
| 717 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 718 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0) || |
| 719 | (*msgf & H2_MSGF_BODY_TUNNEL)) { |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 720 | /* Response without body or tunnel successfully established */ |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 721 | sl_flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | 6557acd | 2024-08-01 16:01:50 +0200 | [diff] [blame] | 722 | if (*msgf & H2_MSGF_BODY_TUNNEL) |
| 723 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 724 | } |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 725 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 726 | /* update the start line with last detected header info */ |
| 727 | sl->flags |= sl_flags; |
| 728 | |
| 729 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 730 | /* FIXME: Do we need to signal anything when we have a body and |
| 731 | * no content-length, to have the equivalent of H1's chunked |
| 732 | * encoding? |
| 733 | */ |
| 734 | } |
| 735 | |
| 736 | /* now send the end of headers marker */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 737 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
| 738 | goto fail; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 739 | |
| 740 | ret = 1; |
| 741 | return ret; |
| 742 | |
| 743 | fail: |
| 744 | return -1; |
| 745 | } |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 746 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 747 | /* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits |
| 748 | * the equivalent HTX trailers blocks. The output contents are emitted in <htx>, |
| 749 | * and a positive value is returned if some bytes were emitted. In case of |
| 750 | * error, a negative error code is returned. The caller must have verified that |
| 751 | * the message in the buffer is compatible with receipt of trailers. |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 752 | * |
| 753 | * The headers list <list> must be composed of : |
| 754 | * - n.name != NULL, n.len > 0 : literal header name |
| 755 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 756 | * among H2_PHDR_IDX_* (illegal here) |
| 757 | * - n.name ignored, n.len == 0 : end of list |
| 758 | * - in all cases except the end of list, v.name and v.len must designate a |
| 759 | * valid value. |
| 760 | */ |
| 761 | int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx) |
| 762 | { |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 763 | const char *ctl; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 764 | uint32_t idx; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 765 | int i; |
| 766 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 767 | for (idx = 0; list[idx].n.len != 0; idx++) { |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 768 | if (!isttest(list[idx].n)) { |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 769 | /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */ |
| 770 | goto fail; |
| 771 | } |
| 772 | |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 773 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 774 | * #10.3: header names must be valid (i.e. match a token). This |
| 775 | * also catches pseudo-headers which are forbidden in trailers. |
| 776 | */ |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 777 | for (i = 0; i < list[idx].n.len; i++) |
Willy Tarreau | 146f53a | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 778 | 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] | 779 | goto fail; |
| 780 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 781 | /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */ |
| 782 | if (isteq(list[idx].n, ist("host")) || |
| 783 | isteq(list[idx].n, ist("content-length")) || |
| 784 | isteq(list[idx].n, ist("connection")) || |
| 785 | isteq(list[idx].n, ist("proxy-connection")) || |
| 786 | isteq(list[idx].n, ist("keep-alive")) || |
| 787 | isteq(list[idx].n, ist("upgrade")) || |
| 788 | isteq(list[idx].n, ist("te")) || |
| 789 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 790 | goto fail; |
| 791 | |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 792 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 793 | * rejecting NUL, CR and LF characters. |
| 794 | */ |
| 795 | ctl = ist_find_ctl(list[idx].v); |
Willy Tarreau | 21c4ffd | 2023-08-08 17:00:50 +0200 | [diff] [blame] | 796 | if (unlikely(ctl) && http_header_has_forbidden_char(list[idx].v, ctl)) |
Willy Tarreau | 54f53ef | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 797 | goto fail; |
| 798 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 799 | if (!htx_add_trailer(htx, list[idx].n, list[idx].v)) |
| 800 | goto fail; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 803 | if (!htx_add_endof(htx, HTX_BLK_EOT)) |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 804 | goto fail; |
| 805 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 806 | return 1; |
| 807 | |
| 808 | fail: |
| 809 | return -1; |
| 810 | } |