forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp2.h
More file actions
220 lines (175 loc) · 4.26 KB
/
Http2.h
File metadata and controls
220 lines (175 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#include "../../utils/Event.h"
#include "../AppRequest.h"
#include <deque>
#include <utility>
#include <string>
#include <unordered_map>
#include <cstddef>
#ifdef WIN32
#undef NO_ERROR
#elif POSIX
#include <arpa/inet.h>
#endif
namespace Http2
{
enum class ErrorCode : uint32_t {
NO_ERROR = 0x0,
PROTOCOL_ERROR,
INTERNAL_ERROR,
FLOW_CONTROL_ERROR,
SETTINGS_TIMEOUT,
STREAM_CLOSED,
FRAME_SIZE_ERROR,
REFUSED_STREAM,
CANCEL,
COMPRESSION_ERROR,
CONNECT_ERROR,
ENHANCE_YOUR_CALM,
INADEQUATE_SECURITY,
HTTP_1_1_REQUIRED
};
enum class FrameType : uint8_t {
DATA = 0x0,
HEADERS,
PRIORITY,
RST_STREAM,
SETTINGS,
PUSH_PROMISE,
PING,
GOAWAY,
WINDOW_UPDATE,
CONTINUATION
};
enum class FrameFlag : uint8_t {
EMPTY = 0x0,
ACK = 0x1,
END_STREAM = 0x1,
END_HEADERS = 0x4,
PADDED = 0x8,
PRIORITY = 0x20
};
bool operator &(const FrameFlag left, const FrameFlag right) noexcept;
FrameFlag operator |(const FrameFlag left, const FrameFlag right) noexcept;
FrameFlag operator |=(FrameFlag &left, const FrameFlag right) noexcept;
struct FrameMeta {
uint32_t stream_id;
uint32_t length;
FrameType type;
FrameFlag flags;
};
constexpr uint32_t FRAME_HEADER_SIZE = 9;
constexpr uint32_t MAX_WINDOW_UPDATE = (uint32_t(1) << 31) - 1;
enum class ConnectionSetting : uint16_t {
SETTINGS_HEADER_TABLE_SIZE = 0x1,
SETTINGS_ENABLE_PUSH = 0x2,
SETTINGS_MAX_CONCURRENT_STREAMS = 0x3,
SETTINGS_INITIAL_WINDOW_SIZE = 0x4,
SETTINGS_MAX_FRAME_SIZE = 0x5,
SETTINGS_MAX_HEADER_LIST_SIZE = 0x6
};
struct ConnectionSettings {
uint32_t header_table_size;
uint32_t enable_push;
uint32_t max_concurrent_streams;
uint32_t initial_window_size;
uint32_t max_frame_size;
uint32_t max_header_list_size;
static ConnectionSettings defaultSettings() noexcept;
};
enum class StreamState : uint8_t {
IDLE,
RESERVED,
OPEN,
HALF_CLOSED,
CLOSED
};
struct ConnectionSync {
Utils::Event event;
std::mutex mtx;
std::atomic<std::size_t> completed {};
};
class DynamicTable
{
private:
std::deque<std::pair<std::string, std::string> > list;
uint32_t header_table_size;
uint32_t max_header_list_size;
uint32_t cur_header_list_size;
public:
DynamicTable() noexcept;
DynamicTable(
const uint32_t headerTableSize,
const uint32_t maxHeaderListSize,
std::deque<std::pair<std::string, std::string> > &&list
) noexcept;
size_t size() const noexcept;
void addHeader(const std::pair<std::string, std::string> &header);
void addHeader(std::pair<std::string, std::string> &&header);
void changeHeaderTableSize(const uint32_t headerTableSize);
void changeMaxHeaderListSize(const uint32_t maxHeaderListSize);
const std::pair<std::string, std::string> &
operator[](const size_t index) const noexcept;
std::pair<std::string, std::string> &
operator[](const size_t index) noexcept;
const std::deque<std::pair<std::string, std::string> > &
getList() const noexcept;
};
struct ConnectionData {
DynamicTable decoding_dynamic_table;
DynamicTable encoding_dynamic_table;
ConnectionSettings client_settings;
ConnectionSettings server_settings;
ConnectionSync sync;
};
class IncStream : public Transfer::request_data
{
public:
ConnectionData &conn;
int32_t window_size_inc;
int32_t window_size_out;
uint32_t stream_id;
StreamState state;
uint8_t priority;
FrameType frame_type;
void *reserved;
public:
IncStream(
const uint32_t streamId,
ConnectionData &conn
) noexcept;
uint8_t *setHttp2FrameHeader(
uint8_t *addr,
const uint32_t frameSize,
const Http2::FrameType frameType,
const Http2::FrameFlag frameFlags
) noexcept;
void lock();
void unlock() noexcept;
void close() noexcept;
};
struct OutStream
{
uint32_t stream_id;
int32_t window_size_out;
ConnectionSettings settings;
DynamicTable dynamic_table;
std::mutex *mtx;
public:
OutStream(
const uint32_t streamId,
const ConnectionSettings &settings,
DynamicTable &&dynamic_table,
std::mutex *mtx
) noexcept;
OutStream(const IncStream &stream);
uint8_t *setHttp2FrameHeader(
uint8_t *addr,
const uint32_t frameSize,
const Http2::FrameType frameType,
const Http2::FrameFlag frameFlags
) noexcept;
void lock();
void unlock() noexcept;
};
}