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