8 #include <boost/config.hpp> 15 : strand_(boost::asio::make_strand(context)),
24 boost::system::error_code ec;
25 stream_.socket().shutdown(Socket::shutdown_both, ec);
26 boost::ignore_unused(ec);
33 res.set(boost::beast::http::field::server,
kServerName);
34 res.set(boost::beast::http::field::content_type,
"text/html");
35 res.keep_alive(keep_alive);
37 res.prepare_payload();
44 if (req.method() != boost::beast::http::verb::post) {
46 "Unsupported HTTP-method", req.version(), req.keep_alive()));
53 parser_ = std::make_unique<Parser>();
57 boost::beast::http::async_read(
61 [
self = shared_from_this()](
auto ec,
auto count) {
63 BOOST_ASSERT_MSG(s !=
nullptr,
"cannot cast to HttpSession");
68 template <
class Message>
71 using message_type = decltype(message);
72 auto m = std::make_shared<std::decay_t<message_type>>(
73 std::forward<message_type>(message));
76 boost::beast::http::async_write(
77 stream_, *m, [
self = shared_from_this(), m](
auto ec,
auto size) {
78 self->onWrite(ec, size, m->need_eof());
83 StringBody::value_type body;
84 body.assign(response);
86 const auto size = body.size();
90 std::make_tuple(std::move(body)));
92 res.set(HttpField::content_type,
"text/html");
93 res.content_length(size);
101 if (HttpError::end_of_stream != ec) {
129 std::string_view message) {
131 "error occured: {}, code: {}, message: {}", message, ec, ec.message());
void onRead(boost::system::error_code ec, std::size_t size)
read completion callback
Duration operation_timeout
Configuration config_
session configuration
static constexpr boost::string_view kServerName
void reportError(boost::system::error_code ec, std::string_view message)
reports error code and message
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Strand to ensure the connection's handlers are not called concurrently.
auto makeBadResponse(std::string_view message, unsigned version, bool keep_alive)
composes bad request message
void handleRequest(Request< Body > &&request)
process http request, compose and execute response
boost::beast::http::response< Body > Response
void onWrite(boost::system::error_code ec, std::size_t, bool close)
write completion callback
boost::beast::tcp_stream stream_
stream
void processRequest(std::string_view request, std::shared_ptr< Session > session)
process request message
void start() override
starts session
HTTP session for api service.
boost::beast::flat_buffer buffer_
read buffer
HttpSession(Context &context, Configuration config)
constructor
void asyncRead()
asynchronously read http message
void asyncWrite(Message &&message)
sends http message
std::unique_ptr< Parser > parser_
http parser
void respond(std::string_view response) override
sends response wrapped by http message