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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITYCORE_BASE_HTTP_SOCKET_H
#define TRINITYCORE_BASE_HTTP_SOCKET_H
#include "HttpCommon.h"
#include "HttpSessionState.h"
#include "Optional.h"
#include "Socket.h"
#include "SocketConnectionInitializer.h"
#include <boost/beast/core/basic_stream.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/string_body.hpp>
namespace Trinity::Net::Http
{
using IoContextHttpSocket = boost::beast::basic_stream<boost::asio::ip::tcp, boost::asio::io_context::executor_type, boost::beast::unlimited_rate_policy>;
namespace Impl
{
class BoostBeastSocketWrapper : public IoContextHttpSocket
{
public:
using IoContextHttpSocket::basic_stream;
bool is_open() const
{
return socket().is_open();
}
void close(boost::system::error_code& /*error*/)
{
IoContextHttpSocket::close();
}
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
{
socket().shutdown(what, shutdownError);
}
template<typename WaitHandlerType>
void async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType&& handler)
{
socket().async_wait(type, std::forward<WaitHandlerType>(handler));
}
template <typename SettableSocketOption>
void set_option(SettableSocketOption const& option, boost::system::error_code& ec)
{
socket().set_option(option, ec);
}
IoContextTcpSocket::endpoint_type remote_endpoint() const
{
return socket().remote_endpoint();
}
};
}
using RequestParser = boost::beast::http::request_parser<RequestBody>;
using ResponseParser = boost::beast::http::response_parser<RequestBody>;
class TC_NETWORK_API AbstractSocket
{
public:
AbstractSocket() = default;
AbstractSocket(AbstractSocket const& other) = default;
AbstractSocket(AbstractSocket&& other) = default;
AbstractSocket& operator=(AbstractSocket const& other) = default;
AbstractSocket& operator=(AbstractSocket&& other) = default;
virtual ~AbstractSocket() = default;
static bool ParseRequest(MessageBuffer& packet, RequestParser& parser);
static bool ParseResponse(MessageBuffer& packet, ResponseParser& parser);
static MessageBuffer SerializeRequest(Request const& request);
static MessageBuffer SerializeResponse(Request const& request, Response const& response);
virtual void SendResponse(RequestContext& context) = 0;
void LogRequestAndResponse(RequestContext const& context, MessageBuffer& buffer) const;
virtual std::string GetClientInfo() const = 0;
static std::string GetClientInfo(boost::asio::ip::address const& address, uint16 port, SessionState const* state);
virtual SessionState* GetSessionState() const = 0;
Optional<boost::uuids::uuid> GetSessionId() const
{
if (SessionState* state = this->GetSessionState())
return state->Id;
return {};
}
virtual void Start() = 0;
virtual bool Update() = 0;
virtual boost::asio::ip::address const& GetRemoteIpAddress() const = 0;
virtual bool IsOpen() const = 0;
virtual void CloseSocket() = 0;
};
template <typename SocketImpl>
struct HttpConnectionInitializer final : SocketConnectionInitializer
{
explicit HttpConnectionInitializer(SocketImpl* socket) : _socket(socket) { }
void Start() override
{
_socket->ResetHttpParser();
this->InvokeNext();
}
private:
SocketImpl* _socket;
};
template<typename Stream>
class BaseSocket : public Trinity::Net::Socket<Stream>, public AbstractSocket
{
using Base = Trinity::Net::Socket<Stream>;
public:
using Base::Base;
BaseSocket(BaseSocket const& other) = delete;
BaseSocket(BaseSocket&& other) = delete;
BaseSocket& operator=(BaseSocket const& other) = delete;
BaseSocket& operator=(BaseSocket&& other) = delete;
~BaseSocket() = default;
SocketReadCallbackResult ReadHandler() final
{
MessageBuffer& packet = this->GetReadBuffer();
while (packet.GetActiveSize() > 0)
{
if (!ParseRequest(packet, *_httpParser))
{
// Couldn't receive the whole data this time.
break;
}
if (!HandleMessage(_httpParser->get()))
{
this->CloseSocket();
return SocketReadCallbackResult::Stop;
}
this->ResetHttpParser();
}
return SocketReadCallbackResult::KeepReading;
}
bool HandleMessage(Request& request)
{
RequestContext context { .request = std::move(request) };
if (!_state)
_state = this->ObtainSessionState(context);
RequestHandlerResult status = this->RequestHandler(context);
if (status != RequestHandlerResult::Async)
this->SendResponse(context);
return status != RequestHandlerResult::Error;
}
virtual RequestHandlerResult RequestHandler(RequestContext& context) = 0;
void SendResponse(RequestContext& context) final
{
context.response.prepare_payload();
MessageBuffer buffer = SerializeResponse(context.request, context.response);
this->LogRequestAndResponse(context, buffer);
this->QueuePacket(std::move(buffer));
if (!context.response.keep_alive())
this->DelayedCloseSocket();
}
void Start() override { return this->Base::Start(); }
bool Update() override { return this->Base::Update(); }
boost::asio::ip::address const& GetRemoteIpAddress() const final { return this->Base::GetRemoteIpAddress(); }
bool IsOpen() const final { return this->Base::IsOpen(); }
void CloseSocket() final { return this->Base::CloseSocket(); }
std::string GetClientInfo() const override
{
return AbstractSocket::GetClientInfo(this->GetRemoteIpAddress(), this->GetRemotePort(), this->_state.get());
}
SessionState* GetSessionState() const override { return _state.get(); }
void ResetHttpParser()
{
this->_httpParser.reset();
this->_httpParser.emplace();
this->_httpParser->eager(true);
}
protected:
virtual std::shared_ptr<SessionState> ObtainSessionState(RequestContext& context) const = 0;
Optional<RequestParser> _httpParser;
std::shared_ptr<SessionState> _state;
};
}
#endif // TRINITYCORE_BASE_HTTP_SOCKET_H
|