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; |
| 136 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 137 | lck = ck = -1; // no cookie for now |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 138 | fields = 0; |
| 139 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 140 | if (!list[idx].n.ptr) { |
| 141 | /* this is an indexed pseudo-header */ |
| 142 | phdr = list[idx].n.len; |
| 143 | } |
| 144 | else { |
| 145 | /* this can be any type of header */ |
| 146 | phdr = h2_str_to_phdr(list[idx].n); |
| 147 | } |
| 148 | |
| 149 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 150 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 151 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 152 | if (fields & H2_PHDR_FND_NONE) { |
| 153 | /* pseudo header field after regular headers */ |
| 154 | goto fail; |
| 155 | } |
| 156 | else { |
| 157 | /* repeated pseudo header field */ |
| 158 | goto fail; |
| 159 | } |
| 160 | } |
| 161 | fields |= 1 << phdr; |
| 162 | phdr_val[phdr] = list[idx].v; |
| 163 | continue; |
| 164 | } |
| 165 | else if (phdr != 0) { |
| 166 | /* invalid pseudo header -- should never happen here */ |
| 167 | goto fail; |
| 168 | } |
| 169 | |
| 170 | /* regular header field in (name,value) */ |
| 171 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 172 | /* no more pseudo-headers, time to build the request line */ |
| 173 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end); |
| 174 | if (ret != 0) |
| 175 | goto leave; |
| 176 | fields |= H2_PHDR_FND_NONE; |
| 177 | } |
| 178 | |
| 179 | if (isteq(list[idx].n, ist("host"))) |
| 180 | fields |= H2_PHDR_FND_HOST; |
| 181 | |
Willy Tarreau | d8d2ac7 | 2017-12-03 18:41:31 +0100 | [diff] [blame] | 182 | if (isteq(list[idx].n, ist("te")) && !isteq(list[idx].v, ist("trailers"))) |
| 183 | goto fail; |
| 184 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 185 | /* cookie requires special processing at the end */ |
| 186 | if (isteq(list[idx].n, ist("cookie"))) { |
| 187 | list[idx].n.len = -1; |
| 188 | |
| 189 | if (ck < 0) |
| 190 | ck = idx; |
| 191 | else |
| 192 | list[lck].n.len = idx; |
| 193 | |
| 194 | lck = idx; |
| 195 | continue; |
| 196 | } |
| 197 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 198 | if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) { |
| 199 | /* too large */ |
| 200 | goto fail; |
| 201 | } |
| 202 | |
| 203 | /* copy "name: value" */ |
| 204 | memcpy(out, list[idx].n.ptr, list[idx].n.len); |
| 205 | out += list[idx].n.len; |
| 206 | *(out++) = ':'; |
| 207 | *(out++) = ' '; |
| 208 | |
| 209 | memcpy(out, list[idx].v.ptr, list[idx].v.len); |
| 210 | out += list[idx].v.len; |
| 211 | *(out++) = '\r'; |
| 212 | *(out++) = '\n'; |
| 213 | } |
| 214 | |
Willy Tarreau | 5208869 | 2017-12-03 20:13:54 +0100 | [diff] [blame^] | 215 | /* RFC7540#8.1.2.1 mandates to reject response pseudo-headers (:status) */ |
| 216 | if (fields & H2_PHDR_FND_STAT) |
| 217 | goto fail; |
| 218 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 219 | /* Let's dump the request now if not yet emitted. */ |
| 220 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 221 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end); |
| 222 | if (ret != 0) |
| 223 | goto leave; |
| 224 | } |
| 225 | |
| 226 | /* complete with missing Host if needed */ |
| 227 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 228 | /* missing Host field, use :authority instead */ |
| 229 | if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) { |
| 230 | /* too large */ |
| 231 | goto fail; |
| 232 | } |
| 233 | |
| 234 | memcpy(out, "host: ", 6); |
| 235 | memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len); |
| 236 | out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len; |
| 237 | *(out++) = '\r'; |
| 238 | *(out++) = '\n'; |
| 239 | } |
| 240 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 241 | /* now we may have to build a cookie list. We'll dump the values of all |
| 242 | * visited headers. |
| 243 | */ |
| 244 | if (ck >= 0) { |
| 245 | if (out + 8 > out_end) { |
| 246 | /* too large */ |
| 247 | goto fail; |
| 248 | } |
| 249 | memcpy(out, "cookie: ", 8); |
| 250 | out += 8; |
| 251 | |
| 252 | do { |
| 253 | if (out + list[ck].v.len + 2 > out_end) { |
| 254 | /* too large */ |
| 255 | goto fail; |
| 256 | } |
| 257 | memcpy(out, list[ck].v.ptr, list[ck].v.len); |
| 258 | out += list[ck].v.len; |
| 259 | ck = list[ck].n.len; |
| 260 | |
| 261 | if (ck >= 0) { |
| 262 | *(out++) = ';'; |
| 263 | *(out++) = ' '; |
| 264 | } |
| 265 | } while (ck >= 0); |
| 266 | |
| 267 | if (out + 2 > out_end) { |
| 268 | /* too large */ |
| 269 | goto fail; |
| 270 | } |
| 271 | *(out++) = '\r'; |
| 272 | *(out++) = '\n'; |
| 273 | } |
| 274 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 275 | /* And finish */ |
| 276 | if (out + 2 > out_end) { |
| 277 | /* too large */ |
| 278 | goto fail; |
| 279 | } |
| 280 | |
| 281 | *(out++) = '\r'; |
| 282 | *(out++) = '\n'; |
| 283 | ret = out + osize - out_end; |
| 284 | leave: |
| 285 | return ret; |
| 286 | |
| 287 | fail: |
| 288 | return -1; |
| 289 | } |