28 boost::asio::any_io_executor ioc, boost::asio::ssl::context & ssl_context,
29 const std::function<
void(
const TResponse &)> response)
30: onResponse{response}, resolver_{ioc}, stream_{ioc, ssl_context}
38 for (
const auto & [key, val] : headers)
50 const std::string & host,
const std::string & target,
const boost::beast::http::verb http_method,
54 req_.version(version);
55 req_.method(http_method);
57 req_.set(boost::beast::http::field::host, host);
58 req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);
61 if (!SSL_set_tlsext_host_name(
stream_.native_handle(), host.c_str()))
63 boost::system::error_code ec{
64 static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category()};
65 std::cerr << ec.message() <<
"\n";
70 host.c_str(),
kPort.c_str(),
75 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
77 if (ec)
return fail(ec,
"resolve");
80 boost::beast::get_lowest_layer(
stream_).expires_after(std::chrono::seconds(1));
83 boost::beast::get_lowest_layer(
stream_).async_connect(
89 std::cout <<
"Failure!..." << std::endl;
90 std::cerr << what <<
": " << ec.message() <<
"\n";
91 res_.result(boost::beast::http::status::bad_request);
92 res_.reason() = ec.message();
97 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type)
99 if (ec)
return fail(ec,
"connect");
102 boost::beast::get_lowest_layer(
stream_).expires_after(std::chrono::seconds(1));
106 boost::asio::ssl::stream_base::client,
112 if (ec)
return fail(ec,
"handshake");
115 boost::beast::get_lowest_layer(
stream_).expires_after(std::chrono::seconds(1));
118 boost::beast::http::async_write(
125 boost::ignore_unused(bytes_transferred);
127 if (ec)
return fail(ec,
"write");
130 boost::beast::http::async_read(
137 boost::ignore_unused(bytes_transferred);
139 if (ec)
return fail(ec,
"read");
141 boost::beast::get_lowest_layer(
stream_).expires_after(std::chrono::seconds(1));
152 if (ec == boost::asio::error::eof)
boost::beast::http::response< boost::beast::http::string_body > TResponse
ssl_http_session(boost::asio::any_io_executor ioc, boost::asio::ssl::context &ssl_context, const std::function< void(const TResponse &)> response)
void fail(boost::beast::error_code ec, const char *what) override
void appendToHeader(const std::string &key, const std::string &val)
void run(const std::string &host, const std::string &target, const boost::beast::http::verb http_method, const int &version) override
boost::beast::ssl_stream< boost::beast::tcp_stream > stream_
boost::beast::flat_buffer buffer_
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override
void on_handshake(boost::beast::error_code ec) override
boost::beast::http::request< boost::beast::http::string_body > req_
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override
void setHeaders(const std::unordered_map< std::string, std::string > &headers) override
std::function< void(const TResponse &)> onResponse
void setBody(const std::string &body) override
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override
void on_shutdown(boost::beast::error_code ec) override
void on_connect(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override
boost::asio::ip::tcp::resolver resolver_