TrinityCore
BaseHttpSocket.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "BaseHttpSocket.h"
19#include <boost/beast/http/serializer.hpp>
20
22{
23using RequestSerializer = boost::beast::http::request_serializer<ResponseBody>;
24using ResponseSerializer = boost::beast::http::response_serializer<ResponseBody>;
25
27{
28 if (!parser.is_done())
29 {
30 // need more data in the payload
31 boost::system::error_code ec = {};
32 std::size_t readDataSize = parser.put(boost::asio::const_buffer(packet.GetReadPointer(), packet.GetActiveSize()), ec);
33 packet.ReadCompleted(readDataSize);
34 }
35
36 return parser.is_done();
37}
38
39std::string AbstractSocket::SerializeRequest(Request const& request)
40{
41 RequestSerializer serializer(request);
42
43 std::string buffer;
44 while (!serializer.is_done())
45 {
46 size_t totalBytes = 0;
47 boost::system::error_code ec = {};
48 serializer.next(ec, [&]<typename ConstBufferSequence>(boost::system::error_code const&, ConstBufferSequence const& buffers)
49 {
50 size_t totalBytesInBuffers = boost::asio::buffer_size(buffers);
51
52 buffer.reserve(buffer.size() + totalBytes);
53
54 auto begin = boost::asio::buffers_begin(buffers);
55 auto end = boost::asio::buffers_end(buffers);
56
57 std::copy(begin, end, std::back_inserter(buffer));
58 totalBytes += totalBytesInBuffers;
59 });
60
61 serializer.consume(totalBytes);
62 }
63
64 return buffer;
65}
66
68{
69 response.prepare_payload();
70
71 ResponseSerializer serializer(response);
72 bool (*serializerIsDone)(ResponseSerializer&);
73 if (request.method() != boost::beast::http::verb::head)
74 {
75 serializerIsDone = [](ResponseSerializer& s) { return s.is_done(); };
76 }
77 else
78 {
79 serializerIsDone = [](ResponseSerializer& s) { return s.is_header_done(); };
80 serializer.split(true);
81 }
82
83 MessageBuffer buffer;
84 while (!serializerIsDone(serializer))
85 {
86 serializer.limit(buffer.GetRemainingSpace());
87
88 size_t totalBytes = 0;
89 boost::system::error_code ec = {};
90 serializer.next(ec, [&]<typename ConstBufferSequence>(boost::system::error_code& currentError, ConstBufferSequence const& buffers)
91 {
92 size_t totalBytesInBuffers = boost::asio::buffer_size(buffers);
93 if (totalBytesInBuffers > buffer.GetRemainingSpace())
94 {
95 currentError = boost::beast::http::error::need_more;
96 return;
97 }
98
99 auto begin = boost::asio::buffers_begin(buffers);
100 auto end = boost::asio::buffers_end(buffers);
101
102 std::copy(begin, end, buffer.GetWritePointer());
103 buffer.WriteCompleted(totalBytesInBuffers);
104 totalBytes += totalBytesInBuffers;
105 });
106
107 serializer.consume(totalBytes);
108
109 if (ec == boost::beast::http::error::need_more)
110 buffer.Resize(buffer.GetBufferSize() + 4096);
111 }
112
113 return buffer;
114}
115}
void Resize(size_type bytes)
Definition: MessageBuffer.h:52
size_type GetRemainingSpace() const
Definition: MessageBuffer.h:69
void ReadCompleted(size_type bytes)
Definition: MessageBuffer.h:63
void WriteCompleted(size_type bytes)
Definition: MessageBuffer.h:65
uint8 * GetReadPointer()
Definition: MessageBuffer.h:59
size_type GetActiveSize() const
Definition: MessageBuffer.h:67
uint8 * GetWritePointer()
Definition: MessageBuffer.h:61
size_type GetBufferSize() const
Definition: MessageBuffer.h:71
static std::string SerializeRequest(Request const &request)
static MessageBuffer SerializeResponse(Request const &request, Response &response)
static bool ParseRequest(MessageBuffer &packet, RequestParser &parser)
boost::beast::http::request_serializer< ResponseBody > RequestSerializer
boost::beast::http::response< ResponseBody > Response
Definition: HttpCommon.h:31
boost::beast::http::request_parser< RequestBody > RequestParser
boost::beast::http::response_serializer< ResponseBody > ResponseSerializer
boost::beast::http::request< RequestBody > Request
Definition: HttpCommon.h:30