blob: 7d9c12e901e0d94f8b4f688eac71dd8a633077ca [file] [log] [blame]
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02001/*
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 Tarreau801250e2018-09-11 11:45:04 +020025/* Legacy version of the HTTP/1 message state, used by the channels, should
26 * ultimately be removed.
27 */
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020028enum h1_state {
29 HTTP_MSG_RQBEFORE = 0, // request: leading LF, before start line
30 HTTP_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line
31 /* these ones define a request start line */
32 HTTP_MSG_RQMETH = 2, // parsing the Method
33 HTTP_MSG_RQMETH_SP = 3, // space(s) after the Method
34 HTTP_MSG_RQURI = 4, // parsing the Request URI
35 HTTP_MSG_RQURI_SP = 5, // space(s) after the Request URI
36 HTTP_MSG_RQVER = 6, // parsing the Request Version
37 HTTP_MSG_RQLINE_END = 7, // end of request line (CR or LF)
38
39 HTTP_MSG_RPBEFORE = 8, // response: leading LF, before start line
40 HTTP_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line
41
42 /* these ones define a response start line */
43 HTTP_MSG_RPVER = 10, // parsing the Response Version
44 HTTP_MSG_RPVER_SP = 11, // space(s) after the Response Version
45 HTTP_MSG_RPCODE = 12, // response code
46 HTTP_MSG_RPCODE_SP = 13, // space(s) after the response code
47 HTTP_MSG_RPREASON = 14, // response reason
48 HTTP_MSG_RPLINE_END = 15, // end of response line (CR or LF)
49
50 /* common header processing */
51 HTTP_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible)
52 HTTP_MSG_HDR_NAME = 17, // parsing header name
53 HTTP_MSG_HDR_COL = 18, // parsing header colon
54 HTTP_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value
55 HTTP_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value
56 HTTP_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS
57 HTTP_MSG_HDR_VAL = 22, // parsing header value
58 HTTP_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value
59 HTTP_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS
60
61 HTTP_MSG_LAST_LF = 25, // parsing last LF
62
63 /* error state : must be before HTTP_MSG_BODY so that (>=BODY) always indicates
64 * that data are being processed.
65 */
66 HTTP_MSG_ERROR = 26, // an error occurred
67 /* Body processing.
68 * The state HTTP_MSG_BODY is a delimiter to know if we're waiting for headers
69 * or body. All the sub-states below also indicate we're processing the body,
70 * with some additional information.
71 */
72 HTTP_MSG_BODY = 27, // parsing body at end of headers
73 HTTP_MSG_100_SENT = 28, // parsing body after a 100-Continue was sent
74 HTTP_MSG_CHUNK_SIZE = 29, // parsing the chunk size (RFC7230 #4.1)
75 HTTP_MSG_DATA = 30, // skipping data chunk / content-length data
76 HTTP_MSG_CHUNK_CRLF = 31, // skipping CRLF after data chunk
77 HTTP_MSG_TRAILERS = 32, // trailers (post-data entity headers)
78 /* we enter this state when we've received the end of the current message */
79 HTTP_MSG_ENDING = 33, // message end received, wait that the filters end too
80 HTTP_MSG_DONE = 34, // message end received, waiting for resync or close
81 HTTP_MSG_CLOSING = 35, // shutdown_w done, not all bytes sent yet
82 HTTP_MSG_CLOSED = 36, // shutdown_w done, all bytes sent
83 HTTP_MSG_TUNNEL = 37, // tunneled data after DONE
84} __attribute__((packed));
85
86
Willy Tarreau801250e2018-09-11 11:45:04 +020087/* Possible states while parsing HTTP/1 messages (request|response) */
88enum h1m_state {
89 H1_MSG_RQBEFORE = 0, // request: leading LF, before start line
90 H1_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line
91 /* these ones define a request start line */
92 H1_MSG_RQMETH = 2, // parsing the Method
93 H1_MSG_RQMETH_SP = 3, // space(s) after the Method
94 H1_MSG_RQURI = 4, // parsing the Request URI
95 H1_MSG_RQURI_SP = 5, // space(s) after the Request URI
96 H1_MSG_RQVER = 6, // parsing the Request Version
97 H1_MSG_RQLINE_END = 7, // end of request line (CR or LF)
98
99 H1_MSG_RPBEFORE = 8, // response: leading LF, before start line
100 H1_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line
101
102 /* these ones define a response start line */
103 H1_MSG_RPVER = 10, // parsing the Response Version
104 H1_MSG_RPVER_SP = 11, // space(s) after the Response Version
105 H1_MSG_RPCODE = 12, // response code
106 H1_MSG_RPCODE_SP = 13, // space(s) after the response code
107 H1_MSG_RPREASON = 14, // response reason
108 H1_MSG_RPLINE_END = 15, // end of response line (CR or LF)
109
110 /* common header processing */
111 H1_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible)
112 H1_MSG_HDR_NAME = 17, // parsing header name
113 H1_MSG_HDR_COL = 18, // parsing header colon
114 H1_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value
115 H1_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value
116 H1_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS
117 H1_MSG_HDR_VAL = 22, // parsing header value
118 H1_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value
119 H1_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS
120
121 H1_MSG_LAST_LF = 25, // parsing last LF
122
123 /* error state : must be before H1_MSG_BODY so that (>=BODY) always indicates
124 * that data are being processed.
125 */
126 H1_MSG_ERROR = 26, // an error occurred
127 /* Body processing.
128 * The state H1_MSG_BODY is a delimiter to know if we're waiting for headers
129 * or body. All the sub-states below also indicate we're processing the body,
130 * with some additional information.
131 */
132 H1_MSG_BODY = 27, // parsing body at end of headers
133 H1_MSG_100_SENT = 28, // parsing body after a 100-Continue was sent
134 H1_MSG_CHUNK_SIZE = 29, // parsing the chunk size (RFC7230 #4.1)
135 H1_MSG_DATA = 30, // skipping data chunk / content-length data
136 H1_MSG_CHUNK_CRLF = 31, // skipping CRLF after data chunk
137 H1_MSG_TRAILERS = 32, // trailers (post-data entity headers)
138 /* we enter this state when we've received the end of the current message */
139 H1_MSG_ENDING = 33, // message end received, wait that the filters end too
140 H1_MSG_DONE = 34, // message end received, waiting for resync or close
141 H1_MSG_CLOSING = 35, // shutdown_w done, not all bytes sent yet
142 H1_MSG_CLOSED = 36, // shutdown_w done, all bytes sent
143 H1_MSG_TUNNEL = 37, // tunneled data after DONE
144} __attribute__((packed));
145
146
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200147/* HTTP/1 message flags (32 bit), for use in h1m->flags only */
148#define H1_MF_NONE 0x00000000
149#define H1_MF_CLEN 0x00000001 // content-length present
150#define H1_MF_CHNK 0x00000002 // chunk present, exclusive with c-l
151
152
153/* basic HTTP/1 message state for use in parsers */
154struct h1m {
Willy Tarreau801250e2018-09-11 11:45:04 +0200155 enum h1m_state state; // H1 message state (H1_MSG_*)
Willy Tarreaud22e83a2017-10-31 08:02:24 +0100156 /* 8 bits available here */
157 uint16_t status; // HTTP status code
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200158 uint32_t flags; // H1 message flags (H1_MF_*)
159 uint64_t curr_len; // content-length or last chunk length
160 uint64_t body_len; // total known size of the body length
Willy Tarreaub3b01522018-09-11 11:51:31 +0200161 uint32_t next; // next byte to parse, relative to buffer's head
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200162 int err_pos; // position in the byte stream of the first error (H1 or H2)
163 int err_state; // state where the first error was met (H1 or H2)
164};
165
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200166#endif /* _TYPES_H1_H */