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