Kagome
Polkadot Runtime Engine in C++17
http_request.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_OFFCHAIN_HTTPREQUEST
7 #define KAGOME_OFFCHAIN_HTTPREQUEST
8 
9 #include <boost/asio/io_context.hpp>
10 #include <boost/asio/ip/tcp.hpp>
11 #include <boost/asio/ssl/context.hpp>
12 #include <boost/asio/ssl/stream.hpp>
13 #include <boost/beast/core.hpp>
14 #include <boost/beast/core/tcp_stream.hpp>
15 #include <boost/beast/http.hpp>
16 #include <boost/beast/ssl/ssl_stream.hpp>
17 
18 #include "common/uri.hpp"
19 #include "log/logger.hpp"
20 #include "offchain/types.hpp"
21 
22 namespace kagome::offchain {
23 
24  class HttpRequest final : public std::enable_shared_from_this<HttpRequest> {
25  public:
26  HttpRequest(HttpRequest &&) noexcept = delete;
27  HttpRequest(const HttpRequest &) = delete;
28 
30 
31  RequestId id() const;
32 
33  HttpStatus status() const;
34 
35  bool init(HttpMethod method, std::string_view uri, common::Buffer meta);
36 
37  Result<Success, Failure> addRequestHeader(std::string_view name,
38  std::string_view value);
39 
41  const common::Buffer &chunk,
42  std::optional<std::chrono::milliseconds> deadline_opt);
43 
44  std::vector<std::pair<std::string, std::string>> getResponseHeaders() const;
45 
47  common::Buffer &chunk,
48  std::optional<std::chrono::milliseconds> deadline);
49 
50  std::string errorMessage() const {
51  return error_message_;
52  }
53 
54  private:
55  void resolve();
56  void connect();
57  void handshake();
58  void sendRequest();
59  void recvResponse();
60  void done();
61 
62  boost::asio::io_context io_context_;
63  int16_t id_;
64 
65  boost::asio::ip::tcp::resolver resolver_;
66  boost::asio::ssl::context ssl_ctx_;
67 
68  using TcpStream = boost::beast::tcp_stream;
69  using SslStream = boost::beast::ssl_stream<TcpStream>;
70  using TcpStreamPtr = std::unique_ptr<TcpStream>;
71  using SslStreamPtr = std::unique_ptr<SslStream>;
72  boost::variant<TcpStreamPtr, SslStreamPtr> stream_;
73 
76  bool request_has_sent_ = false;
77  bool secure_ = false;
78  uint16_t status_ = 0;
79  std::string error_message_;
80  boost::beast::flat_buffer buffer_;
81  boost::asio::steady_timer deadline_timer_;
82  boost::asio::ip::tcp::resolver::iterator resolver_iterator_;
83  boost::beast::http::request<boost::beast::http::string_body> request_;
84  boost::beast::http::response_parser<boost::beast::http::string_body>
86  boost::beast::http::response<boost::beast::http::string_body> response_;
87  bool request_is_ready_ = false;
88  bool connected_ = false;
89 
91  };
92 
93 } // namespace kagome::offchain
94 
95 #endif // KAGOME_OFFCHAIN_HTTPREQUEST
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
boost::beast::http::response< boost::beast::http::string_body > response_
std::unique_ptr< TcpStream > TcpStreamPtr
std::string errorMessage() const
boost::beast::http::response_parser< boost::beast::http::string_body > parser_
HttpRequest(HttpRequest &&) noexcept=delete
HttpStatus status() const
bool init(HttpMethod method, std::string_view uri, common::Buffer meta)
boost::beast::ssl_stream< TcpStream > SslStream
boost::asio::io_context io_context_
uint16_t HttpStatus
HTTP status codes that can get returned by certain Offchain funcs. 0: the specified request identifie...
Definition: types.hpp:46
std::vector< std::pair< std::string, std::string > > getResponseHeaders() const
boost::asio::steady_timer deadline_timer_
boost::beast::flat_buffer buffer_
Result< Success, Failure > addRequestHeader(std::string_view name, std::string_view value)
std::unique_ptr< SslStream > SslStreamPtr
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
Result< uint32_t, HttpError > readResponseBody(common::Buffer &chunk, std::optional< std::chrono::milliseconds > deadline)
int16_t RequestId
Definition: types.hpp:29
boost::asio::ip::tcp::resolver resolver_
boost::asio::ip::tcp::resolver::iterator resolver_iterator_
boost::beast::tcp_stream TcpStream
Result< Success, HttpError > writeRequestBody(const common::Buffer &chunk, std::optional< std::chrono::milliseconds > deadline_opt)
boost::asio::ssl::context ssl_ctx_
boost::variant< TcpStreamPtr, SslStreamPtr > stream_
boost::beast::http::request< boost::beast::http::string_body > request_