blob: 1e1dd39a21adc04f07b2ac89747301627ec50bfe [file] [log] [blame]
QUICHE teamcf9d4ed2021-03-08 15:13:32 -08001#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 teamf723cd92021-05-17 11:09:51 -070011#include "absl/types/variant.h"
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080012
13namespace http2 {
14namespace adapter {
15
16// Represents an HTTP/2 stream ID, consistent with nghttp2.
17using Http2StreamId = int32_t;
18
19// Represents an HTTP/2 SETTINGS parameter as specified in RFC 7540 Section 6.5.
20using Http2SettingsId = uint16_t;
21
22// Represents the payload of an HTTP/2 PING frame.
23using Http2PingId = uint64_t;
24
QUICHE teamf723cd92021-05-17 11:09:51 -070025// Represents a single header name or value.
26using 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.
30std::pair<absl::string_view, bool> GetStringView(const HeaderRep& rep);
31
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080032// 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 teamf723cd92021-05-17 11:09:51 -070034using Header = std::pair<HeaderRep, HeaderRep>;
QUICHE teamcf9d4ed2021-03-08 15:13:32 -080035
36// Represents an HTTP/2 SETTINGS key-value parameter.
37struct Http2Setting {
38 Http2SettingsId id;
39 uint32_t value;
40};
41
42// The maximum possible stream ID.
43const Http2StreamId kMaxStreamId = 0x7FFFFFFF;
44
45// The stream ID that represents the connection (e.g., for connection-level flow
46// control updates).
47const 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).
51const 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.
55const 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).
59ABSL_CONST_INIT extern const char kHttp2MethodPseudoHeader[];
60ABSL_CONST_INIT extern const char kHttp2SchemePseudoHeader[];
61ABSL_CONST_INIT extern const char kHttp2AuthorityPseudoHeader[];
62ABSL_CONST_INIT extern const char kHttp2PathPseudoHeader[];
63ABSL_CONST_INIT extern const char kHttp2StatusPseudoHeader[];
64
65// HTTP/2 error codes as specified in RFC 7540 Section 7.
66enum 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.
89enum 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.
103absl::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.
108absl::string_view Http2ErrorCodeToString(Http2ErrorCode error_code);
109
QUICHE team8a826a22021-05-06 12:47:45 -0700110enum class Perspective {
111 kClient,
112 kServer,
113};
114
QUICHE teamcf9d4ed2021-03-08 15:13:32 -0800115} // namespace adapter
116} // namespace http2
117
118#endif // QUICHE_HTTP2_ADAPTER_HTTP2_PROTOCOL_H_