Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
HttpConnectionEx.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <asio.hpp>
18#include <asio/ssl/stream.hpp>
19
20
21#include <sese/Config.h>
26
27#include <memory>
28#include <queue>
29#include <set>
30
31
33class HttpServiceImpl;
34
36 using Ptr = std::shared_ptr<HttpStream>;
37
38 explicit HttpStream(uint32_t id, uint32_t write_window_size, const sese::net::IPAddress::Ptr &addr) noexcept;
39
41 void prepareRange();
42
43 uint32_t id;
47 uint32_t window_size = 0;
48 uint16_t continue_type = 0;
49 bool end_headers = false;
50 bool end_stream = false;
51 bool do_response = false;
52
55
57
59};
60
61struct HttpConnectionEx : std::enable_shared_from_this<HttpConnectionEx> {
62 using Ptr = std::shared_ptr<HttpConnectionEx>;
63
64 Ptr getPtr() { return shared_from_this(); } // NOLINT
65
66 HttpConnectionEx(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &io_context, const sese::net::IPAddress::Ptr &addr);
67
68 virtual ~HttpConnectionEx() = default;
69
70 bool keepalive = false;
71 asio::system_timer timer;
72
74
75 std::weak_ptr<HttpServiceImpl> service;
76
77 bool is_read = false;
78 bool is_write = false;
79 bool expect_ack = false;
80 uint32_t accept_stream_count = 0;
81 uint32_t latest_stream_ident = 0;
82
83 // 本地最大帧大小
84 static constexpr uint32_t MAX_FRAME_SIZE = 16384;
85 // 本地初始窗口值
86 static constexpr uint32_t INIT_WINDOW_SIZE = 65535;
87 // 默认动态表大小
88 static constexpr uint32_t HEADER_TABLE_SIZE = 8192;
89 // 单连接并发大小
90 static constexpr uint32_t MAX_CONCURRENT_STREAMS = 16;
91
93 // 临时缓存,用于读取初始连接魔数、帧头,取最大帧大小
95 uint32_t header_table_size = 4096;
96 uint32_t enable_push = 0;
98 // 对端初始窗口值
99 uint32_t endpoint_init_window_size = 65535;
100 // 写入到对端窗口大小
101 uint32_t endpoint_window_size = 65535;
102 // 本地读取窗口大小
103 uint32_t window_size = 65535;
104 // 对端帧最大大小
105 uint32_t endpoint_max_frame_size = 16384;
106 // 采用的帧大小
107 uint32_t max_frame_size = 16384;
111 std::map<uint32_t, HttpStream::Ptr> streams;
112 std::set<uint32_t> closed_streams;
113
115 std::vector<sese::net::http::Http2Frame::Ptr> pre_vector;
116 std::vector<sese::net::http::Http2Frame::Ptr> vector;
117 std::vector<asio::const_buffer> asio_buffers;
118
121 void close(uint32_t id);
122
123 virtual void checkKeepalive() = 0;
124
125 void disponse();
126
130 virtual void writeBlocks(const std::vector<asio::const_buffer> &buffers,
131 const std::function<void(const asio::error_code &code)> &callback) = 0;
132
137 virtual void writeBlock(const void *buffer, size_t size,
138 const std::function<void(const asio::error_code &code)> &callback) = 0;
139
144 virtual void readBlock(char *buffer, size_t length,
145 const std::function<void(const asio::error_code &code)> &callback) = 0;
146
147 void readMagic();
148
149 void readFrameHeader();
150
151 void handleFrameHeader();
152
153 uint8_t handleSettingsFrame();
154
155 void handleWindowUpdate();
156
158
159 void handleGoawayFrame();
160
161 void handleHeadersFrame();
162
163 void handleDataFrame();
164
165 void handlePriorityFrame();
166
167 void handlePingFrame();
168
169 void handleRequest(const HttpStream::Ptr &stream);
170
171 void handleWrite();
172
173 void writeSettingsFrame();
174
175 void writeAckFrame();
176
177 void writeGoawayFrame(
178 uint32_t latest_stream_id,
179 uint8_t flags,
180 uint32_t error_code,
181 const std::string &msg,
182 bool once = false
183 );
184
186 uint32_t stream_id,
187 uint8_t flags,
188 uint32_t error_code,
189 bool once = false
190 );
191
193 uint32_t stream_id,
194 uint8_t flags,
195 uint32_t window_size
196 );
197
202 bool writeHeadersFrame(const HttpStream::Ptr &stream, bool verify_end_stream = true);
203
207 bool writeDataFrame4Body(const HttpStream::Ptr &stream);
208
213
217 bool writeDataFrame4Ranges(const HttpStream::Ptr &stream);
218
225 void writeSubheaderAndData(const HttpStream::Ptr &stream, const std::string &subheader, size_t remind);
226};
227
229 using Ptr = std::shared_ptr<HttpConnectionExImpl>;
230 using Socket = asio::ip::tcp::socket;
231 using SharedSocket = std::shared_ptr<Socket>;
232
233 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpConnectionExImpl>(shared_from_this()); } // NOLINT
234
236
237 HttpConnectionExImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
239
240 void writeBlocks(const std::vector<asio::const_buffer> &buffers,
241 const std::function<void(const asio::error_code &code)> &callback) override;
242
243 void writeBlock(const void *buffer, size_t size,
244 const std::function<void(const asio::error_code &code)> &callback) override;
245
246 void readBlock(char *buffer, size_t length,
247 const std::function<void(const asio::error_code &code)> &callback) override;
248
249 void checkKeepalive() override;
250};
251
253 using Ptr = std::shared_ptr<HttpsConnectionExImpl>;
254 using Stream = asio::ssl::stream<asio::ip::tcp::socket>;
255 using SharedStream = std::shared_ptr<Stream>;
256
257 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpsConnectionExImpl>(shared_from_this()); } // NOLINT
258
260
261 HttpsConnectionExImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
263
264 void writeBlocks(const std::vector<asio::const_buffer> &buffers,
265 const std::function<void(const asio::error_code &code)> &callback) override;
266
267 void writeBlock(const void *buffer, size_t size,
268 const std::function<void(const asio::error_code &code)> &callback) override;
269
270 void readBlock(char *buffer, size_t length,
271 const std::function<void(const asio::error_code &code)> &callback) override;
272
273 void checkKeepalive() override;
274};
275
276}