blob: d75a3f465379147e0a546e0ecbcf02cf07620ec9 [file] [log] [blame]
Willy Tarreauffca7362016-12-13 18:25:15 +01001/*
2 * include/common/h2.h
3 * This file contains types and macros used for the HTTP/2 protocol
4 *
5 * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu
6 * Copyright (C) 2017 HAProxy Technologies
7 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +01008 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
Willy Tarreauffca7362016-12-13 18:25:15 +010015 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010016 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
Willy Tarreauffca7362016-12-13 18:25:15 +010018 *
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
Willy Tarreauffca7362016-12-13 18:25:15 +010027 */
28
29#ifndef _COMMON_H2_H
30#define _COMMON_H2_H
31
32#include <common/config.h>
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010033#include <common/http-hdr.h>
34#include <common/ist.h>
Willy Tarreauffca7362016-12-13 18:25:15 +010035
36
Willy Tarreauf24ea8e2017-11-21 19:55:27 +010037/* indexes of most important pseudo headers can be simplified to an almost
38 * linear array by dividing the index by 2 for all values from 1 to 9, and
39 * caping to 4 for values up to 14 ; thus it fits in a single 24-bit array
40 * shifted by 3 times the index value/2, or a 32-bit array shifted by 4x.
41 * Don't change these values, they are assumed by hpack_idx_to_phdr(). There
42 * is an entry for the Host header field which is not a pseudo-header but
43 * needs to be tracked as we should only use :authority if it's absent.
44 */
45enum {
46 H2_PHDR_IDX_NONE = 0,
47 H2_PHDR_IDX_AUTH = 1, /* :authority = 1 */
48 H2_PHDR_IDX_METH = 2, /* :method = 2..3 */
49 H2_PHDR_IDX_PATH = 3, /* :path = 4..5 */
50 H2_PHDR_IDX_SCHM = 4, /* :scheme = 6..7 */
51 H2_PHDR_IDX_STAT = 5, /* :status = 8..14 */
52 H2_PHDR_IDX_HOST = 6, /* Host, never returned, just a place-holder */
53 H2_PHDR_NUM_ENTRIES /* must be last */
54};
55
56/* bit fields indicating the pseudo-headers found. It also covers the HOST
57 * header field as well as any non-pseudo-header field (NONE).
58 */
59enum {
60 H2_PHDR_FND_NONE = 1 << H2_PHDR_IDX_NONE, /* found a regular header */
61 H2_PHDR_FND_AUTH = 1 << H2_PHDR_IDX_AUTH,
62 H2_PHDR_FND_METH = 1 << H2_PHDR_IDX_METH,
63 H2_PHDR_FND_PATH = 1 << H2_PHDR_IDX_PATH,
64 H2_PHDR_FND_SCHM = 1 << H2_PHDR_IDX_SCHM,
65 H2_PHDR_FND_STAT = 1 << H2_PHDR_IDX_STAT,
66 H2_PHDR_FND_HOST = 1 << H2_PHDR_IDX_HOST,
67};
68
Willy Tarreauffca7362016-12-13 18:25:15 +010069/* frame types, from the standard */
70enum h2_ft {
71 H2_FT_DATA = 0x00, // RFC7540 #6.1
72 H2_FT_HEADERS = 0x01, // RFC7540 #6.2
73 H2_FT_PRIORITY = 0x02, // RFC7540 #6.3
74 H2_FT_RST_STREAM = 0x03, // RFC7540 #6.4
75 H2_FT_SETTINGS = 0x04, // RFC7540 #6.5
76 H2_FT_PUSH_PROMISE = 0x05, // RFC7540 #6.6
77 H2_FT_PING = 0x06, // RFC7540 #6.7
78 H2_FT_GOAWAY = 0x07, // RFC7540 #6.8
79 H2_FT_WINDOW_UPDATE = 0x08, // RFC7540 #6.9
80 H2_FT_CONTINUATION = 0x09, // RFC7540 #6.10
81 H2_FT_ENTRIES /* must be last */
82} __attribute__((packed));
83
84/* flags defined for each frame type */
85
86// RFC7540 #6.1
87#define H2_F_DATA_END_STREAM 0x01
88#define H2_F_DATA_PADDED 0x08
89
90// RFC7540 #6.2
91#define H2_F_HEADERS_END_STREAM 0x01
92#define H2_F_HEADERS_END_HEADERS 0x04
93#define H2_F_HEADERS_PADDED 0x08
94#define H2_F_HEADERS_PRIORITY 0x20
95
96// RFC7540 #6.3 : PRIORITY defines no flags
97// RFC7540 #6.4 : RST_STREAM defines no flags
98
99// RFC7540 #6.5
100#define H2_F_SETTINGS_ACK 0x01
101
102// RFC7540 #6.6
103#define H2_F_PUSH_PROMISE_END_HEADERS 0x04
104#define H2_F_PUSH_PROMISE_PADDED 0x08
105
106// RFC7540 #6.7
107#define H2_F_PING_ACK 0x01
108
109// RFC7540 #6.8 : GOAWAY defines no flags
110// RFC7540 #6.9 : WINDOW_UPDATE defines no flags
111
112/* HTTP/2 error codes - RFC7540 #7 */
113enum h2_err {
114 H2_ERR_NO_ERROR = 0x0,
115 H2_ERR_PROTOCOL_ERROR = 0x1,
116 H2_ERR_INTERNAL_ERROR = 0x2,
117 H2_ERR_FLOW_CONTROL_ERROR = 0x3,
118 H2_ERR_SETTINGS_TIMEOUT = 0x4,
119 H2_ERR_STREAM_CLOSED = 0x5,
120 H2_ERR_FRAME_SIZE_ERROR = 0x6,
121 H2_ERR_REFUSED_STREAM = 0x7,
122 H2_ERR_CANCEL = 0x8,
123 H2_ERR_COMPRESSION_ERROR = 0x9,
124 H2_ERR_CONNECT_ERROR = 0xa,
125 H2_ERR_ENHANCE_YOUR_CALM = 0xb,
126 H2_ERR_INADEQUATE_SECURITY = 0xc,
127 H2_ERR_HTTP_1_1_REQUIRED = 0xd,
128} __attribute__((packed));
129
130// RFC7540 #11.3 : Settings Registry
131#define H2_SETTINGS_HEADER_TABLE_SIZE 0x0001
132#define H2_SETTINGS_ENABLE_PUSH 0x0002
133#define H2_SETTINGS_MAX_CONCURRENT_STREAMS 0x0003
134#define H2_SETTINGS_INITIAL_WINDOW_SIZE 0x0004
135#define H2_SETTINGS_MAX_FRAME_SIZE 0x0005
136#define H2_SETTINGS_MAX_HEADER_LIST_SIZE 0x0006
137
138
139/* some protocol constants */
140
141// PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n
142#define H2_CONN_PREFACE \
143 "\x50\x52\x49\x20\x2a\x20\x48\x54" \
144 "\x54\x50\x2f\x32\x2e\x30\x0d\x0a" \
145 "\x0d\x0a\x53\x4d\x0d\x0a\x0d\x0a"
146
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100147
Willy Tarreau174b06a2018-04-25 18:13:58 +0200148/* some flags related to protocol parsing */
149#define H2_MSGF_BODY 0x0001 // a body is present
150#define H2_MSGF_BODY_CL 0x0002 // content-length is present
151#define H2_MSGF_BODY_TUNNEL 0x0004 // a tunnel is in use (CONNECT)
152
153
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100154/* various protocol processing functions */
155
Willy Tarreau174b06a2018-04-25 18:13:58 +0200156int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf);
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100157
Willy Tarreauffca7362016-12-13 18:25:15 +0100158/*
159 * Some helpful debugging functions.
160 */
161
162/* returns the frame type as a string */
163static inline const char *h2_ft_str(int type)
164{
165 switch (type) {
166 case H2_FT_DATA : return "DATA";
167 case H2_FT_HEADERS : return "HEADERS";
168 case H2_FT_PRIORITY : return "PRIORITY";
169 case H2_FT_RST_STREAM : return "RST_STREAM";
170 case H2_FT_SETTINGS : return "SETTINGS";
171 case H2_FT_PUSH_PROMISE : return "PUSH_PROMISE";
172 case H2_FT_PING : return "PING";
173 case H2_FT_GOAWAY : return "GOAWAY";
174 case H2_FT_WINDOW_UPDATE : return "WINDOW_UPDATE";
175 default : return "_UNKNOWN_";
176 }
177}
178
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100179/* returns the pseudo-header <str> corresponds to among H2_PHDR_IDX_*, 0 if not a
180 * pseudo-header, or -1 if not a valid pseudo-header.
181 */
182static inline int h2_str_to_phdr(const struct ist str)
183{
184 if (*str.ptr == ':') {
185 if (isteq(str, ist(":path"))) return H2_PHDR_IDX_PATH;
186 else if (isteq(str, ist(":method"))) return H2_PHDR_IDX_METH;
187 else if (isteq(str, ist(":scheme"))) return H2_PHDR_IDX_SCHM;
188 else if (isteq(str, ist(":status"))) return H2_PHDR_IDX_STAT;
189 else if (isteq(str, ist(":authority"))) return H2_PHDR_IDX_AUTH;
190
191 /* all other names starting with ':' */
192 return -1;
193 }
194
195 /* not a pseudo header */
196 return 0;
197}
198
Willy Tarreau30832762017-12-30 14:39:09 +0100199/* returns the pseudo-header name <num> as a string, or ":UNKNOWN" if unknown */
200static inline const char *h2_phdr_to_str(int phdr)
201{
202 switch (phdr) {
203 case H2_PHDR_IDX_NONE: return ":NONE";
204 case H2_PHDR_IDX_AUTH: return ":authority";
205 case H2_PHDR_IDX_METH: return ":method";
206 case H2_PHDR_IDX_PATH: return ":path";
207 case H2_PHDR_IDX_SCHM: return ":scheme";
208 case H2_PHDR_IDX_STAT: return ":status";
209 case H2_PHDR_IDX_HOST: return "Host";
210 default: return ":UNKNOWN";
211 }
212}
Willy Tarreauf24ea8e2017-11-21 19:55:27 +0100213
Willy Tarreauffca7362016-12-13 18:25:15 +0100214#endif /* _COMMON_H2_H */
215
216/*
217 * Local variables:
218 * c-indent-level: 8
219 * c-basic-offset: 8
220 * End:
221 */