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 | |
| 86 | if (out + phdr[uri_idx].len + 1 + phdr[uri_idx].len + 11 > end) { |
| 87 | /* too large */ |
| 88 | goto fail; |
| 89 | } |
| 90 | |
| 91 | memcpy(out, phdr[H2_PHDR_IDX_METH].ptr, phdr[H2_PHDR_IDX_METH].len); |
| 92 | out += phdr[H2_PHDR_IDX_METH].len; |
| 93 | *(out++) = ' '; |
| 94 | |
| 95 | memcpy(out, phdr[uri_idx].ptr, phdr[uri_idx].len); |
| 96 | out += phdr[uri_idx].len; |
| 97 | memcpy(out, " HTTP/1.1\r\n", 11); |
| 98 | out += 11; |
| 99 | |
| 100 | *ptr = out; |
| 101 | return 0; |
| 102 | fail: |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | /* Takes an H2 request present in the headers list <list> terminated by a name |
| 107 | * being <NULL,0> and emits the equivalent HTTP/1.1 request according to the |
| 108 | * rules documented in RFC7540 #8.1.2. The output contents are emitted in <out> |
| 109 | * for a max of <osize> bytes, and the amount of bytes emitted is returned. In |
| 110 | * case of error, a negative error code is returned. |
| 111 | * |
| 112 | * The headers list <list> must be composed of : |
| 113 | * - n.name != NULL, n.len > 0 : literal header name |
| 114 | * - n.name == NULL, n.len > 0 : indexed pseudo header name number <n.len> |
| 115 | * among H2_PHDR_IDX_* |
| 116 | * - n.name ignored, n.len == 0 : end of list |
| 117 | * - in all cases except the end of list, v.name and v.len must designate a |
| 118 | * valid value. |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 119 | * |
| 120 | * The Cookie header will be reassembled at the end, and for this, the <list> |
| 121 | * 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] | 122 | */ |
| 123 | int h2_make_h1_request(struct http_hdr *list, char *out, int osize) |
| 124 | { |
| 125 | struct ist phdr_val[H2_PHDR_NUM_ENTRIES]; |
| 126 | char *out_end = out + osize; |
| 127 | uint32_t fields; /* bit mask of H2_PHDR_FND_* */ |
| 128 | uint32_t idx; |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 129 | int ck, lck; /* cookie index and last cookie index */ |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 130 | int phdr; |
| 131 | int ret; |
| 132 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 133 | lck = ck = -1; // no cookie for now |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 134 | fields = 0; |
| 135 | for (idx = 0; list[idx].n.len != 0; idx++) { |
| 136 | if (!list[idx].n.ptr) { |
| 137 | /* this is an indexed pseudo-header */ |
| 138 | phdr = list[idx].n.len; |
| 139 | } |
| 140 | else { |
| 141 | /* this can be any type of header */ |
| 142 | phdr = h2_str_to_phdr(list[idx].n); |
| 143 | } |
| 144 | |
| 145 | if (phdr > 0 && phdr < H2_PHDR_NUM_ENTRIES) { |
| 146 | /* insert a pseudo header by its index (in phdr) and value (in value) */ |
| 147 | if (fields & ((1 << phdr) | H2_PHDR_FND_NONE)) { |
| 148 | if (fields & H2_PHDR_FND_NONE) { |
| 149 | /* pseudo header field after regular headers */ |
| 150 | goto fail; |
| 151 | } |
| 152 | else { |
| 153 | /* repeated pseudo header field */ |
| 154 | goto fail; |
| 155 | } |
| 156 | } |
| 157 | fields |= 1 << phdr; |
| 158 | phdr_val[phdr] = list[idx].v; |
| 159 | continue; |
| 160 | } |
| 161 | else if (phdr != 0) { |
| 162 | /* invalid pseudo header -- should never happen here */ |
| 163 | goto fail; |
| 164 | } |
| 165 | |
| 166 | /* regular header field in (name,value) */ |
| 167 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 168 | /* no more pseudo-headers, time to build the request line */ |
| 169 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end); |
| 170 | if (ret != 0) |
| 171 | goto leave; |
| 172 | fields |= H2_PHDR_FND_NONE; |
| 173 | } |
| 174 | |
| 175 | if (isteq(list[idx].n, ist("host"))) |
| 176 | fields |= H2_PHDR_FND_HOST; |
| 177 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 178 | /* cookie requires special processing at the end */ |
| 179 | if (isteq(list[idx].n, ist("cookie"))) { |
| 180 | list[idx].n.len = -1; |
| 181 | |
| 182 | if (ck < 0) |
| 183 | ck = idx; |
| 184 | else |
| 185 | list[lck].n.len = idx; |
| 186 | |
| 187 | lck = idx; |
| 188 | continue; |
| 189 | } |
| 190 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 191 | if (out + list[idx].n.len + 2 + list[idx].v.len + 2 > out_end) { |
| 192 | /* too large */ |
| 193 | goto fail; |
| 194 | } |
| 195 | |
| 196 | /* copy "name: value" */ |
| 197 | memcpy(out, list[idx].n.ptr, list[idx].n.len); |
| 198 | out += list[idx].n.len; |
| 199 | *(out++) = ':'; |
| 200 | *(out++) = ' '; |
| 201 | |
| 202 | memcpy(out, list[idx].v.ptr, list[idx].v.len); |
| 203 | out += list[idx].v.len; |
| 204 | *(out++) = '\r'; |
| 205 | *(out++) = '\n'; |
| 206 | } |
| 207 | |
| 208 | /* Let's dump the request now if not yet emitted. */ |
| 209 | if (!(fields & H2_PHDR_FND_NONE)) { |
| 210 | ret = h2_prepare_h1_reqline(fields, phdr_val, &out, out_end); |
| 211 | if (ret != 0) |
| 212 | goto leave; |
| 213 | } |
| 214 | |
| 215 | /* complete with missing Host if needed */ |
| 216 | if ((fields & (H2_PHDR_FND_HOST|H2_PHDR_FND_AUTH)) == H2_PHDR_FND_AUTH) { |
| 217 | /* missing Host field, use :authority instead */ |
| 218 | if (out + 6 + phdr_val[H2_PHDR_IDX_AUTH].len + 2 > out_end) { |
| 219 | /* too large */ |
| 220 | goto fail; |
| 221 | } |
| 222 | |
| 223 | memcpy(out, "host: ", 6); |
| 224 | memcpy(out + 6, phdr_val[H2_PHDR_IDX_AUTH].ptr, phdr_val[H2_PHDR_IDX_AUTH].len); |
| 225 | out += 6 + phdr_val[H2_PHDR_IDX_AUTH].len; |
| 226 | *(out++) = '\r'; |
| 227 | *(out++) = '\n'; |
| 228 | } |
| 229 | |
Willy Tarreau | 2fb986c | 2017-11-21 21:01:29 +0100 | [diff] [blame] | 230 | /* now we may have to build a cookie list. We'll dump the values of all |
| 231 | * visited headers. |
| 232 | */ |
| 233 | if (ck >= 0) { |
| 234 | if (out + 8 > out_end) { |
| 235 | /* too large */ |
| 236 | goto fail; |
| 237 | } |
| 238 | memcpy(out, "cookie: ", 8); |
| 239 | out += 8; |
| 240 | |
| 241 | do { |
| 242 | if (out + list[ck].v.len + 2 > out_end) { |
| 243 | /* too large */ |
| 244 | goto fail; |
| 245 | } |
| 246 | memcpy(out, list[ck].v.ptr, list[ck].v.len); |
| 247 | out += list[ck].v.len; |
| 248 | ck = list[ck].n.len; |
| 249 | |
| 250 | if (ck >= 0) { |
| 251 | *(out++) = ';'; |
| 252 | *(out++) = ' '; |
| 253 | } |
| 254 | } while (ck >= 0); |
| 255 | |
| 256 | if (out + 2 > out_end) { |
| 257 | /* too large */ |
| 258 | goto fail; |
| 259 | } |
| 260 | *(out++) = '\r'; |
| 261 | *(out++) = '\n'; |
| 262 | } |
| 263 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 264 | /* And finish */ |
| 265 | if (out + 2 > out_end) { |
| 266 | /* too large */ |
| 267 | goto fail; |
| 268 | } |
| 269 | |
| 270 | *(out++) = '\r'; |
| 271 | *(out++) = '\n'; |
| 272 | ret = out + osize - out_end; |
| 273 | leave: |
| 274 | return ret; |
| 275 | |
| 276 | fail: |
| 277 | return -1; |
| 278 | } |