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