QUICHE team | cf9d4ed | 2021-03-08 15:13:32 -0800 | [diff] [blame] | 1 | #ifndef QUICHE_HTTP2_ADAPTER_HTTP2_PROTOCOL_H_ |
| 2 | #define QUICHE_HTTP2_ADAPTER_HTTP2_PROTOCOL_H_ |
| 3 | |
| 4 | #include <cstdint> |
| 5 | #include <string> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "base/integral_types.h" |
| 9 | #include "absl/base/attributes.h" |
| 10 | #include "absl/strings/string_view.h" |
QUICHE team | f723cd9 | 2021-05-17 11:09:51 -0700 | [diff] [blame] | 11 | #include "absl/types/variant.h" |
QUICHE team | cf9d4ed | 2021-03-08 15:13:32 -0800 | [diff] [blame] | 12 | |
| 13 | namespace http2 { |
| 14 | namespace adapter { |
| 15 | |
| 16 | // Represents an HTTP/2 stream ID, consistent with nghttp2. |
| 17 | using Http2StreamId = int32_t; |
| 18 | |
| 19 | // Represents an HTTP/2 SETTINGS parameter as specified in RFC 7540 Section 6.5. |
| 20 | using Http2SettingsId = uint16_t; |
| 21 | |
| 22 | // Represents the payload of an HTTP/2 PING frame. |
| 23 | using Http2PingId = uint64_t; |
| 24 | |
QUICHE team | f723cd9 | 2021-05-17 11:09:51 -0700 | [diff] [blame] | 25 | // Represents a single header name or value. |
| 26 | using HeaderRep = absl::variant<absl::string_view, std::string>; |
| 27 | |
| 28 | // Boolean return value is true if |rep| holds a string_view, which is assumed |
| 29 | // to have an indefinite lifetime. |
| 30 | std::pair<absl::string_view, bool> GetStringView(const HeaderRep& rep); |
| 31 | |
QUICHE team | cf9d4ed | 2021-03-08 15:13:32 -0800 | [diff] [blame] | 32 | // Represents an HTTP/2 header field. A header field is a key-value pair with |
| 33 | // lowercase keys (as specified in RFC 7540 Section 8.1.2). |
QUICHE team | f723cd9 | 2021-05-17 11:09:51 -0700 | [diff] [blame] | 34 | using Header = std::pair<HeaderRep, HeaderRep>; |
QUICHE team | cf9d4ed | 2021-03-08 15:13:32 -0800 | [diff] [blame] | 35 | |
| 36 | // Represents an HTTP/2 SETTINGS key-value parameter. |
| 37 | struct Http2Setting { |
| 38 | Http2SettingsId id; |
| 39 | uint32_t value; |
| 40 | }; |
| 41 | |
| 42 | // The maximum possible stream ID. |
| 43 | const Http2StreamId kMaxStreamId = 0x7FFFFFFF; |
| 44 | |
| 45 | // The stream ID that represents the connection (e.g., for connection-level flow |
| 46 | // control updates). |
| 47 | const Http2StreamId kConnectionStreamId = 0; |
| 48 | |
| 49 | // The default value for the size of the largest frame payload, according to RFC |
| 50 | // 7540 Section 6.5.2 (SETTINGS_MAX_FRAME_SIZE). |
| 51 | const int kDefaultFramePayloadSizeLimit = 16 * 1024; |
| 52 | |
| 53 | // The default value for the initial stream flow control window size, according |
| 54 | // to RFC 7540 Section 6.9.2. |
| 55 | const int kDefaultInitialStreamWindowSize = 64 * 1024 - 1; |
| 56 | |
| 57 | // The pseudo-header fields as specified in RFC 7540 Section 8.1.2.3 (request) |
| 58 | // and Section 8.1.2.4 (response). |
| 59 | ABSL_CONST_INIT extern const char kHttp2MethodPseudoHeader[]; |
| 60 | ABSL_CONST_INIT extern const char kHttp2SchemePseudoHeader[]; |
| 61 | ABSL_CONST_INIT extern const char kHttp2AuthorityPseudoHeader[]; |
| 62 | ABSL_CONST_INIT extern const char kHttp2PathPseudoHeader[]; |
| 63 | ABSL_CONST_INIT extern const char kHttp2StatusPseudoHeader[]; |
| 64 | |
| 65 | // HTTP/2 error codes as specified in RFC 7540 Section 7. |
| 66 | enum class Http2ErrorCode { |
| 67 | NO_ERROR = 0x0, |
| 68 | PROTOCOL_ERROR = 0x1, |
| 69 | INTERNAL_ERROR = 0x2, |
| 70 | FLOW_CONTROL_ERROR = 0x3, |
| 71 | SETTINGS_TIMEOUT = 0x4, |
| 72 | STREAM_CLOSED = 0x5, |
| 73 | FRAME_SIZE_ERROR = 0x6, |
| 74 | REFUSED_STREAM = 0x7, |
| 75 | CANCEL = 0x8, |
| 76 | COMPRESSION_ERROR = 0x9, |
| 77 | CONNECT_ERROR = 0xA, |
| 78 | ENHANCE_YOUR_CALM = 0xB, |
| 79 | INADEQUATE_SECURITY = 0xC, |
| 80 | HTTP_1_1_REQUIRED = 0xD, |
| 81 | MAX_ERROR_CODE = HTTP_1_1_REQUIRED, |
| 82 | }; |
| 83 | |
| 84 | // The SETTINGS parameters defined in RFC 7540 Section 6.5.2. Endpoints may send |
| 85 | // SETTINGS parameters outside of these definitions as per RFC 7540 Section 5.5. |
| 86 | // This is explicitly an enum instead of an enum class for ease of implicit |
| 87 | // conversion to the underlying Http2SettingsId type and use with non-standard |
| 88 | // extension SETTINGS parameters. |
| 89 | enum Http2KnownSettingsId : Http2SettingsId { |
| 90 | HEADER_TABLE_SIZE = 0x1, |
| 91 | MIN_SETTING = HEADER_TABLE_SIZE, |
| 92 | ENABLE_PUSH = 0x2, |
| 93 | MAX_CONCURRENT_STREAMS = 0x3, |
| 94 | INITIAL_WINDOW_SIZE = 0x4, |
| 95 | MAX_FRAME_SIZE = 0x5, |
| 96 | MAX_HEADER_LIST_SIZE = 0x6, |
| 97 | MAX_SETTING = MAX_HEADER_LIST_SIZE |
| 98 | }; |
| 99 | |
| 100 | // Returns a human-readable string representation of the given SETTINGS |id| for |
| 101 | // logging/debugging. Returns "SETTINGS_UNKNOWN" for IDs outside of the RFC 7540 |
| 102 | // Section 6.5.2 definitions. |
| 103 | absl::string_view Http2SettingsIdToString(uint16_t id); |
| 104 | |
| 105 | // Returns a human-readable string representation of the given |error_code| for |
| 106 | // logging/debugging. Returns "UNKNOWN_ERROR" for errors outside of RFC 7540 |
| 107 | // Section 7 definitions. |
| 108 | absl::string_view Http2ErrorCodeToString(Http2ErrorCode error_code); |
| 109 | |
QUICHE team | 8a826a2 | 2021-05-06 12:47:45 -0700 | [diff] [blame] | 110 | enum class Perspective { |
| 111 | kClient, |
| 112 | kServer, |
| 113 | }; |
| 114 | |
QUICHE team | cf9d4ed | 2021-03-08 15:13:32 -0800 | [diff] [blame] | 115 | } // namespace adapter |
| 116 | } // namespace http2 |
| 117 | |
| 118 | #endif // QUICHE_HTTP2_ADAPTER_HTTP2_PROTOCOL_H_ |