1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef QUICHE_HTTP2_HTTP2_CONSTANTS_H_
6 #define QUICHE_HTTP2_HTTP2_CONSTANTS_H_
7
8 // Constants from the HTTP/2 spec, RFC 7540, and associated helper functions.
9
10 #include <cstdint>
11 #include <iosfwd>
12 #include <ostream>
13 #include <string>
14
15 #include "absl/container/flat_hash_set.h"
16 #include "absl/strings/string_view.h"
17 #include "quiche/common/platform/api/quiche_export.h"
18 #include "quiche/common/quiche_text_utils.h"
19
20 namespace http2 {
21
22 // TODO(jamessynge): create http2_simple_types for types similar to
23 // SpdyStreamId, but not for structures like Http2FrameHeader. Then will be
24 // able to move these stream id functions there.
UInt31Mask()25 constexpr uint32_t UInt31Mask() { return 0x7fffffff; }
StreamIdMask()26 constexpr uint32_t StreamIdMask() { return UInt31Mask(); }
27
28 // The value used to identify types of frames. Upper case to match the RFC.
29 // The comments indicate which flags are valid for that frame type.
30 enum class Http2FrameType : uint8_t {
31 DATA = 0, // END_STREAM | PADDED
32 HEADERS = 1, // END_STREAM | END_HEADERS | PADDED | PRIORITY
33 PRIORITY = 2, //
34 RST_STREAM = 3, //
35 SETTINGS = 4, // ACK
36 PUSH_PROMISE = 5, // END_HEADERS | PADDED
37 PING = 6, // ACK
38 GOAWAY = 7, //
39 WINDOW_UPDATE = 8, //
40 CONTINUATION = 9, // END_HEADERS
41 // https://tools.ietf.org/html/rfc7838
42 ALTSVC = 10, // no flags
43 // https://tools.ietf.org/html/draft-ietf-httpbis-priority-02
44 PRIORITY_UPDATE = 16, // no flags
45 };
46
47 // Is the frame type known/supported?
IsSupportedHttp2FrameType(uint32_t v)48 inline bool IsSupportedHttp2FrameType(uint32_t v) {
49 return v <= static_cast<uint32_t>(Http2FrameType::ALTSVC) ||
50 v == static_cast<uint32_t>(Http2FrameType::PRIORITY_UPDATE);
51 }
IsSupportedHttp2FrameType(Http2FrameType v)52 inline bool IsSupportedHttp2FrameType(Http2FrameType v) {
53 return IsSupportedHttp2FrameType(static_cast<uint32_t>(v));
54 }
55
56 // The return type is 'std::string' so that they can generate a unique string
57 // for each unsupported value. Since these are just used for debugging/error
58 // messages, that isn't a cost to we need to worry about. The same applies to
59 // the functions later in this file.
60 QUICHE_EXPORT std::string Http2FrameTypeToString(Http2FrameType v);
61 QUICHE_EXPORT std::string Http2FrameTypeToString(uint8_t v);
62 QUICHE_EXPORT inline std::ostream& operator<<(std::ostream& out,
63 Http2FrameType v) {
64 return out << Http2FrameTypeToString(v);
65 }
66
67 // Flags that appear in supported frame types. These are treated as bit masks.
68 // The comments indicate for which frame types the flag is valid.
69 enum Http2FrameFlag : uint8_t {
70 END_STREAM = 0x01, // DATA, HEADERS
71 ACK = 0x01, // SETTINGS, PING
72 END_HEADERS = 0x04, // HEADERS, PUSH_PROMISE, CONTINUATION
73 PADDED = 0x08, // DATA, HEADERS, PUSH_PROMISE
74 PRIORITY = 0x20, // HEADERS
75 };
76
77 // Formats zero or more flags for the specified type of frame. Returns an
78 // empty string if flags==0.
79 QUICHE_EXPORT std::string Http2FrameFlagsToString(Http2FrameType type,
80 uint8_t flags);
81 QUICHE_EXPORT std::string Http2FrameFlagsToString(uint8_t type, uint8_t flags);
82
83 // Error codes for GOAWAY and RST_STREAM frames.
84 enum class Http2ErrorCode : uint32_t {
85 // The associated condition is not a result of an error. For example, a GOAWAY
86 // might include this code to indicate graceful shutdown of a connection.
87 HTTP2_NO_ERROR = 0x0,
88
89 // The endpoint detected an unspecific protocol error. This error is for use
90 // when a more specific error code is not available.
91 PROTOCOL_ERROR = 0x1,
92
93 // The endpoint encountered an unexpected internal error.
94 INTERNAL_ERROR = 0x2,
95
96 // The endpoint detected that its peer violated the flow-control protocol.
97 FLOW_CONTROL_ERROR = 0x3,
98
99 // The endpoint sent a SETTINGS frame but did not receive a response in a
100 // timely manner. See Section 6.5.3 ("Settings Synchronization").
101 SETTINGS_TIMEOUT = 0x4,
102
103 // The endpoint received a frame after a stream was half-closed.
104 STREAM_CLOSED = 0x5,
105
106 // The endpoint received a frame with an invalid size.
107 FRAME_SIZE_ERROR = 0x6,
108
109 // The endpoint refused the stream prior to performing any application
110 // processing (see Section 8.1.4 for details).
111 REFUSED_STREAM = 0x7,
112
113 // Used by the endpoint to indicate that the stream is no longer needed.
114 CANCEL = 0x8,
115
116 // The endpoint is unable to maintain the header compression context
117 // for the connection.
118 COMPRESSION_ERROR = 0x9,
119
120 // The connection established in response to a CONNECT request (Section 8.3)
121 // was reset or abnormally closed.
122 CONNECT_ERROR = 0xa,
123
124 // The endpoint detected that its peer is exhibiting a behavior that might
125 // be generating excessive load.
126 ENHANCE_YOUR_CALM = 0xb,
127
128 // The underlying transport has properties that do not meet minimum
129 // security requirements (see Section 9.2).
130 INADEQUATE_SECURITY = 0xc,
131
132 // The endpoint requires that HTTP/1.1 be used instead of HTTP/2.
133 HTTP_1_1_REQUIRED = 0xd,
134 };
135
136 // Is the error code supported? (So far that means it is in RFC 7540.)
IsSupportedHttp2ErrorCode(uint32_t v)137 inline bool IsSupportedHttp2ErrorCode(uint32_t v) {
138 return v <= static_cast<uint32_t>(Http2ErrorCode::HTTP_1_1_REQUIRED);
139 }
IsSupportedHttp2ErrorCode(Http2ErrorCode v)140 inline bool IsSupportedHttp2ErrorCode(Http2ErrorCode v) {
141 return IsSupportedHttp2ErrorCode(static_cast<uint32_t>(v));
142 }
143
144 // Format the specified error code.
145 QUICHE_EXPORT std::string Http2ErrorCodeToString(uint32_t v);
146 QUICHE_EXPORT std::string Http2ErrorCodeToString(Http2ErrorCode v);
147 QUICHE_EXPORT inline std::ostream& operator<<(std::ostream& out,
148 Http2ErrorCode v) {
149 return out << Http2ErrorCodeToString(v);
150 }
151
152 // Supported parameters in SETTINGS frames; so far just those in RFC 7540.
153 enum class Http2SettingsParameter : uint16_t {
154 // Allows the sender to inform the remote endpoint of the maximum size of the
155 // header compression table used to decode header blocks, in octets. The
156 // encoder can select any size equal to or less than this value by using
157 // signaling specific to the header compression format inside a header block
158 // (see [COMPRESSION]). The initial value is 4,096 octets.
159 HEADER_TABLE_SIZE = 0x1,
160
161 // This setting can be used to disable server push (Section 8.2). An endpoint
162 // MUST NOT send a PUSH_PROMISE frame if it receives this parameter set to a
163 // value of 0. An endpoint that has both set this parameter to 0 and had it
164 // acknowledged MUST treat the receipt of a PUSH_PROMISE frame as a connection
165 // error (Section 5.4.1) of type PROTOCOL_ERROR.
166 //
167 // The initial value is 1, which indicates that server push is permitted. Any
168 // value other than 0 or 1 MUST be treated as a connection error (Section
169 // 5.4.1) of type PROTOCOL_ERROR.
170 ENABLE_PUSH = 0x2,
171
172 // Indicates the maximum number of concurrent streams that the sender will
173 // allow. This limit is directional: it applies to the number of streams that
174 // the sender permits the receiver to create. Initially, there is no limit to
175 // this value. It is recommended that this value be no smaller than 100, so as
176 // to not unnecessarily limit parallelism.
177 //
178 // A value of 0 for MAX_CONCURRENT_STREAMS SHOULD NOT be treated as
179 // special by endpoints. A zero value does prevent the creation of new
180 // streams; however, this can also happen for any limit that is exhausted with
181 // active streams. Servers SHOULD only set a zero value for short durations;
182 // if a server does not wish to accept requests, closing the connection is
183 // more appropriate.
184 MAX_CONCURRENT_STREAMS = 0x3,
185
186 // Indicates the sender's initial window size (in octets) for stream-level
187 // flow control. The initial value is 2^16-1 (65,535) octets.
188 //
189 // This setting affects the window size of all streams (see Section 6.9.2).
190 //
191 // Values above the maximum flow-control window size of 2^31-1 MUST be treated
192 // as a connection error (Section 5.4.1) of type FLOW_CONTROL_ERROR.
193 INITIAL_WINDOW_SIZE = 0x4,
194
195 // Indicates the size of the largest frame payload that the sender is willing
196 // to receive, in octets.
197 //
198 // The initial value is 2^14 (16,384) octets. The value advertised by an
199 // endpoint MUST be between this initial value and the maximum allowed frame
200 // size (2^24-1 or 16,777,215 octets), inclusive. Values outside this range
201 // MUST be treated as a connection error (Section 5.4.1) of type
202 // PROTOCOL_ERROR.
203 MAX_FRAME_SIZE = 0x5,
204
205 // This advisory setting informs a peer of the maximum size of header list
206 // that the sender is prepared to accept, in octets. The value is based on the
207 // uncompressed size of header fields, including the length of the name and
208 // value in octets plus an overhead of 32 octets for each header field.
209 //
210 // For any given request, a lower limit than what is advertised MAY be
211 // enforced. The initial value of this setting is unlimited.
212 MAX_HEADER_LIST_SIZE = 0x6,
213 };
214
215 // Is the settings parameter supported (so far that means it is in RFC 7540)?
IsSupportedHttp2SettingsParameter(uint32_t v)216 inline bool IsSupportedHttp2SettingsParameter(uint32_t v) {
217 return 0 < v && v <= static_cast<uint32_t>(
218 Http2SettingsParameter::MAX_HEADER_LIST_SIZE);
219 }
IsSupportedHttp2SettingsParameter(Http2SettingsParameter v)220 inline bool IsSupportedHttp2SettingsParameter(Http2SettingsParameter v) {
221 return IsSupportedHttp2SettingsParameter(static_cast<uint32_t>(v));
222 }
223
224 // Format the specified settings parameter.
225 QUICHE_EXPORT std::string Http2SettingsParameterToString(uint32_t v);
226 QUICHE_EXPORT std::string Http2SettingsParameterToString(
227 Http2SettingsParameter v);
228 inline std::ostream& operator<<(std::ostream& out, Http2SettingsParameter v) {
229 return out << Http2SettingsParameterToString(v);
230 }
231
232 // Information about the initial, minimum and maximum value of settings (not
233 // applicable to all settings parameters).
234 class QUICHE_EXPORT Http2SettingsInfo {
235 public:
236 // Default value for HEADER_TABLE_SIZE.
DefaultHeaderTableSize()237 static constexpr uint32_t DefaultHeaderTableSize() { return 4096; }
238
239 // Default value for ENABLE_PUSH.
DefaultEnablePush()240 static constexpr bool DefaultEnablePush() { return true; }
241
242 // Default value for INITIAL_WINDOW_SIZE.
DefaultInitialWindowSize()243 static constexpr uint32_t DefaultInitialWindowSize() { return 65535; }
244
245 // Maximum value for INITIAL_WINDOW_SIZE, and for the connection flow control
246 // window, and for each stream flow control window.
MaximumWindowSize()247 static constexpr uint32_t MaximumWindowSize() { return UInt31Mask(); }
248
249 // Default value for MAX_FRAME_SIZE.
DefaultMaxFrameSize()250 static constexpr uint32_t DefaultMaxFrameSize() { return 16384; }
251
252 // Minimum value for MAX_FRAME_SIZE.
MinimumMaxFrameSize()253 static constexpr uint32_t MinimumMaxFrameSize() { return 16384; }
254
255 // Maximum value for MAX_FRAME_SIZE.
MaximumMaxFrameSize()256 static constexpr uint32_t MaximumMaxFrameSize() { return (1 << 24) - 1; }
257 };
258
259 // Http3 early fails upper case request headers, but Http2 still needs case
260 // insensitive comparison.
261 using InvalidHeaderSet =
262 absl::flat_hash_set<absl::string_view, quiche::StringPieceCaseHash,
263 quiche::StringPieceCaseEqual>;
264
265 // Returns all disallowed HTTP/2 headers.
266 QUICHE_EXPORT const InvalidHeaderSet& GetInvalidHttp2HeaderSet();
267
268 } // namespace http2
269
270 #endif // QUICHE_HTTP2_HTTP2_CONSTANTS_H_
271