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