blob: 75f294e66725b5555ce6744744eda91811888d7d [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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.
QUICHE teamfd50a402018-12-07 22:54:05 -05009
10#include <cstdint>
11#include <iosfwd>
12#include <ostream>
bnc47904002019-08-16 11:49:48 -070013#include <string>
QUICHE teamfd50a402018-12-07 22:54:05 -050014
15#include "net/third_party/quiche/src/http2/platform/api/http2_export.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050016
17namespace http2 {
18
19// TODO(jamessynge): create http2_simple_types for types similar to
20// SpdyStreamId, but not for structures like Http2FrameHeader. Then will be
21// able to move these stream id functions there.
22constexpr uint32_t UInt31Mask() {
23 return 0x7fffffff;
24}
25constexpr uint32_t StreamIdMask() {
26 return UInt31Mask();
27}
28
29// The value used to identify types of frames. Upper case to match the RFC.
30// The comments indicate which flags are valid for that frame type.
31// ALTSVC is defined in http://httpwg.org/http-extensions/alt-svc.html
32// (not yet final standard as of March 2016, but close).
33enum class Http2FrameType : uint8_t {
34 DATA = 0, // END_STREAM | PADDED
35 HEADERS = 1, // END_STREAM | END_HEADERS | PADDED | PRIORITY
36 PRIORITY = 2, //
37 RST_STREAM = 3, //
38 SETTINGS = 4, // ACK
39 PUSH_PROMISE = 5, // END_HEADERS | PADDED
40 PING = 6, // ACK
41 GOAWAY = 7, //
42 WINDOW_UPDATE = 8, //
43 CONTINUATION = 9, // END_HEADERS
44 ALTSVC = 10, //
45};
46
47// Is the frame type known/supported?
48inline bool IsSupportedHttp2FrameType(uint32_t v) {
49 return v <= static_cast<uint32_t>(Http2FrameType::ALTSVC);
50}
51inline bool IsSupportedHttp2FrameType(Http2FrameType v) {
52 return IsSupportedHttp2FrameType(static_cast<uint32_t>(v));
53}
54
bnc47904002019-08-16 11:49:48 -070055// The return type is 'std::string' so that they can generate a unique string
QUICHE teamfd50a402018-12-07 22:54:05 -050056// for each unsupported value. Since these are just used for debugging/error
57// messages, that isn't a cost to we need to worry about. The same applies to
58// the functions later in this file.
bnc47904002019-08-16 11:49:48 -070059HTTP2_EXPORT_PRIVATE std::string Http2FrameTypeToString(Http2FrameType v);
60HTTP2_EXPORT_PRIVATE std::string Http2FrameTypeToString(uint8_t v);
QUICHE teamfd50a402018-12-07 22:54:05 -050061HTTP2_EXPORT_PRIVATE inline std::ostream& operator<<(std::ostream& out,
62 Http2FrameType v) {
63 return out << Http2FrameTypeToString(v);
64}
65
66// Flags that appear in supported frame types. These are treated as bit masks.
67// The comments indicate for which frame types the flag is valid.
68enum Http2FrameFlag {
69 END_STREAM = 0x01, // DATA, HEADERS
70 ACK = 0x01, // SETTINGS, PING
71 END_HEADERS = 0x04, // HEADERS, PUSH_PROMISE, CONTINUATION
72 PADDED = 0x08, // DATA, HEADERS, PUSH_PROMISE
73 PRIORITY = 0x20, // HEADERS
74};
75
76// Formats zero or more flags for the specified type of frame. Returns an
77// empty string if flags==0.
bnc47904002019-08-16 11:49:48 -070078HTTP2_EXPORT_PRIVATE std::string Http2FrameFlagsToString(Http2FrameType type,
QUICHE teamfd50a402018-12-07 22:54:05 -050079 uint8_t flags);
bnc47904002019-08-16 11:49:48 -070080HTTP2_EXPORT_PRIVATE std::string Http2FrameFlagsToString(uint8_t type,
QUICHE teamfd50a402018-12-07 22:54:05 -050081 uint8_t flags);
82
83// Error codes for GOAWAY and RST_STREAM frames.
84enum 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.)
137inline bool IsSupportedHttp2ErrorCode(uint32_t v) {
138 return v <= static_cast<uint32_t>(Http2ErrorCode::HTTP_1_1_REQUIRED);
139}
140inline bool IsSupportedHttp2ErrorCode(Http2ErrorCode v) {
141 return IsSupportedHttp2ErrorCode(static_cast<uint32_t>(v));
142}
143
144// Format the specified error code.
bnc47904002019-08-16 11:49:48 -0700145HTTP2_EXPORT_PRIVATE std::string Http2ErrorCodeToString(uint32_t v);
146HTTP2_EXPORT_PRIVATE std::string Http2ErrorCodeToString(Http2ErrorCode v);
QUICHE teamfd50a402018-12-07 22:54:05 -0500147HTTP2_EXPORT_PRIVATE 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.
153enum 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)?
216inline bool IsSupportedHttp2SettingsParameter(uint32_t v) {
217 return 0 < v && v <= static_cast<uint32_t>(
218 Http2SettingsParameter::MAX_HEADER_LIST_SIZE);
219}
220inline bool IsSupportedHttp2SettingsParameter(Http2SettingsParameter v) {
221 return IsSupportedHttp2SettingsParameter(static_cast<uint32_t>(v));
222}
223
224// Format the specified settings parameter.
bnc47904002019-08-16 11:49:48 -0700225HTTP2_EXPORT_PRIVATE std::string Http2SettingsParameterToString(uint32_t v);
226HTTP2_EXPORT_PRIVATE std::string Http2SettingsParameterToString(
227 Http2SettingsParameter v);
QUICHE teamfd50a402018-12-07 22:54:05 -0500228inline 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).
234class Http2SettingsInfo {
235 public:
236 // Default value for HEADER_TABLE_SIZE.
237 static constexpr uint32_t DefaultHeaderTableSize() { return 4096; }
238
239 // Default value for ENABLE_PUSH.
240 static constexpr bool DefaultEnablePush() { return true; }
241
242 // Default value for INITIAL_WINDOW_SIZE.
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.
247 static constexpr uint32_t MaximumWindowSize() { return UInt31Mask(); }
248
249 // Default value for MAX_FRAME_SIZE.
250 static constexpr uint32_t DefaultMaxFrameSize() { return 16384; }
251
252 // Minimum value for MAX_FRAME_SIZE.
253 static constexpr uint32_t MinimumMaxFrameSize() { return 16384; }
254
255 // Maximum value for MAX_FRAME_SIZE.
256 static constexpr uint32_t MaximumMaxFrameSize() { return (1 << 24) - 1; }
257};
258
259} // namespace http2
260
261#endif // QUICHE_HTTP2_HTTP2_CONSTANTS_H_