blob: b77eb32ca4febc2cff90d9f163ea10f5a9ffb6b0 [file] [log] [blame]
Willy Tarreauafba57a2018-12-11 13:44:24 +01001/*
2 * include/common/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 _COMMON_H1_H
23#define _COMMON_H1_H
24
25#include <common/buffer.h>
26#include <common/compiler.h>
27#include <common/config.h>
28#include <common/http.h>
29#include <common/http-hdr.h>
30#include <common/ist.h>
31#include <common/standard.h>
32
33
34/* Possible states while parsing HTTP/1 messages (request|response) */
35enum h1m_state {
36 H1_MSG_RQBEFORE = 0, // request: leading LF, before start line
37 H1_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line
38 /* these ones define a request start line */
39 H1_MSG_RQMETH = 2, // parsing the Method
40 H1_MSG_RQMETH_SP = 3, // space(s) after the Method
41 H1_MSG_RQURI = 4, // parsing the Request URI
42 H1_MSG_RQURI_SP = 5, // space(s) after the Request URI
43 H1_MSG_RQVER = 6, // parsing the Request Version
44 H1_MSG_RQLINE_END = 7, // end of request line (CR or LF)
45
46 H1_MSG_RPBEFORE = 8, // response: leading LF, before start line
47 H1_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line
48
49 /* these ones define a response start line */
50 H1_MSG_RPVER = 10, // parsing the Response Version
51 H1_MSG_RPVER_SP = 11, // space(s) after the Response Version
52 H1_MSG_RPCODE = 12, // response code
53 H1_MSG_RPCODE_SP = 13, // space(s) after the response code
54 H1_MSG_RPREASON = 14, // response reason
55 H1_MSG_RPLINE_END = 15, // end of response line (CR or LF)
56
57 /* common header processing */
58 H1_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible)
59 H1_MSG_HDR_NAME = 17, // parsing header name
60 H1_MSG_HDR_COL = 18, // parsing header colon
61 H1_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value
62 H1_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value
63 H1_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS
64 H1_MSG_HDR_VAL = 22, // parsing header value
65 H1_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value
66 H1_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS
67
68 H1_MSG_LAST_LF = 25, // parsing last LF, last state for headers
69
70 /* Body processing. */
71
72 H1_MSG_CHUNK_SIZE = 26, // parsing the chunk size (RFC7230 #4.1)
73 H1_MSG_DATA = 27, // skipping data chunk / content-length data
74 H1_MSG_CHUNK_CRLF = 28, // skipping CRLF after data chunk
75 H1_MSG_TRAILERS = 29, // trailers (post-data entity headers)
76 /* we enter this state when we've received the end of the current message */
77 H1_MSG_DONE = 30, // message end received, waiting for resync or close
78 H1_MSG_TUNNEL = 31, // tunneled data after DONE
79} __attribute__((packed));
80
81
82/* HTTP/1 message flags (32 bit), for use in h1m->flags only */
83#define H1_MF_NONE 0x00000000
84#define H1_MF_CLEN 0x00000001 // content-length present
85#define H1_MF_CHNK 0x00000002 // chunk present, exclusive with c-l
86#define H1_MF_RESP 0x00000004 // this message is the response message
87#define H1_MF_TOLOWER 0x00000008 // turn the header names to lower case
88#define H1_MF_VER_11 0x00000010 // message indicates version 1.1 or above
89#define H1_MF_CONN_CLO 0x00000020 // message contains "connection: close"
90#define H1_MF_CONN_KAL 0x00000040 // message contains "connection: keep-alive"
91#define H1_MF_CONN_UPG 0x00000080 // message contains "connection: upgrade"
92#define H1_MF_XFER_LEN 0x00000100 // message xfer size can be determined
93#define H1_MF_XFER_ENC 0x00000200 // transfer-encoding is present
94#define H1_MF_NO_PHDR 0x00000400 // don't add pseudo-headers in the header list
95
96/* Note: for a connection to be persistent, we need this for the request :
97 * - one of CLEN or CHNK
98 * - version 1.0 and KAL and not CLO
99 * - or version 1.1 and not CLO
100 * For the response it's the same except that UPG must not appear either.
101 * So in short, for a request it's (CLEN|CHNK) > 0 && !CLO && (VER_11 || KAL)
102 * and for a response it's (CLEN|CHNK) > 0 && !(CLO|UPG) && (VER_11 || KAL)
103 */
104
105
106/* basic HTTP/1 message state for use in parsers. The err_pos field is special,
107 * it is pre-set to a negative value (-1 or -2), and once non-negative it contains
108 * the relative position in the message of the first parse error. -2 is used to tell
109 * the parser that we want to block the invalid message. -1 is used to only perform
110 * a silent capture.
111 */
112struct h1m {
113 enum h1m_state state; // H1 message state (H1_MSG_*)
114 /* 24 bits available here */
115 uint32_t flags; // H1 message flags (H1_MF_*)
116 uint64_t curr_len; // content-length or last chunk length
117 uint64_t body_len; // total known size of the body length
118 uint32_t next; // next byte to parse, relative to buffer's head
119 int err_pos; // position in the byte stream of the first error (H1 or H2)
120 int err_state; // state where the first error was met (H1 or H2)
121};
122
123/* basic H1 start line, describes either the request and the response */
124union h1_sl { /* useful start line pointers, relative to ->sol */
125 struct {
126 struct ist m; /* METHOD */
127 struct ist u; /* URI */
128 struct ist v; /* VERSION */
129 enum http_meth_t meth; /* method */
130 } rq; /* request line : field, length */
131 struct {
132 struct ist v; /* VERSION */
133 struct ist c; /* CODE */
134 struct ist r; /* REASON */
135 uint16_t status; /* status code */
136 } st; /* status line : field, length */
137};
138
139int h1_headers_to_hdr_list(char *start, const char *stop,
140 struct http_hdr *hdr, unsigned int hdr_num,
141 struct h1m *h1m, union h1_sl *slp);
142int h1_measure_trailers(const struct buffer *buf, unsigned int ofs, unsigned int max);
143
144int h1_parse_cont_len_header(struct h1m *h1m, struct ist *value);
145void h1_parse_xfer_enc_header(struct h1m *h1m, struct ist value);
146void h1_parse_connection_header(struct h1m *h1m, struct ist value);
147
148/* for debugging, reports the HTTP/1 message state name */
149static inline const char *h1m_state_str(enum h1m_state msg_state)
150{
151 switch (msg_state) {
152 case H1_MSG_RQBEFORE: return "MSG_RQBEFORE";
153 case H1_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR";
154 case H1_MSG_RQMETH: return "MSG_RQMETH";
155 case H1_MSG_RQMETH_SP: return "MSG_RQMETH_SP";
156 case H1_MSG_RQURI: return "MSG_RQURI";
157 case H1_MSG_RQURI_SP: return "MSG_RQURI_SP";
158 case H1_MSG_RQVER: return "MSG_RQVER";
159 case H1_MSG_RQLINE_END: return "MSG_RQLINE_END";
160 case H1_MSG_RPBEFORE: return "MSG_RPBEFORE";
161 case H1_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR";
162 case H1_MSG_RPVER: return "MSG_RPVER";
163 case H1_MSG_RPVER_SP: return "MSG_RPVER_SP";
164 case H1_MSG_RPCODE: return "MSG_RPCODE";
165 case H1_MSG_RPCODE_SP: return "MSG_RPCODE_SP";
166 case H1_MSG_RPREASON: return "MSG_RPREASON";
167 case H1_MSG_RPLINE_END: return "MSG_RPLINE_END";
168 case H1_MSG_HDR_FIRST: return "MSG_HDR_FIRST";
169 case H1_MSG_HDR_NAME: return "MSG_HDR_NAME";
170 case H1_MSG_HDR_COL: return "MSG_HDR_COL";
171 case H1_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP";
172 case H1_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF";
173 case H1_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS";
174 case H1_MSG_HDR_VAL: return "MSG_HDR_VAL";
175 case H1_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF";
176 case H1_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS";
177 case H1_MSG_LAST_LF: return "MSG_LAST_LF";
178 case H1_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE";
179 case H1_MSG_DATA: return "MSG_DATA";
180 case H1_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF";
181 case H1_MSG_TRAILERS: return "MSG_TRAILERS";
182 case H1_MSG_DONE: return "MSG_DONE";
183 case H1_MSG_TUNNEL: return "MSG_TUNNEL";
184 default: return "MSG_??????";
185 }
186}
187
188/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
189 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
190 * in order to include this part into the next forwarding phase. Note that the
191 * caller must ensure that head+start points to the first byte to parse. It
192 * returns the number of bytes parsed on success, so the caller can set msg_state
193 * to HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
194 * change anything and returns zero. Otherwise it returns a negative value
195 * indicating the error positionn relative to <stop>. Note: this function is
196 * designed to parse wrapped CRLF at the end of the buffer.
197 */
198static inline int h1_skip_chunk_crlf(const struct buffer *buf, int start, int stop)
199{
200 const char *ptr = b_peek(buf, start);
201 int bytes = 1;
202
203 /* NB: we'll check data availability at the end. It's not a
204 * problem because whatever we match first will be checked
205 * against the correct length.
206 */
207 if (*ptr == '\r') {
208 bytes++;
209 ptr++;
210 if (ptr >= b_wrap(buf))
211 ptr = b_orig(buf);
212 }
213
214 if (bytes > stop - start)
215 return 0;
216
217 if (*ptr != '\n') // negative position to stop
218 return ptr - __b_peek(buf, stop);
219
220 return bytes;
221}
222
223/* Parse the chunk size start at buf + start and stops before buf + stop. The
224 * positions are relative to the buffer's head.
225 * It returns the chunk size in <res> and the amount of bytes read this way :
226 * < 0 : error at this position relative to <stop>
227 * = 0 : not enough bytes to read a complete chunk size
228 * > 0 : number of bytes successfully read that the caller can skip
229 * On success, the caller should adjust its msg->next to point to the first
230 * byte of data after the chunk size, so that we know we can forward exactly
231 * msg->next bytes, and msg->sol to contain the exact number of bytes forming
232 * the chunk size. That way it is always possible to differentiate between the
233 * start of the body and the start of the data. Note: this function is designed
234 * to parse wrapped CRLF at the end of the buffer.
235 */
236static inline int h1_parse_chunk_size(const struct buffer *buf, int start, int stop, unsigned int *res)
237{
238 const char *ptr = b_peek(buf, start);
239 const char *ptr_old = ptr;
240 const char *end = b_wrap(buf);
241 unsigned int chunk = 0;
242
243 stop -= start; // bytes left
244 start = stop; // bytes to transfer
245
246 /* The chunk size is in the following form, though we are only
247 * interested in the size and CRLF :
248 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
249 */
250 while (1) {
251 int c;
252 if (!stop)
253 return 0;
254 c = hex2i(*ptr);
255 if (c < 0) /* not a hex digit anymore */
256 break;
257 if (unlikely(++ptr >= end))
258 ptr = b_orig(buf);
259 if (unlikely(chunk & 0xF8000000)) /* integer overflow will occur if result >= 2GB */
260 goto error;
261 chunk = (chunk << 4) + c;
262 stop--;
263 }
264
265 /* empty size not allowed */
266 if (unlikely(ptr == ptr_old))
267 goto error;
268
269 while (HTTP_IS_SPHT(*ptr)) {
270 if (++ptr >= end)
271 ptr = b_orig(buf);
272 if (--stop == 0)
273 return 0;
274 }
275
276 /* Up to there, we know that at least one byte is present at *ptr. Check
277 * for the end of chunk size.
278 */
279 while (1) {
280 if (likely(HTTP_IS_CRLF(*ptr))) {
281 /* we now have a CR or an LF at ptr */
282 if (likely(*ptr == '\r')) {
283 if (++ptr >= end)
284 ptr = b_orig(buf);
285 if (--stop == 0)
286 return 0;
287 }
288
289 if (*ptr != '\n')
290 goto error;
291 if (++ptr >= end)
292 ptr = b_orig(buf);
293 --stop;
294 /* done */
295 break;
296 }
297 else if (likely(*ptr == ';')) {
298 /* chunk extension, ends at next CRLF */
299 if (++ptr >= end)
300 ptr = b_orig(buf);
301 if (--stop == 0)
302 return 0;
303
304 while (!HTTP_IS_CRLF(*ptr)) {
305 if (++ptr >= end)
306 ptr = b_orig(buf);
307 if (--stop == 0)
308 return 0;
309 }
310 /* we have a CRLF now, loop above */
311 continue;
312 }
313 else
314 goto error;
315 }
316
317 /* OK we found our CRLF and now <ptr> points to the next byte, which may
318 * or may not be present. Let's return the number of bytes parsed.
319 */
320 *res = chunk;
321 return start - stop;
322 error:
323 *res = 0; // just to stop gcc's -Wuninitialized warning :-(
324 return -stop;
325}
326
327/* initializes an H1 message for a request */
328static inline struct h1m *h1m_init_req(struct h1m *h1m)
329{
330 h1m->state = H1_MSG_RQBEFORE;
331 h1m->next = 0;
332 h1m->flags = H1_MF_NONE;
333 h1m->curr_len = 0;
334 h1m->body_len = 0;
335 h1m->err_pos = -2;
336 h1m->err_state = 0;
337 return h1m;
338}
339
340/* initializes an H1 message for a response */
341static inline struct h1m *h1m_init_res(struct h1m *h1m)
342{
343 h1m->state = H1_MSG_RPBEFORE;
344 h1m->next = 0;
345 h1m->flags = H1_MF_RESP;
346 h1m->curr_len = 0;
347 h1m->body_len = 0;
348 h1m->err_pos = -2;
349 h1m->err_state = 0;
350 return h1m;
351}
352
353#endif /* _COMMON_H1_H */