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