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 "absl/base/attributes.h" 9 #include "absl/strings/string_view.h" 10 #include "absl/types/variant.h" 11 #include "quiche/common/platform/api/quiche_export.h" 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 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 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). 34 using Header = std::pair<HeaderRep, HeaderRep>; 35 36 // Represents an HTTP/2 SETTINGS key-value parameter. 37 struct QUICHE_EXPORT Http2Setting { 38 Http2SettingsId id; 39 uint32_t value; 40 }; 41 42 QUICHE_EXPORT bool operator==(const Http2Setting& a, const Http2Setting& b); 43 44 // The maximum possible stream ID. 45 const Http2StreamId kMaxStreamId = 0x7FFFFFFF; 46 47 // The stream ID that represents the connection (e.g., for connection-level flow 48 // control updates). 49 const Http2StreamId kConnectionStreamId = 0; 50 51 // The default value for the size of the largest frame payload, according to RFC 52 // 7540 Section 6.5.2 (SETTINGS_MAX_FRAME_SIZE). 53 const uint32_t kDefaultFramePayloadSizeLimit = 16u * 1024u; 54 55 // The maximum value for the size of the largest frame payload, according to RFC 56 // 7540 Section 6.5.2 (SETTINGS_MAX_FRAME_SIZE). 57 const uint32_t kMaximumFramePayloadSizeLimit = 16777215u; 58 59 // The default value for the initial stream and connection flow control window 60 // size, according to RFC 7540 Section 6.9.2. 61 const int kInitialFlowControlWindowSize = 64 * 1024 - 1; 62 63 // The pseudo-header fields as specified in RFC 7540 Section 8.1.2.3 (request) 64 // and Section 8.1.2.4 (response). 65 ABSL_CONST_INIT QUICHE_EXPORT extern const char kHttp2MethodPseudoHeader[]; 66 ABSL_CONST_INIT QUICHE_EXPORT extern const char kHttp2SchemePseudoHeader[]; 67 ABSL_CONST_INIT QUICHE_EXPORT extern const char kHttp2AuthorityPseudoHeader[]; 68 ABSL_CONST_INIT QUICHE_EXPORT extern const char kHttp2PathPseudoHeader[]; 69 ABSL_CONST_INIT QUICHE_EXPORT extern const char kHttp2StatusPseudoHeader[]; 70 71 ABSL_CONST_INIT QUICHE_EXPORT extern const uint8_t kMetadataFrameType; 72 ABSL_CONST_INIT QUICHE_EXPORT extern const uint8_t kMetadataEndFlag; 73 ABSL_CONST_INIT QUICHE_EXPORT extern const uint16_t kMetadataExtensionId; 74 75 enum class FrameType : uint8_t { 76 DATA = 0x0, 77 HEADERS, 78 PRIORITY, 79 RST_STREAM, 80 SETTINGS, 81 PUSH_PROMISE, 82 PING, 83 GOAWAY, 84 WINDOW_UPDATE, 85 CONTINUATION, 86 }; 87 88 enum FrameFlags : uint8_t { 89 END_STREAM_FLAG = 0x1, 90 ACK_FLAG = END_STREAM_FLAG, 91 END_HEADERS_FLAG = 0x4, 92 PADDED_FLAG = 0x8, 93 PRIORITY_FLAG = 0x20, 94 }; 95 96 // HTTP/2 error codes as specified in RFC 7540 Section 7. 97 enum class Http2ErrorCode { 98 HTTP2_NO_ERROR = 0x0, 99 PROTOCOL_ERROR = 0x1, 100 INTERNAL_ERROR = 0x2, 101 FLOW_CONTROL_ERROR = 0x3, 102 SETTINGS_TIMEOUT = 0x4, 103 STREAM_CLOSED = 0x5, 104 FRAME_SIZE_ERROR = 0x6, 105 REFUSED_STREAM = 0x7, 106 CANCEL = 0x8, 107 COMPRESSION_ERROR = 0x9, 108 CONNECT_ERROR = 0xA, 109 ENHANCE_YOUR_CALM = 0xB, 110 INADEQUATE_SECURITY = 0xC, 111 HTTP_1_1_REQUIRED = 0xD, 112 MAX_ERROR_CODE = HTTP_1_1_REQUIRED, 113 }; 114 115 // The SETTINGS parameters defined in RFC 7540 Section 6.5.2. Endpoints may send 116 // SETTINGS parameters outside of these definitions as per RFC 7540 Section 5.5. 117 // This is explicitly an enum instead of an enum class for ease of implicit 118 // conversion to the underlying Http2SettingsId type and use with non-standard 119 // extension SETTINGS parameters. 120 enum Http2KnownSettingsId : Http2SettingsId { 121 HEADER_TABLE_SIZE = 0x1, 122 MIN_SETTING = HEADER_TABLE_SIZE, 123 ENABLE_PUSH = 0x2, 124 MAX_CONCURRENT_STREAMS = 0x3, 125 INITIAL_WINDOW_SIZE = 0x4, 126 MAX_FRAME_SIZE = 0x5, 127 MAX_HEADER_LIST_SIZE = 0x6, 128 ENABLE_CONNECT_PROTOCOL = 0x8, // See RFC 8441 129 MAX_SETTING = ENABLE_CONNECT_PROTOCOL 130 }; 131 132 // Returns a human-readable string representation of the given SETTINGS |id| for 133 // logging/debugging. Returns "SETTINGS_UNKNOWN" for IDs outside of the RFC 7540 134 // Section 6.5.2 definitions. 135 QUICHE_EXPORT absl::string_view Http2SettingsIdToString(uint16_t id); 136 137 // Returns a human-readable string representation of the given |error_code| for 138 // logging/debugging. Returns "UNKNOWN_ERROR" for errors outside of RFC 7540 139 // Section 7 definitions. 140 QUICHE_EXPORT absl::string_view Http2ErrorCodeToString( 141 Http2ErrorCode error_code); 142 143 enum class Perspective { 144 kClient, 145 kServer, 146 }; 147 148 } // namespace adapter 149 } // namespace http2 150 151 #endif // QUICHE_HTTP2_ADAPTER_HTTP2_PROTOCOL_H_ 152