blob: 840c8890012c8f624731844e2fabf8e766f58e28 [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 Tarreaua41393f2018-09-11 15:34:50 +020025#include <common/http.h>
Christopher Faulet1dc2b492018-10-08 15:34:02 +020026#include <common/ist.h>
Willy Tarreaua41393f2018-09-11 15:34:50 +020027
Willy Tarreau801250e2018-09-11 11:45:04 +020028/* Possible states while parsing HTTP/1 messages (request|response) */
29enum h1m_state {
30 H1_MSG_RQBEFORE = 0, // request: leading LF, before start line
31 H1_MSG_RQBEFORE_CR = 1, // request: leading CRLF, before start line
32 /* these ones define a request start line */
33 H1_MSG_RQMETH = 2, // parsing the Method
34 H1_MSG_RQMETH_SP = 3, // space(s) after the Method
35 H1_MSG_RQURI = 4, // parsing the Request URI
36 H1_MSG_RQURI_SP = 5, // space(s) after the Request URI
37 H1_MSG_RQVER = 6, // parsing the Request Version
38 H1_MSG_RQLINE_END = 7, // end of request line (CR or LF)
39
40 H1_MSG_RPBEFORE = 8, // response: leading LF, before start line
41 H1_MSG_RPBEFORE_CR = 9, // response: leading CRLF, before start line
42
43 /* these ones define a response start line */
44 H1_MSG_RPVER = 10, // parsing the Response Version
45 H1_MSG_RPVER_SP = 11, // space(s) after the Response Version
46 H1_MSG_RPCODE = 12, // response code
47 H1_MSG_RPCODE_SP = 13, // space(s) after the response code
48 H1_MSG_RPREASON = 14, // response reason
49 H1_MSG_RPLINE_END = 15, // end of response line (CR or LF)
50
51 /* common header processing */
52 H1_MSG_HDR_FIRST = 16, // waiting for first header or last CRLF (no LWS possible)
53 H1_MSG_HDR_NAME = 17, // parsing header name
54 H1_MSG_HDR_COL = 18, // parsing header colon
55 H1_MSG_HDR_L1_SP = 19, // parsing header LWS (SP|HT) before value
56 H1_MSG_HDR_L1_LF = 20, // parsing header LWS (LF) before value
57 H1_MSG_HDR_L1_LWS = 21, // checking whether it's a new header or an LWS
58 H1_MSG_HDR_VAL = 22, // parsing header value
59 H1_MSG_HDR_L2_LF = 23, // parsing header LWS (LF) inside/after value
60 H1_MSG_HDR_L2_LWS = 24, // checking whether it's a new header or an LWS
61
Willy Tarreau001823c2018-09-12 17:25:32 +020062 H1_MSG_LAST_LF = 25, // parsing last LF, last state for headers
Willy Tarreau801250e2018-09-11 11:45:04 +020063
Willy Tarreau001823c2018-09-12 17:25:32 +020064 /* Body processing. */
65
66 H1_MSG_CHUNK_SIZE = 26, // parsing the chunk size (RFC7230 #4.1)
67 H1_MSG_DATA = 27, // skipping data chunk / content-length data
68 H1_MSG_CHUNK_CRLF = 28, // skipping CRLF after data chunk
69 H1_MSG_TRAILERS = 29, // trailers (post-data entity headers)
Willy Tarreau801250e2018-09-11 11:45:04 +020070 /* we enter this state when we've received the end of the current message */
Willy Tarreau001823c2018-09-12 17:25:32 +020071 H1_MSG_DONE = 30, // message end received, waiting for resync or close
72 H1_MSG_TUNNEL = 31, // tunneled data after DONE
Willy Tarreau801250e2018-09-11 11:45:04 +020073} __attribute__((packed));
74
75
Willy Tarreau4093a4d2017-09-21 11:46:43 +020076/* HTTP/1 message flags (32 bit), for use in h1m->flags only */
77#define H1_MF_NONE 0x00000000
78#define H1_MF_CLEN 0x00000001 // content-length present
79#define H1_MF_CHNK 0x00000002 // chunk present, exclusive with c-l
Willy Tarreauccaf2332018-09-11 16:47:23 +020080#define H1_MF_RESP 0x00000004 // this message is the response message
Willy Tarreaueb528db2018-09-12 09:54:00 +020081#define H1_MF_TOLOWER 0x00000008 // turn the header names to lower case
Willy Tarreauba5fbca2018-09-13 11:32:51 +020082#define H1_MF_VER_11 0x00000010 // message indicates version 1.1 or above
Willy Tarreau98f5cf72018-09-13 14:15:58 +020083#define H1_MF_CONN_CLO 0x00000020 // message contains "connection: close"
84#define H1_MF_CONN_KAL 0x00000040 // message contains "connection: keep-alive"
85#define H1_MF_CONN_UPG 0x00000080 // message contains "connection: upgrade"
Christopher Fauletc4e53f42018-09-14 11:15:52 +020086#define H1_MF_XFER_LEN 0x00000100 // message xfer size can be determined
Willy Tarreau2557f6a2018-09-14 16:34:47 +020087#define H1_MF_XFER_ENC 0x00000200 // transfer-encoding is present
Christopher Faulet25da9e32018-10-08 15:50:15 +020088#define H1_MF_NO_PHDR 0x00000400 // don't add pseudo-headers in the header list
Willy Tarreau98f5cf72018-09-13 14:15:58 +020089
90/* Note: for a connection to be persistent, we need this for the request :
91 * - one of CLEN or CHNK
92 * - version 1.0 and KAL and not CLO
93 * - or version 1.1 and not CLO
94 * For the response it's the same except that UPG must not appear either.
95 * So in short, for a request it's (CLEN|CHNK) > 0 && !CLO && (VER_11 || KAL)
96 * and for a response it's (CLEN|CHNK) > 0 && !(CLO|UPG) && (VER_11 || KAL)
97 */
Willy Tarreau4093a4d2017-09-21 11:46:43 +020098
99
Willy Tarreaubbf38232018-09-12 09:08:54 +0200100/* basic HTTP/1 message state for use in parsers. The err_pos field is special,
101 * it is pre-set to a negative value (-1 or -2), and once non-negative it contains
102 * the relative position in the message of the first parse error. -2 is used to tell
103 * the parser that we want to block the invalid message. -1 is used to only perform
104 * a silent capture.
105 */
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200106struct h1m {
Willy Tarreau801250e2018-09-11 11:45:04 +0200107 enum h1m_state state; // H1 message state (H1_MSG_*)
Willy Tarreau11da5672018-09-11 19:23:04 +0200108 /* 24 bits available here */
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200109 uint32_t flags; // H1 message flags (H1_MF_*)
110 uint64_t curr_len; // content-length or last chunk length
111 uint64_t body_len; // total known size of the body length
Willy Tarreaub3b01522018-09-11 11:51:31 +0200112 uint32_t next; // next byte to parse, relative to buffer's head
Willy Tarreau4093a4d2017-09-21 11:46:43 +0200113 int err_pos; // position in the byte stream of the first error (H1 or H2)
114 int err_state; // state where the first error was met (H1 or H2)
115};
116
Willy Tarreaua41393f2018-09-11 15:34:50 +0200117/* basic H1 start line, describes either the request and the response */
118union h1_sl { /* useful start line pointers, relative to ->sol */
119 struct {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200120 struct ist m; /* METHOD */
121 struct ist u; /* URI */
122 struct ist v; /* VERSION */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200123 enum http_meth_t meth; /* method */
124 } rq; /* request line : field, length */
125 struct {
Christopher Faulet1dc2b492018-10-08 15:34:02 +0200126 struct ist v; /* VERSION */
127 struct ist c; /* CODE */
128 struct ist r; /* REASON */
Willy Tarreaua41393f2018-09-11 15:34:50 +0200129 uint16_t status; /* status code */
130 } st; /* status line : field, length */
131};
132
Willy Tarreau0da5b3b2017-09-21 09:30:46 +0200133#endif /* _TYPES_H1_H */