Kagome
Polkadot Runtime Engine in C++17
http_session.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_API_TRANSPORT_BEAST_HTTP_SESSION_HPP
7 #define KAGOME_CORE_API_TRANSPORT_BEAST_HTTP_SESSION_HPP
8 
9 #include <boost/asio/strand.hpp>
10 #include <boost/beast.hpp>
11 #include <chrono>
12 #include <cstdlib>
13 #include <memory>
14 
16 #include "log/logger.hpp"
17 
18 namespace kagome::api {
22  class HttpSession : public Session,
23  public std::enable_shared_from_this<HttpSession> {
24  template <typename Body>
25  using Request = boost::beast::http::request<Body>;
26 
27  template <typename Body>
28  using Response = boost::beast::http::response<Body>;
29 
30  using StringBody = boost::beast::http::string_body;
31 
32  template <class Body>
33  using RequestParser = boost::beast::http::request_parser<Body>;
34 
35  using HttpField = boost::beast::http::field;
36 
37  using HttpError = boost::beast::http::error;
38 
39  public:
40  struct Configuration {
41  static constexpr size_t kDefaultRequestSize = 10000u;
42  static constexpr Duration kDefaultTimeout = std::chrono::seconds(30);
43 
44  size_t max_request_size{kDefaultRequestSize};
45  Duration operation_timeout{kDefaultTimeout};
46  };
47 
48  ~HttpSession() override = default;
49 
55  HttpSession(Context &context, Configuration config);
56 
57  Socket &socket() override {
58  return stream_.socket();
59  }
60 
64  void start() override;
65 
70  void respond(std::string_view response) override;
71 
76  SessionId id() const override {
77  return 0ull;
78  }
79 
84  SessionType type() const override {
85  return SessionType::kHttp;
86  }
87 
88  private:
92  void stop();
93 
99  template <class Body>
100  void handleRequest(Request<Body> &&request);
101 
105  void asyncRead();
106 
112  template <class Message>
113  void asyncWrite(Message &&message);
114 
118  void onRead(boost::system::error_code ec, std::size_t size);
119 
123  void onWrite(boost::system::error_code ec, std::size_t, bool close);
124 
133  auto makeBadResponse(std::string_view message,
134  unsigned version,
135  bool keep_alive);
136 
142  void reportError(boost::system::error_code ec, std::string_view message);
143 
144  static constexpr boost::string_view kServerName = "Kagome";
145 
147  boost::asio::strand<boost::asio::io_context::executor_type> strand_;
148 
150  boost::beast::tcp_stream stream_;
151  boost::beast::flat_buffer buffer_;
152 
157 
158  std::unique_ptr<Parser> parser_;
159  log::Logger logger_ = log::createLogger("HttpSession", "rpc_transport");
160  };
161 
162 } // namespace kagome::api
163 
164 #endif // KAGOME_CORE_API_TRANSPORT_BEAST_HTTP_SESSION_HPP
void onRead(boost::system::error_code ec, std::size_t size)
read completion callback
static constexpr Duration kDefaultTimeout
boost::beast::http::string_body StringBody
Timer::duration Duration
Definition: session.hpp:42
boost::beast::http::field HttpField
void stop()
stops session
Configuration config_
session configuration
boost::beast::http::error HttpError
static constexpr boost::string_view kServerName
void reportError(boost::system::error_code ec, std::string_view message)
reports error code and message
boost::asio::ip::tcp::socket Socket
Definition: session.hpp:37
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Strand to ensure the connection&#39;s handlers are not called concurrently.
SessionType type() const override
method to get type of the session
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
Socket & socket() override
boost::beast::http::response< Body > Response
string version
Definition: conf.py:16
void onWrite(boost::system::error_code ec, std::size_t, bool close)
write completion callback
boost::beast::http::request< Body > Request
boost::beast::tcp_stream stream_
stream
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
void start() override
starts session
HTTP session for api service.
boost::beast::flat_buffer buffer_
read buffer
SessionId id() const override
method to get id of the session
rpc session
Definition: session.hpp:27
HttpSession(Context &context, Configuration config)
constructor
void asyncRead()
asynchronously read http message
boost::beast::http::request_parser< Body > RequestParser
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
uint64_t SessionId
Definition: session.hpp:43
void asyncWrite(Message &&message)
sends http message
RequestParser< StringBody > Parser
request parser type
std::unique_ptr< Parser > parser_
http parser
static constexpr size_t kDefaultRequestSize
void respond(std::string_view response) override
sends response wrapped by http message
~HttpSession() override=default