Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/types/h1.h |
| 3 | * This file contains HTTP/1 protocol definitions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _TYPES_H1_H |
| 23 | #define _TYPES_H1_H |
| 24 | |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 25 | #include <common/http.h> |
| 26 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 27 | /* Legacy version of the HTTP/1 message state, used by the channels, should |
| 28 | * ultimately be removed. |
| 29 | */ |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 30 | enum h1_state { |
| 31 | HTTP_MSG_RQBEFORE = 0, // request: leading LF, before start line |
| 32 | HTTP_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line |
| 33 | /* these ones define a request start line */ |
| 34 | HTTP_MSG_RQMETH = 2, // parsing the Method |
| 35 | HTTP_MSG_RQMETH_SP = 3, // space(s) after the Method |
| 36 | HTTP_MSG_RQURI = 4, // parsing the Request URI |
| 37 | HTTP_MSG_RQURI_SP = 5, // space(s) after the Request URI |
| 38 | HTTP_MSG_RQVER = 6, // parsing the Request Version |
| 39 | HTTP_MSG_RQLINE_END = 7, // end of request line (CR or LF) |
| 40 | |
| 41 | HTTP_MSG_RPBEFORE = 8, // response: leading LF, before start line |
| 42 | HTTP_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line |
| 43 | |
| 44 | /* these ones define a response start line */ |
| 45 | HTTP_MSG_RPVER = 10, // parsing the Response Version |
| 46 | HTTP_MSG_RPVER_SP = 11, // space(s) after the Response Version |
| 47 | HTTP_MSG_RPCODE = 12, // response code |
| 48 | HTTP_MSG_RPCODE_SP = 13, // space(s) after the response code |
| 49 | HTTP_MSG_RPREASON = 14, // response reason |
| 50 | HTTP_MSG_RPLINE_END = 15, // end of response line (CR or LF) |
| 51 | |
| 52 | /* common header processing */ |
| 53 | HTTP_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible) |
| 54 | HTTP_MSG_HDR_NAME = 17, // parsing header name |
| 55 | HTTP_MSG_HDR_COL = 18, // parsing header colon |
| 56 | HTTP_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value |
| 57 | HTTP_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value |
| 58 | HTTP_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS |
| 59 | HTTP_MSG_HDR_VAL = 22, // parsing header value |
| 60 | HTTP_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value |
| 61 | HTTP_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS |
| 62 | |
| 63 | HTTP_MSG_LAST_LF = 25, // parsing last LF |
| 64 | |
| 65 | /* error state : must be before HTTP_MSG_BODY so that (>=BODY) always indicates |
| 66 | * that data are being processed. |
| 67 | */ |
| 68 | HTTP_MSG_ERROR = 26, // an error occurred |
| 69 | /* Body processing. |
| 70 | * The state HTTP_MSG_BODY is a delimiter to know if we're waiting for headers |
| 71 | * or body. All the sub-states below also indicate we're processing the body, |
| 72 | * with some additional information. |
| 73 | */ |
| 74 | HTTP_MSG_BODY = 27, // parsing body at end of headers |
| 75 | HTTP_MSG_100_SENT = 28, // parsing body after a 100-Continue was sent |
| 76 | HTTP_MSG_CHUNK_SIZE = 29, // parsing the chunk size (RFC7230 #4.1) |
| 77 | HTTP_MSG_DATA = 30, // skipping data chunk / content-length data |
| 78 | HTTP_MSG_CHUNK_CRLF = 31, // skipping CRLF after data chunk |
| 79 | HTTP_MSG_TRAILERS = 32, // trailers (post-data entity headers) |
| 80 | /* we enter this state when we've received the end of the current message */ |
| 81 | HTTP_MSG_ENDING = 33, // message end received, wait that the filters end too |
| 82 | HTTP_MSG_DONE = 34, // message end received, waiting for resync or close |
| 83 | HTTP_MSG_CLOSING = 35, // shutdown_w done, not all bytes sent yet |
| 84 | HTTP_MSG_CLOSED = 36, // shutdown_w done, all bytes sent |
| 85 | HTTP_MSG_TUNNEL = 37, // tunneled data after DONE |
| 86 | } __attribute__((packed)); |
| 87 | |
| 88 | |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 89 | /* Possible states while parsing HTTP/1 messages (request|response) */ |
| 90 | enum h1m_state { |
| 91 | H1_MSG_RQBEFORE = 0, // request: leading LF, before start line |
| 92 | H1_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line |
| 93 | /* these ones define a request start line */ |
| 94 | H1_MSG_RQMETH = 2, // parsing the Method |
| 95 | H1_MSG_RQMETH_SP = 3, // space(s) after the Method |
| 96 | H1_MSG_RQURI = 4, // parsing the Request URI |
| 97 | H1_MSG_RQURI_SP = 5, // space(s) after the Request URI |
| 98 | H1_MSG_RQVER = 6, // parsing the Request Version |
| 99 | H1_MSG_RQLINE_END = 7, // end of request line (CR or LF) |
| 100 | |
| 101 | H1_MSG_RPBEFORE = 8, // response: leading LF, before start line |
| 102 | H1_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line |
| 103 | |
| 104 | /* these ones define a response start line */ |
| 105 | H1_MSG_RPVER = 10, // parsing the Response Version |
| 106 | H1_MSG_RPVER_SP = 11, // space(s) after the Response Version |
| 107 | H1_MSG_RPCODE = 12, // response code |
| 108 | H1_MSG_RPCODE_SP = 13, // space(s) after the response code |
| 109 | H1_MSG_RPREASON = 14, // response reason |
| 110 | H1_MSG_RPLINE_END = 15, // end of response line (CR or LF) |
| 111 | |
| 112 | /* common header processing */ |
| 113 | H1_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible) |
| 114 | H1_MSG_HDR_NAME = 17, // parsing header name |
| 115 | H1_MSG_HDR_COL = 18, // parsing header colon |
| 116 | H1_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value |
| 117 | H1_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value |
| 118 | H1_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS |
| 119 | H1_MSG_HDR_VAL = 22, // parsing header value |
| 120 | H1_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value |
| 121 | H1_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS |
| 122 | |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 123 | H1_MSG_LAST_LF = 25, // parsing last LF, last state for headers |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 124 | |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 125 | /* Body processing. */ |
| 126 | |
| 127 | H1_MSG_CHUNK_SIZE = 26, // parsing the chunk size (RFC7230 #4.1) |
| 128 | H1_MSG_DATA = 27, // skipping data chunk / content-length data |
| 129 | H1_MSG_CHUNK_CRLF = 28, // skipping CRLF after data chunk |
| 130 | H1_MSG_TRAILERS = 29, // trailers (post-data entity headers) |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 131 | /* we enter this state when we've received the end of the current message */ |
Willy Tarreau | 001823c | 2018-09-12 17:25:32 +0200 | [diff] [blame] | 132 | H1_MSG_DONE = 30, // message end received, waiting for resync or close |
| 133 | H1_MSG_TUNNEL = 31, // tunneled data after DONE |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 134 | } __attribute__((packed)); |
| 135 | |
| 136 | |
Willy Tarreau | 4093a4d | 2017-09-21 11:46:43 +0200 | [diff] [blame] | 137 | /* HTTP/1 message flags (32 bit), for use in h1m->flags only */ |
| 138 | #define H1_MF_NONE 0x00000000 |
| 139 | #define H1_MF_CLEN 0x00000001 // content-length present |
| 140 | #define H1_MF_CHNK 0x00000002 // chunk present, exclusive with c-l |
Willy Tarreau | ccaf233 | 2018-09-11 16:47:23 +0200 | [diff] [blame] | 141 | #define H1_MF_RESP 0x00000004 // this message is the response message |
Willy Tarreau | eb528db | 2018-09-12 09:54:00 +0200 | [diff] [blame] | 142 | #define H1_MF_TOLOWER 0x00000008 // turn the header names to lower case |
Willy Tarreau | ba5fbca | 2018-09-13 11:32:51 +0200 | [diff] [blame] | 143 | #define H1_MF_VER_11 0x00000010 // message indicates version 1.1 or above |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 144 | #define H1_MF_CONN_CLO 0x00000020 // message contains "connection: close" |
| 145 | #define H1_MF_CONN_KAL 0x00000040 // message contains "connection: keep-alive" |
| 146 | #define H1_MF_CONN_UPG 0x00000080 // message contains "connection: upgrade" |
Christopher Faulet | c4e53f4 | 2018-09-14 11:15:52 +0200 | [diff] [blame] | 147 | #define H1_MF_XFER_LEN 0x00000100 // message xfer size can be determined |
Willy Tarreau | 2557f6a | 2018-09-14 16:34:47 +0200 | [diff] [blame] | 148 | #define H1_MF_XFER_ENC 0x00000200 // transfer-encoding is present |
Willy Tarreau | 98f5cf7 | 2018-09-13 14:15:58 +0200 | [diff] [blame] | 149 | |
| 150 | /* Note: for a connection to be persistent, we need this for the request : |
| 151 | * - one of CLEN or CHNK |
| 152 | * - version 1.0 and KAL and not CLO |
| 153 | * - or version 1.1 and not CLO |
| 154 | * For the response it's the same except that UPG must not appear either. |
| 155 | * So in short, for a request it's (CLEN|CHNK) > 0 && !CLO && (VER_11 || KAL) |
| 156 | * and for a response it's (CLEN|CHNK) > 0 && !(CLO|UPG) && (VER_11 || KAL) |
| 157 | */ |
Willy Tarreau | 4093a4d | 2017-09-21 11:46:43 +0200 | [diff] [blame] | 158 | |
| 159 | |
Willy Tarreau | bbf3823 | 2018-09-12 09:08:54 +0200 | [diff] [blame] | 160 | /* basic HTTP/1 message state for use in parsers. The err_pos field is special, |
| 161 | * it is pre-set to a negative value (-1 or -2), and once non-negative it contains |
| 162 | * the relative position in the message of the first parse error. -2 is used to tell |
| 163 | * the parser that we want to block the invalid message. -1 is used to only perform |
| 164 | * a silent capture. |
| 165 | */ |
Willy Tarreau | 4093a4d | 2017-09-21 11:46:43 +0200 | [diff] [blame] | 166 | struct h1m { |
Willy Tarreau | 801250e | 2018-09-11 11:45:04 +0200 | [diff] [blame] | 167 | enum h1m_state state; // H1 message state (H1_MSG_*) |
Willy Tarreau | 11da567 | 2018-09-11 19:23:04 +0200 | [diff] [blame] | 168 | /* 24 bits available here */ |
Willy Tarreau | 4093a4d | 2017-09-21 11:46:43 +0200 | [diff] [blame] | 169 | uint32_t flags; // H1 message flags (H1_MF_*) |
| 170 | uint64_t curr_len; // content-length or last chunk length |
| 171 | uint64_t body_len; // total known size of the body length |
Willy Tarreau | b3b0152 | 2018-09-11 11:51:31 +0200 | [diff] [blame] | 172 | uint32_t next; // next byte to parse, relative to buffer's head |
Willy Tarreau | 4093a4d | 2017-09-21 11:46:43 +0200 | [diff] [blame] | 173 | int err_pos; // position in the byte stream of the first error (H1 or H2) |
| 174 | int err_state; // state where the first error was met (H1 or H2) |
| 175 | }; |
| 176 | |
Willy Tarreau | a41393f | 2018-09-11 15:34:50 +0200 | [diff] [blame] | 177 | /* basic H1 start line, describes either the request and the response */ |
| 178 | union h1_sl { /* useful start line pointers, relative to ->sol */ |
| 179 | struct { |
| 180 | int m, m_l; /* METHOD, length */ |
| 181 | int u, u_l; /* URI, length */ |
| 182 | int v, v_l; /* VERSION, length */ |
| 183 | enum http_meth_t meth; /* method */ |
| 184 | } rq; /* request line : field, length */ |
| 185 | struct { |
| 186 | int v, v_l; /* VERSION, length */ |
| 187 | int c, c_l; /* CODE, length */ |
| 188 | int r, r_l; /* REASON, length */ |
| 189 | uint16_t status; /* status code */ |
| 190 | } st; /* status line : field, length */ |
| 191 | }; |
| 192 | |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 193 | #endif /* _TYPES_H1_H */ |