Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
HttpConnectionEx.h
Go to the documentation of this file.
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 // The maximum local frame size
84 static constexpr uint32_t MAX_FRAME_SIZE = 16384;
85 // Local initial window value
86 static constexpr uint32_t INIT_WINDOW_SIZE = 65535;
87 // Default dynamic table size
88 static constexpr uint32_t HEADER_TABLE_SIZE = 8192;
89 // The size of a single connection concurrency
90 static constexpr uint32_t MAX_CONCURRENT_STREAMS = 16;
91
93 // Temporary cache, which is used to read the initial connection magic number and frame header,
94 // and take the maximum frame size
96 uint32_t header_table_size = 4096;
97 uint32_t enable_push = 0;
99 // The value of the initial window on the peer
101 // Write to the peer window size
102 uint32_t endpoint_window_size = 65535;
103 // The size of the local read window
104 uint32_t window_size = 65535;
105 // The maximum size of the peer frame
106 uint32_t endpoint_max_frame_size = 16384;
107 // The frame size used
108 uint32_t max_frame_size = 16384;
112 std::map<uint32_t, HttpStream::Ptr> streams;
113 std::set<uint32_t> closed_streams;
114
116 std::vector<sese::net::http::Http2Frame::Ptr> pre_vector;
117 std::vector<sese::net::http::Http2Frame::Ptr> vector;
118 std::vector<asio::const_buffer> asio_buffers;
119
122 void close(uint32_t id);
123
124 virtual void checkKeepalive() = 0;
125
126 void disponse();
127
132 virtual void writeBlocks(const std::vector<asio::const_buffer> &buffers,
133 const std::function<void(const asio::error_code &code)> &callback) = 0;
134
140 virtual void writeBlock(const void *buffer, size_t size,
141 const std::function<void(const asio::error_code &code)> &callback) = 0;
142
148 virtual void readBlock(char *buffer, size_t length,
149 const std::function<void(const asio::error_code &code)> &callback) = 0;
150
151 void readMagic();
152
153 void readFrameHeader();
154
155 void handleFrameHeader();
156
157 uint8_t handleSettingsFrame();
158
159 void handleWindowUpdate();
160
162
163 void handleGoawayFrame();
164
165 void handleHeadersFrame();
166
167 void handleDataFrame();
168
169 void handlePriorityFrame();
170
171 void handlePingFrame();
172
173 void handleRequest(const HttpStream::Ptr &stream);
174
175 void handleWrite();
176
177 void writeSettingsFrame();
178
179 void writeAckFrame();
180
181 void writeGoawayFrame(
182 uint32_t latest_stream_id,
183 uint8_t flags,
184 uint32_t error_code,
185 const std::string &msg,
186 bool once = false
187 );
188
190 uint32_t stream_id,
191 uint8_t flags,
192 uint32_t error_code,
193 bool once = false
194 );
195
197 uint32_t stream_id,
198 uint8_t flags,
199 uint32_t window_size
200 );
201
206 bool writeHeadersFrame(const HttpStream::Ptr &stream, bool verify_end_stream = true);
207
211 bool writeDataFrame4Body(const HttpStream::Ptr &stream);
212
217
221 bool writeDataFrame4Ranges(const HttpStream::Ptr &stream);
222
229 void writeSubheaderAndData(const HttpStream::Ptr &stream, const std::string &subheader, size_t remind);
230};
231
233 using Ptr = std::shared_ptr<HttpConnectionExImpl>;
234 using Socket = asio::ip::tcp::socket;
235 using SharedSocket = std::shared_ptr<Socket>;
236
237 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpConnectionExImpl>(shared_from_this()); } // NOLINT
238
240
241 HttpConnectionExImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
243
244 void writeBlocks(const std::vector<asio::const_buffer> &buffers,
245 const std::function<void(const asio::error_code &code)> &callback) override;
246
247 void writeBlock(const void *buffer, size_t size,
248 const std::function<void(const asio::error_code &code)> &callback) override;
249
250 void readBlock(char *buffer, size_t length,
251 const std::function<void(const asio::error_code &code)> &callback) override;
252
253 void checkKeepalive() override;
254};
255
257 using Ptr = std::shared_ptr<HttpsConnectionExImpl>;
258 using Stream = asio::ssl::stream<asio::ip::tcp::socket>;
259 using SharedStream = std::shared_ptr<Stream>;
260
261 Ptr getPtr() { return std::reinterpret_pointer_cast<HttpsConnectionExImpl>(shared_from_this()); } // NOLINT
262
264
265 HttpsConnectionExImpl(const std::shared_ptr<HttpServiceImpl> &service, asio::io_context &context,
267
268 void writeBlocks(const std::vector<asio::const_buffer> &buffers,
269 const std::function<void(const asio::error_code &code)> &callback) override;
270
271 void writeBlock(const void *buffer, size_t size,
272 const std::function<void(const asio::error_code &code)> &callback) override;
273
274 void readBlock(char *buffer, size_t length,
275 const std::function<void(const asio::error_code &code)> &callback) override;
276
277 void checkKeepalive() override;
278};
279
280}