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