Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 protocol processing |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * Copyright (C) 2017 HAProxy Technologies |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining |
| 8 | * a copy of this software and associated documentation files (the |
| 9 | * "Software"), to deal in the Software without restriction, including |
| 10 | * without limitation the rights to use, copy, modify, merge, publish, |
| 11 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | * permit persons to whom the Software is furnished to do so, subject to |
| 13 | * the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be |
| 16 | * included in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 | * OTHER DEALINGS IN THE SOFTWARE. |
| 26 | */ |
| 27 | |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 28 | #include <inttypes.h> |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 29 | #include <common/config.h> |
| 30 | #include <common/h2.h> |
| 31 | #include <common/http-hdr.h> |
| 32 | #include <common/ist.h> |
| 33 | |
Willy Tarreau | 9c84d82 | 2019-01-30 15:09:21 +0100 | [diff] [blame] | 34 | struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES] = { |
| 35 | [H2_FT_DATA ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 36 | [H2_FT_HEADERS ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 1, .max_len = H2_MAX_FRAME_LEN, }, |
| 37 | [H2_FT_PRIORITY ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 5, .max_len = 5, }, |
| 38 | [H2_FT_RST_STREAM ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, }, |
| 39 | [H2_FT_SETTINGS ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 40 | [H2_FT_PUSH_PROMISE ] = { .dir = 0, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = H2_MAX_FRAME_LEN, }, |
| 41 | [H2_FT_PING ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = 8, }, |
| 42 | [H2_FT_GOAWAY ] = { .dir = 3, .min_id = 0, .max_id = 0, .min_len = 8, .max_len = H2_MAX_FRAME_LEN, }, |
| 43 | [H2_FT_WINDOW_UPDATE] = { .dir = 3, .min_id = 0, .max_id = H2_MAX_STREAM_ID, .min_len = 4, .max_len = 4, }, |
| 44 | [H2_FT_CONTINUATION ] = { .dir = 3, .min_id = 1, .max_id = H2_MAX_STREAM_ID, .min_len = 0, .max_len = H2_MAX_FRAME_LEN, }, |
| 45 | }; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 46 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 47 | /* Looks into <ist> for forbidden characters for header values (0x00, 0x0A, |
| 48 | * 0x0D), starting at pointer <start> which must be within <ist>. Returns |
| 49 | * non-zero if such a character is found, 0 otherwise. When run on unlikely |
| 50 | * header match, it's recommended to first check for the presence of control |
| 51 | * chars using ist_find_ctl(). |
| 52 | */ |
| 53 | static int has_forbidden_char(const struct ist ist, const char *start) |
| 54 | { |
| 55 | do { |
| 56 | if ((uint8_t)*start <= 0x0d && |
| 57 | (1U << (uint8_t)*start) & ((1<<13) | (1<<10) | (1<<0))) |
| 58 | return 1; |
| 59 | start++; |
| 60 | } while (start < ist.ptr + ist.len); |
| 61 | return 0; |
| 62 | } |
| 63 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 64 | /* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers |
| 65 | * stored in <phdr[]>. <fields> indicates what was found so far. This should be |
| 66 | * called once at the detection of the first general header field or at the end |
| 67 | * of the request if no general header field was found yet. Returns 0 on success |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 68 | * or a negative error code on failure. Upon success, <msgf> is updated with a |
| 69 | * few H2_MSGF_* flags indicating what was found while parsing. |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 70 | */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 71 | static int h2_prepare_h1_reqline(uint32_t fields, struct ist *phdr, char **ptr, char *end, unsigned int *msgf) |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 72 | { |
| 73 | char *out = *ptr; |
| 74 | int uri_idx = H2_PHDR_IDX_PATH; |
| 75 | |
| 76 | if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) { |
| 77 | /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path" |
| 78 | * MUST be omitted ; ":authority" contains the host and port |
| 79 | * to connect to. |
| 80 | */ |
| 81 | if (fields & H2_PHDR_FND_SCHM) { |
| 82 | /* scheme not allowed */ |
| 83 | goto fail; |
| 84 | } |
| 85 | else if (fields & H2_PHDR_FND_PATH) { |
| 86 | /* path not allowed */ |
| 87 | goto fail; |
| 88 | } |
| 89 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 90 | /* missing authority */ |
| 91 | goto fail; |
| 92 | } |
| 93 | // otherwise OK ; let's use the authority instead of the URI |
| 94 | uri_idx = H2_PHDR_IDX_AUTH; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 95 | *msgf |= H2_MSGF_BODY_TUNNEL; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 96 | } |
| 97 | else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) != |
| 98 | (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) { |
| 99 | /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one |
| 100 | * valid value for the ":method", ":scheme" and ":path" phdr |
| 101 | * unless it is a CONNECT request. |
| 102 | */ |
| 103 | if (!(fields & H2_PHDR_FND_METH)) { |
| 104 | /* missing method */ |
| 105 | goto fail; |
| 106 | } |
| 107 | else if (!(fields & H2_PHDR_FND_SCHM)) { |
| 108 | /* missing scheme */ |
| 109 | goto fail; |
| 110 | } |
| 111 | else { |
| 112 | /* missing path */ |
| 113 | goto fail; |
| 114 | } |
| 115 | } |
| 116 | |
Willy Tarreau | cd4fe17 | 2017-12-03 11:51:31 +0100 | [diff] [blame] | 117 | /* 7540#8.1.2.3: :path must not be empty */ |
| 118 | if (!phdr[uri_idx].len) |
| 119 | goto fail; |
| 120 | |
Willy Tarreau | 811ad12 | 2017-12-03 09:44:50 +0100 | [diff] [blame] | 121 | if (out + phdr[H2_PHDR_IDX_METH].len + 1 + phdr[uri_idx].len + 11 > end) { |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 122 | /* too large */ |
| 123 | goto fail; |
| 124 | } |
| 125 | |
| 126 | memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len); |
| 127 | out += phdr[H2_PHDR_IDX_METH].len; |
| 128 | *(out++) = ' '; |
| 129 | |
| 130 | memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len); |
| 131 | out += phdr[uri_idx].len; |
| 132 | memcpy(out, " HTTP/1.1\r\n", 11); |
| 133 | out += 11; |
| 134 | |
| 135 | *ptr = out; |
| 136 | return 0; |
| 137 | fail: |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 142 | * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the |
| 143 | * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out> |
| 144 | * for a max of <osize> bytes, and the amount of bytes emitted is returned. In |
| 145 | * case of error, a negative error code is returned. |
| 146 | * |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 147 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 148 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 149 | * if a body is detected (!ES). |
| 150 | * |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 151 | * The headers list <list> must be composed of : |
| 152 | * - n.name != NULL, n.len > 0 : literal header name |
| 153 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 154 | * among H2_PHDR_IDX_* |
| 155 | * - n.name ignored, n.len == 0 : end of list |
| 156 | * - in all cases except the end of list, v.name and v.len must designate a |
| 157 | * valid value. |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 158 | * |
| 159 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 160 | * will be used to create a linked list, so its contents may be destroyed. |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 161 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 162 | int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf, unsigned long long *body_len) |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 163 | { |
| 164 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 165 | char *out_end = out + osize; |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 166 | const char *ctl; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 167 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 168 | uint32_t idx; |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 169 | int ck, lck; /* cookie index and last cookie index */ |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 170 | int phdr; |
| 171 | int ret; |
Willy Tarreau | 637f64d | 2017-12-03 20:28:13 +0100 | [diff] [blame] | 172 | int i; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 173 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 174 | lck = ck = -1; // no cookie for now |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 175 | fields = 0; |
| 176 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 177 | if (!list[idx].n.ptr) { |
| 178 | /* this is an indexed pseudo-header */ |
| 179 | phdr = list[idx].n.len; |
| 180 | } |
| 181 | else { |
| 182 | /* this can be any type of header */ |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 183 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 184 | * #10.3: header names must be valid (i.e. match a token). |
| 185 | * For pseudo-headers we check from 2nd char and for other ones |
| 186 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 187 | * the colon. |
| 188 | */ |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 189 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 190 | |
| 191 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 192 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 193 | goto fail; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 196 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 197 | * rejecting NUL, CR and LF characters. |
| 198 | */ |
| 199 | ctl = ist_find_ctl(list[idx].v); |
| 200 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 201 | goto fail; |
| 202 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 203 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 204 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 205 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 206 | if (fields & H2_PHDR_FND_NONE) { |
| 207 | /* pseudo header field after regular headers */ |
| 208 | goto fail; |
| 209 | } |
| 210 | else { |
| 211 | /* repeated pseudo header field */ |
| 212 | goto fail; |
| 213 | } |
| 214 | } |
| 215 | fields |= 1 << phdr; |
| 216 | phdr_val[phdr] = list[idx].v; |
| 217 | continue; |
| 218 | } |
| 219 | else if (phdr != 0) { |
| 220 | /* invalid pseudo header -- should never happen here */ |
| 221 | goto fail; |
| 222 | } |
| 223 | |
| 224 | /* regular header field in (name,value) */ |
| 225 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 226 | /* no more pseudo-headers, time to build the request line */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 227 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf); |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 228 | if (ret != 0) |
| 229 | goto leave; |
| 230 | fields |= H2_PHDR_FND_NONE; |
| 231 | } |
| 232 | |
| 233 | if (isteq(list[idx].n, ist("host"))) |
| 234 | fields |= H2_PHDR_FND_HOST; |
| 235 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 236 | if (isteq(list[idx].n, ist("content-length"))) { |
| 237 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
| 238 | if (ret < 0) |
| 239 | goto fail; |
| 240 | |
| 241 | if (ret == 0) |
| 242 | continue; // skip this duplicate |
| 243 | } |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 244 | |
Willy Tarreau | fe7c356 | 2017-12-03 20:15:34 +0100 | [diff] [blame] | 245 | /* these ones are forbidden in requests (RFC7540#8.1.2.2) */ |
| 246 | if (isteq(list[idx].n, ist("connection")) || |
| 247 | isteq(list[idx].n, ist("proxy-connection")) || |
| 248 | isteq(list[idx].n, ist("keep-alive")) || |
| 249 | isteq(list[idx].n, ist("upgrade")) || |
| 250 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 251 | goto fail; |
| 252 | |
Willy Tarreau | d8d2ac7 | 2017-12-03 18:41:31 +0100 | [diff] [blame] | 253 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 254 | goto fail; |
| 255 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 256 | /* cookie requires special processing at the end */ |
| 257 | if (isteq(list[idx].n, ist("cookie"))) { |
| 258 | list[idx].n.len = -1; |
| 259 | |
| 260 | if (ck < 0) |
| 261 | ck = idx; |
| 262 | else |
| 263 | list[lck].n.len = idx; |
| 264 | |
| 265 | lck = idx; |
| 266 | continue; |
| 267 | } |
| 268 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 269 | if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) { |
| 270 | /* too large */ |
| 271 | goto fail; |
| 272 | } |
| 273 | |
| 274 | /* copy "name: value" */ |
| 275 | memcpy(out, list[idx].n.ptr, list[idx].n.len); |
| 276 | out += list[idx].n.len; |
| 277 | *(out++) = ':'; |
| 278 | *(out++) = ' '; |
| 279 | |
| 280 | memcpy(out, list[idx].v.ptr, list[idx].v.len); |
| 281 | out += list[idx].v.len; |
| 282 | *(out++) = '\r'; |
| 283 | *(out++) = '\n'; |
| 284 | } |
| 285 | |
Willy Tarreau | 5208869 | 2017-12-03 20:13:54 +0100 | [diff] [blame] | 286 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 287 | if (fields & H2_PHDR_FND_STAT) |
| 288 | goto fail; |
| 289 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 290 | /* Let's dump the request now if not yet emitted. */ |
| 291 | if (!(fields & H2_PHDR_FND_NONE)) { |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 292 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf); |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 293 | if (ret != 0) |
| 294 | goto leave; |
| 295 | } |
| 296 | |
| 297 | /* complete with missing Host if needed */ |
| 298 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 299 | /* missing Host field, use :authority instead */ |
| 300 | if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) { |
| 301 | /* too large */ |
| 302 | goto fail; |
| 303 | } |
| 304 | |
| 305 | memcpy(out, "host: ", 6); |
| 306 | memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len); |
| 307 | out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len; |
| 308 | *(out++) = '\r'; |
| 309 | *(out++) = '\n'; |
| 310 | } |
| 311 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 312 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 313 | /* add chunked encoding */ |
| 314 | if (out + 28 > out_end) |
| 315 | goto fail; |
| 316 | memcpy(out, "transfer-encoding: chunked\r\n", 28); |
| 317 | out += 28; |
| 318 | } |
| 319 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 320 | /* now we may have to build a cookie list. We'll dump the values of all |
| 321 | * visited headers. |
| 322 | */ |
| 323 | if (ck >= 0) { |
| 324 | if (out + 8 > out_end) { |
| 325 | /* too large */ |
| 326 | goto fail; |
| 327 | } |
| 328 | memcpy(out, "cookie: ", 8); |
| 329 | out += 8; |
| 330 | |
| 331 | do { |
| 332 | if (out + list[ck].v.len + 2 > out_end) { |
| 333 | /* too large */ |
| 334 | goto fail; |
| 335 | } |
| 336 | memcpy(out, list[ck].v.ptr, list[ck].v.len); |
| 337 | out += list[ck].v.len; |
| 338 | ck = list[ck].n.len; |
| 339 | |
| 340 | if (ck >= 0) { |
| 341 | *(out++) = ';'; |
| 342 | *(out++) = ' '; |
| 343 | } |
| 344 | } while (ck >= 0); |
| 345 | |
| 346 | if (out + 2 > out_end) { |
| 347 | /* too large */ |
| 348 | goto fail; |
| 349 | } |
| 350 | *(out++) = '\r'; |
| 351 | *(out++) = '\n'; |
| 352 | } |
| 353 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 354 | /* And finish */ |
| 355 | if (out + 2 > out_end) { |
| 356 | /* too large */ |
| 357 | goto fail; |
| 358 | } |
| 359 | |
| 360 | *(out++) = '\r'; |
| 361 | *(out++) = '\n'; |
| 362 | ret = out + osize - out_end; |
| 363 | leave: |
| 364 | return ret; |
| 365 | |
| 366 | fail: |
| 367 | return -1; |
| 368 | } |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 369 | |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 370 | /* Takes an H2 headers list <list> terminated by a name being <NULL,0> and |
| 371 | * emits the equivalent HTTP/1.1 trailers block not including the empty line. |
| 372 | * The output contents are emitted in <out> for a max of <osize> bytes, and the |
| 373 | * amount of bytes emitted is returned. In case of error, a negative error code |
| 374 | * is returned. The caller must have verified that the message in the buffer is |
| 375 | * compatible with receipt of trailers. |
| 376 | * |
| 377 | * The headers list <list> must be composed of : |
| 378 | * - n.name != NULL, n.len > 0 : literal header name |
| 379 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 380 | * among H2_PHDR_IDX_* (illegal here) |
| 381 | * - n.name ignored, n.len == 0 : end of list |
| 382 | * - in all cases except the end of list, v.name and v.len must designate a |
| 383 | * valid value. |
| 384 | */ |
| 385 | int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize) |
| 386 | { |
| 387 | char *out_end = out + osize; |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 388 | const char *ctl; |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 389 | uint32_t idx; |
| 390 | int i; |
| 391 | |
| 392 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 393 | if (!list[idx].n.ptr) { |
| 394 | /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */ |
| 395 | goto fail; |
| 396 | } |
| 397 | |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 398 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 399 | * #10.3: header names must be valid (i.e. match a token). This |
| 400 | * also catches pseudo-headers which are forbidden in trailers. |
| 401 | */ |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 402 | for (i = 0; i < list[idx].n.len; i++) |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 403 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 404 | goto fail; |
| 405 | |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 406 | /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */ |
| 407 | if (isteq(list[idx].n, ist("host")) || |
| 408 | isteq(list[idx].n, ist("content-length")) || |
| 409 | isteq(list[idx].n, ist("connection")) || |
| 410 | isteq(list[idx].n, ist("proxy-connection")) || |
| 411 | isteq(list[idx].n, ist("keep-alive")) || |
| 412 | isteq(list[idx].n, ist("upgrade")) || |
| 413 | isteq(list[idx].n, ist("te")) || |
| 414 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 415 | goto fail; |
| 416 | |
| 417 | if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) { |
| 418 | /* too large */ |
| 419 | goto fail; |
| 420 | } |
| 421 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 422 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 423 | * rejecting NUL, CR and LF characters. |
| 424 | */ |
| 425 | ctl = ist_find_ctl(list[idx].v); |
| 426 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 427 | goto fail; |
| 428 | |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 429 | /* copy "name: value" */ |
| 430 | memcpy(out, list[idx].n.ptr, list[idx].n.len); |
| 431 | out += list[idx].n.len; |
| 432 | *(out++) = ':'; |
| 433 | *(out++) = ' '; |
| 434 | |
| 435 | memcpy(out, list[idx].v.ptr, list[idx].v.len); |
| 436 | out += list[idx].v.len; |
| 437 | *(out++) = '\r'; |
| 438 | *(out++) = '\n'; |
| 439 | } |
| 440 | |
| 441 | return out + osize - out_end; |
| 442 | |
| 443 | fail: |
| 444 | return -1; |
| 445 | } |
| 446 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 447 | /* Parse the Content-Length header field of an HTTP/2 request. The function |
| 448 | * checks all possible occurrences of a comma-delimited value, and verifies |
| 449 | * if any of them doesn't match a previous value. It returns <0 if a value |
| 450 | * differs, 0 if the whole header can be dropped (i.e. already known), or >0 |
| 451 | * if the value can be indexed (first one). In the last case, the value might |
| 452 | * be adjusted and the caller must only add the updated value. |
| 453 | */ |
| 454 | int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len) |
| 455 | { |
| 456 | char *e, *n; |
| 457 | unsigned long long cl; |
| 458 | int not_first = !!(*msgf & H2_MSGF_BODY_CL); |
| 459 | struct ist word; |
| 460 | |
| 461 | word.ptr = value->ptr - 1; // -1 for next loop's pre-increment |
| 462 | e = value->ptr + value->len; |
| 463 | |
| 464 | while (++word.ptr < e) { |
| 465 | /* skip leading delimitor and blanks */ |
| 466 | if (unlikely(HTTP_IS_LWS(*word.ptr))) |
| 467 | continue; |
| 468 | |
| 469 | /* digits only now */ |
| 470 | for (cl = 0, n = word.ptr; n < e; n++) { |
| 471 | unsigned int c = *n - '0'; |
| 472 | if (unlikely(c > 9)) { |
| 473 | /* non-digit */ |
| 474 | if (unlikely(n == word.ptr)) // spaces only |
| 475 | goto fail; |
| 476 | break; |
| 477 | } |
| 478 | if (unlikely(cl > ULLONG_MAX / 10ULL)) |
| 479 | goto fail; /* multiply overflow */ |
| 480 | cl = cl * 10ULL; |
| 481 | if (unlikely(cl + c < cl)) |
| 482 | goto fail; /* addition overflow */ |
| 483 | cl = cl + c; |
| 484 | } |
| 485 | |
| 486 | /* keep a copy of the exact cleaned value */ |
| 487 | word.len = n - word.ptr; |
| 488 | |
| 489 | /* skip trailing LWS till next comma or EOL */ |
| 490 | for (; n < e; n++) { |
| 491 | if (!HTTP_IS_LWS(*n)) { |
| 492 | if (unlikely(*n != ',')) |
| 493 | goto fail; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* if duplicate, must be equal */ |
| 499 | if (*msgf & H2_MSGF_BODY_CL && cl != *body_len) |
| 500 | goto fail; |
| 501 | |
| 502 | /* OK, store this result as the one to be indexed */ |
| 503 | *msgf |= H2_MSGF_BODY_CL; |
| 504 | *body_len = cl; |
| 505 | *value = word; |
| 506 | word.ptr = n; |
| 507 | } |
| 508 | /* here we've reached the end with a single value or a series of |
| 509 | * identical values, all matching previous series if any. The last |
| 510 | * parsed value was sent back into <value>. We just have to decide |
| 511 | * if this occurrence has to be indexed (it's the first one) or |
| 512 | * silently skipped (it's not the first one) |
| 513 | */ |
| 514 | return !not_first; |
| 515 | fail: |
| 516 | return -1; |
| 517 | } |
| 518 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 519 | /* Prepare the request line into <htx> from pseudo headers stored in <phdr[]>. |
| 520 | * <fields> indicates what was found so far. This should be called once at the |
| 521 | * detection of the first general header field or at the end of the request if |
| 522 | * no general header field was found yet. Returns the created start line on |
| 523 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 524 | * H2_MSGF_* flags indicating what was found while parsing. |
| 525 | */ |
| 526 | static struct htx_sl *h2_prepare_htx_reqline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 527 | { |
| 528 | int uri_idx = H2_PHDR_IDX_PATH; |
| 529 | unsigned int flags = HTX_SL_F_NONE; |
| 530 | struct htx_sl *sl; |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 531 | size_t i; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 532 | |
| 533 | if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) { |
| 534 | /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path" |
| 535 | * MUST be omitted ; ":authority" contains the host and port |
| 536 | * to connect to. |
| 537 | */ |
| 538 | if (fields & H2_PHDR_FND_SCHM) { |
| 539 | /* scheme not allowed */ |
| 540 | goto fail; |
| 541 | } |
| 542 | else if (fields & H2_PHDR_FND_PATH) { |
| 543 | /* path not allowed */ |
| 544 | goto fail; |
| 545 | } |
| 546 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 547 | /* missing authority */ |
| 548 | goto fail; |
| 549 | } |
| 550 | // otherwise OK ; let's use the authority instead of the URI |
| 551 | uri_idx = H2_PHDR_IDX_AUTH; |
| 552 | *msgf |= H2_MSGF_BODY_TUNNEL; |
| 553 | } |
| 554 | else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) != |
| 555 | (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) { |
| 556 | /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one |
| 557 | * valid value for the ":method", ":scheme" and ":path" phdr |
| 558 | * unless it is a CONNECT request. |
| 559 | */ |
| 560 | if (!(fields & H2_PHDR_FND_METH)) { |
| 561 | /* missing method */ |
| 562 | goto fail; |
| 563 | } |
| 564 | else if (!(fields & H2_PHDR_FND_SCHM)) { |
| 565 | /* missing scheme */ |
| 566 | goto fail; |
| 567 | } |
| 568 | else { |
| 569 | /* missing path */ |
| 570 | goto fail; |
| 571 | } |
| 572 | } |
| 573 | |
Willy Tarreau | b2318bc | 2021-08-11 11:12:46 +0200 | [diff] [blame^] | 574 | /* The method is a non-empty token (RFC7231#4.1) */ |
| 575 | if (!phdr[H2_PHDR_IDX_METH].len) |
| 576 | goto fail; |
| 577 | for (i = 0; i < phdr[H2_PHDR_IDX_METH].len; i++) { |
| 578 | if (!HTTP_IS_TOKEN(phdr[H2_PHDR_IDX_METH].ptr[i])) |
| 579 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 580 | } |
| 581 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 582 | /* 7540#8.1.2.3: :path must not be empty */ |
| 583 | if (!phdr[uri_idx].len) |
| 584 | goto fail; |
| 585 | |
Willy Tarreau | 9255e7e | 2019-03-05 10:47:37 +0100 | [diff] [blame] | 586 | /* make sure :path doesn't contain LWS nor CTL characters */ |
| 587 | for (i = 0; i < phdr[uri_idx].len; i++) { |
| 588 | unsigned char c = phdr[uri_idx].ptr[i]; |
| 589 | if (HTTP_IS_LWS(c) || HTTP_IS_CTL(c)) |
| 590 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 591 | } |
| 592 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 593 | /* Set HTX start-line flags */ |
| 594 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 595 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 596 | |
| 597 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, phdr[H2_PHDR_IDX_METH], phdr[uri_idx], ist("HTTP/2.0")); |
| 598 | if (!sl) |
| 599 | goto fail; |
| 600 | |
| 601 | sl->info.req.meth = find_http_meth(phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len); |
Christopher Faulet | a9a5c04 | 2019-06-14 10:25:47 +0200 | [diff] [blame] | 602 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 603 | sl->flags |= (isteqi(phdr[H2_PHDR_IDX_SCHM], ist("http")) ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 604 | return sl; |
| 605 | fail: |
| 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 610 | * being <NULL,0> and emits the equivalent HTX request according to the rules |
| 611 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 612 | * non-zero is returned if some bytes were emitted. In case of error, a |
| 613 | * negative error code is returned. |
| 614 | * |
| 615 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 616 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 617 | * if a body is detected (!ES). |
| 618 | * |
| 619 | * The headers list <list> must be composed of : |
| 620 | * - n.name != NULL, n.len > 0 : literal header name |
| 621 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 622 | * among H2_PHDR_IDX_* |
| 623 | * - n.name ignored, n.len == 0 : end of list |
| 624 | * - in all cases except the end of list, v.name and v.len must designate a |
| 625 | * valid value. |
| 626 | * |
| 627 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 628 | * will be used to create a linked list, so its contents may be destroyed. |
| 629 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 630 | int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 631 | { |
| 632 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 633 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 634 | uint32_t idx; |
| 635 | int ck, lck; /* cookie index and last cookie index */ |
| 636 | int phdr; |
| 637 | int ret; |
| 638 | int i; |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 639 | uint32_t used = htx_used_space(htx); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 640 | struct htx_sl *sl = NULL; |
| 641 | unsigned int sl_flags = 0; |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 642 | const char *ctl; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 643 | |
| 644 | lck = ck = -1; // no cookie for now |
| 645 | fields = 0; |
| 646 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 647 | if (!list[idx].n.ptr) { |
| 648 | /* this is an indexed pseudo-header */ |
| 649 | phdr = list[idx].n.len; |
| 650 | } |
| 651 | else { |
| 652 | /* this can be any type of header */ |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 653 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 654 | * #10.3: header names must be valid (i.e. match a token). |
| 655 | * For pseudo-headers we check from 2nd char and for other ones |
| 656 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 657 | * the colon. |
| 658 | */ |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 659 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 660 | |
| 661 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 662 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 663 | goto fail; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 666 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 667 | * rejecting NUL, CR and LF characters. |
| 668 | */ |
| 669 | ctl = ist_find_ctl(list[idx].v); |
| 670 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 671 | goto fail; |
| 672 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 673 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 674 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 675 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 676 | if (fields & H2_PHDR_FND_NONE) { |
| 677 | /* pseudo header field after regular headers */ |
| 678 | goto fail; |
| 679 | } |
| 680 | else { |
| 681 | /* repeated pseudo header field */ |
| 682 | goto fail; |
| 683 | } |
| 684 | } |
| 685 | fields |= 1 << phdr; |
| 686 | phdr_val[phdr] = list[idx].v; |
| 687 | continue; |
| 688 | } |
| 689 | else if (phdr != 0) { |
| 690 | /* invalid pseudo header -- should never happen here */ |
| 691 | goto fail; |
| 692 | } |
| 693 | |
| 694 | /* regular header field in (name,value) */ |
| 695 | if (unlikely(!(fields & H2_PHDR_FND_NONE))) { |
| 696 | /* no more pseudo-headers, time to build the request line */ |
| 697 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 698 | if (!sl) |
| 699 | goto fail; |
| 700 | fields |= H2_PHDR_FND_NONE; |
| 701 | } |
| 702 | |
| 703 | if (isteq(list[idx].n, ist("host"))) |
| 704 | fields |= H2_PHDR_FND_HOST; |
| 705 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 706 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 707 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 708 | if (ret < 0) |
| 709 | goto fail; |
| 710 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 711 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 712 | if (ret == 0) |
| 713 | continue; // skip this duplicate |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* these ones are forbidden in requests (RFC7540#8.1.2.2) */ |
| 717 | if (isteq(list[idx].n, ist("connection")) || |
| 718 | isteq(list[idx].n, ist("proxy-connection")) || |
| 719 | isteq(list[idx].n, ist("keep-alive")) || |
| 720 | isteq(list[idx].n, ist("upgrade")) || |
| 721 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 722 | goto fail; |
| 723 | |
| 724 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 725 | goto fail; |
| 726 | |
| 727 | /* cookie requires special processing at the end */ |
| 728 | if (isteq(list[idx].n, ist("cookie"))) { |
| 729 | list[idx].n.len = -1; |
| 730 | |
| 731 | if (ck < 0) |
| 732 | ck = idx; |
| 733 | else |
| 734 | list[lck].n.len = idx; |
| 735 | |
| 736 | lck = idx; |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 741 | goto fail; |
| 742 | } |
| 743 | |
| 744 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 745 | if (fields & H2_PHDR_FND_STAT) |
| 746 | goto fail; |
| 747 | |
| 748 | /* Let's dump the request now if not yet emitted. */ |
| 749 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 750 | sl = h2_prepare_htx_reqline(fields, phdr_val, htx, msgf); |
| 751 | if (!sl) |
| 752 | goto fail; |
| 753 | } |
| 754 | |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 755 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0)) |
| 756 | sl_flags |= HTX_SL_F_BODYLESS; |
| 757 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 758 | /* update the start line with last detected header info */ |
| 759 | sl->flags |= sl_flags; |
| 760 | |
| 761 | /* complete with missing Host if needed */ |
| 762 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 763 | /* missing Host field, use :authority instead */ |
| 764 | if (!htx_add_header(htx, ist("host"), phdr_val[H2_PHDR_IDX_AUTH])) |
| 765 | goto fail; |
| 766 | } |
| 767 | |
| 768 | /* now we may have to build a cookie list. We'll dump the values of all |
| 769 | * visited headers. |
| 770 | */ |
| 771 | if (ck >= 0) { |
| 772 | uint32_t fs; // free space |
| 773 | uint32_t bs; // block size |
| 774 | uint32_t vl; // value len |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 775 | uint32_t tl; // total length |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 776 | struct htx_blk *blk; |
| 777 | |
| 778 | blk = htx_add_header(htx, ist("cookie"), list[ck].v); |
| 779 | if (!blk) |
| 780 | goto fail; |
| 781 | |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 782 | tl = list[ck].v.len; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 783 | fs = htx_free_data_space(htx); |
| 784 | bs = htx_get_blksz(blk); |
| 785 | |
| 786 | /* for each extra cookie, we'll extend the cookie's value and |
| 787 | * insert "; " before the new value. |
| 788 | */ |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 789 | fs += tl; // first one is already counted |
| 790 | for (; (ck = list[ck].n.len) >= 0 ; ) { |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 791 | vl = list[ck].v.len; |
Willy Tarreau | 164e061 | 2018-12-18 11:00:41 +0100 | [diff] [blame] | 792 | tl += vl + 2; |
| 793 | if (tl > fs) |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 794 | goto fail; |
| 795 | |
Christopher Faulet | 41dc843 | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 796 | htx_change_blk_value_len(htx, blk, tl); |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 797 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 0) = ';'; |
| 798 | *(char *)(htx_get_blk_ptr(htx, blk) + bs + 1) = ' '; |
| 799 | memcpy(htx_get_blk_ptr(htx, blk) + bs + 2, list[ck].v.ptr, vl); |
| 800 | bs += vl + 2; |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | } |
| 804 | |
| 805 | /* now send the end of headers marker */ |
| 806 | htx_add_endof(htx, HTX_BLK_EOH); |
| 807 | |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 808 | /* Set bytes used in the HTX mesage for the headers now */ |
| 809 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 810 | |
Willy Tarreau | 6deb412 | 2018-11-27 15:34:18 +0100 | [diff] [blame] | 811 | ret = 1; |
| 812 | return ret; |
| 813 | |
| 814 | fail: |
| 815 | return -1; |
| 816 | } |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 817 | |
| 818 | /* Prepare the status line into <htx> from pseudo headers stored in <phdr[]>. |
| 819 | * <fields> indicates what was found so far. This should be called once at the |
| 820 | * detection of the first general header field or at the end of the message if |
| 821 | * no general header field was found yet. Returns the created start line on |
| 822 | * success, or NULL on failure. Upon success, <msgf> is updated with a few |
| 823 | * H2_MSGF_* flags indicating what was found while parsing. |
| 824 | */ |
| 825 | static struct htx_sl *h2_prepare_htx_stsline(uint32_t fields, struct ist *phdr, struct htx *htx, unsigned int *msgf) |
| 826 | { |
| 827 | unsigned int flags = HTX_SL_F_NONE; |
| 828 | struct htx_sl *sl; |
| 829 | unsigned char h, t, u; |
| 830 | |
| 831 | /* only :status is allowed as a pseudo header */ |
| 832 | if (!(fields & H2_PHDR_FND_STAT)) |
| 833 | goto fail; |
| 834 | |
| 835 | if (phdr[H2_PHDR_IDX_STAT].len != 3) |
| 836 | goto fail; |
| 837 | |
| 838 | /* Set HTX start-line flags */ |
| 839 | flags |= HTX_SL_F_VER_11; // V2 in fact |
| 840 | flags |= HTX_SL_F_XFER_LEN; // xfer len always known with H2 |
| 841 | |
| 842 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/2.0"), phdr[H2_PHDR_IDX_STAT], ist("")); |
| 843 | if (!sl) |
| 844 | goto fail; |
| 845 | |
| 846 | h = phdr[H2_PHDR_IDX_STAT].ptr[0] - '0'; |
| 847 | t = phdr[H2_PHDR_IDX_STAT].ptr[1] - '0'; |
| 848 | u = phdr[H2_PHDR_IDX_STAT].ptr[2] - '0'; |
| 849 | if (h > 9 || t > 9 || u > 9) |
| 850 | goto fail; |
| 851 | |
| 852 | sl->info.res.status = h * 100 + t * 10 + u; |
| 853 | |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 854 | /* On 1xx responses (except 101) there is no ES on the HEADERS frame but |
| 855 | * there is no body. So remove the flag H2_MSGF_BODY and add |
| 856 | * H2_MSGF_RSP_1XX to notify the decoder another HEADERS frame is |
| 857 | * expected. |
| 858 | */ |
| 859 | if (sl->info.res.status < 200 && |
| 860 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) { |
| 861 | *msgf |= H2_MSGF_RSP_1XX; |
| 862 | *msgf &= ~H2_MSGF_BODY; |
| 863 | } |
| 864 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 865 | return sl; |
| 866 | fail: |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | /* Takes an H2 response present in the headers list <list> terminated by a name |
| 871 | * being <NULL,0> and emits the equivalent HTX response according to the rules |
| 872 | * documented in RFC7540 #8.1.2. The output contents are emitted in <htx>, and |
| 873 | * a positive value is returned if some bytes were emitted. In case of error, a |
| 874 | * negative error code is returned. |
| 875 | * |
| 876 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 877 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 878 | * if a body is detected (!ES). |
| 879 | * |
| 880 | * The headers list <list> must be composed of : |
| 881 | * - n.name != NULL, n.len > 0 : literal header name |
| 882 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 883 | * among H2_PHDR_IDX_* |
| 884 | * - n.name ignored, n.len == 0 : end of list |
| 885 | * - in all cases except the end of list, v.name and v.len must designate a |
| 886 | * valid value. |
| 887 | */ |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 888 | int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len) |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 889 | { |
| 890 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 891 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 892 | uint32_t idx; |
| 893 | int phdr; |
| 894 | int ret; |
| 895 | int i; |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 896 | uint32_t used = htx_used_space(htx); |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 897 | struct htx_sl *sl = NULL; |
| 898 | unsigned int sl_flags = 0; |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 899 | const char *ctl; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 900 | |
| 901 | fields = 0; |
| 902 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 903 | if (!list[idx].n.ptr) { |
| 904 | /* this is an indexed pseudo-header */ |
| 905 | phdr = list[idx].n.len; |
| 906 | } |
| 907 | else { |
| 908 | /* this can be any type of header */ |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 909 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 910 | * #10.3: header names must be valid (i.e. match a token). |
| 911 | * For pseudo-headers we check from 2nd char and for other ones |
| 912 | * from the first char, because HTTP_IS_TOKEN() also excludes |
| 913 | * the colon. |
| 914 | */ |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 915 | phdr = h2_str_to_phdr(list[idx].n); |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 916 | |
| 917 | for (i = !!phdr; i < list[idx].n.len; i++) |
| 918 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A' || !HTTP_IS_TOKEN(list[idx].n.ptr[i])) |
| 919 | goto fail; |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 922 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 923 | * rejecting NUL, CR and LF characters. |
| 924 | */ |
| 925 | ctl = ist_find_ctl(list[idx].v); |
| 926 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 927 | goto fail; |
| 928 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 929 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 930 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 931 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 932 | if (fields & H2_PHDR_FND_NONE) { |
| 933 | /* pseudo header field after regular headers */ |
| 934 | goto fail; |
| 935 | } |
| 936 | else { |
| 937 | /* repeated pseudo header field */ |
| 938 | goto fail; |
| 939 | } |
| 940 | } |
| 941 | fields |= 1 << phdr; |
| 942 | phdr_val[phdr] = list[idx].v; |
| 943 | continue; |
| 944 | } |
| 945 | else if (phdr != 0) { |
| 946 | /* invalid pseudo header -- should never happen here */ |
| 947 | goto fail; |
| 948 | } |
| 949 | |
| 950 | /* regular header field in (name,value) */ |
| 951 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 952 | /* no more pseudo-headers, time to build the status line */ |
| 953 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 954 | if (!sl) |
| 955 | goto fail; |
| 956 | fields |= H2_PHDR_FND_NONE; |
| 957 | } |
| 958 | |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 959 | if (isteq(list[idx].n, ist("content-length"))) { |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 960 | ret = h2_parse_cont_len_header(msgf, &list[idx].v, body_len); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 961 | if (ret < 0) |
| 962 | goto fail; |
| 963 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 964 | sl_flags |= HTX_SL_F_CLEN; |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 965 | if (ret == 0) |
| 966 | continue; // skip this duplicate |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* these ones are forbidden in responses (RFC7540#8.1.2.2) */ |
| 970 | if (isteq(list[idx].n, ist("connection")) || |
| 971 | isteq(list[idx].n, ist("proxy-connection")) || |
| 972 | isteq(list[idx].n, ist("keep-alive")) || |
| 973 | isteq(list[idx].n, ist("upgrade")) || |
| 974 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 975 | goto fail; |
| 976 | |
| 977 | if (!htx_add_header(htx, list[idx].n, list[idx].v)) |
| 978 | goto fail; |
| 979 | } |
| 980 | |
| 981 | /* RFC7540#8.1.2.1 mandates to reject request pseudo-headers */ |
| 982 | if (fields & (H2_PHDR_FND_AUTH|H2_PHDR_FND_METH|H2_PHDR_FND_PATH|H2_PHDR_FND_SCHM)) |
| 983 | goto fail; |
| 984 | |
| 985 | /* Let's dump the request now if not yet emitted. */ |
| 986 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 987 | sl = h2_prepare_htx_stsline(fields, phdr_val, htx, msgf); |
| 988 | if (!sl) |
| 989 | goto fail; |
| 990 | } |
| 991 | |
Christopher Faulet | 44af3cf | 2019-02-18 10:12:56 +0100 | [diff] [blame] | 992 | if (!(*msgf & H2_MSGF_BODY) || ((*msgf & H2_MSGF_BODY_CL) && *body_len == 0)) |
| 993 | sl_flags |= HTX_SL_F_BODYLESS; |
| 994 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 995 | /* update the start line with last detected header info */ |
| 996 | sl->flags |= sl_flags; |
| 997 | |
| 998 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 999 | /* FIXME: Do we need to signal anything when we have a body and |
| 1000 | * no content-length, to have the equivalent of H1's chunked |
| 1001 | * encoding? |
| 1002 | */ |
| 1003 | } |
| 1004 | |
| 1005 | /* now send the end of headers marker */ |
| 1006 | htx_add_endof(htx, HTX_BLK_EOH); |
| 1007 | |
Christopher Faulet | 33543e7 | 2019-05-15 15:53:20 +0200 | [diff] [blame] | 1008 | /* Set bytes used in the HTX mesage for the headers now */ |
| 1009 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 1010 | |
Willy Tarreau | 1329b5b | 2018-10-08 14:49:20 +0200 | [diff] [blame] | 1011 | ret = 1; |
| 1012 | return ret; |
| 1013 | |
| 1014 | fail: |
| 1015 | return -1; |
| 1016 | } |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1017 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1018 | /* Takes an H2 headers list <list> terminated by a name being <NULL,0> and emits |
| 1019 | * the equivalent HTX trailers blocks. The output contents are emitted in <htx>, |
| 1020 | * and a positive value is returned if some bytes were emitted. In case of |
| 1021 | * error, a negative error code is returned. The caller must have verified that |
| 1022 | * the message in the buffer is compatible with receipt of trailers. |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1023 | * |
| 1024 | * The headers list <list> must be composed of : |
| 1025 | * - n.name != NULL, n.len > 0 : literal header name |
| 1026 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 1027 | * among H2_PHDR_IDX_* (illegal here) |
| 1028 | * - n.name ignored, n.len == 0 : end of list |
| 1029 | * - in all cases except the end of list, v.name and v.len must designate a |
| 1030 | * valid value. |
| 1031 | */ |
| 1032 | int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx) |
| 1033 | { |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 1034 | const char *ctl; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1035 | uint32_t idx; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1036 | int i; |
| 1037 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1038 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 1039 | if (!list[idx].n.ptr) { |
| 1040 | /* This is an indexed pseudo-header (RFC7540#8.1.2.1) */ |
| 1041 | goto fail; |
| 1042 | } |
| 1043 | |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 1044 | /* RFC7540#8.1.2: upper case not allowed in header field names. |
| 1045 | * #10.3: header names must be valid (i.e. match a token). This |
| 1046 | * also catches pseudo-headers which are forbidden in trailers. |
| 1047 | */ |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1048 | for (i = 0; i < list[idx].n.len; i++) |
Willy Tarreau | 5eaeec5 | 2019-11-24 10:34:39 +0100 | [diff] [blame] | 1049 | 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] | 1050 | goto fail; |
| 1051 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1052 | /* these ones are forbidden in trailers (RFC7540#8.1.2.2) */ |
| 1053 | if (isteq(list[idx].n, ist("host")) || |
| 1054 | isteq(list[idx].n, ist("content-length")) || |
| 1055 | isteq(list[idx].n, ist("connection")) || |
| 1056 | isteq(list[idx].n, ist("proxy-connection")) || |
| 1057 | isteq(list[idx].n, ist("keep-alive")) || |
| 1058 | isteq(list[idx].n, ist("upgrade")) || |
| 1059 | isteq(list[idx].n, ist("te")) || |
| 1060 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 1061 | goto fail; |
| 1062 | |
Willy Tarreau | c36c678 | 2019-11-22 16:02:43 +0100 | [diff] [blame] | 1063 | /* RFC7540#10.3: intermediaries forwarding to HTTP/1 must take care of |
| 1064 | * rejecting NUL, CR and LF characters. |
| 1065 | */ |
| 1066 | ctl = ist_find_ctl(list[idx].v); |
| 1067 | if (unlikely(ctl) && has_forbidden_char(list[idx].v, ctl)) |
| 1068 | goto fail; |
| 1069 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1070 | if (!htx_add_trailer(htx, list[idx].n, list[idx].v)) |
| 1071 | goto fail; |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1074 | if (!htx_add_endof(htx, HTX_BLK_EOT)) |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1075 | goto fail; |
| 1076 | |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 1077 | return 1; |
| 1078 | |
| 1079 | fail: |
| 1080 | return -1; |
| 1081 | } |