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