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 | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 15 | #include <common/http-hdr.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 16 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 17 | #include <proto/channel.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 18 | #include <proto/h1.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 | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 21 | /* |
| 22 | * This function parses a status line between <ptr> and <end>, starting with |
| 23 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 24 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 25 | * will give undefined results. |
| 26 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 27 | * and that msg->sol points to the beginning of the response. |
| 28 | * If a complete line is found (which implies that at least one CR or LF is |
| 29 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 30 | * returned indicating an incomplete line (which does not mean that parts have |
| 31 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 32 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 33 | * upon next call. |
| 34 | * |
| 35 | * This function was intentionally designed to be called from |
| 36 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 37 | * within its state machine and use the same macros, hence the need for same |
| 38 | * labels and variable names. Note that msg->sol is left unchanged. |
| 39 | */ |
| 40 | const char *http_parse_stsline(struct http_msg *msg, |
| 41 | enum h1_state state, const char *ptr, const char *end, |
| 42 | unsigned int *ret_ptr, enum h1_state *ret_state) |
| 43 | { |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 44 | const char *msg_start = ci_head(msg->chn); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 45 | |
| 46 | switch (state) { |
| 47 | case HTTP_MSG_RPVER: |
| 48 | http_msg_rpver: |
| 49 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| 50 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver, http_msg_ood, state, HTTP_MSG_RPVER); |
| 51 | |
| 52 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 53 | msg->sl.st.v_l = ptr - msg_start; |
| 54 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, HTTP_MSG_RPVER_SP); |
| 55 | } |
| 56 | msg->err_state = HTTP_MSG_RPVER; |
| 57 | state = HTTP_MSG_ERROR; |
| 58 | break; |
| 59 | |
| 60 | case HTTP_MSG_RPVER_SP: |
| 61 | http_msg_rpver_sp: |
| 62 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 63 | msg->sl.st.c = ptr - msg_start; |
| 64 | goto http_msg_rpcode; |
| 65 | } |
| 66 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 67 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpver_sp, http_msg_ood, state, HTTP_MSG_RPVER_SP); |
| 68 | /* so it's a CR/LF, this is invalid */ |
| 69 | msg->err_state = HTTP_MSG_RPVER_SP; |
| 70 | state = HTTP_MSG_ERROR; |
| 71 | break; |
| 72 | |
| 73 | case HTTP_MSG_RPCODE: |
| 74 | http_msg_rpcode: |
| 75 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 76 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode, http_msg_ood, state, HTTP_MSG_RPCODE); |
| 77 | |
| 78 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 79 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
| 80 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, HTTP_MSG_RPCODE_SP); |
| 81 | } |
| 82 | |
| 83 | /* so it's a CR/LF, so there is no reason phrase */ |
| 84 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
| 85 | http_msg_rsp_reason: |
| 86 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 87 | msg->sl.st.r = ptr - msg_start; |
| 88 | msg->sl.st.r_l = 0; |
| 89 | goto http_msg_rpline_eol; |
| 90 | |
| 91 | case HTTP_MSG_RPCODE_SP: |
| 92 | http_msg_rpcode_sp: |
| 93 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 94 | msg->sl.st.r = ptr - msg_start; |
| 95 | goto http_msg_rpreason; |
| 96 | } |
| 97 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 98 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpcode_sp, http_msg_ood, state, HTTP_MSG_RPCODE_SP); |
| 99 | /* so it's a CR/LF, so there is no reason phrase */ |
| 100 | goto http_msg_rsp_reason; |
| 101 | |
| 102 | case HTTP_MSG_RPREASON: |
| 103 | http_msg_rpreason: |
| 104 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 105 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, HTTP_MSG_RPREASON); |
| 106 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
| 107 | http_msg_rpline_eol: |
| 108 | /* We have seen the end of line. Note that we do not |
| 109 | * necessarily have the \n yet, but at least we know that we |
| 110 | * have EITHER \r OR \n, otherwise the response would not be |
| 111 | * complete. We can then record the response length and return |
| 112 | * to the caller which will be able to register it. |
| 113 | */ |
| 114 | msg->sl.st.l = ptr - msg_start - msg->sol; |
| 115 | return ptr; |
| 116 | |
| 117 | default: |
| 118 | #ifdef DEBUG_FULL |
| 119 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 120 | exit(1); |
| 121 | #endif |
| 122 | ; |
| 123 | } |
| 124 | |
| 125 | http_msg_ood: |
| 126 | /* out of valid data */ |
| 127 | if (ret_state) |
| 128 | *ret_state = state; |
| 129 | if (ret_ptr) |
| 130 | *ret_ptr = ptr - msg_start; |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * This function parses a request line between <ptr> and <end>, starting with |
| 136 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 137 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 138 | * will give undefined results. |
| 139 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 140 | * and that msg->sol points to the beginning of the request. |
| 141 | * If a complete line is found (which implies that at least one CR or LF is |
| 142 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 143 | * returned indicating an incomplete line (which does not mean that parts have |
| 144 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 145 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 146 | * upon next call. |
| 147 | * |
| 148 | * This function was intentionally designed to be called from |
| 149 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 150 | * within its state machine and use the same macros, hence the need for same |
| 151 | * labels and variable names. Note that msg->sol is left unchanged. |
| 152 | */ |
| 153 | const char *http_parse_reqline(struct http_msg *msg, |
| 154 | enum h1_state state, const char *ptr, const char *end, |
| 155 | unsigned int *ret_ptr, enum h1_state *ret_state) |
| 156 | { |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 157 | const char *msg_start = ci_head(msg->chn); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 158 | |
| 159 | switch (state) { |
| 160 | case HTTP_MSG_RQMETH: |
| 161 | http_msg_rqmeth: |
| 162 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 163 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, HTTP_MSG_RQMETH); |
| 164 | |
| 165 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 166 | msg->sl.rq.m_l = ptr - msg_start; |
| 167 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, HTTP_MSG_RQMETH_SP); |
| 168 | } |
| 169 | |
| 170 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 171 | /* HTTP 0.9 request */ |
| 172 | msg->sl.rq.m_l = ptr - msg_start; |
| 173 | http_msg_req09_uri: |
| 174 | msg->sl.rq.u = ptr - msg_start; |
| 175 | http_msg_req09_uri_e: |
| 176 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
| 177 | http_msg_req09_ver: |
| 178 | msg->sl.rq.v = ptr - msg_start; |
| 179 | msg->sl.rq.v_l = 0; |
| 180 | goto http_msg_rqline_eol; |
| 181 | } |
| 182 | msg->err_state = HTTP_MSG_RQMETH; |
| 183 | state = HTTP_MSG_ERROR; |
| 184 | break; |
| 185 | |
| 186 | case HTTP_MSG_RQMETH_SP: |
| 187 | http_msg_rqmeth_sp: |
| 188 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 189 | msg->sl.rq.u = ptr - msg_start; |
| 190 | goto http_msg_rquri; |
| 191 | } |
| 192 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 193 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, HTTP_MSG_RQMETH_SP); |
| 194 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 195 | goto http_msg_req09_uri; |
| 196 | |
| 197 | case HTTP_MSG_RQURI: |
| 198 | http_msg_rquri: |
| 199 | #if defined(__x86_64__) || \ |
| 200 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 201 | defined(__ARM_ARCH_7A__) |
| 202 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 203 | while (ptr <= end - sizeof(int)) { |
| 204 | int x = *(int *)ptr - 0x21212121; |
| 205 | if (x & 0x80808080) |
| 206 | break; |
| 207 | |
| 208 | x -= 0x5e5e5e5e; |
| 209 | if (!(x & 0x80808080)) |
| 210 | break; |
| 211 | |
| 212 | ptr += sizeof(int); |
| 213 | } |
| 214 | #endif |
| 215 | if (ptr >= end) { |
| 216 | state = HTTP_MSG_RQURI; |
| 217 | goto http_msg_ood; |
| 218 | } |
| 219 | http_msg_rquri2: |
| 220 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
| 221 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, HTTP_MSG_RQURI); |
| 222 | |
| 223 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 224 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
| 225 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, HTTP_MSG_RQURI_SP); |
| 226 | } |
| 227 | |
| 228 | if (likely((unsigned char)*ptr >= 128)) { |
| 229 | /* non-ASCII chars are forbidden unless option |
| 230 | * accept-invalid-http-request is enabled in the frontend. |
| 231 | * In any case, we capture the faulty char. |
| 232 | */ |
| 233 | if (msg->err_pos < -1) |
| 234 | goto invalid_char; |
| 235 | if (msg->err_pos == -1) |
| 236 | msg->err_pos = ptr - msg_start; |
| 237 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, HTTP_MSG_RQURI); |
| 238 | } |
| 239 | |
| 240 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 241 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 242 | goto http_msg_req09_uri_e; |
| 243 | } |
| 244 | |
| 245 | /* OK forbidden chars, 0..31 or 127 */ |
| 246 | invalid_char: |
| 247 | msg->err_pos = ptr - msg_start; |
| 248 | msg->err_state = HTTP_MSG_RQURI; |
| 249 | state = HTTP_MSG_ERROR; |
| 250 | break; |
| 251 | |
| 252 | case HTTP_MSG_RQURI_SP: |
| 253 | http_msg_rquri_sp: |
| 254 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 255 | msg->sl.rq.v = ptr - msg_start; |
| 256 | goto http_msg_rqver; |
| 257 | } |
| 258 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 259 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, HTTP_MSG_RQURI_SP); |
| 260 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 261 | goto http_msg_req09_ver; |
| 262 | |
| 263 | case HTTP_MSG_RQVER: |
| 264 | http_msg_rqver: |
| 265 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| 266 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, HTTP_MSG_RQVER); |
| 267 | |
| 268 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 269 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
| 270 | http_msg_rqline_eol: |
| 271 | /* We have seen the end of line. Note that we do not |
| 272 | * necessarily have the \n yet, but at least we know that we |
| 273 | * have EITHER \r OR \n, otherwise the request would not be |
| 274 | * complete. We can then record the request length and return |
| 275 | * to the caller which will be able to register it. |
| 276 | */ |
| 277 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
| 278 | return ptr; |
| 279 | } |
| 280 | |
| 281 | /* neither an HTTP_VER token nor a CRLF */ |
| 282 | msg->err_state = HTTP_MSG_RQVER; |
| 283 | state = HTTP_MSG_ERROR; |
| 284 | break; |
| 285 | |
| 286 | default: |
| 287 | #ifdef DEBUG_FULL |
| 288 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 289 | exit(1); |
| 290 | #endif |
| 291 | ; |
| 292 | } |
| 293 | |
| 294 | http_msg_ood: |
| 295 | /* out of valid data */ |
| 296 | if (ret_state) |
| 297 | *ret_state = state; |
| 298 | if (ret_ptr) |
| 299 | *ret_ptr = ptr - msg_start; |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * This function parses an HTTP message, either a request or a response, |
| 305 | * depending on the initial msg->msg_state. The caller is responsible for |
| 306 | * ensuring that the message does not wrap. The function can be preempted |
| 307 | * everywhere when data are missing and recalled at the exact same location |
| 308 | * with no information loss. The message may even be realigned between two |
| 309 | * calls. The header index is re-initialized when switching from |
| 310 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 311 | * fields. Note that msg->sol will be initialized after completing the first |
| 312 | * state, so that none of the msg pointers has to be initialized prior to the |
| 313 | * first call. |
| 314 | */ |
| 315 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
| 316 | { |
| 317 | enum h1_state state; /* updated only when leaving the FSM */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 318 | register const char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 950a8a6 | 2018-09-06 10:48:15 +0200 | [diff] [blame] | 319 | struct buffer *buf = &msg->chn->buf; |
| 320 | char *input = b_head(buf); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 321 | |
| 322 | state = msg->msg_state; |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 323 | ptr = input + msg->next; |
| 324 | end = b_stop(buf); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 325 | |
| 326 | if (unlikely(ptr >= end)) |
| 327 | goto http_msg_ood; |
| 328 | |
| 329 | switch (state) { |
| 330 | /* |
| 331 | * First, states that are specific to the response only. |
| 332 | * We check them first so that request and headers are |
| 333 | * closer to each other (accessed more often). |
| 334 | */ |
| 335 | case HTTP_MSG_RPBEFORE: |
| 336 | http_msg_rpbefore: |
| 337 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 338 | /* we have a start of message, but we have to check |
| 339 | * first if we need to remove some CRLF. We can only |
| 340 | * do this when o=0. |
| 341 | */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 342 | if (unlikely(ptr != input)) { |
| 343 | if (co_data(msg->chn)) |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 344 | goto http_msg_ood; |
| 345 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 72a100b | 2018-07-10 09:59:31 +0200 | [diff] [blame] | 346 | b_del(buf, ptr - input); |
Willy Tarreau | 950a8a6 | 2018-09-06 10:48:15 +0200 | [diff] [blame] | 347 | input = b_head(buf); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 348 | } |
| 349 | msg->sol = 0; |
| 350 | msg->sl.st.l = 0; /* used in debug mode */ |
| 351 | hdr_idx_init(idx); |
| 352 | state = HTTP_MSG_RPVER; |
| 353 | goto http_msg_rpver; |
| 354 | } |
| 355 | |
| 356 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 357 | state = HTTP_MSG_RPBEFORE; |
| 358 | goto http_msg_invalid; |
| 359 | } |
| 360 | |
| 361 | if (unlikely(*ptr == '\n')) |
| 362 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, HTTP_MSG_RPBEFORE); |
| 363 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore_cr, http_msg_ood, state, HTTP_MSG_RPBEFORE_CR); |
| 364 | /* stop here */ |
| 365 | |
| 366 | case HTTP_MSG_RPBEFORE_CR: |
| 367 | http_msg_rpbefore_cr: |
| 368 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RPBEFORE_CR); |
| 369 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, HTTP_MSG_RPBEFORE); |
| 370 | /* stop here */ |
| 371 | |
| 372 | case HTTP_MSG_RPVER: |
| 373 | http_msg_rpver: |
| 374 | case HTTP_MSG_RPVER_SP: |
| 375 | case HTTP_MSG_RPCODE: |
| 376 | case HTTP_MSG_RPCODE_SP: |
| 377 | case HTTP_MSG_RPREASON: |
| 378 | ptr = (char *)http_parse_stsline(msg, |
| 379 | state, ptr, end, |
| 380 | &msg->next, &msg->msg_state); |
| 381 | if (unlikely(!ptr)) |
| 382 | return; |
| 383 | |
| 384 | /* we have a full response and we know that we have either a CR |
| 385 | * or an LF at <ptr>. |
| 386 | */ |
| 387 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 388 | |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 389 | msg->sol = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 390 | if (likely(*ptr == '\r')) |
| 391 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpline_end, http_msg_ood, state, HTTP_MSG_RPLINE_END); |
| 392 | goto http_msg_rpline_end; |
| 393 | |
| 394 | case HTTP_MSG_RPLINE_END: |
| 395 | http_msg_rpline_end: |
| 396 | /* msg->sol must point to the first of CR or LF. */ |
| 397 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RPLINE_END); |
| 398 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, HTTP_MSG_HDR_FIRST); |
| 399 | /* stop here */ |
| 400 | |
| 401 | /* |
| 402 | * Second, states that are specific to the request only |
| 403 | */ |
| 404 | case HTTP_MSG_RQBEFORE: |
| 405 | http_msg_rqbefore: |
| 406 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 407 | /* we have a start of message, but we have to check |
| 408 | * first if we need to remove some CRLF. We can only |
| 409 | * do this when o=0. |
| 410 | */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 411 | if (likely(ptr != input)) { |
| 412 | if (co_data(msg->chn)) |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 413 | goto http_msg_ood; |
| 414 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 72a100b | 2018-07-10 09:59:31 +0200 | [diff] [blame] | 415 | b_del(buf, ptr - input); |
Willy Tarreau | 950a8a6 | 2018-09-06 10:48:15 +0200 | [diff] [blame] | 416 | input = b_head(buf); |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 417 | } |
| 418 | msg->sol = 0; |
| 419 | msg->sl.rq.l = 0; /* used in debug mode */ |
| 420 | state = HTTP_MSG_RQMETH; |
| 421 | goto http_msg_rqmeth; |
| 422 | } |
| 423 | |
| 424 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 425 | state = HTTP_MSG_RQBEFORE; |
| 426 | goto http_msg_invalid; |
| 427 | } |
| 428 | |
| 429 | if (unlikely(*ptr == '\n')) |
| 430 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, HTTP_MSG_RQBEFORE); |
| 431 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, HTTP_MSG_RQBEFORE_CR); |
| 432 | /* stop here */ |
| 433 | |
| 434 | case HTTP_MSG_RQBEFORE_CR: |
| 435 | http_msg_rqbefore_cr: |
| 436 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RQBEFORE_CR); |
| 437 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, HTTP_MSG_RQBEFORE); |
| 438 | /* stop here */ |
| 439 | |
| 440 | case HTTP_MSG_RQMETH: |
| 441 | http_msg_rqmeth: |
| 442 | case HTTP_MSG_RQMETH_SP: |
| 443 | case HTTP_MSG_RQURI: |
| 444 | case HTTP_MSG_RQURI_SP: |
| 445 | case HTTP_MSG_RQVER: |
| 446 | ptr = (char *)http_parse_reqline(msg, |
| 447 | state, ptr, end, |
| 448 | &msg->next, &msg->msg_state); |
| 449 | if (unlikely(!ptr)) |
| 450 | return; |
| 451 | |
| 452 | /* we have a full request and we know that we have either a CR |
| 453 | * or an LF at <ptr>. |
| 454 | */ |
| 455 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
| 456 | |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 457 | msg->sol = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 458 | if (likely(*ptr == '\r')) |
| 459 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, HTTP_MSG_RQLINE_END); |
| 460 | goto http_msg_rqline_end; |
| 461 | |
| 462 | case HTTP_MSG_RQLINE_END: |
| 463 | http_msg_rqline_end: |
| 464 | /* check for HTTP/0.9 request : no version information available. |
| 465 | * msg->sol must point to the first of CR or LF. |
| 466 | */ |
| 467 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 468 | goto http_msg_last_lf; |
| 469 | |
| 470 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_RQLINE_END); |
| 471 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, HTTP_MSG_HDR_FIRST); |
| 472 | /* stop here */ |
| 473 | |
| 474 | /* |
| 475 | * Common states below |
| 476 | */ |
| 477 | case HTTP_MSG_HDR_FIRST: |
| 478 | http_msg_hdr_first: |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 479 | msg->sol = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 480 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 481 | goto http_msg_hdr_name; |
| 482 | } |
| 483 | |
| 484 | if (likely(*ptr == '\r')) |
| 485 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, HTTP_MSG_LAST_LF); |
| 486 | goto http_msg_last_lf; |
| 487 | |
| 488 | case HTTP_MSG_HDR_NAME: |
| 489 | http_msg_hdr_name: |
| 490 | /* assumes msg->sol points to the first char */ |
| 491 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 492 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, HTTP_MSG_HDR_NAME); |
| 493 | |
| 494 | if (likely(*ptr == ':')) |
| 495 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, HTTP_MSG_HDR_L1_SP); |
| 496 | |
| 497 | if (likely(msg->err_pos < -1) || *ptr == '\n') { |
| 498 | state = HTTP_MSG_HDR_NAME; |
| 499 | goto http_msg_invalid; |
| 500 | } |
| 501 | |
| 502 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 503 | msg->err_pos = ptr - input; /* >= 0 now */ |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 504 | |
| 505 | /* and we still accept this non-token character */ |
| 506 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_name, http_msg_ood, state, HTTP_MSG_HDR_NAME); |
| 507 | |
| 508 | case HTTP_MSG_HDR_L1_SP: |
| 509 | http_msg_hdr_l1_sp: |
| 510 | /* assumes msg->sol points to the first char */ |
| 511 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 512 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_sp, http_msg_ood, state, HTTP_MSG_HDR_L1_SP); |
| 513 | |
| 514 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 515 | msg->sov = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 516 | |
| 517 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 518 | goto http_msg_hdr_val; |
| 519 | } |
| 520 | |
| 521 | if (likely(*ptr == '\r')) |
| 522 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lf, http_msg_ood, state, HTTP_MSG_HDR_L1_LF); |
| 523 | goto http_msg_hdr_l1_lf; |
| 524 | |
| 525 | case HTTP_MSG_HDR_L1_LF: |
| 526 | http_msg_hdr_l1_lf: |
| 527 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_HDR_L1_LF); |
| 528 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l1_lws, http_msg_ood, state, HTTP_MSG_HDR_L1_LWS); |
| 529 | |
| 530 | case HTTP_MSG_HDR_L1_LWS: |
| 531 | http_msg_hdr_l1_lws: |
| 532 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 533 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 534 | for (; input + msg->sov < ptr; msg->sov++) |
| 535 | input[msg->sov] = ' '; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 536 | goto http_msg_hdr_l1_sp; |
| 537 | } |
| 538 | /* we had a header consisting only in spaces ! */ |
| 539 | msg->eol = msg->sov; |
| 540 | goto http_msg_complete_header; |
| 541 | |
| 542 | case HTTP_MSG_HDR_VAL: |
| 543 | http_msg_hdr_val: |
| 544 | /* assumes msg->sol points to the first char, and msg->sov |
| 545 | * points to the first character of the value. |
| 546 | */ |
| 547 | |
| 548 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 549 | * and lower. In fact since most of the time is spent in the loop, we |
| 550 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 551 | * loop, but we don't care since they're very rare in header values. |
| 552 | */ |
| 553 | #if defined(__x86_64__) |
| 554 | while (ptr <= end - sizeof(long)) { |
| 555 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 556 | goto http_msg_hdr_val2; |
| 557 | ptr += sizeof(long); |
| 558 | } |
| 559 | #endif |
| 560 | #if defined(__x86_64__) || \ |
| 561 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 562 | defined(__ARM_ARCH_7A__) |
| 563 | while (ptr <= end - sizeof(int)) { |
| 564 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 565 | goto http_msg_hdr_val2; |
| 566 | ptr += sizeof(int); |
| 567 | } |
| 568 | #endif |
| 569 | if (ptr >= end) { |
| 570 | state = HTTP_MSG_HDR_VAL; |
| 571 | goto http_msg_ood; |
| 572 | } |
| 573 | http_msg_hdr_val2: |
| 574 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 575 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_val2, http_msg_ood, state, HTTP_MSG_HDR_VAL); |
| 576 | |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 577 | msg->eol = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 578 | /* Note: we could also copy eol into ->eoh so that we have the |
| 579 | * real header end in case it ends with lots of LWS, but is this |
| 580 | * really needed ? |
| 581 | */ |
| 582 | if (likely(*ptr == '\r')) |
| 583 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lf, http_msg_ood, state, HTTP_MSG_HDR_L2_LF); |
| 584 | goto http_msg_hdr_l2_lf; |
| 585 | |
| 586 | case HTTP_MSG_HDR_L2_LF: |
| 587 | http_msg_hdr_l2_lf: |
| 588 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_HDR_L2_LF); |
| 589 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_l2_lws, http_msg_ood, state, HTTP_MSG_HDR_L2_LWS); |
| 590 | |
| 591 | case HTTP_MSG_HDR_L2_LWS: |
| 592 | http_msg_hdr_l2_lws: |
| 593 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 594 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 595 | for (; input + msg->eol < ptr; msg->eol++) |
| 596 | input[msg->eol] = ' '; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 597 | goto http_msg_hdr_val; |
| 598 | } |
| 599 | http_msg_complete_header: |
| 600 | /* |
| 601 | * It was a new header, so the last one is finished. |
| 602 | * Assumes msg->sol points to the first char, msg->sov points |
| 603 | * to the first character of the value and msg->eol to the |
| 604 | * first CR or LF so we know how the line ends. We insert last |
| 605 | * header into the index. |
| 606 | */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 607 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, input[msg->eol] == '\r', |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 608 | idx, idx->tail) < 0)) { |
| 609 | state = HTTP_MSG_HDR_L2_LWS; |
| 610 | goto http_msg_invalid; |
| 611 | } |
| 612 | |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 613 | msg->sol = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 614 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 615 | goto http_msg_hdr_name; |
| 616 | } |
| 617 | |
| 618 | if (likely(*ptr == '\r')) |
| 619 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_last_lf, http_msg_ood, state, HTTP_MSG_LAST_LF); |
| 620 | goto http_msg_last_lf; |
| 621 | |
| 622 | case HTTP_MSG_LAST_LF: |
| 623 | http_msg_last_lf: |
| 624 | /* Assumes msg->sol points to the first of either CR or LF. |
| 625 | * Sets ->sov and ->next to the total header length, ->eoh to |
| 626 | * the last CRLF, and ->eol to the last CRLF length (1 or 2). |
| 627 | */ |
| 628 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, HTTP_MSG_LAST_LF); |
| 629 | ptr++; |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 630 | msg->sov = msg->next = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 631 | msg->eoh = msg->sol; |
| 632 | msg->sol = 0; |
| 633 | msg->eol = msg->sov - msg->eoh; |
| 634 | msg->msg_state = HTTP_MSG_BODY; |
| 635 | return; |
| 636 | |
| 637 | case HTTP_MSG_ERROR: |
| 638 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 639 | break; |
| 640 | |
| 641 | default: |
| 642 | #ifdef DEBUG_FULL |
| 643 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 644 | exit(1); |
| 645 | #endif |
| 646 | ; |
| 647 | } |
| 648 | http_msg_ood: |
| 649 | /* out of data */ |
| 650 | msg->msg_state = state; |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 651 | msg->next = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 652 | return; |
| 653 | |
| 654 | http_msg_invalid: |
| 655 | /* invalid message */ |
| 656 | msg->err_state = state; |
| 657 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 658 | msg->next = ptr - input; |
Willy Tarreau | 8740c8b | 2017-09-21 10:22:25 +0200 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 662 | /* This function parses a contiguous HTTP/1 headers block starting at <start> |
| 663 | * and ending before <stop>, at once, and converts it a list of (name,value) |
| 664 | * pairs representing header fields into the array <hdr> of size <hdr_num>, |
| 665 | * 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] | 666 | * too small to represent the whole message, an error is returned. Some |
| 667 | * protocol elements such as content-length and transfer-encoding will be |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 668 | * parsed and stored into h1m as well. <hdr> may be null, in which case only |
| 669 | * the parsing state will be updated. This may be used to restart the parsing |
| 670 | * where it stopped for example. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 671 | * |
| 672 | * For now it's limited to the response. If the header block is incomplete, |
| 673 | * 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] | 674 | * The caller is responsible for initializing h1m->state to H1_MSG_RPBEFORE, |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 675 | * and h1m->next to zero on the first call, the parser will do the rest. If |
| 676 | * an incomplete message is seen, the caller only needs to present h1m->state |
| 677 | * and h1m->next again, with an empty header list so that the parser can start |
| 678 | * again. In this case, it will detect that it interrupted a previous session |
| 679 | * and will first look for the end of the message before reparsing it again and |
| 680 | * indexing it at the same time. This ensures that incomplete messages fed 1 |
| 681 | * character at a time are never processed entirely more than exactly twice, |
| 682 | * and that there is no need to store all the internal state and pre-parsed |
| 683 | * headers or start line between calls. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 684 | * |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 685 | * A pointer to a start line descriptor may be passed in <slp>, in which case |
| 686 | * the parser will fill it with whatever it found. |
| 687 | * |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 688 | * The code derived from the main HTTP/1 parser above but was simplified and |
| 689 | * optimized to process responses produced or forwarded by haproxy. The caller |
| 690 | * is responsible for ensuring that the message doesn't wrap, and should ensure |
| 691 | * it is complete to avoid having to retry the operation after a failed |
| 692 | * attempt. The message is not supposed to be invalid, which is why a few |
| 693 | * properties such as the character set used in the header field names are not |
| 694 | * checked. In case of an unparsable response message, a negative value will be |
| 695 | * returned with h1m->err_pos and h1m->err_state matching the location and |
| 696 | * state where the error was met. Leading blank likes are tolerated but not |
| 697 | * recommended. |
| 698 | * |
| 699 | * This function returns : |
| 700 | * -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] | 701 | * 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] | 702 | * the position relative to <start> |
| 703 | * -2 if the output is full (hdr_num reached). err_state and err_pos also |
| 704 | * indicate where it failed. |
| 705 | * 0 in case of missing data. |
| 706 | * > 0 on success, it then corresponds to the number of bytes read since |
| 707 | * <start> so that the caller can go on with the payload. |
| 708 | */ |
| 709 | int h1_headers_to_hdr_list(char *start, const char *stop, |
| 710 | struct http_hdr *hdr, unsigned int hdr_num, |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 711 | struct h1m *h1m, union h1_sl *slp) |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 712 | { |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 713 | enum h1m_state state; |
| 714 | register char *ptr; |
| 715 | register const char *end; |
| 716 | unsigned int hdr_count; |
| 717 | unsigned int skip; /* number of bytes skipped at the beginning */ |
| 718 | unsigned int sol; /* start of line */ |
| 719 | unsigned int col; /* position of the colon */ |
| 720 | unsigned int eol; /* end of line */ |
| 721 | unsigned int sov; /* start of value */ |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 722 | union h1_sl sl; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 723 | int skip_update; |
| 724 | int restarting; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 725 | struct ist n, v; /* header name and value during parsing */ |
| 726 | |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 727 | skip = 0; // do it only once to keep track of the leading CRLF. |
| 728 | |
| 729 | try_again: |
| 730 | hdr_count = sol = col = eol = sov = 0; |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 731 | sl.st.status = 0; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 732 | skip_update = restarting = 0; |
| 733 | |
| 734 | ptr = start + h1m->next; |
| 735 | end = stop; |
| 736 | state = h1m->state; |
| 737 | |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 738 | if (state != H1_MSG_RQBEFORE && state != H1_MSG_RPBEFORE) |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 739 | restarting = 1; |
| 740 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 741 | if (unlikely(ptr >= end)) |
| 742 | goto http_msg_ood; |
| 743 | |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 744 | /* don't update output if hdr is NULL or if we're restarting */ |
| 745 | if (!hdr || restarting) |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 746 | skip_update = 1; |
| 747 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 748 | switch (state) { |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 749 | case H1_MSG_RQBEFORE: |
| 750 | http_msg_rqbefore: |
| 751 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 752 | /* we have a start of message, we may have skipped some |
| 753 | * heading CRLF. Skip them now. |
| 754 | */ |
| 755 | skip += ptr - start; |
| 756 | start = ptr; |
| 757 | |
| 758 | sol = 0; |
| 759 | sl.rq.m = skip; |
| 760 | hdr_count = 0; |
| 761 | state = H1_MSG_RQMETH; |
| 762 | goto http_msg_rqmeth; |
| 763 | } |
| 764 | |
| 765 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
| 766 | state = H1_MSG_RQBEFORE; |
| 767 | goto http_msg_invalid; |
| 768 | } |
| 769 | |
| 770 | if (unlikely(*ptr == '\n')) |
| 771 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE); |
| 772 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore_cr, http_msg_ood, state, H1_MSG_RQBEFORE_CR); |
| 773 | /* stop here */ |
| 774 | |
| 775 | case H1_MSG_RQBEFORE_CR: |
| 776 | http_msg_rqbefore_cr: |
| 777 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQBEFORE_CR); |
| 778 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqbefore, http_msg_ood, state, H1_MSG_RQBEFORE); |
| 779 | /* stop here */ |
| 780 | |
| 781 | case H1_MSG_RQMETH: |
| 782 | http_msg_rqmeth: |
| 783 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 784 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth, http_msg_ood, state, H1_MSG_RQMETH); |
| 785 | |
| 786 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 787 | sl.rq.m_l = ptr - start; |
| 788 | sl.rq.meth = find_http_meth(start, sl.rq.m_l); |
| 789 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP); |
| 790 | } |
| 791 | |
| 792 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 793 | /* HTTP 0.9 request */ |
| 794 | sl.rq.m_l = ptr - start; |
| 795 | sl.rq.meth = find_http_meth(start, sl.rq.m_l); |
| 796 | http_msg_req09_uri: |
| 797 | sl.rq.u = ptr - start + skip; |
| 798 | http_msg_req09_uri_e: |
| 799 | sl.rq.u_l = ptr - start + skip - sl.rq.u; |
| 800 | http_msg_req09_ver: |
| 801 | sl.rq.v = ptr - start + skip; |
| 802 | sl.rq.v_l = 0; |
| 803 | goto http_msg_rqline_eol; |
| 804 | } |
| 805 | state = H1_MSG_RQMETH; |
| 806 | goto http_msg_invalid; |
| 807 | |
| 808 | case H1_MSG_RQMETH_SP: |
| 809 | http_msg_rqmeth_sp: |
| 810 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 811 | sl.rq.u = ptr - start + skip; |
| 812 | goto http_msg_rquri; |
| 813 | } |
| 814 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 815 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqmeth_sp, http_msg_ood, state, H1_MSG_RQMETH_SP); |
| 816 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 817 | goto http_msg_req09_uri; |
| 818 | |
| 819 | case H1_MSG_RQURI: |
| 820 | http_msg_rquri: |
| 821 | #if defined(__x86_64__) || \ |
| 822 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 823 | defined(__ARM_ARCH_7A__) |
| 824 | /* speedup: skip bytes not between 0x21 and 0x7e inclusive */ |
| 825 | while (ptr <= end - sizeof(int)) { |
| 826 | int x = *(int *)ptr - 0x21212121; |
| 827 | if (x & 0x80808080) |
| 828 | break; |
| 829 | |
| 830 | x -= 0x5e5e5e5e; |
| 831 | if (!(x & 0x80808080)) |
| 832 | break; |
| 833 | |
| 834 | ptr += sizeof(int); |
| 835 | } |
| 836 | #endif |
| 837 | if (ptr >= end) { |
| 838 | state = H1_MSG_RQURI; |
| 839 | goto http_msg_ood; |
| 840 | } |
| 841 | http_msg_rquri2: |
| 842 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
| 843 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri2, http_msg_ood, state, H1_MSG_RQURI); |
| 844 | |
| 845 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 846 | sl.rq.u_l = ptr - start + skip - sl.rq.u; |
| 847 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP); |
| 848 | } |
| 849 | if (likely((unsigned char)*ptr >= 128)) { |
| 850 | /* non-ASCII chars are forbidden unless option |
| 851 | * accept-invalid-http-request is enabled in the frontend. |
| 852 | * In any case, we capture the faulty char. |
| 853 | */ |
| 854 | if (h1m->err_pos < -1) |
| 855 | goto invalid_char; |
| 856 | if (h1m->err_pos == -1) |
| 857 | h1m->err_pos = ptr - start + skip; |
| 858 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri, http_msg_ood, state, H1_MSG_RQURI); |
| 859 | } |
| 860 | |
| 861 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 862 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 863 | goto http_msg_req09_uri_e; |
| 864 | } |
| 865 | |
| 866 | /* OK forbidden chars, 0..31 or 127 */ |
| 867 | invalid_char: |
| 868 | state = H1_MSG_RQURI; |
| 869 | goto http_msg_invalid; |
| 870 | |
| 871 | case H1_MSG_RQURI_SP: |
| 872 | http_msg_rquri_sp: |
| 873 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 874 | sl.rq.v = ptr - start + skip; |
| 875 | goto http_msg_rqver; |
| 876 | } |
| 877 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 878 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rquri_sp, http_msg_ood, state, H1_MSG_RQURI_SP); |
| 879 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 880 | goto http_msg_req09_ver; |
| 881 | |
| 882 | |
| 883 | case H1_MSG_RQVER: |
| 884 | http_msg_rqver: |
| 885 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
| 886 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqver, http_msg_ood, state, H1_MSG_RQVER); |
| 887 | |
| 888 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 889 | sl.rq.v_l = ptr - start + skip - sl.rq.v; |
| 890 | http_msg_rqline_eol: |
| 891 | /* We have seen the end of line. Note that we do not |
| 892 | * necessarily have the \n yet, but at least we know that we |
| 893 | * have EITHER \r OR \n, otherwise the request would not be |
| 894 | * complete. We can then record the request length and return |
| 895 | * to the caller which will be able to register it. |
| 896 | */ |
| 897 | |
| 898 | if (likely(!skip_update)) { |
| 899 | if (unlikely(hdr_count >= hdr_num)) { |
| 900 | state = H1_MSG_RQVER; |
| 901 | goto http_output_full; |
| 902 | } |
| 903 | http_set_hdr(&hdr[hdr_count++], ist(":method"), ist2(start + sl.rq.m, sl.rq.m_l)); |
| 904 | |
| 905 | if (unlikely(hdr_count >= hdr_num)) { |
| 906 | state = H1_MSG_RQVER; |
| 907 | goto http_output_full; |
| 908 | } |
| 909 | http_set_hdr(&hdr[hdr_count++], ist(":path"), ist2(start + sl.rq.u, sl.rq.u_l)); |
| 910 | } |
| 911 | |
| 912 | sol = ptr - start; |
| 913 | if (likely(*ptr == '\r')) |
| 914 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rqline_end, http_msg_ood, state, H1_MSG_RQLINE_END); |
| 915 | goto http_msg_rqline_end; |
| 916 | } |
| 917 | |
| 918 | /* neither an HTTP_VER token nor a CRLF */ |
| 919 | state = H1_MSG_RQVER; |
| 920 | goto http_msg_invalid; |
| 921 | |
| 922 | case H1_MSG_RQLINE_END: |
| 923 | http_msg_rqline_end: |
| 924 | /* check for HTTP/0.9 request : no version information |
| 925 | * available. sol must point to the first of CR or LF. However |
| 926 | * since we don't save these elements between calls, if we come |
| 927 | * here from a restart, we don't necessarily know. Thus in this |
| 928 | * case we simply start over. |
| 929 | */ |
| 930 | if (restarting) |
| 931 | goto restart; |
| 932 | |
| 933 | if (unlikely(sl.rq.v_l == 0)) |
| 934 | goto http_msg_last_lf; |
| 935 | |
| 936 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RQLINE_END); |
| 937 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_hdr_first, http_msg_ood, state, H1_MSG_HDR_FIRST); |
| 938 | /* stop here */ |
| 939 | |
| 940 | /* |
| 941 | * Common states below |
| 942 | */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 943 | case H1_MSG_RPBEFORE: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 944 | http_msg_rpbefore: |
| 945 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 946 | /* we have a start of message, we may have skipped some |
| 947 | * heading CRLF. Skip them now. |
| 948 | */ |
| 949 | skip += ptr - start; |
| 950 | start = ptr; |
| 951 | |
| 952 | sol = 0; |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 953 | sl.st.v = skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 954 | hdr_count = 0; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 955 | state = H1_MSG_RPVER; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 956 | goto http_msg_rpver; |
| 957 | } |
| 958 | |
| 959 | if (unlikely(!HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 960 | state = H1_MSG_RPBEFORE; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 961 | goto http_msg_invalid; |
| 962 | } |
| 963 | |
| 964 | if (unlikely(*ptr == '\n')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 965 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpbefore, http_msg_ood, state, H1_MSG_RPBEFORE); |
| 966 | 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] | 967 | /* stop here */ |
| 968 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 969 | case H1_MSG_RPBEFORE_CR: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 970 | http_msg_rpbefore_cr: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 971 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPBEFORE_CR); |
| 972 | 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] | 973 | /* stop here */ |
| 974 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 975 | case H1_MSG_RPVER: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 976 | http_msg_rpver: |
| 977 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 978 | 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] | 979 | |
| 980 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 981 | sl.st.v_l = ptr - start; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 982 | 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] | 983 | } |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 984 | state = H1_MSG_RPVER; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 985 | goto http_msg_invalid; |
| 986 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 987 | case H1_MSG_RPVER_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 988 | http_msg_rpver_sp: |
| 989 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 990 | sl.st.status = 0; |
| 991 | sl.st.c = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 992 | goto http_msg_rpcode; |
| 993 | } |
| 994 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 995 | 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] | 996 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 997 | state = H1_MSG_RPVER_SP; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 998 | goto http_msg_invalid; |
| 999 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1000 | case H1_MSG_RPCODE: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1001 | http_msg_rpcode: |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 1002 | if (likely(HTTP_IS_DIGIT(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1003 | sl.st.status = sl.st.status * 10 + *ptr - '0'; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1004 | 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] | 1005 | } |
| 1006 | |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 1007 | if (unlikely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1008 | state = H1_MSG_RPCODE; |
Willy Tarreau | 1b4cf9b | 2017-11-09 11:15:45 +0100 | [diff] [blame] | 1009 | goto http_msg_invalid; |
| 1010 | } |
| 1011 | |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1012 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1013 | sl.st.c_l = ptr - start + skip - sl.st.c; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1014 | 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] | 1015 | } |
| 1016 | |
| 1017 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1018 | sl.st.c_l = ptr - start + skip - sl.st.c; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1019 | |
| 1020 | http_msg_rsp_reason: |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1021 | sl.st.r = ptr - start + skip; |
| 1022 | sl.st.r_l = 0; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1023 | goto http_msg_rpline_eol; |
| 1024 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1025 | case H1_MSG_RPCODE_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1026 | http_msg_rpcode_sp: |
| 1027 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1028 | sl.st.r = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1029 | goto http_msg_rpreason; |
| 1030 | } |
| 1031 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1032 | 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] | 1033 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1034 | goto http_msg_rsp_reason; |
| 1035 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1036 | case H1_MSG_RPREASON: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1037 | http_msg_rpreason: |
| 1038 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1039 | EAT_AND_JUMP_OR_RETURN(ptr, end, http_msg_rpreason, http_msg_ood, state, H1_MSG_RPREASON); |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1040 | sl.st.r_l = ptr - start + skip - sl.st.r; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1041 | http_msg_rpline_eol: |
| 1042 | /* We have seen the end of line. Note that we do not |
| 1043 | * necessarily have the \n yet, but at least we know that we |
| 1044 | * have EITHER \r OR \n, otherwise the response would not be |
| 1045 | * complete. We can then record the response length and return |
| 1046 | * to the caller which will be able to register it. |
| 1047 | */ |
| 1048 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1049 | if (likely(!skip_update)) { |
| 1050 | if (unlikely(hdr_count >= hdr_num)) { |
| 1051 | state = H1_MSG_RPREASON; |
| 1052 | goto http_output_full; |
| 1053 | } |
| 1054 | http_set_hdr(&hdr[hdr_count++], ist(":status"), ist2(start + sl.st.c, sl.st.c_l)); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1055 | } |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1056 | |
| 1057 | sol = ptr - start; |
| 1058 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1059 | 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] | 1060 | goto http_msg_rpline_end; |
| 1061 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1062 | case H1_MSG_RPLINE_END: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1063 | http_msg_rpline_end: |
| 1064 | /* sol must point to the first of CR or LF. */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1065 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_RPLINE_END); |
| 1066 | 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] | 1067 | /* stop here */ |
| 1068 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1069 | case H1_MSG_HDR_FIRST: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1070 | http_msg_hdr_first: |
| 1071 | sol = ptr - start; |
| 1072 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1073 | goto http_msg_hdr_name; |
| 1074 | } |
| 1075 | |
| 1076 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1077 | 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] | 1078 | goto http_msg_last_lf; |
| 1079 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1080 | case H1_MSG_HDR_NAME: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1081 | http_msg_hdr_name: |
| 1082 | /* assumes sol points to the first char */ |
| 1083 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1084 | /* turn it to lower case if needed */ |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 1085 | if (isupper((unsigned char)*ptr) && h1m->flags & H1_MF_TOLOWER) |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1086 | *ptr = tolower(*ptr); |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1087 | 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] | 1088 | } |
| 1089 | |
| 1090 | if (likely(*ptr == ':')) { |
| 1091 | col = ptr - start; |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1092 | 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] | 1093 | } |
| 1094 | |
Willy Tarreau | 9aec305 | 2018-09-12 09:20:40 +0200 | [diff] [blame] | 1095 | if (likely(h1m->err_pos < -1) || *ptr == '\n') { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1096 | state = H1_MSG_HDR_NAME; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1097 | goto http_msg_invalid; |
| 1098 | } |
| 1099 | |
Willy Tarreau | 9aec305 | 2018-09-12 09:20:40 +0200 | [diff] [blame] | 1100 | if (h1m->err_pos == -1) /* capture the error pointer */ |
| 1101 | h1m->err_pos = ptr - start + skip; /* >= 0 now */ |
| 1102 | |
| 1103 | /* and we still accept this non-token character */ |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1104 | 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] | 1105 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1106 | case H1_MSG_HDR_L1_SP: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1107 | http_msg_hdr_l1_sp: |
| 1108 | /* assumes sol points to the first char */ |
| 1109 | if (likely(HTTP_IS_SPHT(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1110 | 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] | 1111 | |
| 1112 | /* header value can be basically anything except CR/LF */ |
| 1113 | sov = ptr - start; |
| 1114 | |
| 1115 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1116 | goto http_msg_hdr_val; |
| 1117 | } |
| 1118 | |
| 1119 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1120 | 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] | 1121 | goto http_msg_hdr_l1_lf; |
| 1122 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1123 | case H1_MSG_HDR_L1_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1124 | http_msg_hdr_l1_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1125 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L1_LF); |
| 1126 | 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] | 1127 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1128 | case H1_MSG_HDR_L1_LWS: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1129 | http_msg_hdr_l1_lws: |
| 1130 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1131 | /* replace HT,CR,LF with spaces */ |
| 1132 | for (; start + sov < ptr; sov++) |
| 1133 | start[sov] = ' '; |
| 1134 | goto http_msg_hdr_l1_sp; |
| 1135 | } |
| 1136 | /* we had a header consisting only in spaces ! */ |
| 1137 | eol = sov; |
| 1138 | goto http_msg_complete_header; |
| 1139 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1140 | case H1_MSG_HDR_VAL: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1141 | http_msg_hdr_val: |
| 1142 | /* assumes sol points to the first char, and sov |
| 1143 | * points to the first character of the value. |
| 1144 | */ |
| 1145 | |
| 1146 | /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D |
| 1147 | * and lower. In fact since most of the time is spent in the loop, we |
| 1148 | * also remove the sign bit test so that bytes 0x8e..0x0d break the |
| 1149 | * loop, but we don't care since they're very rare in header values. |
| 1150 | */ |
| 1151 | #if defined(__x86_64__) |
| 1152 | while (ptr <= end - sizeof(long)) { |
| 1153 | if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL) |
| 1154 | goto http_msg_hdr_val2; |
| 1155 | ptr += sizeof(long); |
| 1156 | } |
| 1157 | #endif |
| 1158 | #if defined(__x86_64__) || \ |
| 1159 | defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ |
| 1160 | defined(__ARM_ARCH_7A__) |
| 1161 | while (ptr <= end - sizeof(int)) { |
| 1162 | if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080) |
| 1163 | goto http_msg_hdr_val2; |
| 1164 | ptr += sizeof(int); |
| 1165 | } |
| 1166 | #endif |
| 1167 | if (ptr >= end) { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1168 | state = H1_MSG_HDR_VAL; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1169 | goto http_msg_ood; |
| 1170 | } |
| 1171 | http_msg_hdr_val2: |
| 1172 | if (likely(!HTTP_IS_CRLF(*ptr))) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1173 | 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] | 1174 | |
| 1175 | eol = ptr - start; |
| 1176 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1177 | * real header end in case it ends with lots of LWS, but is this |
| 1178 | * really needed ? |
| 1179 | */ |
| 1180 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1181 | 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] | 1182 | goto http_msg_hdr_l2_lf; |
| 1183 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1184 | case H1_MSG_HDR_L2_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1185 | http_msg_hdr_l2_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1186 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_HDR_L2_LF); |
| 1187 | 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] | 1188 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1189 | case H1_MSG_HDR_L2_LWS: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1190 | http_msg_hdr_l2_lws: |
| 1191 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1192 | /* LWS: replace HT,CR,LF with spaces */ |
| 1193 | for (; start + eol < ptr; eol++) |
| 1194 | start[eol] = ' '; |
| 1195 | goto http_msg_hdr_val; |
| 1196 | } |
| 1197 | http_msg_complete_header: |
| 1198 | /* |
| 1199 | * It was a new header, so the last one is finished. Assumes |
| 1200 | * <sol> points to the first char of the name, <col> to the |
| 1201 | * colon, <sov> points to the first character of the value and |
| 1202 | * <eol> to the first CR or LF so we know how the line ends. We |
| 1203 | * will trim spaces around the value. It's possible to do it by |
| 1204 | * adjusting <eol> and <sov> which are no more used after this. |
| 1205 | * We can add the header field to the list. |
| 1206 | */ |
| 1207 | while (sov < eol && HTTP_IS_LWS(start[sov])) |
| 1208 | sov++; |
| 1209 | |
| 1210 | while (eol - 1 > sov && HTTP_IS_LWS(start[eol - 1])) |
| 1211 | eol--; |
| 1212 | |
| 1213 | |
| 1214 | n = ist2(start + sol, col - sol); |
| 1215 | v = ist2(start + sov, eol - sov); |
| 1216 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1217 | if (likely(!skip_update)) { |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1218 | long long cl; |
| 1219 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1220 | if (unlikely(hdr_count >= hdr_num)) { |
| 1221 | state = H1_MSG_HDR_L2_LWS; |
| 1222 | goto http_output_full; |
| 1223 | } |
| 1224 | |
| 1225 | http_set_hdr(&hdr[hdr_count++], n, v); |
| 1226 | |
Willy Tarreau | 11da567 | 2018-09-11 19:23:04 +0200 | [diff] [blame] | 1227 | if (sl.st.status >= 100 && sl.st.status < 200) |
Willy Tarreau | d22e83a | 2017-10-31 08:02:24 +0100 | [diff] [blame] | 1228 | h1m->curr_len = h1m->body_len = 0; |
Willy Tarreau | 11da567 | 2018-09-11 19:23:04 +0200 | [diff] [blame] | 1229 | else if (sl.st.status == 304 || sl.st.status == 204) { |
Willy Tarreau | 8ea0f38 | 2017-10-30 19:31:59 +0100 | [diff] [blame] | 1230 | /* no contents, claim c-len is present and set to zero */ |
| 1231 | h1m->flags |= H1_MF_CLEN; |
| 1232 | h1m->curr_len = h1m->body_len = 0; |
| 1233 | } |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 1234 | else if (isteqi(n, ist("transfer-encoding"))) { |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1235 | h1m->flags &= ~H1_MF_CLEN; |
| 1236 | h1m->flags |= H1_MF_CHNK; |
| 1237 | } |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 1238 | else if (isteqi(n, ist("content-length")) && !(h1m->flags & H1_MF_CHNK)) { |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1239 | h1m->flags |= H1_MF_CLEN; |
| 1240 | strl2llrc(v.ptr, v.len, &cl); |
| 1241 | h1m->curr_len = h1m->body_len = cl; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | sol = ptr - start; |
| 1246 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1247 | goto http_msg_hdr_name; |
| 1248 | |
| 1249 | if (likely(*ptr == '\r')) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1250 | 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] | 1251 | goto http_msg_last_lf; |
| 1252 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1253 | case H1_MSG_LAST_LF: |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1254 | http_msg_last_lf: |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 1255 | EXPECT_LF_HERE(ptr, http_msg_invalid, state, H1_MSG_LAST_LF); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1256 | ptr++; |
| 1257 | /* <ptr> now points to the first byte of payload. If needed sol |
| 1258 | * still points to the first of either CR or LF of the empty |
| 1259 | * line ending the headers block. |
| 1260 | */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1261 | if (likely(!skip_update)) { |
| 1262 | if (unlikely(hdr_count >= hdr_num)) { |
| 1263 | state = H1_MSG_LAST_LF; |
| 1264 | goto http_output_full; |
| 1265 | } |
| 1266 | http_set_hdr(&hdr[hdr_count++], ist(""), ist("")); |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1267 | } |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 1268 | |
| 1269 | /* reaching here we've parsed the whole message. We may detect |
| 1270 | * that we were already continuing an interrupted parsing pass |
| 1271 | * so we were silently looking for the end of message not |
| 1272 | * updating anything before deciding to parse it fully at once. |
| 1273 | * It's guaranteed that we won't match this test twice in a row |
| 1274 | * since restarting will turn zero. |
| 1275 | */ |
| 1276 | if (restarting) |
| 1277 | goto restart; |
| 1278 | |
| 1279 | if (h1m->flags & H1_MF_CHNK) |
| 1280 | state = H1_MSG_CHUNK_SIZE; |
| 1281 | else |
| 1282 | state = H1_MSG_DATA; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1283 | break; |
| 1284 | |
| 1285 | default: |
| 1286 | /* impossible states */ |
| 1287 | goto http_msg_invalid; |
| 1288 | } |
| 1289 | |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 1290 | /* Now we've left the headers state and are either in H1_MSG_DATA or |
| 1291 | * H1_MSG_CHUNK_SIZE. |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1292 | */ |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1293 | |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1294 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1295 | *slp = sl; |
| 1296 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1297 | h1m->state = state; |
| 1298 | h1m->next = ptr - start + skip; |
| 1299 | return h1m->next; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1300 | |
| 1301 | http_msg_ood: |
| 1302 | /* out of data at <ptr> during state <state> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1303 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1304 | *slp = sl; |
| 1305 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1306 | h1m->state = state; |
| 1307 | h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1308 | return 0; |
| 1309 | |
| 1310 | http_msg_invalid: |
| 1311 | /* invalid message, error at <ptr> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1312 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1313 | *slp = sl; |
| 1314 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1315 | h1m->err_state = h1m->state = state; |
| 1316 | h1m->err_pos = h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1317 | return -1; |
| 1318 | |
| 1319 | http_output_full: |
| 1320 | /* no more room to store the current header, error at <ptr> */ |
Willy Tarreau | 5384aac | 2018-09-11 16:04:48 +0200 | [diff] [blame] | 1321 | if (slp && !skip_update) |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 1322 | *slp = sl; |
| 1323 | |
Willy Tarreau | 4433c08 | 2018-09-11 15:33:32 +0200 | [diff] [blame] | 1324 | h1m->err_state = h1m->state = state; |
| 1325 | h1m->err_pos = h1m->next = ptr - start + skip; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1326 | return -2; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1327 | |
| 1328 | restart: |
| 1329 | h1m->next = 0; |
Willy Tarreau | c2ab9f5 | 2018-09-11 17:57:05 +0200 | [diff] [blame] | 1330 | if (h1m->flags & H1_MF_RESP) |
| 1331 | h1m->state = H1_MSG_RPBEFORE; |
| 1332 | else |
| 1333 | h1m->state = H1_MSG_RQBEFORE; |
Willy Tarreau | 4c34c0e | 2018-09-11 16:20:30 +0200 | [diff] [blame] | 1334 | goto try_again; |
Willy Tarreau | 794f9af | 2017-07-26 09:07:47 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1337 | /* This function performs a very minimal parsing of the trailers block present |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1338 | * 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] | 1339 | * bytes to delete to skip the trailers. It may return 0 if it's missing some |
| 1340 | * input data, or < 0 in case of parse error (in which case the caller may have |
| 1341 | * to decide how to proceed, possibly eating everything). |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1342 | */ |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1343 | 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] | 1344 | { |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1345 | const char *stop = b_peek(buf, ofs + max); |
| 1346 | int count = ofs; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1347 | |
| 1348 | while (1) { |
| 1349 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1350 | const char *start = b_peek(buf, count); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1351 | const char *ptr = start; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1352 | |
| 1353 | /* scan current line and stop at LF or CRLF */ |
| 1354 | while (1) { |
| 1355 | if (ptr == stop) |
| 1356 | return 0; |
| 1357 | |
| 1358 | if (*ptr == '\n') { |
| 1359 | if (!p1) |
| 1360 | p1 = ptr; |
| 1361 | p2 = ptr; |
| 1362 | break; |
| 1363 | } |
| 1364 | |
| 1365 | if (*ptr == '\r') { |
| 1366 | if (p1) |
| 1367 | return -1; |
| 1368 | p1 = ptr; |
| 1369 | } |
| 1370 | |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1371 | ptr = b_next(buf, ptr); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /* after LF; point to beginning of next line */ |
Willy Tarreau | 7314be8 | 2018-06-14 13:32:50 +0200 | [diff] [blame] | 1375 | p2 = b_next(buf, p2); |
| 1376 | count += b_dist(buf, start, p2); |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1377 | |
| 1378 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1379 | * Everything was scheduled for forwarding, there's nothing left |
| 1380 | * from this message. */ |
| 1381 | if (p1 == start) |
| 1382 | break; |
| 1383 | /* OK, next line then */ |
| 1384 | } |
Willy Tarreau | f40e682 | 2018-06-14 16:52:02 +0200 | [diff] [blame] | 1385 | return count - ofs; |
Willy Tarreau | 2510f70 | 2017-10-31 17:14:16 +0100 | [diff] [blame] | 1386 | } |
| 1387 | |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1388 | /* This function skips trailers in the buffer associated with HTTP message |
| 1389 | * <msg>. The first visited position is msg->next. If the end of the trailers is |
| 1390 | * found, the function returns >0. So, the caller can automatically schedul it |
| 1391 | * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough |
| 1392 | * data are available, the function does not change anything except maybe |
| 1393 | * msg->sol if it could parse some lines, and returns zero. If a parse error |
| 1394 | * is encountered, the function returns < 0 and does not change anything except |
| 1395 | * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS |
| 1396 | * state before calling this function, which implies that all non-trailers data |
| 1397 | * have already been scheduled for forwarding, and that msg->next exactly |
| 1398 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1399 | * important to note that this function is designed to be able to parse wrapped |
| 1400 | * headers at end of buffer. |
| 1401 | */ |
| 1402 | int http_forward_trailers(struct http_msg *msg) |
| 1403 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1404 | const struct buffer *buf = &msg->chn->buf; |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 1405 | const char *parse = ci_head(msg->chn); |
| 1406 | const char *stop = b_tail(buf); |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1407 | |
| 1408 | /* we have msg->next which points to next line. Look for CRLF. But |
| 1409 | * first, we reset msg->sol */ |
| 1410 | msg->sol = 0; |
| 1411 | while (1) { |
| 1412 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 1413 | const char *start = c_ptr(msg->chn, msg->next + msg->sol); |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1414 | const char *ptr = start; |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1415 | |
| 1416 | /* scan current line and stop at LF or CRLF */ |
| 1417 | while (1) { |
| 1418 | if (ptr == stop) |
| 1419 | return 0; |
| 1420 | |
| 1421 | if (*ptr == '\n') { |
| 1422 | if (!p1) |
| 1423 | p1 = ptr; |
| 1424 | p2 = ptr; |
| 1425 | break; |
| 1426 | } |
| 1427 | |
| 1428 | if (*ptr == '\r') { |
| 1429 | if (p1) { |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 1430 | msg->err_pos = b_dist(buf, parse, ptr); |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1431 | return -1; |
| 1432 | } |
| 1433 | p1 = ptr; |
| 1434 | } |
| 1435 | |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 1436 | ptr = b_next(buf, ptr); |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* after LF; point to beginning of next line */ |
Willy Tarreau | 5e74b0b | 2018-06-19 08:03:19 +0200 | [diff] [blame] | 1440 | p2 = b_next(buf, p2); |
| 1441 | msg->sol += b_dist(buf, start, p2); |
Willy Tarreau | db4893d | 2017-09-21 08:40:02 +0200 | [diff] [blame] | 1442 | |
| 1443 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1444 | * Everything was scheduled for forwarding, there's nothing left |
| 1445 | * from this message. */ |
| 1446 | if (p1 == start) |
| 1447 | return 1; |
| 1448 | |
| 1449 | /* OK, next line then */ |
| 1450 | } |
| 1451 | } |