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