Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/1 protocol analyzer |
| 3 | * |
| 4 | * Copyright 2000-2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 13 | #include <ctype.h> |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 14 | |
| 15 | #include <import/sha1.h> |
| 16 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 18 | #include <haproxy/base64.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 19 | #include <haproxy/h1.h> |
Willy Tarreau | 0017be0 | 2020-06-02 19:25:28 +0200 | [diff] [blame] | 20 | #include <haproxy/http-hdr.h> |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | 73373ab | 2018-09-14 17:11:33 +0200 | [diff] [blame] | 23 | /* Parse the Content-Length header field of an HTTP/1 request. The function |
| 24 | * checks all possible occurrences of a comma-delimited value, and verifies |
| 25 | * if any of them doesn't match a previous value. It returns <0 if a value |
| 26 | * differs, 0 if the whole header can be dropped (i.e. already known), or >0 |
| 27 | * if the value can be indexed (first one). In the last case, the value might |
| 28 | * be adjusted and the caller must only add the updated value. |
| 29 | */ |
| 30 | int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value) |
| 31 | { |
| 32 | char *e, *n; |
| 33 | long long cl; |
| 34 | int not_first = !!(h1m->flags & H1_MF_CLEN); |
| 35 | struct ist word; |
| 36 | |
| 37 | word.ptr = value->ptr - 1; // -1 for next loop's pre-increment |
| 38 | e = value->ptr + value->len; |
| 39 | |
| 40 | while (++word.ptr < e) { |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 41 | /* skip leading delimiter and blanks */ |
Willy Tarreau | 73373ab | 2018-09-14 17:11:33 +0200 | [diff] [blame] | 42 | if (unlikely(HTTP_IS_LWS(*word.ptr))) |
| 43 | continue; |
| 44 | |
| 45 | /* digits only now */ |
| 46 | for (cl = 0, n = word.ptr; n < e; n++) { |
| 47 | unsigned int c = *n - '0'; |
| 48 | if (unlikely(c > 9)) { |
| 49 | /* non-digit */ |
| 50 | if (unlikely(n == word.ptr)) // spaces only |
| 51 | goto fail; |
| 52 | break; |
| 53 | } |
| 54 | if (unlikely(cl > ULLONG_MAX / 10ULL)) |
| 55 | goto fail; /* multiply overflow */ |
| 56 | cl = cl * 10ULL; |
| 57 | if (unlikely(cl + c < cl)) |
| 58 | goto fail; /* addition overflow */ |
| 59 | cl = cl + c; |
| 60 | } |
| 61 | |
| 62 | /* keep a copy of the exact cleaned value */ |
| 63 | word.len = n - word.ptr; |
| 64 | |
| 65 | /* skip trailing LWS till next comma or EOL */ |
| 66 | for (; n < e; n++) { |
| 67 | if (!HTTP_IS_LWS(*n)) { |
| 68 | if (unlikely(*n != ',')) |
| 69 | goto fail; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* if duplicate, must be equal */ |
| 75 | if (h1m->flags & H1_MF_CLEN && cl != h1m->body_len) |
| 76 | goto fail; |
| 77 | |
| 78 | /* OK, store this result as the one to be indexed */ |
| 79 | h1m->flags |= H1_MF_CLEN; |
| 80 | h1m->curr_len = h1m->body_len = cl; |
| 81 | *value = word; |
| 82 | word.ptr = n; |
| 83 | } |
| 84 | /* here we've reached the end with a single value or a series of |
| 85 | * identical values, all matching previous series if any. The last |
| 86 | * parsed value was sent back into <value>. We just have to decide |
| 87 | * if this occurrence has to be indexed (it's the first one) or |
| 88 | * silently skipped (it's not the first one) |
| 89 | */ |
| 90 | return !not_first; |
| 91 | fail: |
| 92 | return -1; |
| 93 | } |
| 94 | |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 95 | /* Parse the Transfer-Encoding: header field of an HTTP/1 request, looking for |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 96 | * "chunked" encoding to perform some checks (it must be the last encoding for |
| 97 | * the request and must not be performed twice for any message). The |
| 98 | * H1_MF_TE_CHUNKED is set if a valid "chunked" encoding is found. The |
| 99 | * H1_MF_TE_OTHER flag is set if any other encoding is found. The H1_MF_XFER_ENC |
| 100 | * flag is always set. The H1_MF_CHNK is set when "chunked" encoding is the last |
| 101 | * one. Note that transfer codings are case-insensitive (cf RFC7230#4). This |
| 102 | * function returns <0 if a error is found, 0 if the whole header can be dropped |
| 103 | * (not used yet), or >0 if the value can be indexed. |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 104 | */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 105 | int h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value) |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 106 | { |
| 107 | char *e, *n; |
| 108 | struct ist word; |
| 109 | |
| 110 | h1m->flags |= H1_MF_XFER_ENC; |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 111 | |
| 112 | word.ptr = value.ptr - 1; // -1 for next loop's pre-increment |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 113 | e = istend(value); |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 114 | |
| 115 | while (++word.ptr < e) { |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 116 | /* skip leading delimiter and blanks */ |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 117 | if (HTTP_IS_LWS(*word.ptr)) |
| 118 | continue; |
| 119 | |
| 120 | n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line |
| 121 | word.len = n - word.ptr; |
| 122 | |
| 123 | /* trim trailing blanks */ |
| 124 | while (word.len && HTTP_IS_LWS(word.ptr[word.len-1])) |
| 125 | word.len--; |
| 126 | |
| 127 | h1m->flags &= ~H1_MF_CHNK; |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 128 | if (isteqi(word, ist("chunked"))) { |
| 129 | if (h1m->flags & H1_MF_TE_CHUNKED) { |
| 130 | /* cf RFC7230#3.3.1 : A sender MUST NOT apply |
| 131 | * chunked more than once to a message body |
| 132 | * (i.e., chunking an already chunked message is |
| 133 | * not allowed) |
| 134 | */ |
| 135 | goto fail; |
| 136 | } |
| 137 | h1m->flags |= (H1_MF_TE_CHUNKED|H1_MF_CHNK); |
| 138 | } |
| 139 | else { |
| 140 | if ((h1m->flags & (H1_MF_RESP|H1_MF_TE_CHUNKED)) == H1_MF_TE_CHUNKED) { |
| 141 | /* cf RFC7230#3.3.1 : If any transfer coding |
| 142 | * other than chunked is applied to a request |
| 143 | * payload body, the sender MUST apply chunked |
| 144 | * as the final transfer coding to ensure that |
| 145 | * the message is properly framed. |
| 146 | */ |
| 147 | goto fail; |
| 148 | } |
| 149 | h1m->flags |= H1_MF_TE_OTHER; |
| 150 | } |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 151 | |
| 152 | word.ptr = n; |
| 153 | } |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 154 | |
| 155 | return 1; |
| 156 | fail: |
| 157 | return -1; |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 160 | /* Validate the authority and the host header value for CONNECT method. If there |
| 161 | * is hast header, its value is normalized. 0 is returned on success, -1 if the |
| 162 | * authority is invalid and -2 if the host is invalid. |
| 163 | */ |
| 164 | static int h1_validate_connect_authority(struct ist authority, struct ist *host_hdr) |
| 165 | { |
| 166 | struct ist uri_host, uri_port, host, host_port; |
| 167 | |
| 168 | if (!isttest(authority)) |
| 169 | goto invalid_authority; |
| 170 | uri_host = authority; |
| 171 | uri_port = http_get_host_port(authority); |
Christopher Faulet | 75348c2 | 2022-11-22 10:27:54 +0100 | [diff] [blame] | 172 | if (!istlen(uri_port)) |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 173 | goto invalid_authority; |
| 174 | uri_host.len -= (istlen(uri_port) + 1); |
| 175 | |
| 176 | if (!host_hdr || !isttest(*host_hdr)) |
| 177 | goto end; |
| 178 | |
| 179 | /* Get the port of the host header value, if any */ |
| 180 | host = *host_hdr; |
| 181 | host_port = http_get_host_port(*host_hdr); |
Christopher Faulet | 75348c2 | 2022-11-22 10:27:54 +0100 | [diff] [blame] | 182 | if (isttest(host_port)) |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 183 | host.len -= (istlen(host_port) + 1); |
Christopher Faulet | 75348c2 | 2022-11-22 10:27:54 +0100 | [diff] [blame] | 184 | |
| 185 | if (istlen(host_port)) { |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 186 | if (!isteqi(host, uri_host) || !isteq(host_port, uri_port)) |
| 187 | goto invalid_host; |
| 188 | if (http_is_default_port(IST_NULL, uri_port)) |
| 189 | *host_hdr = host; /* normalize */ |
| 190 | } |
| 191 | else { |
| 192 | if (!http_is_default_port(IST_NULL, uri_port) || !isteqi(host, uri_host)) |
| 193 | goto invalid_host; |
| 194 | } |
| 195 | |
| 196 | end: |
| 197 | return 0; |
| 198 | |
| 199 | invalid_authority: |
| 200 | return -1; |
| 201 | |
| 202 | invalid_host: |
| 203 | return -2; |
| 204 | } |
| 205 | |
Christopher Faulet | e16ffb0 | 2022-11-22 10:04:16 +0100 | [diff] [blame] | 206 | |
| 207 | /* Validate the authority and the host header value for non-CONNECT method, when |
| 208 | * an absolute-URI is detected but when it does not exactly match the host |
| 209 | * value. The idea is to detect default port (http or https). authority and host |
| 210 | * are defined here. 0 is returned on success, -1 if the host is does not match |
| 211 | * the authority. |
| 212 | */ |
| 213 | static int h1_validate_mismatch_authority(struct ist scheme, struct ist authority, struct ist host_hdr) |
| 214 | { |
| 215 | struct ist uri_host, uri_port, host, host_port; |
| 216 | |
| 217 | if (!isttest(scheme)) |
| 218 | goto mismatch; |
| 219 | |
| 220 | uri_host = authority; |
| 221 | uri_port = http_get_host_port(authority); |
| 222 | if (isttest(uri_port)) |
| 223 | uri_host.len -= (istlen(uri_port) + 1); |
| 224 | |
| 225 | host = host_hdr; |
| 226 | host_port = http_get_host_port(host_hdr); |
| 227 | if (isttest(host_port)) |
| 228 | host.len -= (istlen(host_port) + 1); |
| 229 | |
| 230 | if (!isttest(uri_port) && !isttest(host_port)) { |
| 231 | /* No port on both: we already know the authority does not match |
| 232 | * the host value |
| 233 | */ |
| 234 | goto mismatch; |
| 235 | } |
| 236 | else if (isttest(uri_port) && !http_is_default_port(scheme, uri_port)) { |
| 237 | /* here there is no port for the host value and the port for the |
| 238 | * authority is not the default one |
| 239 | */ |
| 240 | goto mismatch; |
| 241 | } |
| 242 | else if (isttest(host_port) && !http_is_default_port(scheme, host_port)) { |
| 243 | /* here there is no port for the authority and the port for the |
| 244 | * host value is not the default one |
| 245 | */ |
| 246 | goto mismatch; |
| 247 | } |
| 248 | else { |
| 249 | /* the authority or the host value contain a default port and |
| 250 | * there is no port on the other value |
| 251 | */ |
| 252 | if (!isteqi(uri_host, host)) |
| 253 | goto mismatch; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | |
| 258 | mismatch: |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 263 | /* Parse the Connection: header of an HTTP/1 request, looking for "close", |
| 264 | * "keep-alive", and "upgrade" values, and updating h1m->flags according to |
| 265 | * what was found there. Note that flags are only added, not removed, so the |
| 266 | * function is safe for being called multiple times if multiple occurrences |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 267 | * are found. If the flag H1_MF_CLEAN_CONN_HDR, the header value is cleaned |
| 268 | * up from "keep-alive" and "close" values. To do so, the header value is |
| 269 | * rewritten in place and its length is updated. |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 270 | */ |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 271 | void h1_parse_connection_header(struct h1m *h1m, struct ist *value) |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 272 | { |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 273 | char *e, *n, *p; |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 274 | struct ist word; |
| 275 | |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 276 | word.ptr = value->ptr - 1; // -1 for next loop's pre-increment |
| 277 | p = value->ptr; |
| 278 | e = value->ptr + value->len; |
| 279 | if (h1m->flags & H1_MF_CLEAN_CONN_HDR) |
| 280 | value->len = 0; |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 281 | |
| 282 | while (++word.ptr < e) { |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 283 | /* skip leading delimiter and blanks */ |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 284 | if (HTTP_IS_LWS(*word.ptr)) |
| 285 | continue; |
| 286 | |
| 287 | n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line |
| 288 | word.len = n - word.ptr; |
| 289 | |
| 290 | /* trim trailing blanks */ |
| 291 | while (word.len && HTTP_IS_LWS(word.ptr[word.len-1])) |
| 292 | word.len--; |
| 293 | |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 294 | if (isteqi(word, ist("keep-alive"))) { |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 295 | h1m->flags |= H1_MF_CONN_KAL; |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 296 | if (h1m->flags & H1_MF_CLEAN_CONN_HDR) |
| 297 | goto skip_val; |
| 298 | } |
| 299 | else if (isteqi(word, ist("close"))) { |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 300 | h1m->flags |= H1_MF_CONN_CLO; |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 301 | if (h1m->flags & H1_MF_CLEAN_CONN_HDR) |
| 302 | goto skip_val; |
| 303 | } |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 304 | else if (isteqi(word, ist("upgrade"))) |
| 305 | h1m->flags |= H1_MF_CONN_UPG; |
| 306 | |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 307 | if (h1m->flags & H1_MF_CLEAN_CONN_HDR) { |
| 308 | if (value->ptr + value->len == p) { |
| 309 | /* no rewrite done till now */ |
| 310 | value->len = n - value->ptr; |
| 311 | } |
| 312 | else { |
| 313 | if (value->len) |
| 314 | value->ptr[value->len++] = ','; |
| 315 | istcat(value, word, e - value->ptr); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | skip_val: |
| 320 | word.ptr = p = n; |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 324 | /* Parse the Upgrade: header of an HTTP/1 request. |
| 325 | * If "websocket" is found, set H1_MF_UPG_WEBSOCKET flag |
| 326 | */ |
| 327 | void h1_parse_upgrade_header(struct h1m *h1m, struct ist value) |
| 328 | { |
| 329 | char *e, *n; |
| 330 | struct ist word; |
| 331 | |
| 332 | h1m->flags &= ~H1_MF_UPG_WEBSOCKET; |
| 333 | |
| 334 | word.ptr = value.ptr - 1; // -1 for next loop's pre-increment |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 335 | e = istend(value); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 336 | |
| 337 | while (++word.ptr < e) { |
| 338 | /* skip leading delimiter and blanks */ |
| 339 | if (HTTP_IS_LWS(*word.ptr)) |
| 340 | continue; |
| 341 | |
| 342 | n = http_find_hdr_value_end(word.ptr, e); // next comma or end of line |
| 343 | word.len = n - word.ptr; |
| 344 | |
| 345 | /* trim trailing blanks */ |
| 346 | while (word.len && HTTP_IS_LWS(word.ptr[word.len-1])) |
| 347 | word.len--; |
| 348 | |
| 349 | if (isteqi(word, ist("websocket"))) |
| 350 | h1m->flags |= H1_MF_UPG_WEBSOCKET; |
| 351 | |
| 352 | word.ptr = n; |
| 353 | } |
| 354 | } |
| 355 | |
Willy Tarreau | 538746a | 2018-12-11 10:59:20 +0100 | [diff] [blame] | 356 | /* Macros used in the HTTP/1 parser, to check for the expected presence of |
| 357 | * certain bytes (ef: LF) or to skip to next byte and yield in case of failure. |
| 358 | */ |
| 359 | |
| 360 | /* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to |
| 361 | * <bad>. |
| 362 | */ |
| 363 | #define EXPECT_LF_HERE(ptr, bad, state, where) \ |
| 364 | do { \ |
| 365 | if (unlikely(*(ptr) != '\n')) { \ |
| 366 | state = (where); \ |
| 367 | goto bad; \ |
| 368 | } \ |
| 369 | } while (0) |
| 370 | |
| 371 | /* Increments pointer <ptr>, continues to label <more> if it's still below |
| 372 | * pointer <end>, or goes to <stop> and sets <state> to <where> if the end |
| 373 | * of buffer was reached. |
| 374 | */ |
| 375 | #define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \ |
| 376 | do { \ |
| 377 | if (likely(++(ptr) < (end))) \ |
| 378 | goto more; \ |
| 379 | else { \ |
| 380 | state = (where); \ |
| 381 | goto stop; \ |
| 382 | } \ |
| 383 | } while (0) |
| 384 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 385 | /* This function parses a contiguous HTTP/1 headers block starting at <start> |
| 386 | * and ending before <stop>, at once, and converts it a list of (name,value) |
| 387 | * pairs representing header fields into the array <hdr> of size <hdr_num>, |
| 388 | * whose last entry will have an empty name and an empty value. If <hdr_num> is |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 389 | * too small to represent the whole message, an error is returned. Some |
| 390 | * protocol elements such as content-length and transfer-encoding will be |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 391 | * parsed and stored into h1m as well. <hdr> may be null, in which case only |
| 392 | * the parsing state will be updated. This may be used to restart the parsing |
| 393 | * where it stopped for example. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 394 | * |
| 395 | * For now it's limited to the response. If the header block is incomplete, |
| 396 | * 0 is returned, waiting to be called again with more data to try it again. |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 397 | * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE, |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 398 | * and h1m->next to zero on the first call, the parser will do the rest. If |
| 399 | * an incomplete message is seen, the caller only needs to present h1m->state |
| 400 | * and h1m->next again, with an empty header list so that the parser can start |
| 401 | * again. In this case, it will detect that it interrupted a previous session |
| 402 | * and will first look for the end of the message before reparsing it again and |
| 403 | * indexing it at the same time. This ensures that incomplete messages fed 1 |
| 404 | * character at a time are never processed entirely more than exactly twice, |
| 405 | * and that there is no need to store all the internal state and pre-parsed |
| 406 | * headers or start line between calls. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 407 | * |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 408 | * A pointer to a start line descriptor may be passed in <slp>, in which case |
| 409 | * the parser will fill it with whatever it found. |
| 410 | * |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 411 | * The code derived from the main HTTP/1 parser above but was simplified and |
| 412 | * optimized to process responses produced or forwarded by haproxy. The caller |
| 413 | * is responsible for ensuring that the message doesn't wrap, and should ensure |
| 414 | * it is complete to avoid having to retry the operation after a failed |
| 415 | * attempt. The message is not supposed to be invalid, which is why a few |
| 416 | * properties such as the character set used in the header field names are not |
| 417 | * checked. In case of an unparsable response message, a negative value will be |
| 418 | * returned with h1m->err_pos and h1m->err_state matching the location and |
| 419 | * state where the error was met. Leading blank likes are tolerated but not |
Willy Tarreau | 0f8fb6b | 2019-01-04 10:48:03 +0100 | [diff] [blame] | 420 | * recommended. If flag H1_MF_HDRS_ONLY is set in h1m->flags, only headers are |
| 421 | * parsed and the start line is skipped. It is not required to set h1m->state |
| 422 | * nor h1m->next in this case. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 423 | * |
| 424 | * This function returns : |
| 425 | * -1 in case of error. In this case, h1m->err_state is filled (if h1m is |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 426 | * set) with the state the error occurred in and h1m->err_pos with the |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 427 | * the position relative to <start> |
| 428 | * -2 if the output is full (hdr_num reached). err_state and err_pos also |
| 429 | * indicate where it failed. |
| 430 | * 0 in case of missing data. |
| 431 | * > 0 on success, it then corresponds to the number of bytes read since |
| 432 | * <start> so that the caller can go on with the payload. |
| 433 | */ |
| 434 | int h1_headers_to_hdr_list(char *start, const char *stop, |
| 435 | struct http_hdr *hdr, unsigned int hdr_num, |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 436 | struct h1m *h1m, union h1_sl *slp) |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 437 | { |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 438 | enum h1m_state state; |
| 439 | register char *ptr; |
| 440 | register const char *end; |
| 441 | unsigned int hdr_count; |
| 442 | unsigned int skip; /* number of bytes skipped at the beginning */ |
| 443 | unsigned int sol; /* start of line */ |
| 444 | unsigned int col; /* position of the colon */ |
| 445 | unsigned int eol; /* end of line */ |
| 446 | unsigned int sov; /* start of value */ |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 447 | union h1_sl sl; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 448 | int skip_update; |
| 449 | int restarting; |
Christopher Faulet | 497ab4f | 2019-10-11 09:01:44 +0200 | [diff] [blame] | 450 | int host_idx; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 451 | struct ist n, v; /* header name and value during parsing */ |
| 452 | |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 453 | skip = 0; // do it only once to keep track of the leading CRLF. |
| 454 | |
| 455 | try_again: |
| 456 | hdr_count = sol = col = eol = sov = 0; |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 457 | sl.st.status = 0; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 458 | skip_update = restarting = 0; |
Christopher Faulet | 497ab4f | 2019-10-11 09:01:44 +0200 | [diff] [blame] | 459 | host_idx = -1; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 460 | |
Willy Tarreau | 0f8fb6b | 2019-01-04 10:48:03 +0100 | [diff] [blame] | 461 | if (h1m->flags & H1_MF_HDRS_ONLY) { |
| 462 | state = H1_MSG_HDR_FIRST; |
| 463 | h1m->next = 0; |
| 464 | } |
Christopher Faulet | 68b1bbd | 2019-01-04 16:06:48 +0100 | [diff] [blame] | 465 | else { |
Willy Tarreau | 0f8fb6b | 2019-01-04 10:48:03 +0100 | [diff] [blame] | 466 | state = h1m->state; |
Christopher Faulet | 68b1bbd | 2019-01-04 16:06:48 +0100 | [diff] [blame] | 467 | if (h1m->state != H1_MSG_RQBEFORE && h1m->state != H1_MSG_RPBEFORE) |
| 468 | restarting = 1; |
| 469 | } |
Willy Tarreau | 0f8fb6b | 2019-01-04 10:48:03 +0100 | [diff] [blame] | 470 | |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 471 | ptr = start + h1m->next; |
| 472 | end = stop; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 473 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 474 | if (unlikely(ptr >= end)) |
| 475 | goto http_msg_ood; |
| 476 | |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 477 | /* don't update output if hdr is NULL or if we're restarting */ |
| 478 | if (!hdr || restarting) |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 479 | skip_update = 1; |
| 480 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 481 | switch (state) { |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 482 | case H1_MSG_RQBEFORE: |
| 483 | http_msg_rqbefore: |
| 484 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 485 | /* we have a start of message, we may have skipped some |
| 486 | * heading CRLF. Skip them now. |
| 487 | */ |
| 488 | skip += ptr - start; |
| 489 | start = ptr; |
| 490 | |
| 491 | sol = 0; |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 492 | sl.rq.m.ptr = ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 493 | hdr_count = 0; |
| 494 | state = H1_MSG_RQMETH; |
| 495 | goto http_msg_rqmeth; |
| 496 | } |
| 497 | |
| 498 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 499 | state = H1_MSG_RQBEFORE; |
| 500 | goto http_msg_invalid; |
| 501 | } |
| 502 | |
| 503 | if (unlikely(*ptr == '\n')) |
| 504 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE); |
| 505 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR); |
| 506 | /* stop here */ |
| 507 | |
| 508 | case H1_MSG_RQBEFORE_CR: |
| 509 | http_msg_rqbefore_cr: |
| 510 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR); |
| 511 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE); |
| 512 | /* stop here */ |
| 513 | |
| 514 | case H1_MSG_RQMETH: |
| 515 | http_msg_rqmeth: |
| 516 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 517 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH); |
| 518 | |
| 519 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 520 | sl.rq.m.len = ptr - sl.rq.m.ptr; |
| 521 | sl.rq.meth = find_http_meth(start, sl.rq.m.len); |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 522 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP); |
| 523 | } |
| 524 | |
| 525 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 526 | /* HTTP 0.9 request */ |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 527 | sl.rq.m.len = ptr - sl.rq.m.ptr; |
| 528 | sl.rq.meth = find_http_meth(sl.rq.m.ptr, sl.rq.m.len); |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 529 | http_msg_req09_uri: |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 530 | sl.rq.u.ptr = ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 531 | http_msg_req09_uri_e: |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 532 | sl.rq.u.len = ptr - sl.rq.u.ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 533 | http_msg_req09_ver: |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 534 | sl.rq.v = ist2(ptr, 0); |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 535 | goto http_msg_rqline_eol; |
| 536 | } |
| 537 | state = H1_MSG_RQMETH; |
| 538 | goto http_msg_invalid; |
| 539 | |
| 540 | case H1_MSG_RQMETH_SP: |
| 541 | http_msg_rqmeth_sp: |
| 542 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 543 | sl.rq.u.ptr = ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 544 | goto http_msg_rquri; |
| 545 | } |
| 546 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 547 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP); |
| 548 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 549 | goto http_msg_req09_uri; |
| 550 | |
| 551 | case H1_MSG_RQURI: |
| 552 | http_msg_rquri: |
Willy Tarreau | 02ac950 | 2020-02-21 16:31:22 +0100 | [diff] [blame] | 553 | #ifdef HA_UNALIGNED_LE |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 554 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 555 | while (ptr <= end - sizeof(int)) { |
| 556 | int x = *(int *)ptr - 0x21212121; |
| 557 | if (x & 0x80808080) |
| 558 | break; |
| 559 | |
| 560 | x -= 0x5e5e5e5e; |
| 561 | if (!(x & 0x80808080)) |
| 562 | break; |
| 563 | |
| 564 | ptr += sizeof(int); |
| 565 | } |
| 566 | #endif |
| 567 | if (ptr >= end) { |
| 568 | state = H1_MSG_RQURI; |
| 569 | goto http_msg_ood; |
| 570 | } |
| 571 | http_msg_rquri2: |
| 572 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
| 573 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI); |
| 574 | |
| 575 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 576 | sl.rq.u.len = ptr - sl.rq.u.ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 577 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP); |
| 578 | } |
| 579 | if (likely((unsigned char)*ptr >= 128)) { |
| 580 | /* non-ASCII chars are forbidden unless option |
| 581 | * accept-invalid-http-request is enabled in the frontend. |
| 582 | * In any case, we capture the faulty char. |
| 583 | */ |
| 584 | if (h1m->err_pos < -1) |
| 585 | goto invalid_char; |
| 586 | if (h1m->err_pos == -1) |
| 587 | h1m->err_pos = ptr - start + skip; |
| 588 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI); |
| 589 | } |
| 590 | |
| 591 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 592 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 593 | goto http_msg_req09_uri_e; |
| 594 | } |
| 595 | |
| 596 | /* OK forbidden chars, 0..31 or 127 */ |
| 597 | invalid_char: |
| 598 | state = H1_MSG_RQURI; |
| 599 | goto http_msg_invalid; |
| 600 | |
| 601 | case H1_MSG_RQURI_SP: |
| 602 | http_msg_rquri_sp: |
| 603 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 604 | sl.rq.v.ptr = ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 605 | goto http_msg_rqver; |
| 606 | } |
| 607 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 608 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP); |
| 609 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 610 | goto http_msg_req09_ver; |
| 611 | |
| 612 | |
| 613 | case H1_MSG_RQVER: |
| 614 | http_msg_rqver: |
| 615 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| 616 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER); |
| 617 | |
| 618 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 619 | sl.rq.v.len = ptr - sl.rq.v.ptr; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 620 | http_msg_rqline_eol: |
| 621 | /* We have seen the end of line. Note that we do not |
| 622 | * necessarily have the \n yet, but at least we know that we |
| 623 | * have EITHER \r OR \n, otherwise the request would not be |
| 624 | * complete. We can then record the request length and return |
| 625 | * to the caller which will be able to register it. |
| 626 | */ |
| 627 | |
| 628 | if (likely(!skip_update)) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 629 | if ((sl.rq.v.len == 8) && |
| 630 | (*(sl.rq.v.ptr + 5) > '1' || |
| 631 | (*(sl.rq.v.ptr + 5) == '1' && *(sl.rq.v.ptr + 7) >= '1'))) |
Willy Tarreau | ba5fbca | 2018-09-13 11:32:51 +0200 | [diff] [blame] | 632 | h1m->flags |= H1_MF_VER_11; |
| 633 | |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 634 | if (unlikely(hdr_count >= hdr_num)) { |
| 635 | state = H1_MSG_RQVER; |
| 636 | goto http_output_full; |
| 637 | } |
Christopher Faulet | 25da9e3 | 2018-10-08 15:50:15 +0200 | [diff] [blame] | 638 | if (!(h1m->flags & H1_MF_NO_PHDR)) |
| 639 | http_set_hdr(&hdr[hdr_count++], ist(":method"), sl.rq.m); |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 640 | |
| 641 | if (unlikely(hdr_count >= hdr_num)) { |
| 642 | state = H1_MSG_RQVER; |
| 643 | goto http_output_full; |
| 644 | } |
Christopher Faulet | 25da9e3 | 2018-10-08 15:50:15 +0200 | [diff] [blame] | 645 | if (!(h1m->flags & H1_MF_NO_PHDR)) |
| 646 | http_set_hdr(&hdr[hdr_count++], ist(":path"), sl.rq.u); |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | sol = ptr - start; |
| 650 | if (likely(*ptr == '\r')) |
| 651 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END); |
| 652 | goto http_msg_rqline_end; |
| 653 | } |
| 654 | |
| 655 | /* neither an HTTP_VER token nor a CRLF */ |
| 656 | state = H1_MSG_RQVER; |
| 657 | goto http_msg_invalid; |
| 658 | |
| 659 | case H1_MSG_RQLINE_END: |
| 660 | http_msg_rqline_end: |
| 661 | /* check for HTTP/0.9 request : no version information |
| 662 | * available. sol must point to the first of CR or LF. However |
| 663 | * since we don't save these elements between calls, if we come |
| 664 | * here from a restart, we don't necessarily know. Thus in this |
| 665 | * case we simply start over. |
| 666 | */ |
| 667 | if (restarting) |
| 668 | goto restart; |
| 669 | |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 670 | if (unlikely(sl.rq.v.len == 0)) |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 671 | goto http_msg_last_lf; |
| 672 | |
| 673 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END); |
| 674 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST); |
| 675 | /* stop here */ |
| 676 | |
| 677 | /* |
| 678 | * Common states below |
| 679 | */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 680 | case H1_MSG_RPBEFORE: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 681 | http_msg_rpbefore: |
| 682 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 683 | /* we have a start of message, we may have skipped some |
| 684 | * heading CRLF. Skip them now. |
| 685 | */ |
| 686 | skip += ptr - start; |
| 687 | start = ptr; |
| 688 | |
| 689 | sol = 0; |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 690 | sl.st.v.ptr = ptr; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 691 | hdr_count = 0; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 692 | state = H1_MSG_RPVER; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 693 | goto http_msg_rpver; |
| 694 | } |
| 695 | |
| 696 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 697 | state = H1_MSG_RPBEFORE; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 698 | goto http_msg_invalid; |
| 699 | } |
| 700 | |
| 701 | if (unlikely(*ptr == '\n')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 702 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE); |
| 703 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore_cr, http_msg_ood, state, H1_MSG_RPBEFORE_CR); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 704 | /* stop here */ |
| 705 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 706 | case H1_MSG_RPBEFORE_CR: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 707 | http_msg_rpbefore_cr: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 708 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR); |
| 709 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 710 | /* stop here */ |
| 711 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 712 | case H1_MSG_RPVER: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 713 | http_msg_rpver: |
| 714 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 715 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, H1_MSG_RPVER); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 716 | |
| 717 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 718 | sl.st.v.len = ptr - sl.st.v.ptr; |
Willy Tarreau | ba5fbca | 2018-09-13 11:32:51 +0200 | [diff] [blame] | 719 | |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 720 | if ((sl.st.v.len == 8) && |
| 721 | (*(sl.st.v.ptr + 5) > '1' || |
| 722 | (*(sl.st.v.ptr + 5) == '1' && *(sl.st.v.ptr + 7) >= '1'))) |
Willy Tarreau | ba5fbca | 2018-09-13 11:32:51 +0200 | [diff] [blame] | 723 | h1m->flags |= H1_MF_VER_11; |
| 724 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 725 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 726 | } |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 727 | state = H1_MSG_RPVER; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 728 | goto http_msg_invalid; |
| 729 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 730 | case H1_MSG_RPVER_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 731 | http_msg_rpver_sp: |
| 732 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 733 | sl.st.status = 0; |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 734 | sl.st.c.ptr = ptr; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 735 | goto http_msg_rpcode; |
| 736 | } |
| 737 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 738 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, H1_MSG_RPVER_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 739 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 740 | state = H1_MSG_RPVER_SP; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 741 | goto http_msg_invalid; |
| 742 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 743 | case H1_MSG_RPCODE: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 744 | http_msg_rpcode: |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 745 | if (likely(HTTP_IS_DIGIT(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 746 | sl.st.status = sl.st.status * 10 + *ptr - '0'; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 747 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, H1_MSG_RPCODE); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 750 | if (unlikely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 751 | state = H1_MSG_RPCODE; |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 752 | goto http_msg_invalid; |
| 753 | } |
| 754 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 755 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 756 | sl.st.c.len = ptr - sl.st.c.ptr; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 757 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* so it's a CR/LF, so there is no reason phrase */ |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 761 | sl.st.c.len = ptr - sl.st.c.ptr; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 762 | |
| 763 | http_msg_rsp_reason: |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 764 | sl.st.r = ist2(ptr, 0); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 765 | goto http_msg_rpline_eol; |
| 766 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 767 | case H1_MSG_RPCODE_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 768 | http_msg_rpcode_sp: |
| 769 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 770 | sl.st.r.ptr = ptr; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 771 | goto http_msg_rpreason; |
| 772 | } |
| 773 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 774 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, H1_MSG_RPCODE_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 775 | /* so it's a CR/LF, so there is no reason phrase */ |
| 776 | goto http_msg_rsp_reason; |
| 777 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 778 | case H1_MSG_RPREASON: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 779 | http_msg_rpreason: |
| 780 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 781 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON); |
Christopher Faulet | 1dc2b49 | 2018-10-08 15:34:02 +0200 | [diff] [blame] | 782 | sl.st.r.len = ptr - sl.st.r.ptr; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 783 | http_msg_rpline_eol: |
| 784 | /* We have seen the end of line. Note that we do not |
| 785 | * necessarily have the \n yet, but at least we know that we |
| 786 | * have EITHER \r OR \n, otherwise the response would not be |
| 787 | * complete. We can then record the response length and return |
| 788 | * to the caller which will be able to register it. |
| 789 | */ |
| 790 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 791 | if (likely(!skip_update)) { |
| 792 | if (unlikely(hdr_count >= hdr_num)) { |
| 793 | state = H1_MSG_RPREASON; |
| 794 | goto http_output_full; |
| 795 | } |
Christopher Faulet | 25da9e3 | 2018-10-08 15:50:15 +0200 | [diff] [blame] | 796 | if (!(h1m->flags & H1_MF_NO_PHDR)) |
| 797 | http_set_hdr(&hdr[hdr_count++], ist(":status"), sl.st.c); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 798 | } |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 799 | |
| 800 | sol = ptr - start; |
| 801 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 802 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpline_end, http_msg_ood, state, H1_MSG_RPLINE_END); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 803 | goto http_msg_rpline_end; |
| 804 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 805 | case H1_MSG_RPLINE_END: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 806 | http_msg_rpline_end: |
| 807 | /* sol must point to the first of CR or LF. */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 808 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END); |
| 809 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 810 | /* stop here */ |
| 811 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 812 | case H1_MSG_HDR_FIRST: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 813 | http_msg_hdr_first: |
| 814 | sol = ptr - start; |
| 815 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 816 | goto http_msg_hdr_name; |
| 817 | } |
| 818 | |
| 819 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 820 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 821 | goto http_msg_last_lf; |
| 822 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 823 | case H1_MSG_HDR_NAME: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 824 | http_msg_hdr_name: |
| 825 | /* assumes sol points to the first char */ |
| 826 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 827 | if (!skip_update) { |
| 828 | /* turn it to lower case if needed */ |
| 829 | if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 830 | *ptr = tolower((unsigned char)*ptr); |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 831 | } |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 832 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (likely(*ptr == ':')) { |
| 836 | col = ptr - start; |
Willy Tarreau | a8598a2 | 2023-02-09 21:36:54 +0100 | [diff] [blame] | 837 | if (col <= sol) { |
| 838 | state = H1_MSG_HDR_NAME; |
| 839 | goto http_msg_invalid; |
| 840 | } |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 841 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Willy Tarreau | 9aec305 | 2018-09-12 09:20:40 +0200 | [diff] [blame] | 844 | if (likely(h1m->err_pos < -1) || *ptr == '\n') { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 845 | state = H1_MSG_HDR_NAME; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 846 | goto http_msg_invalid; |
| 847 | } |
| 848 | |
Willy Tarreau | 9aec305 | 2018-09-12 09:20:40 +0200 | [diff] [blame] | 849 | if (h1m->err_pos == -1) /* capture the error pointer */ |
| 850 | h1m->err_pos = ptr - start + skip; /* >= 0 now */ |
| 851 | |
| 852 | /* and we still accept this non-token character */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 853 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, H1_MSG_HDR_NAME); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 854 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 855 | case H1_MSG_HDR_L1_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 856 | http_msg_hdr_l1_sp: |
| 857 | /* assumes sol points to the first char */ |
| 858 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 859 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, H1_MSG_HDR_L1_SP); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 860 | |
| 861 | /* header value can be basically anything except CR/LF */ |
| 862 | sov = ptr - start; |
| 863 | |
| 864 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 865 | goto http_msg_hdr_val; |
| 866 | } |
| 867 | |
| 868 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 869 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lf, http_msg_ood, state, H1_MSG_HDR_L1_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 870 | goto http_msg_hdr_l1_lf; |
| 871 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 872 | case H1_MSG_HDR_L1_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 873 | http_msg_hdr_l1_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 874 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF); |
| 875 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lws, http_msg_ood, state, H1_MSG_HDR_L1_LWS); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 876 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 877 | case H1_MSG_HDR_L1_LWS: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 878 | http_msg_hdr_l1_lws: |
| 879 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 880 | if (!skip_update) { |
| 881 | /* replace HT,CR,LF with spaces */ |
| 882 | for (; start + sov < ptr; sov++) |
| 883 | start[sov] = ' '; |
| 884 | } |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 885 | goto http_msg_hdr_l1_sp; |
| 886 | } |
| 887 | /* we had a header consisting only in spaces ! */ |
| 888 | eol = sov; |
| 889 | goto http_msg_complete_header; |
| 890 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 891 | case H1_MSG_HDR_VAL: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 892 | http_msg_hdr_val: |
| 893 | /* assumes sol points to the first char, and sov |
| 894 | * points to the first character of the value. |
| 895 | */ |
| 896 | |
| 897 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 898 | * and lower. In fact since most of the time is spent in the loop, we |
| 899 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 900 | * loop, but we don't care since they're very rare in header values. |
| 901 | */ |
Willy Tarreau | 02ac950 | 2020-02-21 16:31:22 +0100 | [diff] [blame] | 902 | #ifdef HA_UNALIGNED_LE64 |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 903 | while (ptr <= end - sizeof(long)) { |
| 904 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 905 | goto http_msg_hdr_val2; |
| 906 | ptr += sizeof(long); |
| 907 | } |
| 908 | #endif |
Willy Tarreau | 02ac950 | 2020-02-21 16:31:22 +0100 | [diff] [blame] | 909 | #ifdef HA_UNALIGNED_LE |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 910 | while (ptr <= end - sizeof(int)) { |
| 911 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 912 | goto http_msg_hdr_val2; |
| 913 | ptr += sizeof(int); |
| 914 | } |
| 915 | #endif |
| 916 | if (ptr >= end) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 917 | state = H1_MSG_HDR_VAL; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 918 | goto http_msg_ood; |
| 919 | } |
| 920 | http_msg_hdr_val2: |
| 921 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 922 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_val2, http_msg_ood, state, H1_MSG_HDR_VAL); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 923 | |
| 924 | eol = ptr - start; |
| 925 | /* Note: we could also copy eol into ->eoh so that we have the |
| 926 | * real header end in case it ends with lots of LWS, but is this |
| 927 | * really needed ? |
| 928 | */ |
| 929 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 930 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lf, http_msg_ood, state, H1_MSG_HDR_L2_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 931 | goto http_msg_hdr_l2_lf; |
| 932 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 933 | case H1_MSG_HDR_L2_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 934 | http_msg_hdr_l2_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 935 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF); |
| 936 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lws, http_msg_ood, state, H1_MSG_HDR_L2_LWS); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 937 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 938 | case H1_MSG_HDR_L2_LWS: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 939 | http_msg_hdr_l2_lws: |
| 940 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 941 | if (!skip_update) { |
| 942 | /* LWS: replace HT,CR,LF with spaces */ |
| 943 | for (; start + eol < ptr; eol++) |
| 944 | start[eol] = ' '; |
| 945 | } |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 946 | goto http_msg_hdr_val; |
| 947 | } |
| 948 | http_msg_complete_header: |
| 949 | /* |
| 950 | * It was a new header, so the last one is finished. Assumes |
| 951 | * <sol> points to the first char of the name, <col> to the |
| 952 | * colon, <sov> points to the first character of the value and |
| 953 | * <eol> to the first CR or LF so we know how the line ends. We |
| 954 | * will trim spaces around the value. It's possible to do it by |
| 955 | * adjusting <eol> and <sov> which are no more used after this. |
| 956 | * We can add the header field to the list. |
| 957 | */ |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 958 | if (likely(!skip_update)) { |
| 959 | while (sov < eol && HTTP_IS_LWS(start[sov])) |
| 960 | sov++; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 961 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 962 | while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1])) |
| 963 | eol--; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 964 | |
| 965 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 966 | n = ist2(start + sol, col - sol); |
| 967 | v = ist2(start + sov, eol - sov); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 968 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 969 | do { |
| 970 | int ret; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 971 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 972 | if (unlikely(hdr_count >= hdr_num)) { |
| 973 | state = H1_MSG_HDR_L2_LWS; |
| 974 | goto http_output_full; |
| 975 | } |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 976 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 977 | if (isteqi(n, ist("transfer-encoding"))) { |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 978 | ret = h1_parse_xfer_enc_header(h1m, v); |
| 979 | if (ret < 0) { |
| 980 | state = H1_MSG_HDR_L2_LWS; |
| 981 | ptr = v.ptr; /* Set ptr on the error */ |
| 982 | goto http_msg_invalid; |
| 983 | } |
| 984 | else if (ret == 0) { |
| 985 | /* skip it */ |
| 986 | break; |
| 987 | } |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 988 | } |
| 989 | else if (isteqi(n, ist("content-length"))) { |
| 990 | ret = h1_parse_cont_len_header(h1m, &v); |
Willy Tarreau | 73373ab | 2018-09-14 17:11:33 +0200 | [diff] [blame] | 991 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 992 | if (ret < 0) { |
| 993 | state = H1_MSG_HDR_L2_LWS; |
Christopher Faulet | 1703478 | 2020-01-06 13:41:01 +0100 | [diff] [blame] | 994 | ptr = v.ptr; /* Set ptr on the error */ |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 995 | goto http_msg_invalid; |
| 996 | } |
| 997 | else if (ret == 0) { |
| 998 | /* skip it */ |
| 999 | break; |
| 1000 | } |
Willy Tarreau | 73373ab | 2018-09-14 17:11:33 +0200 | [diff] [blame] | 1001 | } |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 1002 | else if (isteqi(n, ist("connection"))) { |
Christopher Faulet | a51ebb7 | 2019-03-29 15:03:13 +0100 | [diff] [blame] | 1003 | h1_parse_connection_header(h1m, &v); |
| 1004 | if (!v.len) { |
| 1005 | /* skip it */ |
| 1006 | break; |
| 1007 | } |
Willy Tarreau | 73373ab | 2018-09-14 17:11:33 +0200 | [diff] [blame] | 1008 | } |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1009 | else if (isteqi(n, ist("upgrade"))) { |
| 1010 | h1_parse_upgrade_header(h1m, v); |
| 1011 | } |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1012 | else if (!(h1m->flags & H1_MF_RESP) && isteqi(n, ist("host"))) { |
| 1013 | if (host_idx == -1) |
Christopher Faulet | 497ab4f | 2019-10-11 09:01:44 +0200 | [diff] [blame] | 1014 | host_idx = hdr_count; |
| 1015 | else { |
| 1016 | if (!isteqi(v, hdr[host_idx].v)) { |
| 1017 | state = H1_MSG_HDR_L2_LWS; |
Christopher Faulet | 1703478 | 2020-01-06 13:41:01 +0100 | [diff] [blame] | 1018 | ptr = v.ptr; /* Set ptr on the error */ |
Christopher Faulet | 497ab4f | 2019-10-11 09:01:44 +0200 | [diff] [blame] | 1019 | goto http_msg_invalid; |
| 1020 | } |
| 1021 | /* if the same host, skip it */ |
| 1022 | break; |
| 1023 | } |
| 1024 | } |
Willy Tarreau | 2ea6bb5 | 2018-09-14 16:28:15 +0200 | [diff] [blame] | 1025 | |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 1026 | http_set_hdr(&hdr[hdr_count++], n, v); |
| 1027 | } while (0); |
| 1028 | } |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1029 | |
| 1030 | sol = ptr - start; |
Christopher Faulet | 2912f87 | 2018-09-19 14:01:04 +0200 | [diff] [blame] | 1031 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1032 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1033 | goto http_msg_hdr_name; |
| 1034 | |
| 1035 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1036 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, H1_MSG_LAST_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1037 | goto http_msg_last_lf; |
| 1038 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1039 | case H1_MSG_LAST_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1040 | http_msg_last_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1041 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1042 | ptr++; |
| 1043 | /* <ptr> now points to the first byte of payload. If needed sol |
| 1044 | * still points to the first of either CR or LF of the empty |
| 1045 | * line ending the headers block. |
| 1046 | */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1047 | if (likely(!skip_update)) { |
| 1048 | if (unlikely(hdr_count >= hdr_num)) { |
| 1049 | state = H1_MSG_LAST_LF; |
| 1050 | goto http_output_full; |
| 1051 | } |
Christopher Faulet | ff08a92 | 2018-09-25 13:59:46 +0200 | [diff] [blame] | 1052 | http_set_hdr(&hdr[hdr_count++], ist2(start+sol, 0), ist("")); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1053 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 1054 | |
| 1055 | /* reaching here we've parsed the whole message. We may detect |
| 1056 | * that we were already continuing an interrupted parsing pass |
| 1057 | * so we were silently looking for the end of message not |
| 1058 | * updating anything before deciding to parse it fully at once. |
| 1059 | * It's guaranteed that we won't match this test twice in a row |
| 1060 | * since restarting will turn zero. |
| 1061 | */ |
| 1062 | if (restarting) |
| 1063 | goto restart; |
| 1064 | |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1065 | |
| 1066 | if (!(h1m->flags & (H1_MF_HDRS_ONLY|H1_MF_RESP))) { |
| 1067 | struct http_uri_parser parser = http_uri_parser_init(sl.rq.u); |
Christopher Faulet | e16ffb0 | 2022-11-22 10:04:16 +0100 | [diff] [blame] | 1068 | struct ist scheme, authority; |
| 1069 | int ret; |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1070 | |
Christopher Faulet | e16ffb0 | 2022-11-22 10:04:16 +0100 | [diff] [blame] | 1071 | scheme = http_parse_scheme(&parser); |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1072 | authority = http_parse_authority(&parser, 1); |
| 1073 | if (sl.rq.meth == HTTP_METH_CONNECT) { |
| 1074 | struct ist *host = ((host_idx != -1) ? &hdr[host_idx].v : NULL); |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1075 | |
| 1076 | ret = h1_validate_connect_authority(authority, host); |
| 1077 | if (ret < 0) { |
| 1078 | if (h1m->err_pos < -1) { |
| 1079 | state = H1_MSG_LAST_LF; |
Willy Tarreau | 55d2e85 | 2022-10-04 08:02:03 +0200 | [diff] [blame] | 1080 | /* WT: gcc seems to see a path where sl.rq.u.ptr was used |
| 1081 | * uninitialized, but it doesn't know that the function is |
| 1082 | * called with initial states making this impossible. |
| 1083 | */ |
| 1084 | ALREADY_CHECKED(sl.rq.u.ptr); |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1085 | ptr = ((ret == -1) ? sl.rq.u.ptr : host->ptr); /* Set ptr on the error */ |
| 1086 | goto http_msg_invalid; |
| 1087 | } |
| 1088 | if (h1m->err_pos == -1) /* capture the error pointer */ |
| 1089 | h1m->err_pos = ((ret == -1) ? sl.rq.u.ptr : host->ptr) - start + skip; /* >= 0 now */ |
| 1090 | } |
| 1091 | } |
| 1092 | else if (host_idx != -1 && istlen(authority)) { |
| 1093 | struct ist host = hdr[host_idx].v; |
| 1094 | |
| 1095 | /* For non-CONNECT method, the authority must match the host header value */ |
| 1096 | if (!isteqi(authority, host)) { |
Christopher Faulet | e16ffb0 | 2022-11-22 10:04:16 +0100 | [diff] [blame] | 1097 | ret = h1_validate_mismatch_authority(scheme, authority, host); |
| 1098 | if (ret < 0) { |
| 1099 | if (h1m->err_pos < -1) { |
| 1100 | state = H1_MSG_LAST_LF; |
| 1101 | ptr = host.ptr; /* Set ptr on the error */ |
| 1102 | goto http_msg_invalid; |
| 1103 | } |
| 1104 | if (h1m->err_pos == -1) /* capture the error pointer */ |
| 1105 | h1m->err_pos = v.ptr - start + skip; /* >= 0 now */ |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1106 | } |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1107 | } |
Christopher Faulet | 3f5fbe9 | 2022-07-05 14:50:17 +0200 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 1111 | state = H1_MSG_DATA; |
| 1112 | if (h1m->flags & H1_MF_XFER_ENC) { |
| 1113 | if (h1m->flags & H1_MF_CLEN) { |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 1114 | /* T-E + C-L: force close and remove C-L */ |
| 1115 | h1m->flags |= H1_MF_CONN_CLO; |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 1116 | h1m->flags &= ~H1_MF_CLEN; |
| 1117 | hdr_count = http_del_hdr(hdr, ist("content-length")); |
| 1118 | } |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 1119 | else if (!(h1m->flags & H1_MF_VER_11)) { |
| 1120 | /* T-E + HTTP/1.0: force close */ |
| 1121 | h1m->flags |= H1_MF_CONN_CLO; |
| 1122 | } |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 1123 | |
| 1124 | if (h1m->flags & H1_MF_CHNK) |
| 1125 | state = H1_MSG_CHUNK_SIZE; |
| 1126 | else if (!(h1m->flags & H1_MF_RESP)) { |
| 1127 | /* cf RFC7230#3.3.3 : transfer-encoding in |
| 1128 | * request without chunked encoding is invalid. |
| 1129 | */ |
| 1130 | goto http_msg_invalid; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | |
| 1136 | default: |
| 1137 | /* impossible states */ |
| 1138 | goto http_msg_invalid; |
| 1139 | } |
| 1140 | |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 1141 | /* Now we've left the headers state and are either in H1_MSG_DATA or |
| 1142 | * H1_MSG_CHUNK_SIZE. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1143 | */ |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1144 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1145 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1146 | *slp = sl; |
| 1147 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1148 | h1m->state = state; |
| 1149 | h1m->next = ptr - start + skip; |
| 1150 | return h1m->next; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1151 | |
| 1152 | http_msg_ood: |
| 1153 | /* out of data at <ptr> during state <state> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1154 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1155 | *slp = sl; |
| 1156 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1157 | h1m->state = state; |
| 1158 | h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1159 | return 0; |
| 1160 | |
| 1161 | http_msg_invalid: |
| 1162 | /* invalid message, error at <ptr> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1163 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1164 | *slp = sl; |
| 1165 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1166 | h1m->err_state = h1m->state = state; |
| 1167 | h1m->err_pos = h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1168 | return -1; |
| 1169 | |
| 1170 | http_output_full: |
| 1171 | /* no more room to store the current header, error at <ptr> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1172 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1173 | *slp = sl; |
| 1174 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1175 | h1m->err_state = h1m->state = state; |
| 1176 | h1m->err_pos = h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1177 | return -2; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1178 | |
| 1179 | restart: |
Christopher Faulet | 02c8933 | 2021-12-01 18:01:48 +0100 | [diff] [blame] | 1180 | h1m->flags &= H1_MF_RESTART_MASK; |
Christopher Faulet | 84f0653 | 2019-09-03 16:05:31 +0200 | [diff] [blame] | 1181 | h1m->curr_len = h1m->body_len = h1m->next = 0; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 1182 | if (h1m->flags & H1_MF_RESP) |
| 1183 | h1m->state = H1_MSG_RPBEFORE; |
| 1184 | else |
| 1185 | h1m->state = H1_MSG_RQBEFORE; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1186 | goto try_again; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1189 | /* This function performs a very minimal parsing of the trailers block present |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1190 | * at offset <ofs> in <buf> for up to <max> bytes, and returns the number of |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1191 | * bytes to delete to skip the trailers. It may return 0 if it's missing some |
| 1192 | * input data, or < 0 in case of parse error (in which case the caller may have |
| 1193 | * to decide how to proceed, possibly eating everything). |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1194 | */ |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1195 | int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max) |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1196 | { |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1197 | const char *stop = b_peek(buf, ofs + max); |
| 1198 | int count = ofs; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1199 | |
| 1200 | while (1) { |
| 1201 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1202 | const char *start = b_peek(buf, count); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1203 | const char *ptr = start; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1204 | |
| 1205 | /* scan current line and stop at LF or CRLF */ |
| 1206 | while (1) { |
| 1207 | if (ptr == stop) |
| 1208 | return 0; |
| 1209 | |
| 1210 | if (*ptr == '\n') { |
| 1211 | if (!p1) |
| 1212 | p1 = ptr; |
| 1213 | p2 = ptr; |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | if (*ptr == '\r') { |
| 1218 | if (p1) |
| 1219 | return -1; |
| 1220 | p1 = ptr; |
| 1221 | } |
| 1222 | |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1223 | ptr = b_next(buf, ptr); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* after LF; point to beginning of next line */ |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1227 | p2 = b_next(buf, p2); |
| 1228 | count += b_dist(buf, start, p2); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1229 | |
| 1230 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1231 | * Everything was scheduled for forwarding, there's nothing left |
| 1232 | * from this message. */ |
| 1233 | if (p1 == start) |
| 1234 | break; |
| 1235 | /* OK, next line then */ |
| 1236 | } |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1237 | return count - ofs; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1238 | } |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1239 | |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 1240 | /* Generate a random key for a WebSocket Handshake in respect with rfc6455 |
| 1241 | * The key is 128-bits long encoded as a base64 string in <key_out> parameter |
| 1242 | * (25 bytes long). |
| 1243 | */ |
| 1244 | void h1_generate_random_ws_input_key(char key_out[25]) |
| 1245 | { |
| 1246 | /* generate a random websocket key */ |
| 1247 | const uint64_t rand1 = ha_random64(), rand2 = ha_random64(); |
| 1248 | char key[16]; |
| 1249 | |
| 1250 | memcpy(key, &rand1, 8); |
| 1251 | memcpy(&key[8], &rand2, 8); |
| 1252 | a2base64(key, 16, key_out, 25); |
| 1253 | } |
| 1254 | |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1255 | #define H1_WS_KEY_SUFFIX_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| 1256 | |
| 1257 | /* |
| 1258 | * Calculate the WebSocket handshake response key from <key_in>. Following the |
| 1259 | * rfc6455, <key_in> must be 24 bytes longs. The result is stored in <key_out> |
| 1260 | * as a 29 bytes long string. |
| 1261 | */ |
| 1262 | void h1_calculate_ws_output_key(const char *key, char *result) |
| 1263 | { |
| 1264 | blk_SHA_CTX sha1_ctx; |
| 1265 | char hash_in[60], hash_out[20]; |
| 1266 | |
| 1267 | /* concatenate the key with a fixed suffix */ |
| 1268 | memcpy(hash_in, key, 24); |
| 1269 | memcpy(&hash_in[24], H1_WS_KEY_SUFFIX_GUID, 36); |
| 1270 | |
| 1271 | /* sha1 the result */ |
| 1272 | blk_SHA1_Init(&sha1_ctx); |
| 1273 | blk_SHA1_Update(&sha1_ctx, hash_in, 60); |
| 1274 | blk_SHA1_Final((unsigned char *)hash_out, &sha1_ctx); |
| 1275 | |
| 1276 | /* encode in base64 the hash */ |
| 1277 | a2base64(hash_out, 20, result, 29); |
| 1278 | } |