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 | |
| 28 | #include <stdint.h> |
| 29 | #include <common/config.h> |
| 30 | #include <common/h2.h> |
| 31 | #include <common/http-hdr.h> |
| 32 | #include <common/ist.h> |
| 33 | |
| 34 | |
| 35 | /* Prepare the request line into <*ptr> (stopping at <end>) from pseudo headers |
| 36 | * stored in <phdr[]>. <fields> indicates what was found so far. This should be |
| 37 | * called once at the detection of the first general header field or at the end |
| 38 | * 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] | 39 | * or a negative error code on failure. Upon success, <msgf> is updated with a |
| 40 | * few H2_MSGF_* flags indicating what was found while parsing. |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 41 | */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | char *out = *ptr; |
| 45 | int uri_idx = H2_PHDR_IDX_PATH; |
| 46 | |
| 47 | if ((fields & H2_PHDR_FND_METH) && isteq(phdr[H2_PHDR_IDX_METH], ist("CONNECT"))) { |
| 48 | /* RFC 7540 #8.2.6 regarding CONNECT: ":scheme" and ":path" |
| 49 | * MUST be omitted ; ":authority" contains the host and port |
| 50 | * to connect to. |
| 51 | */ |
| 52 | if (fields & H2_PHDR_FND_SCHM) { |
| 53 | /* scheme not allowed */ |
| 54 | goto fail; |
| 55 | } |
| 56 | else if (fields & H2_PHDR_FND_PATH) { |
| 57 | /* path not allowed */ |
| 58 | goto fail; |
| 59 | } |
| 60 | else if (!(fields & H2_PHDR_FND_AUTH)) { |
| 61 | /* missing authority */ |
| 62 | goto fail; |
| 63 | } |
| 64 | // otherwise OK ; let's use the authority instead of the URI |
| 65 | uri_idx = H2_PHDR_IDX_AUTH; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 66 | *msgf |= H2_MSGF_BODY_TUNNEL; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 67 | } |
| 68 | else if ((fields & (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) != |
| 69 | (H2_PHDR_FND_METH|H2_PHDR_FND_SCHM|H2_PHDR_FND_PATH)) { |
| 70 | /* RFC 7540 #8.1.2.3 : all requests MUST include exactly one |
| 71 | * valid value for the ":method", ":scheme" and ":path" phdr |
| 72 | * unless it is a CONNECT request. |
| 73 | */ |
| 74 | if (!(fields & H2_PHDR_FND_METH)) { |
| 75 | /* missing method */ |
| 76 | goto fail; |
| 77 | } |
| 78 | else if (!(fields & H2_PHDR_FND_SCHM)) { |
| 79 | /* missing scheme */ |
| 80 | goto fail; |
| 81 | } |
| 82 | else { |
| 83 | /* missing path */ |
| 84 | goto fail; |
| 85 | } |
| 86 | } |
| 87 | |
Willy Tarreau | cd4fe17 | 2017-12-03 11:51:31 +0100 | [diff] [blame] | 88 | /* 7540#8.1.2.3: :path must not be empty */ |
| 89 | if (!phdr[uri_idx].len) |
| 90 | goto fail; |
| 91 | |
Willy Tarreau | 811ad12 | 2017-12-03 09:44:50 +0100 | [diff] [blame] | 92 | 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] | 93 | /* too large */ |
| 94 | goto fail; |
| 95 | } |
| 96 | |
| 97 | memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len); |
| 98 | out += phdr[H2_PHDR_IDX_METH].len; |
| 99 | *(out++) = ' '; |
| 100 | |
| 101 | memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len); |
| 102 | out += phdr[uri_idx].len; |
| 103 | memcpy(out, " HTTP/1.1\r\n", 11); |
| 104 | out += 11; |
| 105 | |
| 106 | *ptr = out; |
| 107 | return 0; |
| 108 | fail: |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 113 | * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the |
| 114 | * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out> |
| 115 | * for a max of <osize> bytes, and the amount of bytes emitted is returned. In |
| 116 | * case of error, a negative error code is returned. |
| 117 | * |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 118 | * Upon success, <msgf> is filled with a few H2_MSGF_* flags indicating what |
| 119 | * was found while parsing. The caller must set it to zero in or H2_MSGF_BODY |
| 120 | * if a body is detected (!ES). |
| 121 | * |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 122 | * The headers list <list> must be composed of : |
| 123 | * - n.name != NULL, n.len > 0 : literal header name |
| 124 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 125 | * among H2_PHDR_IDX_* |
| 126 | * - n.name ignored, n.len == 0 : end of list |
| 127 | * - in all cases except the end of list, v.name and v.len must designate a |
| 128 | * valid value. |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 129 | * |
| 130 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 131 | * 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] | 132 | */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 133 | int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf) |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 134 | { |
| 135 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 136 | char *out_end = out + osize; |
| 137 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 138 | uint32_t idx; |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 139 | int ck, lck; /* cookie index and last cookie index */ |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 140 | int phdr; |
| 141 | int ret; |
Willy Tarreau | 637f64d | 2017-12-03 20:28:13 +0100 | [diff] [blame] | 142 | int i; |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 144 | lck = ck = -1; // no cookie for now |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 145 | fields = 0; |
| 146 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 147 | if (!list[idx].n.ptr) { |
| 148 | /* this is an indexed pseudo-header */ |
| 149 | phdr = list[idx].n.len; |
| 150 | } |
| 151 | else { |
| 152 | /* this can be any type of header */ |
Willy Tarreau | 637f64d | 2017-12-03 20:28:13 +0100 | [diff] [blame] | 153 | /* RFC7540#8.1.2: upper case not allowed in header field names */ |
| 154 | for (i = 0; i < list[idx].n.len; i++) |
| 155 | if ((uint8_t)(list[idx].n.ptr[i] - 'A') < 'Z' - 'A') |
| 156 | goto fail; |
| 157 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 158 | phdr = h2_str_to_phdr(list[idx].n); |
| 159 | } |
| 160 | |
| 161 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 162 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 163 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 164 | if (fields & H2_PHDR_FND_NONE) { |
| 165 | /* pseudo header field after regular headers */ |
| 166 | goto fail; |
| 167 | } |
| 168 | else { |
| 169 | /* repeated pseudo header field */ |
| 170 | goto fail; |
| 171 | } |
| 172 | } |
| 173 | fields |= 1 << phdr; |
| 174 | phdr_val[phdr] = list[idx].v; |
| 175 | continue; |
| 176 | } |
| 177 | else if (phdr != 0) { |
| 178 | /* invalid pseudo header -- should never happen here */ |
| 179 | goto fail; |
| 180 | } |
| 181 | |
| 182 | /* regular header field in (name,value) */ |
| 183 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 184 | /* no more pseudo-headers, time to build the request line */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 185 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf); |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 186 | if (ret != 0) |
| 187 | goto leave; |
| 188 | fields |= H2_PHDR_FND_NONE; |
| 189 | } |
| 190 | |
| 191 | if (isteq(list[idx].n, ist("host"))) |
| 192 | fields |= H2_PHDR_FND_HOST; |
| 193 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 194 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY && |
| 195 | isteq(list[idx].n, ist("content-length"))) |
| 196 | *msgf |= H2_MSGF_BODY_CL; |
| 197 | |
Willy Tarreau | fe7c356 | 2017-12-03 20:15:34 +0100 | [diff] [blame] | 198 | /* these ones are forbidden in requests (RFC7540#8.1.2.2) */ |
| 199 | if (isteq(list[idx].n, ist("connection")) || |
| 200 | isteq(list[idx].n, ist("proxy-connection")) || |
| 201 | isteq(list[idx].n, ist("keep-alive")) || |
| 202 | isteq(list[idx].n, ist("upgrade")) || |
| 203 | isteq(list[idx].n, ist("transfer-encoding"))) |
| 204 | goto fail; |
| 205 | |
Willy Tarreau | d8d2ac7 | 2017-12-03 18:41:31 +0100 | [diff] [blame] | 206 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 207 | goto fail; |
| 208 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 209 | /* cookie requires special processing at the end */ |
| 210 | if (isteq(list[idx].n, ist("cookie"))) { |
| 211 | list[idx].n.len = -1; |
| 212 | |
| 213 | if (ck < 0) |
| 214 | ck = idx; |
| 215 | else |
| 216 | list[lck].n.len = idx; |
| 217 | |
| 218 | lck = idx; |
| 219 | continue; |
| 220 | } |
| 221 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 222 | if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) { |
| 223 | /* too large */ |
| 224 | goto fail; |
| 225 | } |
| 226 | |
| 227 | /* copy "name: value" */ |
| 228 | memcpy(out, list[idx].n.ptr, list[idx].n.len); |
| 229 | out += list[idx].n.len; |
| 230 | *(out++) = ':'; |
| 231 | *(out++) = ' '; |
| 232 | |
| 233 | memcpy(out, list[idx].v.ptr, list[idx].v.len); |
| 234 | out += list[idx].v.len; |
| 235 | *(out++) = '\r'; |
| 236 | *(out++) = '\n'; |
| 237 | } |
| 238 | |
Willy Tarreau | 5208869 | 2017-12-03 20:13:54 +0100 | [diff] [blame] | 239 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 240 | if (fields & H2_PHDR_FND_STAT) |
| 241 | goto fail; |
| 242 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 243 | /* Let's dump the request now if not yet emitted. */ |
| 244 | if (!(fields & H2_PHDR_FND_NONE)) { |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 245 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end, msgf); |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 246 | if (ret != 0) |
| 247 | goto leave; |
| 248 | } |
| 249 | |
| 250 | /* complete with missing Host if needed */ |
| 251 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 252 | /* missing Host field, use :authority instead */ |
| 253 | if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) { |
| 254 | /* too large */ |
| 255 | goto fail; |
| 256 | } |
| 257 | |
| 258 | memcpy(out, "host: ", 6); |
| 259 | memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len); |
| 260 | out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len; |
| 261 | *(out++) = '\r'; |
| 262 | *(out++) = '\n'; |
| 263 | } |
| 264 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 265 | if ((*msgf & (H2_MSGF_BODY|H2_MSGF_BODY_TUNNEL|H2_MSGF_BODY_CL)) == H2_MSGF_BODY) { |
| 266 | /* add chunked encoding */ |
| 267 | if (out + 28 > out_end) |
| 268 | goto fail; |
| 269 | memcpy(out, "transfer-encoding: chunked\r\n", 28); |
| 270 | out += 28; |
| 271 | } |
| 272 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 273 | /* now we may have to build a cookie list. We'll dump the values of all |
| 274 | * visited headers. |
| 275 | */ |
| 276 | if (ck >= 0) { |
| 277 | if (out + 8 > out_end) { |
| 278 | /* too large */ |
| 279 | goto fail; |
| 280 | } |
| 281 | memcpy(out, "cookie: ", 8); |
| 282 | out += 8; |
| 283 | |
| 284 | do { |
| 285 | if (out + list[ck].v.len + 2 > out_end) { |
| 286 | /* too large */ |
| 287 | goto fail; |
| 288 | } |
| 289 | memcpy(out, list[ck].v.ptr, list[ck].v.len); |
| 290 | out += list[ck].v.len; |
| 291 | ck = list[ck].n.len; |
| 292 | |
| 293 | if (ck >= 0) { |
| 294 | *(out++) = ';'; |
| 295 | *(out++) = ' '; |
| 296 | } |
| 297 | } while (ck >= 0); |
| 298 | |
| 299 | if (out + 2 > out_end) { |
| 300 | /* too large */ |
| 301 | goto fail; |
| 302 | } |
| 303 | *(out++) = '\r'; |
| 304 | *(out++) = '\n'; |
| 305 | } |
| 306 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 307 | /* And finish */ |
| 308 | if (out + 2 > out_end) { |
| 309 | /* too large */ |
| 310 | goto fail; |
| 311 | } |
| 312 | |
| 313 | *(out++) = '\r'; |
| 314 | *(out++) = '\n'; |
| 315 | ret = out + osize - out_end; |
| 316 | leave: |
| 317 | return ret; |
| 318 | |
| 319 | fail: |
| 320 | return -1; |
| 321 | } |